forked from vartanbeno/go-reddit
/
multi.go
307 lines (255 loc) · 8.3 KB
/
multi.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
package reddit
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
// MultiService handles communication with the multireddit
// related methods of the Reddit API.
//
// Reddit API docs: https://www.reddit.com/dev/api#section_multis
type MultiService struct {
client *Client
}
// Multi is a multireddit, i.e. a customizable group of subreddits.
// Users can create multis for custom navigation, instead of browsing
// one subreddit or all subreddits at a time.
type Multi struct {
Name string `json:"name,omitempty"`
DisplayName string `json:"display_name,omitempty"`
// Format: user/{username}/m/{multiname}
Path string `json:"path,omitempty"`
Description string `json:"description_md,omitempty"`
Subreddits SubredditNames `json:"subreddits"`
CopiedFrom *string `json:"copied_from"`
Owner string `json:"owner,omitempty"`
OwnerID string `json:"owner_id,omitempty"`
Created *Timestamp `json:"created_utc,omitempty"`
NumberOfSubscribers int `json:"num_subscribers"`
Visibility string `json:"visibility,omitempty"`
Subscribed bool `json:"is_subscriber"`
Favorite bool `json:"is_favorited"`
CanEdit bool `json:"can_edit"`
NSFW bool `json:"over_18"`
}
// SubredditNames is a list of subreddit names.
type SubredditNames []string
// UnmarshalJSON implements the json.Unmarshaler interface.
func (n *SubredditNames) UnmarshalJSON(data []byte) error {
type subreddit struct {
Name string `json:"name"`
}
var subreddits []subreddit
err := json.Unmarshal(data, &subreddits)
if err != nil {
return err
}
*n = make(SubredditNames, len(subreddits))
for i, sr := range subreddits {
(*n)[i] = sr.Name
}
return nil
}
// MarshalJSON implements the json.Marshaler interface.
func (n *SubredditNames) MarshalJSON() ([]byte, error) {
type subreddit struct {
Name string `json:"name"`
}
subreddits := make([]subreddit, 0)
for _, name := range *n {
subreddits = append(subreddits, subreddit{name})
}
return json.Marshal(subreddits)
}
// MultiCopyRequest represents a request to copy a multireddit.
type MultiCopyRequest struct {
FromPath string `url:"from"`
ToPath string `url:"to"`
// Raw markdown text.
Description string `url:"description_md,omitempty"`
// No longer than 50 characters.
DisplayName string `url:"display_name,omitempty"`
}
// MultiCreateOrUpdateRequest represents a request to create/update a multireddit.
type MultiCreateOrUpdateRequest struct {
// For updates, this is the display name, i.e. the header of the multi.
// Not part of the path necessarily.
Name string `json:"display_name,omitempty"`
Description string `json:"description_md,omitempty"`
Subreddits SubredditNames `json:"subreddits,omitempty"`
// One of: private, public, hidden.
Visibility string `json:"visibility,omitempty"`
}
type rootMultiDescription struct {
Body string `json:"body_md"`
}
// Get the multireddit from its url path.
func (s *MultiService) Get(ctx context.Context, multiPath string) (*Multi, *Response, error) {
path := fmt.Sprintf("api/multi/%s", multiPath)
t, resp, err := s.client.getThing(ctx, path, nil)
if err != nil {
return nil, resp, err
}
multi, _ := t.Multi()
return multi, resp, nil
}
// Mine returns your multireddits.
func (s *MultiService) Mine(ctx context.Context) ([]*Multi, *Response, error) {
path := "api/multi/mine"
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(things)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Multis, resp, nil
}
// Of returns the user's public multireddits.
// Or, if the user is you, all of your multireddits.
func (s *MultiService) Of(ctx context.Context, username string) ([]*Multi, *Response, error) {
path := fmt.Sprintf("api/multi/user/%s", username)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(things)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Multis, resp, nil
}
// Copy a multireddit.
func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest) (*Multi, *Response, error) {
if copyRequest == nil {
return nil, nil, errors.New("*MultiCopyRequest: cannot be nil")
}
path := "api/multi/copy"
form, err := query.Values(copyRequest)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
multi, _ := root.Multi()
return multi, resp, nil
}
// Create a multireddit.
func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) {
if createRequest == nil {
return nil, nil, errors.New("*MultiCreateOrUpdateRequest: cannot be nil")
}
byteValue, err := json.Marshal(createRequest)
if err != nil {
return nil, nil, err
}
form := url.Values{}
form.Set("model", string(byteValue))
path := "api/multi"
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
multi, _ := root.Multi()
return multi, resp, nil
}
// Update a multireddit.
// If the multireddit does not exist, it will be created.
func (s *MultiService) Update(ctx context.Context, multiPath string, updateRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) {
if updateRequest == nil {
return nil, nil, errors.New("*MultiCreateOrUpdateRequest: cannot be nil")
}
byteValue, err := json.Marshal(updateRequest)
if err != nil {
return nil, nil, err
}
form := url.Values{}
form.Set("model", string(byteValue))
path := fmt.Sprintf("api/multi/%s", multiPath)
req, err := s.client.NewRequest(http.MethodPut, path, form)
if err != nil {
return nil, nil, err
}
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
multi, _ := root.Multi()
return multi, resp, nil
}
// Delete a multireddit.
func (s *MultiService) Delete(ctx context.Context, multiPath string) (*Response, error) {
path := fmt.Sprintf("api/multi/%s", multiPath)
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Description gets a multireddit's description.
func (s *MultiService) Description(ctx context.Context, multiPath string) (string, *Response, error) {
path := fmt.Sprintf("api/multi/%s/description", multiPath)
t, resp, err := s.client.getThing(ctx, path, nil)
if err != nil {
return "", resp, err
}
multiDescription, _ := t.MultiDescription()
return multiDescription, resp, nil
}
// UpdateDescription updates a multireddit's description.
func (s *MultiService) UpdateDescription(ctx context.Context, multiPath string, description string) (string, *Response, error) {
form := url.Values{}
form.Set("model", fmt.Sprintf(`{"body_md":"%s"}`, description))
path := fmt.Sprintf("api/multi/%s/description", multiPath)
req, err := s.client.NewRequest(http.MethodPut, path, form)
if err != nil {
return "", nil, err
}
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return "", resp, err
}
multiDescription, _ := root.MultiDescription()
return multiDescription, resp, nil
}
// AddSubreddit adds a subreddit to a multireddit.
func (s *MultiService) AddSubreddit(ctx context.Context, multiPath string, subreddit string) (*Response, error) {
path := fmt.Sprintf("api/multi/%s/r/%s", multiPath, subreddit)
form := url.Values{}
form.Set("model", fmt.Sprintf(`{"name":"%s"}`, subreddit))
req, err := s.client.NewRequest(http.MethodPut, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// DeleteSubreddit removes a subreddit from a multireddit.
func (s *MultiService) DeleteSubreddit(ctx context.Context, multiPath string, subreddit string) (*Response, error) {
path := fmt.Sprintf("api/multi/%s/r/%s", multiPath, subreddit)
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}