-
Notifications
You must be signed in to change notification settings - Fork 13
/
status.go
307 lines (272 loc) · 8.65 KB
/
status.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Copyright 2017-2018 Mikael Berthe
Licensed under the MIT license. Please see the LICENSE file is this directory.
*/
package madon
import (
"fmt"
"github.com/pkg/errors"
"github.com/sendgrid/rest"
)
// PostStatusParams contains option fields for the PostStatus command
type PostStatusParams struct {
Text string
InReplyTo ActivityID
MediaIDs []ActivityID
Sensitive bool
SpoilerText string
Visibility string
}
// updateStatusOptions contains option fields for POST and DELETE API calls
type updateStatusOptions struct {
// The ID is used for most commands
ID ActivityID
// The following fields are used for posting a new status
Status string
InReplyToID ActivityID
MediaIDs []ActivityID
Sensitive bool
SpoilerText string
Visibility string // "direct", "private", "unlisted" or "public"
}
// getMultipleStatuses returns a list of status entities
// If lopt.All is true, several requests will be made until the API server
// has nothing to return.
func (mc *Client) getMultipleStatuses(endPoint string, params apiCallParams, lopt *LimitParams) ([]Status, error) {
var statuses []Status
var links apiLinks
if err := mc.apiCall("v1/"+endPoint, rest.Get, params, lopt, &links, &statuses); err != nil {
return nil, err
}
if lopt != nil { // Fetch more pages to reach our limit
for (lopt.All || lopt.Limit > len(statuses)) && links.next != nil {
statusSlice := []Status{}
newlopt := links.next
links = apiLinks{}
if err := mc.apiCall("v1/"+endPoint, rest.Get, params, newlopt, &links, &statusSlice); err != nil {
return nil, err
}
statuses = append(statuses, statusSlice...)
}
}
return statuses, nil
}
// queryStatusData queries the statuses API
// The operation 'op' can be empty or "status" (the status itself), "context",
// "card", "reblogged_by", "favourited_by".
// The data argument will receive the object(s) returned by the API server.
func (mc *Client) queryStatusData(statusID ActivityID, op string, data interface{}) error {
if statusID == "" {
return ErrInvalidID
}
endPoint := "statuses/" + statusID
if op != "" && op != "status" {
switch op {
case "context", "card", "reblogged_by", "favourited_by":
default:
return ErrInvalidParameter
}
endPoint += "/" + op
}
return mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, data)
}
// updateStatusData updates the statuses
// The operation 'op' can be empty or "status" (to post a status), "delete"
// (for deleting a status), "reblog"/"unreblog", "favourite"/"unfavourite",
// "mute"/"unmute" (for conversations) or "pin"/"unpin".
// The data argument will receive the object(s) returned by the API server.
func (mc *Client) updateStatusData(op string, opts updateStatusOptions, data interface{}) error {
method := rest.Post
endPoint := "statuses"
params := make(apiCallParams)
switch op {
case "", "status":
op = "status"
if opts.Status == "" {
return ErrInvalidParameter
}
switch opts.Visibility {
case "", "direct", "private", "unlisted", "public":
// Okay
default:
return ErrInvalidParameter
}
if len(opts.MediaIDs) > 4 {
return errors.New("too many (>4) media IDs")
}
case "delete":
method = rest.Delete
if opts.ID == "" {
return ErrInvalidID
}
endPoint += "/" + opts.ID
case "reblog", "unreblog", "favourite", "unfavourite":
if opts.ID == "" {
return ErrInvalidID
}
endPoint += "/" + opts.ID + "/" + op
case "mute", "unmute", "pin", "unpin":
if opts.ID == "" {
return ErrInvalidID
}
endPoint += "/" + opts.ID + "/" + op
default:
return ErrInvalidParameter
}
// Form items for a new toot
if op == "status" {
params["status"] = opts.Status
if opts.InReplyToID != "" {
params["in_reply_to_id"] = opts.InReplyToID
}
for i, id := range opts.MediaIDs {
if id == "" {
return ErrInvalidID
}
qID := fmt.Sprintf("[%d]media_ids", i)
params[qID] = id
}
if opts.Sensitive {
params["sensitive"] = "true"
}
if opts.SpoilerText != "" {
params["spoiler_text"] = opts.SpoilerText
}
if opts.Visibility != "" {
params["visibility"] = opts.Visibility
}
}
return mc.apiCall("v1/"+endPoint, method, params, nil, nil, data)
}
// GetStatus returns a status
// The returned status can be nil if there is an error or if the
// requested ID does not exist.
func (mc *Client) GetStatus(statusID ActivityID) (*Status, error) {
var status Status
if err := mc.queryStatusData(statusID, "status", &status); err != nil {
return nil, err
}
if status.ID == "" {
return nil, ErrEntityNotFound
}
return &status, nil
}
// GetStatusContext returns a status context
func (mc *Client) GetStatusContext(statusID ActivityID) (*Context, error) {
var context Context
if err := mc.queryStatusData(statusID, "context", &context); err != nil {
return nil, err
}
return &context, nil
}
// GetStatusCard returns a status card
func (mc *Client) GetStatusCard(statusID ActivityID) (*Card, error) {
var card Card
if err := mc.queryStatusData(statusID, "card", &card); err != nil {
return nil, err
}
return &card, nil
}
// GetStatusRebloggedBy returns a list of the accounts who reblogged a status
func (mc *Client) GetStatusRebloggedBy(statusID ActivityID, lopt *LimitParams) ([]Account, error) {
o := &getAccountsOptions{ID: statusID, Limit: lopt}
return mc.getMultipleAccountsHelper("reblogged_by", o)
}
// GetStatusFavouritedBy returns a list of the accounts who favourited a status
func (mc *Client) GetStatusFavouritedBy(statusID ActivityID, lopt *LimitParams) ([]Account, error) {
o := &getAccountsOptions{ID: statusID, Limit: lopt}
return mc.getMultipleAccountsHelper("favourited_by", o)
}
// PostStatus posts a new "toot"
// All parameters but "text" can be empty.
// Visibility must be empty, or one of "direct", "private", "unlisted" and "public".
func (mc *Client) PostStatus(cmdParams PostStatusParams) (*Status, error) {
var status Status
o := updateStatusOptions{
Status: cmdParams.Text,
InReplyToID: cmdParams.InReplyTo,
MediaIDs: cmdParams.MediaIDs,
Sensitive: cmdParams.Sensitive,
SpoilerText: cmdParams.SpoilerText,
Visibility: cmdParams.Visibility,
}
err := mc.updateStatusData("status", o, &status)
if err != nil {
return nil, err
}
if status.ID == "" {
return nil, ErrEntityNotFound // TODO Change error message
}
return &status, err
}
// DeleteStatus deletes a status
func (mc *Client) DeleteStatus(statusID ActivityID) error {
var status Status
o := updateStatusOptions{ID: statusID}
err := mc.updateStatusData("delete", o, &status)
return err
}
// ReblogStatus reblogs a status
func (mc *Client) ReblogStatus(statusID ActivityID) error {
var status Status
o := updateStatusOptions{ID: statusID}
err := mc.updateStatusData("reblog", o, &status)
return err
}
// UnreblogStatus unreblogs a status
func (mc *Client) UnreblogStatus(statusID ActivityID) error {
var status Status
o := updateStatusOptions{ID: statusID}
err := mc.updateStatusData("unreblog", o, &status)
return err
}
// FavouriteStatus favourites a status
func (mc *Client) FavouriteStatus(statusID ActivityID) error {
var status Status
o := updateStatusOptions{ID: statusID}
err := mc.updateStatusData("favourite", o, &status)
return err
}
// UnfavouriteStatus unfavourites a status
func (mc *Client) UnfavouriteStatus(statusID ActivityID) error {
var status Status
o := updateStatusOptions{ID: statusID}
err := mc.updateStatusData("unfavourite", o, &status)
return err
}
// PinStatus pins a status
func (mc *Client) PinStatus(statusID ActivityID) error {
var status Status
o := updateStatusOptions{ID: statusID}
err := mc.updateStatusData("pin", o, &status)
return err
}
// UnpinStatus unpins a status
func (mc *Client) UnpinStatus(statusID ActivityID) error {
var status Status
o := updateStatusOptions{ID: statusID}
err := mc.updateStatusData("unpin", o, &status)
return err
}
// MuteConversation mutes the conversation containing a status
func (mc *Client) MuteConversation(statusID ActivityID) (*Status, error) {
var status Status
o := updateStatusOptions{ID: statusID}
err := mc.updateStatusData("mute", o, &status)
return &status, err
}
// UnmuteConversation unmutes the conversation containing a status
func (mc *Client) UnmuteConversation(statusID ActivityID) (*Status, error) {
var status Status
o := updateStatusOptions{ID: statusID}
err := mc.updateStatusData("unmute", o, &status)
return &status, err
}
// GetFavourites returns the list of the user's favourites
// If lopt.All is true, several requests will be made until the API server
// has nothing to return.
// If lopt.Limit is set (and not All), several queries can be made until the
// limit is reached.
func (mc *Client) GetFavourites(lopt *LimitParams) ([]Status, error) {
return mc.getMultipleStatuses("favourites", nil, lopt)
}