2020-07-17 11:30:02 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-09-30 01:11:33 -04:00
|
|
|
"code.gitea.io/tea/cmd/flags"
|
2020-12-15 12:38:22 -05:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-09-30 01:11:33 -04:00
|
|
|
"code.gitea.io/tea/modules/print"
|
|
|
|
|
2020-07-17 11:30:02 -04:00
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdNotifications is the main command to operate with notifications
|
|
|
|
var CmdNotifications = cli.Command{
|
|
|
|
Name: "notifications",
|
2020-12-16 11:47:40 -05:00
|
|
|
Aliases: []string{"notification", "n"},
|
2020-12-21 08:37:20 -05:00
|
|
|
Category: catHelpers,
|
2020-09-27 10:35:53 -04:00
|
|
|
Usage: "Show notifications",
|
|
|
|
Description: "Show notifications, by default based of the current repo and unread one",
|
2020-07-17 11:30:02 -04:00
|
|
|
Action: runNotifications,
|
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "all",
|
|
|
|
Aliases: []string{"a"},
|
|
|
|
Usage: "show all notifications of related gitea instance",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "read",
|
|
|
|
Aliases: []string{"rd"},
|
|
|
|
Usage: "show read notifications instead unread",
|
|
|
|
},
|
2020-09-15 22:01:41 -04:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "pinned",
|
|
|
|
Aliases: []string{"pd"},
|
|
|
|
Usage: "show pinned notifications instead unread",
|
|
|
|
},
|
2020-09-30 01:11:33 -04:00
|
|
|
&flags.PaginationPageFlag,
|
|
|
|
&flags.PaginationLimitFlag,
|
|
|
|
}, flags.AllDefaultFlags...),
|
2020-07-17 11:30:02 -04:00
|
|
|
}
|
|
|
|
|
2020-12-15 12:38:22 -05:00
|
|
|
func runNotifications(cmd *cli.Context) error {
|
2020-07-17 11:30:02 -04:00
|
|
|
var news []*gitea.NotificationThread
|
|
|
|
var err error
|
|
|
|
|
2020-12-15 12:38:22 -05:00
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
client := ctx.Login.Client()
|
2020-12-11 04:07:29 -05:00
|
|
|
|
2020-12-15 12:38:22 -05:00
|
|
|
listOpts := ctx.GetListOptions()
|
2020-09-27 08:07:46 -04:00
|
|
|
if listOpts.Page == 0 {
|
|
|
|
listOpts.Page = 1
|
2020-07-17 11:30:02 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
var status []gitea.NotifyStatus
|
|
|
|
if ctx.Bool("read") {
|
|
|
|
status = []gitea.NotifyStatus{gitea.NotifyStatusRead}
|
|
|
|
}
|
|
|
|
if ctx.Bool("pinned") {
|
|
|
|
status = append(status, gitea.NotifyStatusPinned)
|
|
|
|
}
|
|
|
|
|
2020-07-17 11:30:02 -04:00
|
|
|
if ctx.Bool("all") {
|
2020-12-15 12:38:22 -05:00
|
|
|
news, _, err = client.ListNotifications(gitea.ListNotificationOptions{
|
2020-07-17 11:30:02 -04:00
|
|
|
ListOptions: listOpts,
|
2020-09-15 22:01:41 -04:00
|
|
|
Status: status,
|
2020-07-17 11:30:02 -04:00
|
|
|
})
|
|
|
|
} else {
|
2020-12-15 12:38:22 -05:00
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
|
|
|
news, _, err = client.ListRepoNotifications(ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{
|
2020-07-17 11:30:02 -04:00
|
|
|
ListOptions: listOpts,
|
2020-09-15 22:01:41 -04:00
|
|
|
Status: status,
|
2020-07-17 11:30:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-12-16 11:18:10 -05:00
|
|
|
return err
|
2020-07-17 11:30:02 -04:00
|
|
|
}
|
|
|
|
|
2020-12-15 12:38:22 -05:00
|
|
|
print.NotificationsList(news, ctx.Output, ctx.Bool("all"))
|
2020-07-17 11:30:02 -04:00
|
|
|
return nil
|
|
|
|
}
|