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