-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_post.go
161 lines (136 loc) · 4.06 KB
/
action_post.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package app
import (
"fmt"
"io"
"strings"
"github.com/arrow2nd/nekomata/api/sharedapi"
"github.com/arrow2nd/nekomata/config"
"github.com/atotto/clipboard"
"github.com/pkg/browser"
)
// actionPost : 投稿に対してアクション
func (p *postList) actionPost(action string) {
target := p.getSelectPost()
if target == nil {
return
}
id := target.ID
f := func() {
var result *sharedapi.Post = nil
err := fmt.Errorf("unknown action: %s", action)
switch action {
case config.ActionReaction:
// TODO: リアクション種別が複数ある場合どうにかする (Misskey)
result, err = global.client.Reaction(id, "")
case config.ActionUnreaction:
result, err = global.client.Unreaction(id)
case config.ActionRepost:
result, err = global.client.Repost(id)
case config.ActionUnrepost:
result, err = global.client.Unrepost(id)
case config.ActionBookmark:
result, err = global.client.Bookmark(id)
case config.ActionUnbookmark:
result, err = global.client.Unbookmark(id)
case config.ActionDelete:
err = global.client.DeletePost(id)
}
if err != nil {
global.SetErrorStatus(action, err.Error())
return
}
// ステータスを反映
switch action {
case config.ActionReaction:
// TODO: リアクション種別が複数ある場合どうにかする (Misskey)
synchronizeResponseCounts(target.Reactions[0].Count, &result.Reactions[0].Count, 1)
target.Reactions = result.Reactions
case config.ActionUnreaction:
// TODO: リアクション種別が複数ある場合どうにかする (Misskey)
synchronizeResponseCounts(target.Reactions[0].Count, &result.Reactions[0].Count, -1)
target.Reactions = result.Reactions
case config.ActionRepost:
synchronizeResponseCounts(target.RepostCount, &result.RepostCount, 1)
target.Reposted = result.Reposted
target.RepostCount = result.RepostCount
case config.ActionUnrepost:
synchronizeResponseCounts(target.RepostCount, &result.RepostCount, -1)
target.Reposted = result.Reposted
target.RepostCount = result.RepostCount
case config.ActionBookmark, config.ActionUnbookmark:
target.Bookmarked = result.Bookmarked
case config.ActionDelete:
if err := p.DeletePost(id); err != nil {
global.SetErrorStatus("Delete", err.Error())
return
}
}
// 再描画
p.draw(p.getCurrentCursorPos())
if !strings.HasSuffix(action, "e") {
action += "e"
}
global.SetStatus(action+"d", createPostSummary(target))
}
// 確認画面が不要ならそのまま実行
if !global.conf.Pref.Confirm[strings.ToLower(action)] {
f()
return
}
title := fmt.Sprintf(
"Do you want to [%s]%s[-:-:-] this post?",
global.conf.Style.App.EmphasisText,
strings.ToLower(action),
)
global.ReqestPopupModal(&ModalOpts{title, "", f})
}
// synchronizeResponseCounts : レスポンスの数値と実際の状態とを同期させる
func synchronizeResponseCounts(prev int, next *int, add int) {
if prev == *next {
*next = *next + add
}
}
// createPostSummary : 投稿の要約を作成
func createPostSummary(p *sharedapi.Post) string {
text := p.Text
if text == "" {
text = "<empty>"
}
return fmt.Sprintf("%s | %s", createUserSummary(p.Author), text)
}
// openBrowser : 投稿をブラウザで表示
func (p *postList) openBrowser() {
post := p.getSelectPost()
if post == nil {
return
}
u, err := global.client.CreatePostURL(post)
if err != nil {
global.SetErrorStatus("Open", err.Error())
return
}
browser.Stdout = io.Discard
browser.Stderr = io.Discard
if err := browser.OpenURL(u); err != nil {
global.SetErrorStatus("Open", err.Error())
return
}
global.SetStatus("Opened", createPostSummary(post))
}
// copyToClipboard : 投稿のURLをクリップボードにコピー
func (p *postList) copyToClipboard() {
post := p.getSelectPost()
if post == nil {
return
}
u, err := global.client.CreatePostURL(post)
if err != nil {
global.SetErrorStatus("Copy", err.Error())
return
}
if err := clipboard.WriteAll(u); err != nil {
global.SetErrorStatus("Copy", err.Error())
return
}
global.SetStatus("Copied", createPostSummary(post))
}