/
flair.go
562 lines (452 loc) · 17.3 KB
/
flair.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package reddit
import (
"bytes"
"context"
"encoding/csv"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
// FlairService handles communication with the flair
// related methods of the Reddit API.
//
// Reddit API docs: https://www.reddit.com/dev/api/#section_flair
type FlairService struct {
client *Client
}
// Flair is a tag that can be attached to a user or a post.
type Flair struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
Color string `json:"text_color,omitempty"`
BackgroundColor string `json:"background_color,omitempty"`
CSSClass string `json:"css_class,omitempty"`
Editable bool `json:"text_editable"`
ModOnly bool `json:"mod_only"`
}
// FlairSummary is a condensed version of Flair.
type FlairSummary struct {
User string `json:"user,omitempty"`
Text string `json:"flair_text,omitempty"`
CSSClass string `json:"flair_css_class,omitempty"`
}
// FlairChoice is a choice of flair when selecting one for yourself or for a post.
type FlairChoice struct {
TemplateID string `json:"flair_template_id"`
Text string `json:"flair_text"`
Editable bool `json:"flair_text_editable"`
Position string `json:"flair_position"`
CSSClass string `json:"flair_css_class"`
}
// FlairConfigureRequest represents a request to configure a subreddit's flair settings.
// Not setting an attribute can have unexpected side effects, so assign every one just in case.
type FlairConfigureRequest struct {
// Enable user flair in the subreddit.
UserFlairEnabled *bool `url:"flair_enabled,omitempty"`
// One of: left, right.
UserFlairPosition string `url:"flair_position,omitempty"`
// Allow users to assign their own flair.
UserFlairSelfAssignEnabled *bool `url:"flair_self_assign_enabled,omitempty"`
// One of: none, left, right.
PostFlairPosition string `url:"link_flair_position,omitempty"`
// Allow submitters to assign their own post flair.
PostFlairSelfAssignEnabled *bool `url:"link_flair_self_assign_enabled,omitempty"`
}
// FlairTemplateCreateOrUpdateRequest represents a request to create/update a flair template.
// Not setting an attribute can have unexpected side effects, so assign every one just in case.
type FlairTemplateCreateOrUpdateRequest struct {
// The id of the template. Only provide this if it's an update request.
// If provided and it's not a valid id, the template will be created.
ID string `url:"flair_template_id,omitempty"`
// One of: all, emoji, text.
AllowableContent string `url:"allowable_content,omitempty"`
// No longer than 64 characters.
Text string `url:"text,omitempty"`
// One of: light, dark.
TextColor string `url:"text_color,omitempty"`
// Allow user to edit the text of the flair.
TextEditable *bool `url:"text_editable,omitempty"`
ModOnly *bool `url:"mod_only,omitempty"`
// Between 1 and 10 (inclusive). Default: 10.
MaxEmojis *int `url:"max_emojis,omitempty"`
// One of: none, transparent, 6-digit rgb hex color, e.g. #AABBCC.
BackgroundColor string `url:"background_color,omitempty"`
CSSClass string `url:"css_class,omitempty"`
}
// FlairTemplate is a generic flair structure that can users can use next to their username
// or posts in a subreddit.
type FlairTemplate struct {
ID string `json:"id"`
// USER_FLAIR (for users) or LINK_FLAIR (for posts).
Type string `json:"flairType"`
ModOnly bool `json:"modOnly"`
AllowableContent string `json:"allowableContent"`
Text string `json:"text"`
TextType string `json:"type"`
TextColor string `json:"textColor"`
TextEditable bool `json:"textEditable"`
RichText []map[string]string `json:"richtext"`
OverrideCSS bool `json:"overrideCss"`
MaxEmojis int `json:"maxEmojis"`
BackgroundColor string `json:"backgroundColor"`
CSSClass string `json:"cssClass"`
}
// FlairSelectRequest represents a request to select a flair.
type FlairSelectRequest struct {
// The id of the template.
ID string `url:"flair_template_id,omitempty"`
// No longer than 64 characters.
// Only use this if the flair is editable (it is by default if you're a mod of the subreddit).
Text string `url:"text,omitempty"`
}
// FlairChangeRequest represents a request to change a user's flair.
// If Text and CSSClass are empty, the request will just clear the user's flair.
type FlairChangeRequest struct {
User string
Text string
CSSClass string
}
// FlairChangeResponse represents a response to a FlairChangeRequest.
type FlairChangeResponse struct {
// Whether or not the request was successful.
OK bool `json:"ok"`
Status string `json:"status"`
Warnings map[string]string `json:"warnings,omitempty"`
Errors map[string]string `json:"errors,omitempty"`
}
// GetUserFlairs returns the user flairs from the subreddit.
func (s *FlairService) GetUserFlairs(ctx context.Context, subreddit string) ([]*Flair, *Response, error) {
path := fmt.Sprintf("r/%s/api/user_flair_v2", subreddit)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var flairs []*Flair
resp, err := s.client.Do(ctx, req, &flairs)
if err != nil {
return nil, resp, err
}
return flairs, resp, nil
}
// GetPostFlairs returns the post flairs from the subreddit.
func (s *FlairService) GetPostFlairs(ctx context.Context, subreddit string) ([]*Flair, *Response, error) {
path := fmt.Sprintf("r/%s/api/link_flair_v2", subreddit)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var flairs []*Flair
resp, err := s.client.Do(ctx, req, &flairs)
if err != nil {
return nil, resp, err
}
return flairs, resp, nil
}
// ListUserFlairs returns all flairs of individual users in the subreddit.
func (s *FlairService) ListUserFlairs(ctx context.Context, subreddit string) ([]*FlairSummary, *Response, error) {
path := fmt.Sprintf("r/%s/api/flairlist", subreddit)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(struct {
UserFlairs []*FlairSummary `json:"users"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.UserFlairs, resp, nil
}
// Configure the subreddit's flair settings.
func (s *FlairService) Configure(ctx context.Context, subreddit string, request *FlairConfigureRequest) (*Response, error) {
if request == nil {
return nil, errors.New("*FlairConfigureRequest: cannot be nil")
}
path := fmt.Sprintf("r/%s/api/flairconfig", subreddit)
form, err := query.Values(request)
if err != nil {
return nil, err
}
form.Set("api_type", "json")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Enable your flair in the subreddit.
func (s *FlairService) Enable(ctx context.Context, subreddit string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/setflairenabled", subreddit)
form := url.Values{}
form.Set("api_type", "json")
form.Set("flair_enabled", "true")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Disable your flair in the subreddit.
func (s *FlairService) Disable(ctx context.Context, subreddit string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/setflairenabled", subreddit)
form := url.Values{}
form.Set("api_type", "json")
form.Set("flair_enabled", "false")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// UpsertUserTemplate creates a user flair template, or updates it if the request.ID is valid.
// It returns the created/updated flair template.
func (s *FlairService) UpsertUserTemplate(ctx context.Context, subreddit string, request *FlairTemplateCreateOrUpdateRequest) (*FlairTemplate, *Response, error) {
if request == nil {
return nil, nil, errors.New("*FlairTemplateCreateOrUpdateRequest: cannot be nil")
}
path := fmt.Sprintf("r/%s/api/flairtemplate_v2", subreddit)
form, err := query.Values(request)
if err != nil {
return nil, nil, err
}
form.Set("api_type", "json")
form.Set("flair_type", "USER_FLAIR")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
root := new(FlairTemplate)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, nil
}
// UpsertPostTemplate creates a post flair template, or updates it if the request.ID is valid.
// It returns the created/updated flair template.
func (s *FlairService) UpsertPostTemplate(ctx context.Context, subreddit string, request *FlairTemplateCreateOrUpdateRequest) (*FlairTemplate, *Response, error) {
if request == nil {
return nil, nil, errors.New("*FlairTemplateCreateOrUpdateRequest: cannot be nil")
}
path := fmt.Sprintf("r/%s/api/flairtemplate_v2", subreddit)
form, err := query.Values(request)
if err != nil {
return nil, nil, err
}
form.Set("api_type", "json")
form.Set("flair_type", "LINK_FLAIR")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
root := new(FlairTemplate)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, nil
}
// Delete the flair of the user.
func (s *FlairService) Delete(ctx context.Context, subreddit, username string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/deleteflair", subreddit)
form := url.Values{}
form.Set("api_type", "json")
form.Set("name", username)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// DeleteTemplate deletes the flair template via its id.
func (s *FlairService) DeleteTemplate(ctx context.Context, subreddit, id string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/deleteflairtemplate", subreddit)
form := url.Values{}
form.Set("api_type", "json")
form.Set("flair_template_id", id)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// DeleteAllUserTemplates deletes all user flair templates.
func (s *FlairService) DeleteAllUserTemplates(ctx context.Context, subreddit string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/clearflairtemplates", subreddit)
form := url.Values{}
form.Set("api_type", "json")
form.Set("flair_type", "USER_FLAIR")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// DeleteAllPostTemplates deletes all post flair templates.
func (s *FlairService) DeleteAllPostTemplates(ctx context.Context, subreddit string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/clearflairtemplates", subreddit)
form := url.Values{}
form.Set("api_type", "json")
form.Set("flair_type", "LINK_FLAIR")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// ReorderUserTemplates reorders the user flair templates in the order provided in the slice.
// The order should contain every single flair id of this flair type; omitting any id will result in an error.
func (s *FlairService) ReorderUserTemplates(ctx context.Context, subreddit string, ids []string) (*Response, error) {
path := fmt.Sprintf("api/v1/%s/flair_template_order/USER_FLAIR", subreddit)
req, err := s.client.NewJSONRequest(http.MethodPatch, path, ids)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// ReorderPostTemplates reorders the post flair templates in the order provided in the slice.
// The order should contain every single flair id of this flair type; omitting any id will result in an error.
func (s *FlairService) ReorderPostTemplates(ctx context.Context, subreddit string, ids []string) (*Response, error) {
path := fmt.Sprintf("api/v1/%s/flair_template_order/LINK_FLAIR", subreddit)
req, err := s.client.NewJSONRequest(http.MethodPatch, path, ids)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Choices returns a list of flairs you can assign to yourself in the subreddit, and your current one.
func (s *FlairService) Choices(ctx context.Context, subreddit string) ([]*FlairChoice, *FlairChoice, *Response, error) {
return s.ChoicesOf(ctx, subreddit, s.client.Username)
}
// ChoicesOf returns a list of flairs the user can assign to themself in the subreddit, and their current one.
// Unless the user is you, this only works if you're a moderator of the subreddit.
func (s *FlairService) ChoicesOf(ctx context.Context, subreddit, username string) ([]*FlairChoice, *FlairChoice, *Response, error) {
path := fmt.Sprintf("r/%s/api/flairselector", subreddit)
form := url.Values{}
form.Set("name", username)
return s.choices(ctx, path, form)
}
// ChoicesForPost returns a list of flairs you can assign to an existing post, and the current one assigned to it.
// If the post isn't yours, this only works if you're the moderator of the subreddit it's in.
func (s *FlairService) ChoicesForPost(ctx context.Context, postID string) ([]*FlairChoice, *FlairChoice, *Response, error) {
path := "api/flairselector"
form := url.Values{}
form.Set("link", postID)
return s.choices(ctx, path, form)
}
// ChoicesForNewPost returns a list of flairs you can assign to a new post in a subreddit.
func (s *FlairService) ChoicesForNewPost(ctx context.Context, subreddit string) ([]*FlairChoice, *Response, error) {
path := fmt.Sprintf("r/%s/api/flairselector", subreddit)
form := url.Values{}
form.Set("is_newlink", "true")
choices, _, resp, err := s.choices(ctx, path, form)
return choices, resp, err
}
func (s *FlairService) choices(ctx context.Context, path string, form url.Values) ([]*FlairChoice, *FlairChoice, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, nil, err
}
root := new(struct {
Choices []*FlairChoice `json:"choices"`
Current *FlairChoice `json:"current"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, nil, resp, err
}
return root.Choices, root.Current, resp, nil
}
// Select a flair to display next to your username in the subreddit.
func (s *FlairService) Select(ctx context.Context, subreddit string, request *FlairSelectRequest) (*Response, error) {
return s.Assign(ctx, subreddit, s.client.Username, request)
}
// Assign a flair to another user in the subreddit.
// This only works if you're a moderator of the subreddit, or if the user is you.
func (s *FlairService) Assign(ctx context.Context, subreddit, user string, request *FlairSelectRequest) (*Response, error) {
if request == nil {
return nil, errors.New("*FlairSelectRequest: cannot be nil")
}
path := fmt.Sprintf("r/%s/api/selectflair", subreddit)
form, err := query.Values(request)
if err != nil {
return nil, err
}
form.Set("api_type", "json")
form.Set("name", user)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// SelectForPost assigns a flair to the post.
// If the post isn't yours, you have to be a moderator of the post's subreddit for this to work.
func (s *FlairService) SelectForPost(ctx context.Context, postID string, request *FlairSelectRequest) (*Response, error) {
if request == nil {
return nil, errors.New("*FlairSelectRequest: cannot be nil")
}
path := "api/selectflair"
form, err := query.Values(request)
if err != nil {
return nil, err
}
form.Set("api_type", "json")
form.Set("link", postID)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// RemoveFromPost removes the flair from the post.
// If the post isn't yours, you have to be a moderator of the post's subreddit for this to work.
func (s *FlairService) RemoveFromPost(ctx context.Context, postID string) (*Response, error) {
path := "api/selectflair"
form := url.Values{}
form.Set("api_type", "json")
form.Set("link", postID)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Change the flair of multiple users in the subreddit at once.
// You have to be a moderator of the subreddit for this to work.
func (s *FlairService) Change(ctx context.Context, subreddit string, requests []FlairChangeRequest) ([]*FlairChangeResponse, *Response, error) {
if len(requests) == 0 || len(requests) > 100 {
return nil, nil, errors.New("requests: must provide between 1 and 100")
}
records := make([][]string, len(requests))
for i, req := range requests {
records[i] = []string{req.User, req.Text, req.CSSClass}
}
buf := new(bytes.Buffer)
w := csv.NewWriter(buf)
err := w.WriteAll(records)
if err != nil {
return nil, nil, err
}
err = w.Error()
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("r/%s/api/flaircsv", subreddit)
form := url.Values{}
form.Set("flair_csv", buf.String())
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
var root []*FlairChangeResponse
resp, err := s.client.Do(ctx, req, &root)
if err != nil {
return nil, resp, err
}
return root, resp, nil
}