/
toot.go
272 lines (233 loc) · 6.68 KB
/
toot.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package mastodon
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/arrow2nd/nekomata/api/sharedapi"
)
// status : 投稿
type status struct {
// ID : 投稿ID
ID string `json:"id"`
// CreatedAt : 投稿日時
CreatedAt time.Time `json:"created_at"`
// Sensitive : センシティブな内容を含むか
Sensitive bool `json:"sensitive"`
// Visibility : 公開範囲 (public, unlisted, private, direct)
Visibility string `json:"visibility"`
// ReblogsCount : ブースト数
ReblogsCount int `json:"reblogs_count"`
// FavouritesCount : いいね数
FavouritesCount int `json:"favourites_count"`
// Favourited : いいね済か
Favourited bool `json:"favourited"`
// Reblogged : ブースト済か
Reblogged bool `json:"reblogged"`
// Bookmarked : ブックマーク済か
Bookmarked bool `json:"bookmarked"`
// Content : 本文 (HTML)
Content string `json:"content"`
// SpoilerText : 要約文 (これより下の文章は展開するまで折りたたまれる)
SpoilerText string `json:"spoiler_text"`
// Reblog : 引用元
Reblog *status `json:"reblog"`
// Application : 投稿元のクライアント
Application *struct {
Name string `json:"name"`
} `json:"application"`
// Account : 投稿ユーザー
Account account `json:"account"`
// MediaAttachments : 添付メディア
MediaAttachments []media `json:"media_attachments"`
// Mentions : メンション先
Mentions []mentionAttribute `json:"mentions"`
// Tags : タグ
Tags []struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"tags"`
// Poll : アンケート
Poll *poll `json:"poll"`
}
// mentionAttribute : メンション先のユーザー
type mentionAttribute struct {
ID string `json:"id"`
Username string `json:"username"`
Acct string `json:"acct"`
}
// poll : アンケート
type poll struct {
// ID : アンケートID
ID string `json:"id"`
// ExpiresAt : 終了日時
ExpiresAt *time.Time `json:"expires_at"`
// Expired : 終了しているか
Expired bool `json:"expired"`
// Multiple : 複数投票可能か
Multiple bool `json:"multiple"`
// VotesCount : 総投票数
VotesCount int `json:"votes_count"`
// VotersCount : 総投票ユーザー数
VotersCount *int `json:"voters_count"`
// Voted : 投票済か
Voted bool `json:"voted"`
// OwnVotes : 自分が投票した項目
OwnVotes []int `json:"own_votes"`
// Options : アンケート項目
Options []pollOption `json:"options"`
}
// pollOption : アンケート項目
type pollOption struct {
// Title : タイトル
Title string `json:"title"`
// VotersCount : 投票数
VotesCount int `json:"votes_count"`
}
// ToShared : api.Post に変換
// TODO: テスト書く
func (s *status) ToShared() *sharedapi.Post {
post := &sharedapi.Post{
ID: s.ID,
CreatedAt: s.CreatedAt,
Visibility: s.Visibility,
Sensitive: s.Sensitive,
RepostCount: s.ReblogsCount,
Reactions: []sharedapi.Reaction{{
Name: "Like",
Count: s.FavouritesCount,
Reacted: s.Favourited,
}},
Reposted: s.Reblogged,
Bookmarked: s.Bookmarked,
Text: html2text(s.Content),
Tags: []sharedapi.Tag{},
Mentions: []sharedapi.Mention{},
Author: s.Account.ToShared(),
}
if s.Tags != nil && len(s.Tags) > 0 {
for _, tag := range s.Tags {
post.Tags = append(post.Tags, sharedapi.Tag{
Name: tag.Name,
URL: tag.URL,
})
}
}
if s.Mentions != nil && len(s.Mentions) > 0 {
for _, mention := range s.Mentions {
post.Mentions = append(post.Mentions, sharedapi.Mention{
ID: mention.ID,
DisplayName: mention.Username,
Username: mention.Acct,
})
}
}
if app := s.Application; app != nil {
post.Via = app.Name
}
if s.Reblog != nil {
post.Reference = s.Reblog.ToShared()
}
return post
}
func (m *Mastodon) createPostQuery(opts *sharedapi.CreatePostOpts) url.Values {
q := url.Values{}
q.Add("status", opts.Text)
q.Add("visibility", opts.Visibility)
if opts.Sensitive {
q.Add("sensitive", "true")
}
if len(opts.MediaIDs) > 0 {
q.Add("media_ids[]", strings.Join(opts.MediaIDs, ","))
}
return q
}
func (m *Mastodon) CreatePost(opts *sharedapi.CreatePostOpts) (*sharedapi.Post, error) {
q := m.createPostQuery(opts)
requestOpts := &requestOpts{
method: http.MethodPost,
url: endpointStatuses.URL(m.user.Server, nil),
q: q,
isAuth: true,
}
res := status{}
if err := m.request(requestOpts, &res); err != nil {
return nil, err
}
return res.ToShared(), nil
}
func (m *Mastodon) QuotePost(id string, opts *sharedapi.CreatePostOpts) (*sharedapi.Post, error) {
// NOTE: 引用する機能が公式の手段では存在しないので実装しない
return nil, errors.New("quote is not available on Mastodon")
}
func (m *Mastodon) ReplyPost(replyToId string, opts *sharedapi.CreatePostOpts) (*sharedapi.Post, error) {
q := m.createPostQuery(opts)
q.Add("in_reply_to_id", replyToId)
requestOpts := &requestOpts{
method: http.MethodPost,
url: endpointStatuses.URL(m.user.Server, nil),
q: q,
isAuth: true,
}
res := status{}
if err := m.request(requestOpts, &res); err != nil {
return nil, err
}
return res.ToShared(), nil
}
func (m *Mastodon) DeletePost(id string) error {
u, err := url.JoinPath(endpointStatuses.URL(m.user.Server, nil), id)
if err != nil {
return fmt.Errorf("failed to delete: %w", err)
}
opts := &requestOpts{
method: http.MethodDelete,
url: u,
q: nil,
isAuth: true,
}
return m.request(opts, nil)
}
func (m *Mastodon) doTootAction(id string, e sharedapi.Endpoint) (*sharedapi.Post, error) {
p := url.Values{}
p.Add(":id", id)
opts := &requestOpts{
method: http.MethodPost,
url: e.URL(m.user.Server, p),
q: nil,
isAuth: true,
}
res := status{}
if err := m.request(opts, &res); err != nil {
return nil, err
}
return res.ToShared(), nil
}
func (m *Mastodon) Reaction(id, reaction string) (*sharedapi.Post, error) {
return m.doTootAction(id, endpointFavourite)
}
func (m *Mastodon) Unreaction(id string) (*sharedapi.Post, error) {
return m.doTootAction(id, endpointUnfavourite)
}
func (m *Mastodon) Repost(id string) (*sharedapi.Post, error) {
post, err := m.doTootAction(id, endpointReblog)
if err != nil {
return nil, err
}
return post.Reference, nil
}
func (m *Mastodon) Unrepost(id string) (*sharedapi.Post, error) {
return m.doTootAction(id, endpointUnreblog)
}
func (m *Mastodon) Bookmark(id string) (*sharedapi.Post, error) {
return m.doTootAction(id, endpointBookmark)
}
func (m *Mastodon) Unbookmark(id string) (*sharedapi.Post, error) {
return m.doTootAction(id, endpointUnbookmark)
}
func (m *Mastodon) GetVisibilityList() []string {
// NOTE: directはサポートしない
return []string{"public", "unlisted", "private"}
}