forked from vartanbeno/go-reddit
/
subreddit.go
1182 lines (974 loc) · 39.3 KB
/
subreddit.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package reddit
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/google/go-querystring/query"
)
// SubredditService handles communication with the subreddit
// related methods of the Reddit API.
//
// Reddit API docs: https://www.reddit.com/dev/api/#section_subreddits
type SubredditService struct {
client *Client
}
type rootSubredditNames struct {
Names []string `json:"names,omitempty"`
}
// Relationship holds information about a relationship (friend/blocked).
type Relationship struct {
ID string `json:"rel_id,omitempty"`
User string `json:"name,omitempty"`
UserID string `json:"id,omitempty"`
Created *Timestamp `json:"date,omitempty"`
}
// Moderator is a user who moderates a subreddit.
type Moderator struct {
*Relationship
Permissions []string `json:"mod_permissions"`
}
// Ban represents a banned relationship.
type Ban struct {
*Relationship
// nil means the ban is permanent
DaysLeft *int `json:"days_left"`
Note string `json:"note,omitempty"`
}
// SubredditRule is a rule in the subreddit.
type SubredditRule struct {
// One of: comment, link (i.e. post), or all (i.e. both comment and link).
Kind string `json:"kind,omitempty"`
// Short description of the rule.
Name string `json:"short_name,omitempty"`
// The reason that will appear when a thing is reported in violation to this rule.
ViolationReason string `json:"violation_reason,omitempty"`
Description string `json:"description,omitempty"`
Priority int `json:"priority"`
Created *Timestamp `json:"created_utc,omitempty"`
}
// SubredditRuleCreateRequest represents a request to add a subreddit rule.
type SubredditRuleCreateRequest struct {
// One of: comment, link (i.e. post) or all (i.e. both).
Kind string `url:"kind"`
// Short description of the rule. No longer than 100 characters.
Name string `url:"short_name"`
// The reason that will appear when a thing is reported in violation to this rule.
// If this is empty, Reddit will set its value to Name by default.
// No longer than 100 characters.
ViolationReason string `url:"violation_reason,omitempty"`
// Optional. No longer than 500 characters.
Description string `url:"description,omitempty"`
}
func (r *SubredditRuleCreateRequest) validate() error {
if r == nil {
return errors.New("*SubredditRuleCreateRequest: cannot be nil")
}
switch r.Kind {
case "comment", "link", "all":
// intentionally left blank
default:
return errors.New("(*SubredditRuleCreateRequest).Kind: must be one of: comment, link, all")
}
if r.Name == "" || len(r.Name) > 100 {
return errors.New("(*SubredditRuleCreateRequest).Name: must be between 1-100 characters")
}
if len(r.ViolationReason) > 100 {
return errors.New("(*SubredditRuleCreateRequest).ViolationReason: cannot be longer than 100 characters")
}
if len(r.Description) > 500 {
return errors.New("(*SubredditRuleCreateRequest).Description: cannot be longer than 500 characters")
}
return nil
}
// SubredditTrafficStats hold information about subreddit traffic.
type SubredditTrafficStats struct {
// Traffic data is returned in the form of day, hour, and month.
// Start is a timestamp indicating the start of the category, i.e.
// start of the day for day, start of the hour for hour, and start of the month for month.
Start *Timestamp `json:"start"`
UniqueViews int `json:"unique_views"`
TotalViews int `json:"total_views"`
// This is only available for "day" traffic, not hour and month.
// Therefore, it is always 0 by default for hour and month.
Subscribers int `json:"subscribers"`
}
// SubredditImage is an image part of the image set of a subreddit.
type SubredditImage struct {
Name string `json:"name"`
Link string `json:"link"`
URL string `json:"url"`
}
// SubredditStyleSheet contains the subreddit's styling information.
type SubredditStyleSheet struct {
SubredditID string `json:"subreddit_id"`
Images []*SubredditImage `json:"images"`
StyleSheet string `json:"stylesheet"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *SubredditTrafficStats) UnmarshalJSON(b []byte) error {
var data [4]int
err := json.Unmarshal(b, &data)
if err != nil {
return err
}
timestampByteValue, err := json.Marshal(data[0])
if err != nil {
return err
}
timestamp := new(Timestamp)
err = timestamp.UnmarshalJSON(timestampByteValue)
if err != nil {
return err
}
s.Start = timestamp
s.UniqueViews = data[1]
s.TotalViews = data[2]
s.Subscribers = data[3]
return nil
}
// SubredditSettings are a subreddit's settings.
type SubredditSettings struct {
// The id of the subreddit.
ID string `url:"-" json:"subreddit_id,omitempty"`
// One of: public, restricted, private, gold_restricted, archived, employees_only, gold_only, user.
Type *string `url:"type,omitempty" json:"subreddit_type,omitempty"`
// A valid IETF language tag (underscore separated).
Language *string `url:"lang,omitempty" json:"language,omitempty"`
// No longer than 100 characters.
Title *string `url:"title,omitempty" json:"title,omitempty"`
// Raw markdown text. No longer than 500 characters.
Description *string `url:"public_description,omitempty" json:"public_description,omitempty"`
// Raw markdown text. No longer than 10240 characters.
Sidebar *string `url:"description,omitempty" json:"description,omitempty"`
// Raw markdown text. No longer than 1024 characters.
SubmissionText *string `url:"submit_text,omitempty" json:"submit_text,omitempty"`
// Raw markdown text. No longer than 5000 characters.
WelcomeMessage *string `url:"welcome_message_text,omitempty" json:"welcome_message_text,omitempty"`
WelcomeMessageEnabled *bool `url:"welcome_message_enabled,omitempty" json:"welcome_message_enabled,omitempty"`
AllowCrossposts *bool `url:"allow_post_crossposts,omitempty" json:"allow_post_crossposts,omitempty"`
AllowChatPosts *bool `url:"allow_chat_post_creation,omitempty" json:"allow_chat_post_creation,omitempty"`
AllowPollPosts *bool `url:"allow_polls,omitempty" json:"allow_polls,omitempty"`
AllowFreeFormReports *bool `url:"free_form_reports,omitempty" json:"free_form_reports,omitempty"`
AllowOriginalContent *bool `url:"original_content_tag_enabled,omitempty" json:"original_content_tag_enabled,omitempty"`
// Allow image uploads and links to image hosting sites.
AllowImages *bool `url:"allow_images,omitempty" json:"allow_images,omitempty"`
AllowMultipleImagesPerPost *bool `url:"allow_galleries,omitempty" json:"allow_galleries,omitempty"`
ExcludeSitewideBannedUsersContent *bool `url:"exclude_banned_modqueue,omitempty" json:"exclude_banned_modqueue,omitempty"`
// An integer from 0 to 3.
CrowdControlChalLevel *int `url:"crowd_control_chat_level,omitempty" json:"crowd_control_chat_level,omitempty"`
// Mark all posts in this subreddit as Original Content (OC) on the desktop redesign.
AllOriginalContent *bool `url:"all_original_content,omitempty" json:"all_original_content,omitempty"`
// One of: none (recommended), confidence, top, new, controversial, old, random, qa, live.
SuggestedCommentSort *string `url:"suggested_comment_sort,omitempty" json:"suggested_comment_sort,omitempty"`
// No longer than 60 characters.
SubmitLinkPostLabel *string `url:"submit_link_label,omitempty" json:"submit_link_label,omitempty"`
// No longer than 60 characters.
SubmitTextPostLabel *string `url:"submit_text_label,omitempty" json:"submit_text_label,omitempty"`
// One of: any, link, self.
PostType *string `url:"link_type,omitempty" json:"content_options,omitempty"`
// One of: low (disable most filtering), high (standard), all (filter everything, requiring mod approval).
SpamFilterStrengthLinkPosts *string `url:"spam_links,omitempty" json:"spam_links,omitempty"`
// One of: low (disable most filtering), high (standard), all (filter everything, requiring mod approval).
SpamFilterStrengthTextPosts *string `url:"spam_selfposts,omitempty" json:"spam_selfposts,omitempty"`
// One of: low (disable most filtering), high (standard), all (filter everything, requiring mod approval).
SpamFilterStrengthComments *string `url:"spam_comments,omitempty" json:"spam_comments,omitempty"`
ShowContentThumbnails *bool `url:"show_media,omitempty" json:"show_media,omitempty"`
ExpandMediaPreviewsOnCommentsPages *bool `url:"show_media_preview,omitempty" json:"show_media_preview,omitempty"`
CollapseDeletedComments *bool `url:"collapse_deleted_comments,omitempty" json:"collapse_deleted_comments,omitempty"`
// An integer between 0 and 1440.
MinutesToHideCommentScores *int `url:"comment_score_hide_mins,omitempty" json:"comment_score_hide_mins,omitempty"`
// Enable marking posts as containing spoilers.
SpoilersEnabled *bool `url:"spoilers_enabled,omitempty" json:"spoilers_enabled,omitempty"`
// If there's an image header set, hovering the mouse over it will display this text.
HeaderMouseoverText *string `url:"header-title,omitempty" json:"header_hover_text,omitempty"`
// 6-digit rgb hex colour, e.g. #AABBCC.
// Thematic colour for the subreddit on mobile.
MobileColour *string `url:"key_color,omitempty" json:"key_color,omitempty"`
// Can only be set to true if subreddit type is gold_only.
HideAds *bool `url:"hide_ads,omitempty" json:"hide_ads,omitempty"`
// Require viewers to be over 18 years old.
NSFW *bool `url:"over_18,omitempty" json:"over_18,omitempty"`
// Show up in high-traffic feeds: Allow your community to be in r/all, r/popular, and trending lists where it can be
// seen by the general Reddit population.
AllowDiscoveryInHighTrafficFeeds *bool `url:"allow_top,omitempty" json:"default_set,omitempty"`
// Get recommended to individual redditors. Let Reddit recommend your community to people who have similar interests.
AllowDiscoveryByIndividualUsers *bool `url:"allow_discovery,omitempty" json:"allow_discovery,omitempty"`
// One of:
// - disabled: wiki is disabled for everyone except mods.
// - modonly: only mods, approved wiki contributors, or those on a page's edit list may edit.
// - anyone: anyone who can submit to the subreddit may edit.
WikiMode *string `url:"wikimode,omitempty" json:"wikimode,omitempty"`
// Account age (in days) required to create and edit wiki pages.
WikiMinimumAccountAge *int `url:"wiki_edit_age,omitempty" json:"wiki_edit_age,omitempty"`
// Subreddit karma required to create and edit wiki pages.
WikiMinimumKarma *int `url:"wiki_edit_karma,omitempty" json:"wiki_edit_karma,omitempty"`
}
// SubredditPostRequirements is a list of moderator-designed requirements to post to a subreddit.
type SubredditPostRequirements struct {
// Sentence or two on how to successfully post to the subreddit.
Guidelines string `json:"guidelines_text"`
GuidelinesDisplayPolicy string `json:"guidelines_display_policy"`
TitleMinLength int `json:"title_text_min_length"`
TitleMaxLength int `json:"title_text_max_length"`
BodyMinLength int `json:"body_text_min_length"`
BodyMaxLength int `json:"body_text_max_length"`
// Do not allow any of these words in the title.
TitleBlacklistedStrings []string `json:"title_blacklisted_strings"`
// Do not allow any of these words in the body.
BodyBlacklistedStrings []string `json:"body_blacklisted_strings"`
// Require at least one of these words in the title.
TitleRequiredStrings []string `json:"title_required_strings"`
// Require at least one of these words in the body.
BodyRequiredStrings []string `json:"body_required_strings"`
// Block link posts with these domains.
DomainBlacklist []string `json:"domain_blacklist"`
// Only allow link posts with these domains.
DomainWhitelist []string `json:"domain_whitelist"`
// One of: required, notAllowed, none.
// Meaning that a selftext post's body is required, not allowed, or optional, respectively.
BodyRestrictionPolicy string `json:"body_restriction_policy"`
LinkRestrictionPolicy string `json:"link_restriction_policy"`
GalleryMinItems int `json:"gallery_min_items"`
GalleryMaxItems int `json:"gallery_max_items"`
GalleryCaptionsRequirement string `json:"gallery_captions_requirement"`
GalleryURLsRequirement string `json:"gallery_urls_requirement"`
// Prevent users from posting a link that was already posted to your community within x days of the original.
LinkRepostAge int `json:"link_repost_age"`
FlairRequired bool `json:"is_flair_required"`
TitleRegexes []string `json:"title_regexes"`
BodyRegexes []string `json:"body_regexes"`
}
func (s *SubredditService) getPosts(ctx context.Context, sort string, subreddit string, opts interface{}) ([]*Post, *Response, error) {
path := sort
if subreddit != "" {
path = fmt.Sprintf("r/%s/%s", subreddit, sort)
}
l, resp, err := s.client.getListing(ctx, path, opts)
if err != nil {
return nil, resp, err
}
return l.Posts(), resp, nil
}
// HotPosts returns the hottest posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
// To search through all, just specify "all".
// To search through all and filter out subreddits, provide "all-name1-name2".
// Note: when looking for hot posts in a subreddit, it will include the stickied
// posts (if any) PLUS posts from the limit parameter (25 by default).
func (s *SubredditService) HotPosts(ctx context.Context, subreddit string, opts *ListOptions) ([]*Post, *Response, error) {
return s.getPosts(ctx, "hot", subreddit, opts)
}
// NewPosts returns the newest posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
// To search through all, just specify "all".
// To search through all and filter out subreddits, provide "all-name1-name2".
func (s *SubredditService) NewPosts(ctx context.Context, subreddit string, opts *ListOptions) ([]*Post, *Response, error) {
return s.getPosts(ctx, "new", subreddit, opts)
}
// RisingPosts returns the rising posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
// To search through all, just specify "all".
// To search through all and filter out subreddits, provide "all-name1-name2".
func (s *SubredditService) RisingPosts(ctx context.Context, subreddit string, opts *ListOptions) ([]*Post, *Response, error) {
return s.getPosts(ctx, "rising", subreddit, opts)
}
// ControversialPosts returns the most controversial posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
// To search through all, just specify "all".
// To search through all and filter out subreddits, provide "all-name1-name2".
func (s *SubredditService) ControversialPosts(ctx context.Context, subreddit string, opts *ListPostOptions) ([]*Post, *Response, error) {
return s.getPosts(ctx, "controversial", subreddit, opts)
}
// TopPosts returns the top posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
// To search through all, just specify "all".
// To search through all and filter out subreddits, provide "all-name1-name2".
func (s *SubredditService) TopPosts(ctx context.Context, subreddit string, opts *ListPostOptions) ([]*Post, *Response, error) {
return s.getPosts(ctx, "top", subreddit, opts)
}
// Get a subreddit by name.
func (s *SubredditService) Get(ctx context.Context, name string) (*Subreddit, *Response, error) {
if name == "" {
return nil, nil, errors.New("name: cannot be empty")
}
path := fmt.Sprintf("r/%s/about", name)
t, resp, err := s.client.getThing(ctx, path, nil)
if err != nil {
return nil, resp, err
}
sr, _ := t.Subreddit()
return sr, resp, nil
}
// Popular returns popular subreddits.
func (s *SubredditService) Popular(ctx context.Context, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
return s.getSubreddits(ctx, "subreddits/popular", opts)
}
// New returns new subreddits.
func (s *SubredditService) New(ctx context.Context, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
return s.getSubreddits(ctx, "subreddits/new", opts)
}
// Gold returns gold subreddits (i.e. only accessible to users with gold).
// It seems like it returns an empty list if you don't have gold.
func (s *SubredditService) Gold(ctx context.Context, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
return s.getSubreddits(ctx, "subreddits/gold", opts)
}
// Default returns default subreddits.
func (s *SubredditService) Default(ctx context.Context, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
return s.getSubreddits(ctx, "subreddits/default", opts)
}
// Subscribed returns the list of subreddits you are subscribed to.
func (s *SubredditService) Subscribed(ctx context.Context, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
return s.getSubreddits(ctx, "subreddits/mine/subscriber", opts)
}
// Approved returns the list of subreddits you are an approved user in.
func (s *SubredditService) Approved(ctx context.Context, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
return s.getSubreddits(ctx, "subreddits/mine/contributor", opts)
}
// Moderated returns the list of subreddits you are a moderator of.
func (s *SubredditService) Moderated(ctx context.Context, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
return s.getSubreddits(ctx, "subreddits/mine/moderator", opts)
}
// GetSticky1 returns the first stickied post on a subreddit (if it exists).
func (s *SubredditService) GetSticky1(ctx context.Context, subreddit string) (*PostAndComments, *Response, error) {
return s.getSticky(ctx, subreddit, 1)
}
// GetSticky2 returns the second stickied post on a subreddit (if it exists).
func (s *SubredditService) GetSticky2(ctx context.Context, subreddit string) (*PostAndComments, *Response, error) {
return s.getSticky(ctx, subreddit, 2)
}
func (s *SubredditService) handleSubscription(ctx context.Context, form url.Values) (*Response, error) {
path := "api/subscribe"
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Subscribe subscribes to subreddits based on their names.
func (s *SubredditService) Subscribe(ctx context.Context, subreddits ...string) (*Response, error) {
form := url.Values{}
form.Set("action", "sub")
form.Set("sr_name", strings.Join(subreddits, ","))
return s.handleSubscription(ctx, form)
}
// SubscribeByID subscribes to subreddits based on their id.
func (s *SubredditService) SubscribeByID(ctx context.Context, ids ...string) (*Response, error) {
form := url.Values{}
form.Set("action", "sub")
form.Set("sr", strings.Join(ids, ","))
return s.handleSubscription(ctx, form)
}
// Unsubscribe unsubscribes from subreddits based on their names.
func (s *SubredditService) Unsubscribe(ctx context.Context, subreddits ...string) (*Response, error) {
form := url.Values{}
form.Set("action", "unsub")
form.Set("sr_name", strings.Join(subreddits, ","))
return s.handleSubscription(ctx, form)
}
// UnsubscribeByID unsubscribes from subreddits based on their id.
func (s *SubredditService) UnsubscribeByID(ctx context.Context, ids ...string) (*Response, error) {
form := url.Values{}
form.Set("action", "unsub")
form.Set("sr", strings.Join(ids, ","))
return s.handleSubscription(ctx, form)
}
// Favorite the subreddit.
func (s *SubredditService) Favorite(ctx context.Context, subreddit string) (*Response, error) {
path := "api/favorite"
form := url.Values{}
form.Set("sr_name", subreddit)
form.Set("make_favorite", "true")
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)
}
// Unfavorite the subreddit.
func (s *SubredditService) Unfavorite(ctx context.Context, subreddit string) (*Response, error) {
path := "api/favorite"
form := url.Values{}
form.Set("sr_name", subreddit)
form.Set("make_favorite", "false")
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)
}
// Search for subreddits.
func (s *SubredditService) Search(ctx context.Context, query string, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
path := fmt.Sprintf("subreddits/search?q=%s", query)
l, resp, err := s.client.getListing(ctx, path, opts)
if err != nil {
return nil, resp, err
}
return l.Subreddits(), resp, nil
}
// SearchNames searches for subreddits with names beginning with the query provided.
func (s *SubredditService) SearchNames(ctx context.Context, query string) ([]string, *Response, error) {
path := fmt.Sprintf("api/search_reddit_names?query=%s", query)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootSubredditNames)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Names, resp, nil
}
// SearchPosts searches for posts in the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If no subreddit is provided, the search is run against r/all.
func (s *SubredditService) SearchPosts(ctx context.Context, query string, subreddit string, opts *ListPostSearchOptions) ([]*Post, *Response, error) {
if subreddit == "" {
subreddit = "all"
}
path := fmt.Sprintf("r/%s/search", subreddit)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
notAll := !strings.EqualFold(subreddit, "all")
params := struct {
Query string `url:"q"`
RestrictSubreddits bool `url:"restrict_sr,omitempty"`
}{query, notAll}
t, resp, err := s.client.getThing(ctx, path, params)
if err != nil {
return nil, resp, err
}
l, _ := t.Listing()
return l.Posts(), resp, nil
}
func (s *SubredditService) getSubreddits(ctx context.Context, path string, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
l, resp, err := s.client.getListing(ctx, path, opts)
if err != nil {
return nil, resp, err
}
return l.Subreddits(), resp, nil
}
// getSticky returns one of the 2 stickied posts of the subreddit (if they exist).
// Num should be equal to 1 or 2, depending on which one you want.
func (s *SubredditService) getSticky(ctx context.Context, subreddit string, num int) (*PostAndComments, *Response, error) {
params := struct {
Num int `url:"num"`
}{num}
path := fmt.Sprintf("r/%s/about/sticky", subreddit)
path, err := addOptions(path, params)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(PostAndComments)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, nil
}
// todo: sr_detail's NSFW indicator is over_18 instead of over18
func (s *SubredditService) random(ctx context.Context, nsfw bool) (*Subreddit, *Response, error) {
path := "r/random"
if nsfw {
path = "r/randnsfw"
}
params := struct {
ExpandSubreddit bool `url:"sr_detail"`
Limit int `url:"limit,omitempty"`
}{true, 1}
path, err := addOptions(path, params)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(struct {
Data struct {
Children [1]struct {
Data struct {
Subreddit *Subreddit `json:"sr_detail"`
} `json:"data"`
} `json:"children"`
} `json:"data"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
subreddit := root.Data.Children[0].Data.Subreddit
return subreddit, resp, nil
}
// Random returns a random SFW subreddit.
func (s *SubredditService) Random(ctx context.Context) (*Subreddit, *Response, error) {
return s.random(ctx, false)
}
// RandomNSFW returns a random NSFW subreddit.
func (s *SubredditService) RandomNSFW(ctx context.Context) (*Subreddit, *Response, error) {
return s.random(ctx, true)
}
// SubmissionText gets the submission text for the subreddit.
// This text is set by the subreddit moderators and intended to be displayed on the submission form.
func (s *SubredditService) SubmissionText(ctx context.Context, name string) (string, *Response, error) {
if name == "" {
return "", nil, errors.New("name: cannot be empty")
}
path := fmt.Sprintf("r/%s/api/submit_text", name)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return "", nil, err
}
root := new(struct {
Text string `json:"submit_text"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return "", resp, err
}
return root.Text, resp, err
}
// Banned gets banned users from the subreddit.
func (s *SubredditService) Banned(ctx context.Context, subreddit string, opts *ListOptions) ([]*Ban, *Response, error) {
path := fmt.Sprintf("r/%s/about/banned", subreddit)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(struct {
Data struct {
Bans []*Ban `json:"children"`
After string `json:"after"`
} `json:"data"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
resp.After = root.Data.After
return root.Data.Bans, resp, nil
}
// Muted gets muted users from the subreddit.
func (s *SubredditService) Muted(ctx context.Context, subreddit string, opts *ListOptions) ([]*Relationship, *Response, error) {
path := fmt.Sprintf("r/%s/about/muted", subreddit)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(struct {
Data struct {
Relationships []*Relationship `json:"children"`
After string `json:"after"`
} `json:"data"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
resp.After = root.Data.After
return root.Data.Relationships, resp, nil
}
// WikiBanned gets banned users from the subreddit.
func (s *SubredditService) WikiBanned(ctx context.Context, subreddit string, opts *ListOptions) ([]*Ban, *Response, error) {
path := fmt.Sprintf("r/%s/about/wikibanned", subreddit)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(struct {
Data struct {
Bans []*Ban `json:"children"`
After string `json:"after"`
} `json:"data"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
resp.After = root.Data.After
return root.Data.Bans, resp, nil
}
// Contributors gets contributors (also known as approved users) from the subreddit.
func (s *SubredditService) Contributors(ctx context.Context, subreddit string, opts *ListOptions) ([]*Relationship, *Response, error) {
path := fmt.Sprintf("r/%s/about/contributors", subreddit)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(struct {
Data struct {
Relationships []*Relationship `json:"children"`
After string `json:"after"`
} `json:"data"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
resp.After = root.Data.After
return root.Data.Relationships, resp, nil
}
// WikiContributors gets contributors of the wiki from the subreddit.
func (s *SubredditService) WikiContributors(ctx context.Context, subreddit string, opts *ListOptions) ([]*Relationship, *Response, error) {
path := fmt.Sprintf("r/%s/about/wikicontributors", subreddit)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(struct {
Data struct {
Relationships []*Relationship `json:"children"`
After string `json:"after"`
} `json:"data"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
resp.After = root.Data.After
return root.Data.Relationships, resp, nil
}
// Moderators gets the moderators of the subreddit.
func (s *SubredditService) Moderators(ctx context.Context, subreddit string) ([]*Moderator, *Response, error) {
path := fmt.Sprintf("r/%s/about/moderators", subreddit)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(struct {
Data struct {
Moderators []*Moderator `json:"children"`
} `json:"data"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Data.Moderators, resp, nil
}
// Rules gets the rules of the subreddit.
func (s *SubredditService) Rules(ctx context.Context, subreddit string) ([]*SubredditRule, *Response, error) {
path := fmt.Sprintf("r/%s/about/rules", subreddit)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(struct {
Rules []*SubredditRule `json:"rules"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Rules, resp, nil
}
// CreateRule adds a rule to the subreddit.
func (s *SubredditService) CreateRule(ctx context.Context, subreddit string, request *SubredditRuleCreateRequest) (*Response, error) {
err := request.validate()
if err != nil {
return nil, err
}
form, err := query.Values(request)
if err != nil {
return nil, err
}
form.Set("api_type", "json")
path := fmt.Sprintf("r/%s/api/add_subreddit_rule", subreddit)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Traffic gets the traffic data of the subreddit.
// It returns traffic data by day, hour, and month, respectively.
func (s *SubredditService) Traffic(ctx context.Context, subreddit string) ([]*SubredditTrafficStats, []*SubredditTrafficStats, []*SubredditTrafficStats, *Response, error) {
path := fmt.Sprintf("r/%s/about/traffic", subreddit)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, nil, nil, err
}
root := new(struct {
Day []*SubredditTrafficStats `json:"day"`
Hour []*SubredditTrafficStats `json:"hour"`
Month []*SubredditTrafficStats `json:"month"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, nil, nil, resp, err
}
return root.Day, root.Hour, root.Month, resp, nil
}
// StyleSheet returns the subreddit's style sheet, as well as some information about images.
func (s *SubredditService) StyleSheet(ctx context.Context, subreddit string) (*SubredditStyleSheet, *Response, error) {
path := fmt.Sprintf("r/%s/about/stylesheet", subreddit)
t, resp, err := s.client.getThing(ctx, path, nil)
if err != nil {
return nil, resp, err
}
styleSheet, _ := t.StyleSheet()
return styleSheet, resp, nil
}
// StyleSheetRaw returns the subreddit's style sheet with all comments and newlines stripped.
func (s *SubredditService) StyleSheetRaw(ctx context.Context, subreddit string) (string, *Response, error) {
path := fmt.Sprintf("r/%s/stylesheet", subreddit)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return "", nil, err
}
buf := new(bytes.Buffer)
resp, err := s.client.Do(ctx, req, buf)
if err != nil {
return "", resp, err
}
return buf.String(), resp, nil
}
// UpdateStyleSheet updates the style sheet of the subreddit.
// Providing a reason is optional.
func (s *SubredditService) UpdateStyleSheet(ctx context.Context, subreddit, styleSheet, reason string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/subreddit_stylesheet", subreddit)
form := url.Values{}
form.Set("api_type", "json")
form.Set("op", "save")
form.Set("stylesheet_contents", styleSheet)
if reason != "" {
form.Set("reason", reason)
}
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// RemoveImage removes an image from the subreddit's custom image set.
// The call succeeds even if the named image does not exist.
func (s *SubredditService) RemoveImage(ctx context.Context, subreddit, imageName string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/delete_sr_img", subreddit)
form := url.Values{}
form.Set("api_type", "json")
form.Set("img_name", imageName)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// RemoveHeader removes the subreddit's current header image.
// The call succeeds even if there's no header image.
func (s *SubredditService) RemoveHeader(ctx context.Context, subreddit string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/delete_sr_header", subreddit)
form := url.Values{}
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)
}
// RemoveMobileHeader removes the subreddit's current mobile header.
// The call succeeds even if there's no mobile header.
func (s *SubredditService) RemoveMobileHeader(ctx context.Context, subreddit string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/delete_sr_banner", subreddit)
form := url.Values{}
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)
}
// RemoveMobileIcon removes the subreddit's current mobile icon.
// The call succeeds even if there's no mobile icon.
func (s *SubredditService) RemoveMobileIcon(ctx context.Context, subreddit string) (*Response, error) {