author | Mikael Berthe <mikael@lilotux.net> |
Sat, 15 Jul 2017 21:58:24 +0200 | |
changeset 166 | 79c0f8db66ff |
parent 165 | e76422bfe6ee |
child 167 | 1341bacd01c9 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
2 |
// |
|
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
9 |
"os" |
165
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
10 |
"strings" |
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
11 |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
12 |
"github.com/pkg/errors" |
0 | 13 |
"github.com/spf13/cobra" |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
14 |
|
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
15 |
"github.com/McKael/madon" |
0 | 16 |
) |
17 |
||
18 |
var notificationsOpts struct { |
|
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
19 |
list, clear, dismiss bool |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
20 |
notifID int64 |
165
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
21 |
types string |
0 | 22 |
} |
23 |
||
24 |
// notificationsCmd represents the notifications subcommand |
|
25 |
var notificationsCmd = &cobra.Command{ |
|
26 |
Use: "notifications", // XXX |
|
27 |
Aliases: []string{"notification", "notif"}, |
|
28 |
Short: "Manage notifications", |
|
29 |
Example: ` madonctl accounts notifications --list |
|
30 |
madonctl accounts notifications --list --clear |
|
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
31 |
madonctl accounts notifications --dismiss --notification-id N |
165
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
32 |
madonctl accounts notifications --notification-id N |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
33 |
madonctl accounts notifications --list --notification-types mentions |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
34 |
madonctl accounts notifications --list --notification-types favourites |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
35 |
madonctl accounts notifications --list --notification-types follows,reblogs`, |
0 | 36 |
//Long: `TBW...`, |
37 |
RunE: notificationRunE, |
|
38 |
} |
|
39 |
||
40 |
func init() { |
|
41 |
accountsCmd.AddCommand(notificationsCmd) |
|
42 |
||
43 |
notificationsCmd.Flags().BoolVar(¬ificationsOpts.list, "list", false, "List all current notifications") |
|
44 |
notificationsCmd.Flags().BoolVar(¬ificationsOpts.clear, "clear", false, "Clear all current notifications") |
|
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
45 |
notificationsCmd.Flags().BoolVar(¬ificationsOpts.dismiss, "dismiss", false, "Delete a notification") |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
46 |
notificationsCmd.Flags().Int64Var(¬ificationsOpts.notifID, "notification-id", 0, "Get a notification") |
165
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
47 |
notificationsCmd.Flags().StringVar(¬ificationsOpts.types, "notification-types", "", "Filter notifications (mentions, favourites, reblogs, follows)") |
0 | 48 |
} |
49 |
||
50 |
func notificationRunE(cmd *cobra.Command, args []string) error { |
|
51 |
opt := notificationsOpts |
|
52 |
||
53 |
if !opt.list && !opt.clear && opt.notifID < 1 { |
|
54 |
return errors.New("missing parameters") |
|
55 |
} |
|
56 |
||
57 |
if err := madonInit(true); err != nil { |
|
58 |
return err |
|
59 |
} |
|
60 |
||
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
61 |
var limOpts *madon.LimitParams |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
62 |
if accountsOpts.all || accountsOpts.limit > 0 || accountsOpts.sinceID > 0 || accountsOpts.maxID > 0 { |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
63 |
limOpts = new(madon.LimitParams) |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
64 |
limOpts.All = accountsOpts.all |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
65 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
66 |
|
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
67 |
if accountsOpts.limit > 0 { |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
68 |
limOpts.Limit = int(accountsOpts.limit) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
69 |
} |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
70 |
if accountsOpts.maxID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
71 |
limOpts.MaxID = int64(accountsOpts.maxID) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
72 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
73 |
if accountsOpts.sinceID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
74 |
limOpts.SinceID = int64(accountsOpts.sinceID) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
75 |
} |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
76 |
|
166
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
77 |
var filterMap *map[string]bool |
165
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
78 |
if opt.types != "" { |
166
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
79 |
var err error |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
80 |
filterMap, err = buildFilterMap(opt.types) |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
81 |
if err != nil { |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
82 |
return nil |
165
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
83 |
} |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
84 |
} |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
85 |
|
0 | 86 |
var obj interface{} |
87 |
var err error |
|
88 |
||
89 |
if opt.list { |
|
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
90 |
var notifications []madon.Notification |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
91 |
notifications, err = gClient.GetNotifications(limOpts) |
165
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
92 |
|
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
93 |
// Filter notifications |
166
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
94 |
if filterMap != nil && len(*filterMap) > 0 { |
165
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
95 |
if verbose { |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
96 |
errPrint("Filtering notifications") |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
97 |
} |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
98 |
var newNotifications []madon.Notification |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
99 |
for _, notif := range notifications { |
166
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
100 |
if (*filterMap)[notif.Type] { |
165
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
101 |
newNotifications = append(newNotifications, notif) |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
102 |
} |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
103 |
} |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
104 |
notifications = newNotifications |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
105 |
} |
e76422bfe6ee
Add --notification-types flag to filter notifications
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
106 |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
107 |
if accountsOpts.limit > 0 && len(notifications) > int(accountsOpts.limit) { |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
108 |
notifications = notifications[:accountsOpts.limit] |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
109 |
} |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
110 |
obj = notifications |
0 | 111 |
} else if opt.notifID > 0 { |
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
112 |
if opt.dismiss { |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
113 |
err = gClient.DismissNotification(opt.notifID) |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
114 |
} else { |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
115 |
obj, err = gClient.GetNotification(opt.notifID) |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
116 |
} |
0 | 117 |
} |
118 |
||
119 |
if err == nil && opt.clear { |
|
120 |
err = gClient.ClearNotifications() |
|
121 |
} |
|
122 |
||
123 |
if err != nil { |
|
124 |
errPrint("Error: %s", err.Error()) |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
125 |
os.Exit(1) |
0 | 126 |
} |
127 |
if obj == nil { |
|
128 |
return nil |
|
129 |
} |
|
130 |
||
131 |
p, err := getPrinter() |
|
132 |
if err != nil { |
|
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
133 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
134 |
os.Exit(1) |
0 | 135 |
} |
110 | 136 |
return p.printObj(obj) |
0 | 137 |
} |
166
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
138 |
|
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
139 |
func buildFilterMap(types string) (*map[string]bool, error) { |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
140 |
filterMap := make(map[string]bool) |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
141 |
if types != "" { |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
142 |
for _, f := range strings.Split(types, ",") { |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
143 |
switch f { |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
144 |
case "mention", "mentions": |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
145 |
filterMap["mention"] = true |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
146 |
case "favourite", "favourites", "favorite", "favorites", "fave", "faves": |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
147 |
filterMap["favourite"] = true |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
148 |
case "reblog", "reblogs", "retoot", "retoots": |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
149 |
filterMap["reblog"] = true |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
150 |
case "follow", "follows": |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
151 |
filterMap["follow"] = true |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
152 |
default: |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
153 |
return nil, errors.Errorf("unknown notification type: '%s'", f) |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
154 |
} |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
155 |
} |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
156 |
} |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
157 |
return &filterMap, nil |
79c0f8db66ff
Add --notification-types to the stream command as well
Mikael Berthe <mikael@lilotux.net>
parents:
165
diff
changeset
|
158 |
} |