2021-09-06 01:11:17 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2023-09-08 09:40:02 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-09-06 01:11:17 +08:00
|
|
|
|
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"code.gitea.io/tea/cmd/flags"
|
|
|
|
"code.gitea.io/tea/modules/context"
|
|
|
|
"code.gitea.io/tea/modules/print"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2022-09-14 03:08:18 +08:00
|
|
|
var notifyFieldsFlag = flags.FieldsFlag(print.NotificationFields, []string{
|
|
|
|
"id", "status", "index", "type", "state", "title",
|
|
|
|
})
|
|
|
|
|
|
|
|
var notifyTypeFlag = flags.NewCsvFlag("types", "subject types to filter by", []string{"t"},
|
2021-09-06 01:11:17 +08:00
|
|
|
[]string{"issue", "pull", "repository", "commit"}, nil)
|
|
|
|
|
|
|
|
// CmdNotificationsList represents a sub command of notifications to list notifications
|
|
|
|
var CmdNotificationsList = cli.Command{
|
|
|
|
Name: "ls",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Usage: "List notifications",
|
|
|
|
Description: `List notifications`,
|
2022-09-14 02:14:02 +08:00
|
|
|
ArgsUsage: " ", // command does not accept arguments
|
2021-09-06 01:11:17 +08:00
|
|
|
Action: RunNotificationsList,
|
2022-09-14 03:08:18 +08:00
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
notifyFieldsFlag,
|
|
|
|
notifyTypeFlag,
|
|
|
|
}, flags.NotificationFlags...),
|
2021-09-06 01:11:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunNotificationsList list notifications
|
|
|
|
func RunNotificationsList(ctx *cli.Context) error {
|
|
|
|
var states []gitea.NotifyStatus
|
|
|
|
statesStr, err := flags.NotificationStateFlag.GetValues(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, s := range statesStr {
|
|
|
|
states = append(states, gitea.NotifyStatus(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
var types []gitea.NotifySubjectType
|
2022-09-14 03:08:18 +08:00
|
|
|
typesStr, err := notifyTypeFlag.GetValues(ctx)
|
2021-09-06 01:11:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, t := range typesStr {
|
|
|
|
types = append(types, gitea.NotifySubjectType(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
return listNotifications(ctx, states, types)
|
|
|
|
}
|
|
|
|
|
|
|
|
// listNotifications will get the notifications based on status and subject type
|
|
|
|
func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus, subjects []gitea.NotifySubjectType) error {
|
|
|
|
var news []*gitea.NotificationThread
|
|
|
|
var err error
|
|
|
|
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
client := ctx.Login.Client()
|
|
|
|
all := ctx.Bool("mine")
|
|
|
|
|
|
|
|
// This enforces pagination (see https://github.com/go-gitea/gitea/issues/16733)
|
|
|
|
listOpts := ctx.GetListOptions()
|
|
|
|
if listOpts.Page == 0 {
|
|
|
|
listOpts.Page = 1
|
|
|
|
}
|
|
|
|
|
2022-09-14 03:08:18 +08:00
|
|
|
fields, err := notifyFieldsFlag.GetValues(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-06 01:11:17 +08:00
|
|
|
if all {
|
2022-09-14 03:08:18 +08:00
|
|
|
// add repository to the default fields
|
|
|
|
if !cmd.IsSet("fields") {
|
|
|
|
fields = append(fields, "repository")
|
|
|
|
}
|
|
|
|
|
2021-09-06 01:11:17 +08:00
|
|
|
news, _, err = client.ListNotifications(gitea.ListNotificationOptions{
|
|
|
|
ListOptions: listOpts,
|
|
|
|
Status: status,
|
|
|
|
SubjectTypes: subjects,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
|
|
|
news, _, err = client.ListRepoNotifications(ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{
|
|
|
|
ListOptions: listOpts,
|
|
|
|
Status: status,
|
|
|
|
SubjectTypes: subjects,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-09-14 03:08:18 +08:00
|
|
|
print.NotificationsList(news, ctx.Output, fields)
|
2021-09-06 01:11:17 +08:00
|
|
|
return nil
|
|
|
|
}
|