forked from vartanbeno/go-reddit
/
wiki.go
396 lines (324 loc) · 11.2 KB
/
wiki.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
package reddit
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/google/go-querystring/query"
)
// WikiService handles communication with the wiki
// related methods of the Reddit API.
//
// Reddit API docs: https://www.reddit.com/dev/api/#section_wiki
type WikiService struct {
client *Client
}
// WikiPage is a wiki page in a subreddit.
type WikiPage struct {
Content string `json:"content_md,omitempty"`
Reason string `json:"reason,omitempty"`
MayRevise bool `json:"may_revise"`
RevisionID string `json:"revision_id,omitempty"`
RevisionDate *Timestamp `json:"revision_date,omitempty"`
RevisionBy *User `json:"revision_by,omitempty"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (p *WikiPage) UnmarshalJSON(b []byte) error {
root := new(struct {
Content string `json:"content_md,omitempty"`
Reason string `json:"reason,omitempty"`
MayRevise bool `json:"may_revise"`
RevisionID string `json:"revision_id,omitempty"`
RevisionDate *Timestamp `json:"revision_date,omitempty"`
RevisionBy thing `json:"revision_by,omitempty"`
})
err := json.Unmarshal(b, root)
if err != nil {
return err
}
p.Content = root.Content
p.Reason = root.Reason
p.MayRevise = root.MayRevise
p.RevisionID = root.RevisionID
p.RevisionDate = root.RevisionDate
if user, ok := root.RevisionBy.User(); ok {
p.RevisionBy = user
}
return nil
}
// WikiPageEditRequest represents a request to edit a wiki page in a subreddit.
type WikiPageEditRequest struct {
Subreddit string `url:"-"`
Page string `url:"page"`
Content string `url:"content"`
// Optional, up to 256 characters long.
Reason string `url:"reason,omitempty"`
}
// WikiPagePermissionLevel defines who can edit a specific wiki page in a subreddit.
type WikiPagePermissionLevel int
const (
// PermissionSubredditWikiPermissions uses subreddit wiki permissions.
PermissionSubredditWikiPermissions WikiPagePermissionLevel = iota
// PermissionApprovedContributorsOnly is only for approved wiki contributors.
PermissionApprovedContributorsOnly
// PermissionModeratorsOnly is only for moderators.
PermissionModeratorsOnly
)
// WikiPageSettings holds the settings for a specific wiki page.
type WikiPageSettings struct {
PermissionLevel WikiPagePermissionLevel `json:"permlevel"`
Listed bool `json:"listed"`
Editors []*User `json:"editors"`
}
// WikiPageSettingsUpdateRequest represents a request to update the visibility and
// permissions of a wiki page.
type WikiPageSettingsUpdateRequest struct {
// This HAS to be provided no matter what, or else we get a 500 response.
PermissionLevel WikiPagePermissionLevel `url:"permlevel"`
Listed *bool `url:"listed,omitempty"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *WikiPageSettings) UnmarshalJSON(b []byte) error {
root := new(struct {
PermissionLevel WikiPagePermissionLevel `json:"permlevel"`
Listed bool `json:"listed"`
Things []thing `json:"editors"`
})
err := json.Unmarshal(b, root)
if err != nil {
return err
}
s.PermissionLevel = root.PermissionLevel
s.Listed = root.Listed
for _, thing := range root.Things {
if user, ok := thing.User(); ok {
s.Editors = append(s.Editors, user)
}
}
return nil
}
type wikiPageRevisionListing struct {
Data struct {
Revisions []*WikiPageRevision `json:"children"`
After string `json:"after"`
} `json:"data"`
}
func (l *wikiPageRevisionListing) After() string {
return l.Data.After
}
// WikiPageRevision is a revision of a wiki page.
type WikiPageRevision struct {
ID string `json:"id,omitempty"`
Page string `json:"page,omitempty"`
Created *Timestamp `json:"timestamp,omitempty"`
Reason string `json:"reason,omitempty"`
Hidden bool `json:"revision_hidden"`
Author *User `json:"author,omitempty"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (r *WikiPageRevision) UnmarshalJSON(b []byte) error {
root := new(struct {
ID string `json:"id,omitempty"`
Page string `json:"page,omitempty"`
Created *Timestamp `json:"timestamp,omitempty"`
Reason string `json:"reason,omitempty"`
Hidden bool `json:"revision_hidden"`
Author thing `json:"author,omitempty"`
})
err := json.Unmarshal(b, root)
if err != nil {
return err
}
r.ID = root.ID
r.Page = root.Page
r.Created = root.Created
r.Reason = root.Reason
r.Hidden = root.Hidden
if user, ok := root.Author.User(); ok {
r.Author = user
}
return nil
}
// Page gets a wiki page.
func (s *WikiService) Page(ctx context.Context, subreddit, page string) (*WikiPage, *Response, error) {
return s.PageRevision(ctx, subreddit, page, "")
}
// PageRevision gets a wiki page at the version it was at the revisionID provided.
// If revisionID is an empty string, it will get the most recent version.
func (s *WikiService) PageRevision(ctx context.Context, subreddit, page, revisionID string) (*WikiPage, *Response, error) {
path := fmt.Sprintf("r/%s/wiki/%s", subreddit, page)
params := struct {
RevisionID string `url:"v,omitempty"`
}{revisionID}
t, resp, err := s.client.getThing(ctx, path, params)
if err != nil {
return nil, resp, err
}
wikiPage, _ := t.WikiPage()
return wikiPage, resp, nil
}
// Pages gets a list of wiki pages in the subreddit.
// Returns 403 Forbidden if the wiki is disabled.
func (s *WikiService) Pages(ctx context.Context, subreddit string) ([]string, *Response, error) {
path := fmt.Sprintf("r/%s/wiki/pages", subreddit)
t, resp, err := s.client.getThing(ctx, path, nil)
if err != nil {
return nil, resp, err
}
wikiPages, _ := t.WikiPages()
return wikiPages, resp, nil
}
// Edit a wiki page.
func (s *WikiService) Edit(ctx context.Context, editRequest *WikiPageEditRequest) (*Response, error) {
if editRequest == nil {
return nil, errors.New("*WikiPageEditRequest: cannot be nil")
}
form, err := query.Values(editRequest)
if err != nil {
return nil, err
}
path := fmt.Sprintf("r/%s/api/wiki/edit", editRequest.Subreddit)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Revert a wiki page to a specific revision.
func (s *WikiService) Revert(ctx context.Context, subreddit, page, revisionID string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/wiki/revert", subreddit)
form := url.Values{}
form.Set("page", page)
form.Set("revision", revisionID)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Settings gets the subreddit's wiki page's settings.
func (s *WikiService) Settings(ctx context.Context, subreddit, page string) (*WikiPageSettings, *Response, error) {
path := fmt.Sprintf("r/%s/wiki/settings/%s", subreddit, page)
t, resp, err := s.client.getThing(ctx, path, nil)
if err != nil {
return nil, resp, err
}
settings, _ := t.WikiPageSettings()
return settings, resp, nil
}
// UpdateSettings updates the subreddit's wiki page's settings.
func (s *WikiService) UpdateSettings(ctx context.Context, subreddit, page string, updateRequest *WikiPageSettingsUpdateRequest) (*WikiPageSettings, *Response, error) {
if updateRequest == nil {
return nil, nil, errors.New("*WikiPageSettingsUpdateRequest: cannot be nil")
}
form, err := query.Values(updateRequest)
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("r/%s/wiki/settings/%s", subreddit, page)
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
}
settings, _ := root.WikiPageSettings()
return settings, resp, nil
}
// Discussions gets a list of discussions (posts) about the wiki page.
func (s *WikiService) Discussions(ctx context.Context, subreddit, page string, opts *ListOptions) ([]*Post, *Response, error) {
path := fmt.Sprintf("r/%s/wiki/discussions/%s", subreddit, page)
l, resp, err := s.client.getListing(ctx, path, opts)
if err != nil {
return nil, resp, err
}
return l.Posts(), resp, nil
}
// ToggleVisibility toggles the public visibility of a wiki page revision.
// The returned bool is whether the page was set to hidden or not.
func (s *WikiService) ToggleVisibility(ctx context.Context, subreddit, page, revisionID string) (bool, *Response, error) {
path := fmt.Sprintf("r/%s/api/wiki/hide", subreddit)
form := url.Values{}
form.Set("page", page)
form.Set("revision", revisionID)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return false, nil, err
}
root := new(struct {
Status bool `json:"status"`
})
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return false, resp, err
}
return root.Status, resp, nil
}
func (s *WikiService) revisions(ctx context.Context, subreddit, page string, opts *ListOptions) ([]*WikiPageRevision, *Response, error) {
path := fmt.Sprintf("r/%s/wiki/revisions", subreddit)
if page != "" {
path += "/" + page
}
if opts != nil {
const idPrefix = "WikiRevision_"
if opts.After != "" && !strings.HasPrefix(opts.After, idPrefix) {
opts.After = idPrefix + opts.After
}
if opts.Before != "" && !strings.HasPrefix(opts.Before, idPrefix) {
opts.Before = idPrefix + opts.Before
}
}
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(wikiPageRevisionListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Data.Revisions, resp, nil
}
// Revisions gets revisions of all pages in the wiki.
func (s *WikiService) Revisions(ctx context.Context, subreddit string, opts *ListOptions) ([]*WikiPageRevision, *Response, error) {
return s.revisions(ctx, subreddit, "", opts)
}
// RevisionsPage gets revisions of the specific wiki page.
// If page is an empty string, it gets revisions of all pages in the wiki.
func (s *WikiService) RevisionsPage(ctx context.Context, subreddit, page string, opts *ListOptions) ([]*WikiPageRevision, *Response, error) {
return s.revisions(ctx, subreddit, page, opts)
}
// Allow the user to edit the specified wiki page in the subreddit.
func (s *WikiService) Allow(ctx context.Context, subreddit, page, username string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/wiki/alloweditor/add", subreddit)
form := url.Values{}
form.Set("page", page)
form.Set("username", username)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Deny the user the ability to edit the specified wiki page in the subreddit.
func (s *WikiService) Deny(ctx context.Context, subreddit, page, username string) (*Response, error) {
path := fmt.Sprintf("r/%s/api/wiki/alloweditor/del", subreddit)
form := url.Values{}
form.Set("page", page)
form.Set("username", username)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}