mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-02 18:08:30 +02:00
Implement notification subcommands (#389)
- [x] enhance notification listing - add `--states` and `--type` filters - toggle per-user or per-repo listing via `--mine` flag - print more fields - [x] add subcommands to mark notifications as read, unread, pinned, unpinned. operates on - all notifications matching the `--state` and `--mine` filter flags, or - a notification specified by ID. - [ ] ~~add a `--fields` flag for notifications listing.~~ *not in this PR* - [ ] ~~interactive mode~~ *not in this PR*. this would go well together with #324 fixes #243, fixes #155 based on initial work in #283 and #386, but opening a new PR for @6543 to review as I changed quite a lot --- ### ⚠️ breaking ⚠️ - `tea notifications --all` has moved to `tea notifications --mine` - `tea notifications` now only works with the context of a remote repo. To run this outside of a local git dir, run either `tea n --mine` or `tea n --repo <my/repo>` --- Co-authored-by: Karl Heinz Marbaise <kama@soebes.de> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Norwin Roosen <git@nroo.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/389 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Alexey 〒erentyev <axifive@noreply.gitea.io> Co-authored-by: Norwin <noerw@noreply.gitea.io> Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
89
cmd/notifications/list.go
Normal file
89
cmd/notifications/list.go
Normal file
@ -0,0 +1,89 @@
|
||||
// Copyright 2021 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 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"
|
||||
)
|
||||
|
||||
var notifTypeFlag = flags.NewCsvFlag("types", "subject types to filter by", []string{"t"},
|
||||
[]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`,
|
||||
Action: RunNotificationsList,
|
||||
Flags: append([]cli.Flag{notifTypeFlag}, flags.NotificationFlags...),
|
||||
}
|
||||
|
||||
// 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
|
||||
typesStr, err := notifTypeFlag.GetValues(ctx)
|
||||
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
|
||||
}
|
||||
|
||||
if all {
|
||||
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)
|
||||
}
|
||||
|
||||
print.NotificationsList(news, ctx.Output, all)
|
||||
return nil
|
||||
}
|
139
cmd/notifications/mark_as.go
Normal file
139
cmd/notifications/mark_as.go
Normal file
@ -0,0 +1,139 @@
|
||||
// Copyright 2021 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 notifications
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
"code.gitea.io/tea/modules/utils"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// CmdNotificationsMarkRead represents a sub command of notifications to list read notifications
|
||||
var CmdNotificationsMarkRead = cli.Command{
|
||||
Name: "read",
|
||||
Aliases: []string{"r"},
|
||||
Usage: "Mark all filtered or a specific notification as read",
|
||||
Description: "Mark all filtered or a specific notification as read",
|
||||
ArgsUsage: "[all | <notification id>]",
|
||||
Flags: flags.NotificationFlags,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
cmd := context.InitCommand(ctx)
|
||||
filter, err := flags.NotificationStateFlag.GetValues(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !flags.NotificationStateFlag.IsSet() {
|
||||
filter = []string{string(gitea.NotifyStatusUnread)}
|
||||
}
|
||||
return markNotificationAs(cmd, filter, gitea.NotifyStatusRead)
|
||||
},
|
||||
}
|
||||
|
||||
// CmdNotificationsMarkUnread will mark notifications as unread.
|
||||
var CmdNotificationsMarkUnread = cli.Command{
|
||||
Name: "unread",
|
||||
Aliases: []string{"u"},
|
||||
Usage: "Mark all filtered or a specific notification as unread",
|
||||
Description: "Mark all filtered or a specific notification as unread",
|
||||
ArgsUsage: "[all | <notification id>]",
|
||||
Flags: flags.NotificationFlags,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
cmd := context.InitCommand(ctx)
|
||||
filter, err := flags.NotificationStateFlag.GetValues(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !flags.NotificationStateFlag.IsSet() {
|
||||
filter = []string{string(gitea.NotifyStatusRead)}
|
||||
}
|
||||
return markNotificationAs(cmd, filter, gitea.NotifyStatusUnread)
|
||||
},
|
||||
}
|
||||
|
||||
// CmdNotificationsMarkPinned will mark notifications as unread.
|
||||
var CmdNotificationsMarkPinned = cli.Command{
|
||||
Name: "pin",
|
||||
Aliases: []string{"p"},
|
||||
Usage: "Mark all filtered or a specific notification as pinned",
|
||||
Description: "Mark all filtered or a specific notification as pinned",
|
||||
ArgsUsage: "[all | <notification id>]",
|
||||
Flags: flags.NotificationFlags,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
cmd := context.InitCommand(ctx)
|
||||
filter, err := flags.NotificationStateFlag.GetValues(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !flags.NotificationStateFlag.IsSet() {
|
||||
filter = []string{string(gitea.NotifyStatusUnread)}
|
||||
}
|
||||
return markNotificationAs(cmd, filter, gitea.NotifyStatusPinned)
|
||||
},
|
||||
}
|
||||
|
||||
// CmdNotificationsUnpin will mark pinned notifications as unread.
|
||||
var CmdNotificationsUnpin = cli.Command{
|
||||
Name: "unpin",
|
||||
Usage: "Unpin all pinned or a specific notification",
|
||||
Description: "Marks all pinned or a specific notification as read",
|
||||
ArgsUsage: "[all | <notification id>]",
|
||||
Flags: flags.NotificationFlags,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
cmd := context.InitCommand(ctx)
|
||||
filter := []string{string(gitea.NotifyStatusPinned)}
|
||||
// NOTE: we implicitly mark it as read, to match web UI semantics. marking as unread might be more useful?
|
||||
return markNotificationAs(cmd, filter, gitea.NotifyStatusRead)
|
||||
},
|
||||
}
|
||||
|
||||
func markNotificationAs(cmd *context.TeaContext, filterStates []string, targetState gitea.NotifyStatus) (err error) {
|
||||
client := cmd.Login.Client()
|
||||
subject := cmd.Args().First()
|
||||
allRepos := cmd.Bool("mine")
|
||||
|
||||
states := []gitea.NotifyStatus{}
|
||||
for _, s := range filterStates {
|
||||
states = append(states, gitea.NotifyStatus(s))
|
||||
}
|
||||
|
||||
switch subject {
|
||||
case "", "all":
|
||||
opts := gitea.MarkNotificationOptions{Status: states, ToStatus: targetState}
|
||||
|
||||
if allRepos {
|
||||
_, err = client.ReadNotifications(opts)
|
||||
} else {
|
||||
cmd.Ensure(context.CtxRequirement{RemoteRepo: true})
|
||||
_, err = client.ReadRepoNotifications(cmd.Owner, cmd.Repo, opts)
|
||||
}
|
||||
|
||||
// TODO: print all affected notification subject URLs
|
||||
// (not supported by API currently, https://github.com/go-gitea/gitea/issues/16797)
|
||||
|
||||
default:
|
||||
id, err := utils.ArgToIndex(subject)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = client.ReadNotification(id, targetState)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n, _, err := client.GetNotification(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// FIXME: this is an API URL, we want to display a web ui link..
|
||||
fmt.Println(n.Subject.URL)
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user