This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.go
113 lines (95 loc) · 2.5 KB
/
tweet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cmd
import (
"net/url"
"github.com/arrow2nd/ishell"
)
func (cmd *Cmd) newTweetCmd() *ishell.Cmd {
// tweet
tweetCmd := &ishell.Cmd{
Name: "tweet",
Aliases: []string{"tw"},
Func: cmd.execTweetCmd,
Help: "post a tweet",
LongHelp: createLongHelp(
"Post a tweet.\nIf there is no tweet text, 'にゃーん' will be posted.\nIf you are submitting an image, please add the file name separated by a space.",
"tw",
"tweet [text] [image]...",
"tweet meow🐱 cat.png supercat.jpg",
),
}
// tweet multi
tweetCmd.AddCmd(&ishell.Cmd{
Name: "multi",
Aliases: []string{"ml"},
Func: cmd.execTweetMultiCmd,
Help: "post a multi-line tweet",
LongHelp: createLongHelp(
"Post a multi-line tweet.\nEnter a semicolon to end the input.\nAnd if you want to cancel, input \":exit\".",
"ml",
"tweet multi [image]...",
"",
),
})
// tweet remove
tweetCmd.AddCmd(&ishell.Cmd{
Name: "remove",
Aliases: []string{"rm"},
Func: cmd.execTweetRemoveCmd,
Help: "delete a tweet",
LongHelp: createLongHelp(
"Delete a tweet.\nIf there is more than one, please separate them with a space.",
"rm",
"tweet remove [<tweetnumber>]",
"tweet remove 0 1",
),
})
return tweetCmd
}
func (cmd *Cmd) execTweetCmd(c *ishell.Context) {
text, images := cmd.parseTweetCmdArgs(c.Args)
cmd.tweet(text, images)
}
func (cmd *Cmd) execTweetMultiCmd(c *ishell.Context) {
// 添付画像を取得
_, images := cmd.parseTweetCmdArgs(c.Args)
text := cmd.inputMultiLine()
if text == "" {
return
}
cmd.tweet(text, images)
}
func (cmd *Cmd) execTweetRemoveCmd(c *ishell.Context) {
if len(c.Args) <= 0 {
cmd.showWrongArgMessage("tweet " + c.Cmd.Name)
return
}
// 引数の数だけ削除処理
for _, tweetNumStr := range c.Args {
tweetID, err := cmd.view.GetDataFromTweetNum(tweetNumStr, "tweetID")
if err != nil {
cmd.showErrorMessage(err.Error())
return
}
tweetText, err := cmd.api.DeleteTweet(tweetID)
if err != nil {
cmd.showErrorMessage(err.Error())
return
}
cmd.showMessage("DELETED", tweetText, cmd.cfg.Color.Accent2)
}
}
func (cmd *Cmd) tweet(text string, images []string) {
query := url.Values{}
// 画像をアップロード
if err := cmd.upload(images, &query); err != nil {
cmd.showErrorMessage(err.Error())
return
}
// ツイートを投稿
tweetText, err := cmd.api.PostTweet(query, text)
if err != nil {
cmd.showErrorMessage(err.Error())
return
}
cmd.showMessage("TWEETED", tweetText, cmd.cfg.Color.Accent2)
}