Implement notification subcommands #389

Merged
6543 merged 20 commits from noerw/tea:notification-subcmds into master 2021-09-05 17:11:17 +00:00
8 changed files with 118 additions and 93 deletions
Showing only changes of commit ccb10bbbe4 - Show all commits

View File

@ -20,9 +20,10 @@ var CmdNotifications = cli.Command{
Action: notifications.RunNotificationsList,
Subcommands: []*cli.Command{
&notifications.CmdNotificationsList,
&notifications.CmdNotificationsPinned,
&notifications.CmdNotificationsRead,
&notifications.CmdNotificationsUnread,
&notifications.CmdNotificationsPin,
&notifications.CmdNotificationsUnpin,
&notifications.CmdNotificationsMarkRead,
&notifications.CmdNotificationsMarkUnread,
},
Flags: notifications.CmdNotificationsList.Flags,
}

View File

@ -1,4 +1,4 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// 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.
@ -22,18 +22,27 @@ var CmdNotificationsList = cli.Command{
Description: `List notifications`,
Action: RunNotificationsList,
Flags: append([]cli.Flag{
// FIXME: shouldn't --for-user only be applied to the ls command here, instead of all notification operations?
&cli.StringFlag{
Name: "state",
Aliases: []string{"s"},
Usage: "filter by notification state (pinned,read,unread,all)",
DefaultText: "pinned + unread",
},
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Usage: "filter by subject type (repo,issue,pr,commit)",
},
}, flags.NotificationFlags...),
}
// RunNotificationsList list notifications
func RunNotificationsList(ctx *cli.Context) error {
var states []gitea.NotifyStatus
var types []gitea.NotifySubjectType
// FIXME: make these commaseparated slice flags..?
switch ctx.String("state") {
case "":
@ -51,5 +60,21 @@ func RunNotificationsList(ctx *cli.Context) error {
os.Exit(1)
}
return listNotifications(ctx, states)
switch ctx.String("type") {
case "":
types = []gitea.NotifySubjectType{}
case "commit":
types = []gitea.NotifySubjectType{gitea.NotifySubjectCommit}
case "issue":
types = []gitea.NotifySubjectType{gitea.NotifySubjectIssue}
case "pull", "pr":
types = []gitea.NotifySubjectType{gitea.NotifySubjectPull}
case "repo":
types = []gitea.NotifySubjectType{gitea.NotifySubjectRepository}
default:
fmt.Printf("Unknown type '%s'\n", ctx.String("type"))
os.Exit(1)
}
return listNotifications(ctx, states, types)
}

View File

@ -0,0 +1,43 @@
// 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 (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
"github.com/urfave/cli/v2"
)
// CmdNotificationsRead represents a sub command of notifications to list read notifications
var CmdNotificationsMarkRead = cli.Command{
Name: "read",
Aliases: []string{},
Usage: "Show read notifications only",
Description: `Show read notifications only`,
Action: RunNotificationsRead,
Flags: flags.NotificationFlags,
}
// RunNotificationsRead will show notifications with status read.
func RunNotificationsRead(ctx *cli.Context) error {
var statuses = []gitea.NotifyStatus{gitea.NotifyStatusRead}
return listNotifications(ctx, statuses, []gitea.NotifySubjectType{})
}
// CmdNotificationsUnread represents a sub command of notifications to list unread notifications.
var CmdNotificationsMarkUnread = cli.Command{
Name: "unread",
Aliases: []string{},
Usage: "Show unread notifications only",
Description: `Show unread notifications only`,
Action: RunNotificationsUnread,
Flags: flags.NotificationFlags,
}
// RunNotificationsUnread will show notifications with status unread.
func RunNotificationsUnread(ctx *cli.Context) error {
var statuses = []gitea.NotifyStatus{gitea.NotifyStatusUnread}
return listNotifications(ctx, statuses, []gitea.NotifySubjectType{})
}

View File

@ -1,4 +1,4 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// 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.
@ -15,7 +15,7 @@ import (
)
// listNotifications will get the notifications based on status
func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus) error {
func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus, subjects []gitea.NotifySubjectType) error {
var news []*gitea.NotificationThread
var err error
@ -31,15 +31,16 @@ func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus) error {
if all {
news, _, err = client.ListNotifications(gitea.ListNotificationOptions{
ListOptions: listOpts,
Status: status,
// TODO: SubjectTypes
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,
ListOptions: listOpts,
Status: status,
SubjectTypes: subjects,
})
}
if err != nil {

36
cmd/notifications/pin.go Normal file
View File

@ -0,0 +1,36 @@
// 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 (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
"github.com/urfave/cli/v2"
)
// CmdNotificationsUnpin represents a sub command of notifications to unpin a notification
var CmdNotificationsUnpin = cli.Command{
Name: "unpin",
Usage: "Remove a notification from pins",
Description: `Remove a notification from pins`,
Action: RunNotificationsPinned,
Flags: flags.NotificationFlags,
}
// CmdNotificationsPin represents a sub command of notifications to pin a notification
var CmdNotificationsPin = cli.Command{
Name: "pin",
Aliases: []string{"p"},
Usage: "Save a notification as pin",
Description: `Save a notification as pin`,
Action: RunNotificationsPinned,
Flags: flags.NotificationFlags,
}
// RunNotificationsPinned will show notifications with status pinned.
func RunNotificationsPinned(ctx *cli.Context) error {
var statuses = []gitea.NotifyStatus{gitea.NotifyStatusPinned}
return listNotifications(ctx, statuses, []gitea.NotifySubjectType{})
}

View File

@ -1,27 +0,0 @@
// 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 notifications
import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
"github.com/urfave/cli/v2"
)
// CmdNotificationsPinned represents a sub command of notifications to list pinned notifications
var CmdNotificationsPinned = cli.Command{
Name: "pinned",
Aliases: []string{"pd"},
Usage: "show pinned notifications",
Description: `show pinned notifications`,
Action: RunNotificationsPinned,
Flags: flags.NotificationFlags,
}
// RunNotificationsPinned will show notifications with status pinned.
func RunNotificationsPinned(ctx *cli.Context) error {
var statuses = []gitea.NotifyStatus{gitea.NotifyStatusPinned}
return listNotifications(ctx, statuses)
}

View File

@ -1,27 +0,0 @@
// 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 notifications
import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
"github.com/urfave/cli/v2"
)
// CmdNotificationsRead represents a sub command of notifications to list read notifications
var CmdNotificationsRead = cli.Command{
Name: "read",
Aliases: []string{},
Usage: "show read notifications instead",
Description: `show read notifications instead`,
Action: RunNotificationsRead,
Flags: flags.NotificationFlags,
}
// RunNotificationsRead will show notifications with status read.
func RunNotificationsRead(ctx *cli.Context) error {
var statuses = []gitea.NotifyStatus{gitea.NotifyStatusRead}
return listNotifications(ctx, statuses)
}

View File

@ -1,27 +0,0 @@
// 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 notifications
import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
"github.com/urfave/cli/v2"
)
// CmdNotificationsUnread represents a sub command of notifications to list unread notifications.
var CmdNotificationsUnread = cli.Command{
Name: "unread",
Aliases: []string{},
Usage: "show unread notifications",
Description: `show unread notifications`,
Action: RunNotificationsUnread,
Flags: flags.NotificationFlags,
}
// RunNotificationsUnread will show notifications with status unread.
func RunNotificationsUnread(ctx *cli.Context) error {
var statuses = []gitea.NotifyStatus{gitea.NotifyStatusUnread}
return listNotifications(ctx, statuses)
}