author | Mikael Berthe <mikael@lilotux.net> |
Sat, 21 Oct 2017 19:17:07 +0200 | |
changeset 173 | 1812a794b759 |
parent 168 | ce4000ac7294 |
child 178 | 15d211137c20 |
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 ( |
|
16 | 9 |
"os" |
0 | 10 |
"strings" |
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" |
6 | 14 |
flag "github.com/spf13/pflag" |
0 | 15 |
|
16 |
"github.com/McKael/madon" |
|
17 |
) |
|
18 |
||
19 |
var accountsOpts struct { |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
20 |
accountID int64 |
16 | 21 |
accountUID string |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
22 |
unset bool // TODO remove eventually? |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
23 |
limit, keep uint // Limit the results |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
24 |
sinceID, maxID int64 // Query boundaries |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
29
diff
changeset
|
25 |
all bool // Try to fetch all results |
0 | 26 |
onlyMedia, excludeReplies bool // For acccount statuses |
27 |
remoteUID string // For account follow |
|
28 |
acceptFR, rejectFR bool // For account follow_requests |
|
29 |
list bool // For account follow_requests/reports |
|
30 |
accountIDs string // For account relationships |
|
31 |
statusIDs string // For account reports |
|
32 |
comment string // For account reports |
|
6 | 33 |
displayName, note string // For account update |
34 |
avatar, header string // For account update |
|
0 | 35 |
} |
36 |
||
6 | 37 |
var updateFlags *flag.FlagSet |
38 |
||
0 | 39 |
func init() { |
40 |
RootCmd.AddCommand(accountsCmd) |
|
41 |
||
42 |
// Subcommands |
|
43 |
accountsCmd.AddCommand(accountSubcommands...) |
|
44 |
||
45 |
// Global flags |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
46 |
accountsCmd.PersistentFlags().Int64VarP(&accountsOpts.accountID, "account-id", "a", 0, "Account ID number") |
16 | 47 |
accountsCmd.PersistentFlags().StringVarP(&accountsOpts.accountUID, "user-id", "u", "", "Account user ID") |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
48 |
accountsCmd.PersistentFlags().UintVarP(&accountsOpts.limit, "limit", "l", 0, "Limit number of API results") |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
49 |
accountsCmd.PersistentFlags().UintVarP(&accountsOpts.keep, "keep", "k", 0, "Limit number of results") |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
50 |
accountsCmd.PersistentFlags().Int64Var(&accountsOpts.sinceID, "since-id", 0, "Request IDs greater than a value") |
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
51 |
accountsCmd.PersistentFlags().Int64Var(&accountsOpts.maxID, "max-id", 0, "Request IDs less (or equal) than a value") |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
29
diff
changeset
|
52 |
accountsCmd.PersistentFlags().BoolVar(&accountsOpts.all, "all", false, "Fetch all results") |
0 | 53 |
|
54 |
// Subcommand flags |
|
55 |
accountStatusesSubcommand.Flags().BoolVar(&accountsOpts.onlyMedia, "only-media", false, "Only statuses with media attachments") |
|
56 |
accountStatusesSubcommand.Flags().BoolVar(&accountsOpts.excludeReplies, "exclude-replies", false, "Exclude replies to other statuses") |
|
57 |
||
58 |
accountFollowRequestsSubcommand.Flags().BoolVar(&accountsOpts.list, "list", false, "List pending follow requests") |
|
59 |
accountFollowRequestsSubcommand.Flags().BoolVar(&accountsOpts.acceptFR, "accept", false, "Accept the follow request from the account ID") |
|
60 |
accountFollowRequestsSubcommand.Flags().BoolVar(&accountsOpts.rejectFR, "reject", false, "Reject the follow request from the account ID") |
|
61 |
||
62 |
accountBlockSubcommand.Flags().BoolVarP(&accountsOpts.unset, "unset", "", false, "Unblock the account") |
|
63 |
accountMuteSubcommand.Flags().BoolVarP(&accountsOpts.unset, "unset", "", false, "Unmute the account") |
|
64 |
accountFollowSubcommand.Flags().BoolVarP(&accountsOpts.unset, "unset", "", false, "Unfollow the account") |
|
65 |
accountFollowSubcommand.Flags().StringVarP(&accountsOpts.remoteUID, "remote", "r", "", "Follow remote account (user@domain)") |
|
66 |
||
67 |
accountRelationshipsSubcommand.Flags().StringVar(&accountsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs") |
|
68 |
||
69 |
accountReportsSubcommand.Flags().StringVar(&accountsOpts.statusIDs, "status-ids", "", "Comma-separated list of status IDs") |
|
70 |
accountReportsSubcommand.Flags().StringVar(&accountsOpts.comment, "comment", "", "Report comment") |
|
71 |
accountReportsSubcommand.Flags().BoolVar(&accountsOpts.list, "list", false, "List current user reports") |
|
6 | 72 |
|
73 |
accountUpdateSubcommand.Flags().StringVar(&accountsOpts.displayName, "display-name", "", "User display name") |
|
74 |
accountUpdateSubcommand.Flags().StringVar(&accountsOpts.note, "note", "", "User note (a.k.a. bio)") |
|
75 |
accountUpdateSubcommand.Flags().StringVar(&accountsOpts.avatar, "avatar", "", "User avatar image") |
|
76 |
accountUpdateSubcommand.Flags().StringVar(&accountsOpts.header, "header", "", "User header image") |
|
77 |
||
168
ce4000ac7294
Add flag to reply with same visibility as original message
Mikael Berthe <mikael@lilotux.net>
parents:
167
diff
changeset
|
78 |
// This one will be used to check if the options were explicitly set or not |
6 | 79 |
updateFlags = accountUpdateSubcommand.Flags() |
0 | 80 |
} |
81 |
||
82 |
// accountsCmd represents the accounts command |
|
83 |
// This command does nothing without a subcommand |
|
84 |
var accountsCmd = &cobra.Command{ |
|
85 |
Use: "accounts [--account-id ID] subcommand", |
|
86 |
Aliases: []string{"account"}, |
|
87 |
Short: "Account-related functions", |
|
88 |
//Long: `TBW...`, // TODO |
|
89 |
} |
|
90 |
||
91 |
// Note: Some account subcommands are not defined in this file. |
|
92 |
var accountSubcommands = []*cobra.Command{ |
|
93 |
&cobra.Command{ |
|
94 |
Use: "show", |
|
95 |
Long: `Displays the details about the requested account. |
|
96 |
If no account ID is specified, the current user account is used.`, |
|
97 |
Aliases: []string{"display"}, |
|
98 |
Short: "Display the account", |
|
99 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
100 |
return accountSubcommandsRunE(cmd.Name(), args) |
|
101 |
}, |
|
102 |
}, |
|
103 |
&cobra.Command{ |
|
104 |
Use: "followers", |
|
105 |
Short: "Display the accounts following the specified account", |
|
106 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
107 |
return accountSubcommandsRunE(cmd.Name(), args) |
|
108 |
}, |
|
109 |
}, |
|
110 |
&cobra.Command{ |
|
111 |
Use: "following", |
|
112 |
Short: "Display the accounts followed by the specified account", |
|
113 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
114 |
return accountSubcommandsRunE(cmd.Name(), args) |
|
115 |
}, |
|
116 |
}, |
|
117 |
&cobra.Command{ |
|
118 |
Use: "favourites", |
|
119 |
Aliases: []string{"favorites", "favourited", "favorited"}, |
|
120 |
Short: "Display the user's favourites", |
|
121 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
122 |
return accountSubcommandsRunE(cmd.Name(), args) |
|
123 |
}, |
|
124 |
}, |
|
125 |
&cobra.Command{ |
|
126 |
Use: "blocks", |
|
127 |
Aliases: []string{"blocked"}, |
|
128 |
Short: "Display the user's blocked accounts", |
|
129 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
130 |
return accountSubcommandsRunE(cmd.Name(), args) |
|
131 |
}, |
|
132 |
}, |
|
133 |
&cobra.Command{ |
|
134 |
Use: "mutes", |
|
135 |
Aliases: []string{"muted"}, |
|
136 |
Short: "Display the user's muted accounts", |
|
137 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
138 |
return accountSubcommandsRunE(cmd.Name(), args) |
|
139 |
}, |
|
140 |
}, |
|
141 |
&cobra.Command{ |
|
142 |
Use: "search TEXT", |
|
143 |
Short: "Search for user accounts", |
|
144 |
Long: `Search for user accounts. |
|
145 |
The server will lookup an account remotely if the search term is in the |
|
146 |
username@domain format and not yet in the database.`, |
|
147 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
148 |
return accountSubcommandsRunE(cmd.Name(), args) |
|
149 |
}, |
|
150 |
}, |
|
151 |
accountStatusesSubcommand, |
|
152 |
accountFollowRequestsSubcommand, |
|
153 |
accountFollowSubcommand, |
|
154 |
accountBlockSubcommand, |
|
155 |
accountMuteSubcommand, |
|
156 |
accountRelationshipsSubcommand, |
|
157 |
accountReportsSubcommand, |
|
6 | 158 |
accountUpdateSubcommand, |
0 | 159 |
} |
160 |
||
161 |
var accountStatusesSubcommand = &cobra.Command{ |
|
162 |
Use: "statuses", |
|
163 |
Aliases: []string{"st"}, |
|
164 |
Short: "Display the account statuses", |
|
165 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
166 |
return accountSubcommandsRunE(cmd.Name(), args) |
|
167 |
}, |
|
168 |
} |
|
169 |
||
170 |
var accountFollowRequestsSubcommand = &cobra.Command{ |
|
171 |
Use: "follow-requests", |
|
172 |
Aliases: []string{"follow-request", "fr"}, |
|
173 |
Example: ` madonctl accounts follow-requests --list |
|
174 |
madonctl accounts follow-requests --account-id X --accept |
|
175 |
madonctl accounts follow-requests --account-id Y --reject`, |
|
176 |
Short: "List, accept or deny a follow request", |
|
177 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
12
e94c9ed9b1c8
Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
178 |
return accountSubcommandsRunE(cmd.Name(), args) |
0 | 179 |
}, |
180 |
} |
|
181 |
var accountFollowSubcommand = &cobra.Command{ |
|
182 |
Use: "follow", |
|
183 |
Short: "Follow or unfollow the account", |
|
184 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
12
e94c9ed9b1c8
Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
185 |
return accountSubcommandsRunE(cmd.Name(), args) |
0 | 186 |
}, |
187 |
} |
|
188 |
||
189 |
var accountBlockSubcommand = &cobra.Command{ |
|
190 |
Use: "block", |
|
191 |
Short: "Block or unblock the account", |
|
192 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
12
e94c9ed9b1c8
Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
193 |
return accountSubcommandsRunE(cmd.Name(), args) |
0 | 194 |
}, |
195 |
} |
|
196 |
||
197 |
var accountMuteSubcommand = &cobra.Command{ |
|
198 |
Use: "mute", |
|
199 |
Short: "Mute or unmute the account", |
|
200 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
12
e94c9ed9b1c8
Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
201 |
return accountSubcommandsRunE(cmd.Name(), args) |
0 | 202 |
}, |
203 |
} |
|
204 |
||
205 |
var accountRelationshipsSubcommand = &cobra.Command{ |
|
206 |
Use: "relationships --account-ids ACC1,ACC2...", |
|
207 |
Short: "List relationships with the accounts", |
|
208 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
12
e94c9ed9b1c8
Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
209 |
return accountSubcommandsRunE(cmd.Name(), args) |
0 | 210 |
}, |
211 |
} |
|
212 |
||
213 |
var accountReportsSubcommand = &cobra.Command{ |
|
214 |
Use: "reports", |
|
215 |
Short: "List reports or report a user account", |
|
216 |
Example: ` madonctl accounts reports --list |
|
217 |
madonctl accounts reports --account-id ACCOUNT --status-ids ID... --comment TEXT`, |
|
218 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
12
e94c9ed9b1c8
Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
219 |
return accountSubcommandsRunE(cmd.Name(), args) |
0 | 220 |
}, |
221 |
} |
|
222 |
||
6 | 223 |
var accountUpdateSubcommand = &cobra.Command{ |
224 |
Use: "update", |
|
225 |
Short: "Update connected user account", |
|
226 |
Long: `Update connected user account |
|
227 |
||
228 |
All flags are optional (set to an empty string if you want to delete a field). |
|
229 |
The flags --avatar and --header can be paths to image files or base64-encoded |
|
230 |
images (see Mastodon API specifications for the details). |
|
231 |
||
232 |
Please note the avatar and header images cannot be removed, they can only be |
|
233 |
replaced.`, |
|
234 |
Example: ` madonctl accounts update --display-name "Mr President" |
|
235 |
madonctl accounts update --note "I like madonctl" |
|
236 |
madonctl accounts update --avatar happyface.png`, |
|
237 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
238 |
return accountSubcommandsRunE(cmd.Name(), args) |
|
239 |
}, |
|
240 |
} |
|
241 |
||
0 | 242 |
// accountSubcommandsRunE is a generic function for status subcommands |
243 |
func accountSubcommandsRunE(subcmd string, args []string) error { |
|
244 |
opt := accountsOpts |
|
245 |
||
16 | 246 |
if opt.accountUID != "" { |
247 |
if opt.accountID > 0 { |
|
248 |
return errors.New("cannot use both account ID and UID") |
|
249 |
} |
|
250 |
// Sign in early to look the user id up |
|
102
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
251 |
var err error |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
252 |
if err = madonInit(true); err != nil { |
16 | 253 |
return err |
254 |
} |
|
102
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
255 |
opt.accountID, err = accountLookupUser(opt.accountUID) |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
256 |
if err != nil || opt.accountID < 1 { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
257 |
if err != nil { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
258 |
errPrint("Cannot find user '%s': %v", opt.accountUID, err) |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
259 |
} else { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
260 |
errPrint("Cannot find user '%s'", opt.accountUID) |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
261 |
} |
16 | 262 |
os.Exit(1) |
263 |
} |
|
264 |
} |
|
265 |
||
0 | 266 |
switch subcmd { |
6 | 267 |
case "show", "search", "update": |
0 | 268 |
// These subcommands do not require an account ID |
269 |
case "favourites", "blocks", "mutes": |
|
270 |
// Those subcommands can not use an account ID |
|
271 |
if opt.accountID > 0 { |
|
272 |
return errors.New("useless account ID") |
|
273 |
} |
|
274 |
case "follow": |
|
275 |
if opt.accountID < 1 && opt.remoteUID == "" { |
|
276 |
return errors.New("missing account ID or URI") |
|
277 |
} |
|
278 |
if opt.accountID > 0 && opt.remoteUID != "" { |
|
279 |
return errors.New("cannot use both account ID and URI") |
|
280 |
} |
|
281 |
if opt.unset && opt.accountID < 1 { |
|
282 |
return errors.New("unfollowing requires an account ID") |
|
283 |
} |
|
284 |
case "follow-requests": |
|
285 |
if opt.list { |
|
286 |
if opt.acceptFR || opt.rejectFR { |
|
287 |
return errors.New("incompatible options") |
|
288 |
} |
|
289 |
} else { |
|
290 |
if !opt.acceptFR && !opt.rejectFR { // No flag |
|
291 |
return errors.New("missing parameter (--list, --accept or --reject)") |
|
292 |
} |
|
293 |
// This is a FR reply |
|
294 |
if opt.acceptFR && opt.rejectFR { |
|
295 |
return errors.New("incompatible options") |
|
296 |
} |
|
297 |
if opt.accountID < 1 { |
|
298 |
return errors.New("missing account ID") |
|
299 |
} |
|
300 |
} |
|
301 |
case "relationships": |
|
302 |
if opt.accountID < 1 && len(opt.accountIDs) == 0 { |
|
303 |
return errors.New("missing account IDs") |
|
304 |
} |
|
305 |
if opt.accountID > 0 && len(opt.accountIDs) > 0 { |
|
306 |
return errors.New("incompatible options") |
|
307 |
} |
|
308 |
case "reports": |
|
309 |
if opt.list { |
|
310 |
break // No argument needed |
|
311 |
} |
|
312 |
if opt.accountID < 1 || len(opt.statusIDs) == 0 || opt.comment == "" { |
|
313 |
return errors.New("missing parameter") |
|
314 |
} |
|
28
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
315 |
case "followers", "following", "statuses": |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
316 |
// If the user's account ID is missing, get it |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
317 |
if opt.accountID < 1 { |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
318 |
// Sign in now to look the user id up |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
319 |
if err := madonInit(true); err != nil { |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
320 |
return err |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
321 |
} |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
322 |
account, err := gClient.GetCurrentAccount() |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
323 |
if err != nil { |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
324 |
return err |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
325 |
} |
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
326 |
opt.accountID = account.ID |
29
4f12e5d4ef75
Display account ID in verbose mode when it is looked up
Mikael Berthe <mikael@lilotux.net>
parents:
28
diff
changeset
|
327 |
if verbose { |
4f12e5d4ef75
Display account ID in verbose mode when it is looked up
Mikael Berthe <mikael@lilotux.net>
parents:
28
diff
changeset
|
328 |
errPrint("User account ID: %d", opt.accountID) |
4f12e5d4ef75
Display account ID in verbose mode when it is looked up
Mikael Berthe <mikael@lilotux.net>
parents:
28
diff
changeset
|
329 |
} |
28
79aa812c0dd2
Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
330 |
} |
0 | 331 |
default: |
332 |
// The other subcommands here require an account ID |
|
333 |
if opt.accountID < 1 { |
|
334 |
return errors.New("missing account ID") |
|
335 |
} |
|
336 |
} |
|
337 |
||
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
338 |
var limOpts *madon.LimitParams |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
29
diff
changeset
|
339 |
if opt.all || opt.limit > 0 || opt.sinceID > 0 || opt.maxID > 0 { |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
340 |
limOpts = new(madon.LimitParams) |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
29
diff
changeset
|
341 |
limOpts.All = opt.all |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
342 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
343 |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
344 |
if opt.limit > 0 { |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
345 |
limOpts.Limit = int(opt.limit) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
346 |
} |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
347 |
if opt.maxID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
348 |
limOpts.MaxID = opt.maxID |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
349 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
350 |
if opt.sinceID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
351 |
limOpts.SinceID = opt.sinceID |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
352 |
} |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
353 |
|
0 | 354 |
// All account subcommands need to have signed in |
355 |
if err := madonInit(true); err != nil { |
|
356 |
return err |
|
357 |
} |
|
358 |
||
359 |
var obj interface{} |
|
360 |
var err error |
|
361 |
||
362 |
switch subcmd { |
|
363 |
case "show": |
|
364 |
var account *madon.Account |
|
365 |
if opt.accountID > 0 { |
|
366 |
account, err = gClient.GetAccount(opt.accountID) |
|
367 |
} else { |
|
368 |
account, err = gClient.GetCurrentAccount() |
|
369 |
} |
|
370 |
obj = account |
|
371 |
case "search": |
|
372 |
var accountList []madon.Account |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
373 |
accountList, err = gClient.SearchAccounts(strings.Join(args, " "), limOpts) |
0 | 374 |
obj = accountList |
375 |
case "followers": |
|
376 |
var accountList []madon.Account |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
377 |
accountList, err = gClient.GetAccountFollowers(opt.accountID, limOpts) |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
378 |
if opt.keep > 0 && len(accountList) > int(opt.keep) { |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
379 |
accountList = accountList[:opt.keep] |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
380 |
} |
0 | 381 |
obj = accountList |
382 |
case "following": |
|
383 |
var accountList []madon.Account |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
384 |
accountList, err = gClient.GetAccountFollowing(opt.accountID, limOpts) |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
385 |
if opt.keep > 0 && len(accountList) > int(opt.keep) { |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
386 |
accountList = accountList[:opt.keep] |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
387 |
} |
0 | 388 |
obj = accountList |
389 |
case "statuses": |
|
390 |
var statusList []madon.Status |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
391 |
statusList, err = gClient.GetAccountStatuses(opt.accountID, opt.onlyMedia, opt.excludeReplies, limOpts) |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
392 |
if opt.keep > 0 && len(statusList) > int(opt.keep) { |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
393 |
statusList = statusList[:opt.keep] |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
394 |
} |
0 | 395 |
obj = statusList |
396 |
case "follow": |
|
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
397 |
var relationship *madon.Relationship |
0 | 398 |
if opt.unset { |
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
399 |
relationship, err = gClient.UnfollowAccount(opt.accountID) |
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
400 |
obj = relationship |
0 | 401 |
} else { |
402 |
if opt.accountID > 0 { |
|
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
403 |
relationship, err = gClient.FollowAccount(opt.accountID) |
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
404 |
obj = relationship |
0 | 405 |
} else { |
406 |
var account *madon.Account |
|
407 |
account, err = gClient.FollowRemoteAccount(opt.remoteUID) |
|
408 |
obj = account |
|
409 |
} |
|
410 |
} |
|
411 |
case "follow-requests": |
|
412 |
if opt.list { |
|
413 |
var followRequests []madon.Account |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
414 |
followRequests, err = gClient.GetAccountFollowRequests(limOpts) |
160
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
415 |
if opt.accountID > 0 { // Display a specific request |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
416 |
var fRequest *madon.Account |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
417 |
for _, fr := range followRequests { |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
418 |
if fr.ID == opt.accountID { |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
419 |
fRequest = &fr |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
420 |
break |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
421 |
} |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
422 |
} |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
423 |
if fRequest != nil { |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
424 |
followRequests = []madon.Account{*fRequest} |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
425 |
} else { |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
426 |
followRequests = []madon.Account{} |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
427 |
} |
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
428 |
} else { |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
429 |
if opt.keep > 0 && len(followRequests) > int(opt.keep) { |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
430 |
followRequests = followRequests[:opt.keep] |
160
452865b363fb
Allow accounts follow-requests --account-id X --list
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
431 |
} |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
432 |
} |
0 | 433 |
obj = followRequests |
434 |
} else { |
|
435 |
err = gClient.FollowRequestAuthorize(opt.accountID, !opt.rejectFR) |
|
436 |
} |
|
437 |
case "block": |
|
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
438 |
var relationship *madon.Relationship |
0 | 439 |
if opt.unset { |
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
440 |
relationship, err = gClient.UnblockAccount(opt.accountID) |
0 | 441 |
} else { |
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
442 |
relationship, err = gClient.BlockAccount(opt.accountID) |
0 | 443 |
} |
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
444 |
obj = relationship |
0 | 445 |
case "mute": |
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
446 |
var relationship *madon.Relationship |
0 | 447 |
if opt.unset { |
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
448 |
relationship, err = gClient.UnmuteAccount(opt.accountID) |
0 | 449 |
} else { |
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
450 |
relationship, err = gClient.MuteAccount(opt.accountID) |
0 | 451 |
} |
145
0f6b8411ad36
Sync with Madon library 1.6: Some calls return a Relationship entity
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
452 |
obj = relationship |
0 | 453 |
case "favourites": |
454 |
var statusList []madon.Status |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
455 |
statusList, err = gClient.GetFavourites(limOpts) |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
456 |
if opt.keep > 0 && len(statusList) > int(opt.keep) { |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
457 |
statusList = statusList[:opt.keep] |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
458 |
} |
0 | 459 |
obj = statusList |
460 |
case "blocks": |
|
461 |
var accountList []madon.Account |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
462 |
accountList, err = gClient.GetBlockedAccounts(limOpts) |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
463 |
if opt.keep > 0 && len(accountList) > int(opt.keep) { |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
464 |
accountList = accountList[:opt.keep] |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
465 |
} |
0 | 466 |
obj = accountList |
467 |
case "mutes": |
|
468 |
var accountList []madon.Account |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
469 |
accountList, err = gClient.GetMutedAccounts(limOpts) |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
470 |
if opt.keep > 0 && len(accountList) > int(opt.keep) { |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
471 |
accountList = accountList[:opt.keep] |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
472 |
} |
0 | 473 |
obj = accountList |
474 |
case "relationships": |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
475 |
var ids []int64 |
0 | 476 |
ids, err = splitIDs(opt.accountIDs) |
477 |
if err != nil { |
|
478 |
return errors.New("cannot parse account IDs") |
|
479 |
} |
|
480 |
if opt.accountID > 0 { // Allow --account-id |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
481 |
ids = []int64{opt.accountID} |
0 | 482 |
} |
483 |
if len(ids) < 1 { |
|
484 |
return errors.New("missing account IDs") |
|
485 |
} |
|
486 |
var relationships []madon.Relationship |
|
487 |
relationships, err = gClient.GetAccountRelationships(ids) |
|
488 |
obj = relationships |
|
489 |
case "reports": |
|
490 |
if opt.list { |
|
491 |
var reports []madon.Report |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
492 |
reports, err = gClient.GetReports(limOpts) |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
493 |
if opt.keep > 0 && len(reports) > int(opt.keep) { |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
160
diff
changeset
|
494 |
reports = reports[:opt.keep] |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
495 |
} |
0 | 496 |
obj = reports |
497 |
break |
|
498 |
} |
|
499 |
// Send a report |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
500 |
var ids []int64 |
0 | 501 |
ids, err = splitIDs(opt.statusIDs) |
502 |
if err != nil { |
|
503 |
return errors.New("cannot parse status IDs") |
|
504 |
} |
|
505 |
if len(ids) < 1 { |
|
506 |
return errors.New("missing status IDs") |
|
507 |
} |
|
508 |
var report *madon.Report |
|
509 |
report, err = gClient.ReportUser(opt.accountID, ids, opt.comment) |
|
510 |
obj = report |
|
6 | 511 |
case "update": |
512 |
var dn, note, avatar, header *string |
|
513 |
change := false |
|
514 |
if updateFlags.Lookup("display-name").Changed { |
|
515 |
dn = &opt.displayName |
|
516 |
change = true |
|
517 |
} |
|
518 |
if updateFlags.Lookup("note").Changed { |
|
519 |
note = &opt.note |
|
520 |
change = true |
|
521 |
} |
|
522 |
if updateFlags.Lookup("avatar").Changed { |
|
523 |
avatar = &opt.avatar |
|
524 |
change = true |
|
525 |
} |
|
526 |
if updateFlags.Lookup("header").Changed { |
|
527 |
header = &opt.header |
|
528 |
change = true |
|
529 |
} |
|
530 |
||
531 |
if !change { // We want at least one update |
|
532 |
return errors.New("missing parameters") |
|
533 |
} |
|
534 |
||
535 |
var account *madon.Account |
|
536 |
account, err = gClient.UpdateAccount(dn, note, avatar, header) |
|
537 |
obj = account |
|
0 | 538 |
default: |
539 |
return errors.New("accountSubcommand: internal error") |
|
540 |
} |
|
541 |
||
542 |
if err != nil { |
|
543 |
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
|
544 |
os.Exit(1) |
0 | 545 |
} |
546 |
if obj == nil { |
|
547 |
return nil |
|
548 |
} |
|
549 |
||
550 |
p, err := getPrinter() |
|
551 |
if err != nil { |
|
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
552 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
553 |
os.Exit(1) |
0 | 554 |
} |
110 | 555 |
return p.printObj(obj) |
0 | 556 |
} |
102
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
557 |
|
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
558 |
// accountLookupUser tries to find a (single) user matching 'user' |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
559 |
// If the user is an HTTP URL, it will use the search API, else |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
560 |
// it will use the accounts/search API. |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
561 |
func accountLookupUser(user string) (int64, error) { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
562 |
var accID int64 |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
563 |
|
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
564 |
if strings.HasPrefix(user, "https://") || strings.HasPrefix(user, "http://") { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
565 |
res, err := gClient.Search(user, true) |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
566 |
if err != nil { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
567 |
return 0, err |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
568 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
569 |
if res != nil { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
570 |
if len(res.Accounts) > 1 { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
571 |
return 0, errors.New("several results") |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
572 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
573 |
if len(res.Accounts) == 1 { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
574 |
accID = res.Accounts[0].ID |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
575 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
576 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
577 |
} else { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
578 |
// Remove leading '@' |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
579 |
user = strings.TrimLeft(user, "@") |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
580 |
|
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
581 |
accList, err := gClient.SearchAccounts(user, &madon.LimitParams{Limit: 2}) |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
582 |
if err != nil { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
583 |
return 0, err |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
584 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
585 |
for _, u := range accList { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
586 |
if u.Acct == user { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
587 |
accID = u.ID |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
588 |
break |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
589 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
590 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
591 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
592 |
|
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
593 |
if accID < 1 { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
594 |
return 0, errors.New("user not found") |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
595 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
596 |
if verbose { |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
597 |
errPrint("User '%s' is account ID %d", user, user) |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
598 |
} |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
599 |
return accID, nil |
84ad56b643c8
Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
600 |
} |