author | Mikael Berthe <mikael@lilotux.net> |
Sat, 13 May 2017 09:55:07 +0200 | |
changeset 129 | 11966471aac3 |
parent 79 | bca27c55be9f |
child 168 | ce4000ac7294 |
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 ( |
|
129
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
9 |
"strings" |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
10 |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
11 |
"github.com/pkg/errors" |
0 | 12 |
"github.com/spf13/cobra" |
79
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
13 |
"github.com/spf13/viper" |
0 | 14 |
|
15 |
"github.com/McKael/madon" |
|
16 |
) |
|
17 |
||
18 |
// toot is a kind of alias for status post |
|
19 |
||
20 |
func init() { |
|
21 |
RootCmd.AddCommand(tootAliasCmd) |
|
22 |
||
23 |
tootAliasCmd.Flags().BoolVar(&statusOpts.sensitive, "sensitive", false, "Mark post as sensitive (NSFW)") |
|
24 |
tootAliasCmd.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)") |
|
25 |
tootAliasCmd.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)") |
|
26 |
tootAliasCmd.Flags().StringVar(&statusOpts.mediaIDs, "media-ids", "", "Comma-separated list of media IDs") |
|
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
27 |
tootAliasCmd.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media attachment file name") |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
28 |
tootAliasCmd.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)") |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
29 |
tootAliasCmd.Flags().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to") |
71
67e3234c26a9
Add --stdin to send a message from standard input
Mikael Berthe <mikael@lilotux.net>
parents:
70
diff
changeset
|
30 |
tootAliasCmd.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input") |
129
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
31 |
tootAliasCmd.Flags().BoolVar(&statusOpts.addMentions, "add-mentions", false, "Add mentions when replying") |
35
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
32 |
|
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
33 |
// Flag completion |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
34 |
annotation := make(map[string][]string) |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
35 |
annotation[cobra.BashCompCustom] = []string{"__madonctl_visibility"} |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
36 |
|
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
37 |
tootAliasCmd.Flags().Lookup("visibility").Annotations = annotation |
0 | 38 |
} |
39 |
||
40 |
var tootAliasCmd = &cobra.Command{ |
|
41 |
Use: "toot", |
|
42 |
Aliases: []string{"post", "pouet"}, |
|
43 |
Short: "Post a message (toot)", |
|
44 |
Example: ` madonctl toot message |
|
45 |
madonctl toot --spoiler Warning "Hello, World" |
|
46 |
madonctl status post --media-ids ID1,ID2 "Here are the photos" |
|
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
47 |
madonctl post --sensitive --file image.jpg Image |
71
67e3234c26a9
Add --stdin to send a message from standard input
Mikael Berthe <mikael@lilotux.net>
parents:
70
diff
changeset
|
48 |
madonctl toot --text-file message.txt |
129
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
49 |
madonctl toot --in-reply-to STATUSID "@user response" |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
50 |
madonctl toot --in-reply-to STATUSID --add-mentions "response" |
79
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
51 |
echo "Hello from #madonctl" | madonctl toot --visibility unlisted --stdin |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
52 |
|
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
53 |
The default visibility can be set in the configuration file with the option |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
54 |
'default_visibility' (or with an environmnent variable).`, |
0 | 55 |
RunE: func(cmd *cobra.Command, args []string) error { |
56 |
if err := madonInit(true); err != nil { |
|
57 |
return err |
|
58 |
} |
|
59 |
return statusSubcommandRunE("post", args) |
|
60 |
}, |
|
61 |
} |
|
62 |
||
63 |
func toot(tootText string) (*madon.Status, error) { |
|
64 |
opt := statusOpts |
|
65 |
||
79
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
66 |
// Get default visibility from configuration |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
67 |
if opt.visibility == "" { |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
68 |
if v := viper.GetString("default_visibility"); v != "" { |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
69 |
opt.visibility = v |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
70 |
} |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
71 |
} |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
72 |
|
0 | 73 |
switch opt.visibility { |
74 |
case "", "direct", "private", "unlisted", "public": |
|
75 |
// OK |
|
76 |
default: |
|
79
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
77 |
return nil, errors.Errorf("invalid visibility argument value '%s'", opt.visibility) |
0 | 78 |
} |
79 |
||
80 |
if opt.inReplyToID < 0 { |
|
81 |
return nil, errors.New("invalid in-reply-to argument value") |
|
82 |
} |
|
83 |
||
84 |
ids, err := splitIDs(opt.mediaIDs) |
|
85 |
if err != nil { |
|
86 |
return nil, errors.New("cannot parse media IDs") |
|
87 |
} |
|
88 |
||
129
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
89 |
if tootText == "" && len(ids) == 0 && opt.spoiler == "" && opt.mediaFilePath == "" { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
90 |
return nil, errors.New("toot is empty") |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
91 |
} |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
92 |
|
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
93 |
if opt.addMentions && opt.inReplyToID > 0 { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
94 |
mentions, err := mentionsList(opt.inReplyToID) |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
95 |
if err != nil { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
96 |
return nil, err |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
97 |
} |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
98 |
tootText = mentions + tootText |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
99 |
} |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
100 |
|
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
101 |
// Uploading media file last |
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
102 |
if opt.mediaFilePath != "" { |
0 | 103 |
if len(ids) > 3 { |
104 |
return nil, errors.New("too many media attachments") |
|
105 |
} |
|
106 |
||
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
107 |
fileMediaID, err := uploadFile(opt.mediaFilePath) |
0 | 108 |
if err != nil { |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
109 |
return nil, errors.Wrap(err, "cannot attach media file") |
0 | 110 |
} |
111 |
if fileMediaID > 0 { |
|
112 |
ids = append(ids, fileMediaID) |
|
113 |
} |
|
114 |
} |
|
115 |
||
129
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
116 |
return gClient.PostStatus(tootText, opt.inReplyToID, ids, opt.sensitive, opt.spoiler, opt.visibility) |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
117 |
} |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
118 |
|
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
119 |
func mentionsList(statusID int64) (string, error) { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
120 |
a, err := gClient.GetCurrentAccount() |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
121 |
if err != nil { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
122 |
return "", errors.Wrap(err, "cannot check account details") |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
123 |
} |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
124 |
|
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
125 |
s, err := gClient.GetStatus(statusID) |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
126 |
if err != nil { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
127 |
return "", errors.Wrap(err, "cannot get mentions") |
70
155fdea254c3
Do not make an API call if toot has no content
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
128 |
} |
155fdea254c3
Do not make an API call if toot has no content
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
129 |
|
129
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
130 |
var mentions []string |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
131 |
// Add the sender if she is not the connected user |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
132 |
if s.Account.Acct != a.Acct { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
133 |
mentions = append(mentions, "@"+s.Account.Acct) |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
134 |
} |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
135 |
for _, m := range s.Mentions { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
136 |
if m.Acct != a.Acct { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
137 |
mentions = append(mentions, "@"+m.Acct) |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
138 |
} |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
139 |
} |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
140 |
mentionsStr := strings.Join(mentions, " ") |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
141 |
if len(mentionsStr) > 0 { |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
142 |
return mentionsStr + " ", nil |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
143 |
} |
11966471aac3
toot: Add flag --add-mentions to add mentions when replying
Mikael Berthe <mikael@lilotux.net>
parents:
79
diff
changeset
|
144 |
return "", nil |
0 | 145 |
} |