forked from vartanbeno/go-reddit
/
message.go
294 lines (234 loc) · 6.92 KB
/
message.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
package reddit
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"
"github.com/google/go-querystring/query"
)
// MessageService handles communication with the message
// related methods of the Reddit API.
//
// Reddit API docs: https://www.reddit.com/dev/api/#section_messages
type MessageService struct {
client *Client
}
// Message is a message.
type Message struct {
ID string `json:"id"`
FullID string `json:"name"`
Created *Timestamp `json:"created_utc"`
Subject string `json:"subject"`
Text string `json:"body"`
ParentID string `json:"parent_id"`
Author string `json:"author"`
To string `json:"dest"`
IsComment bool `json:"was_comment"`
}
type inboxThing struct {
Kind string `json:"kind"`
Data *Message `json:"data"`
}
type inboxListing struct {
inboxThings
after string
}
func (l *inboxListing) After() string {
return l.after
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (l *inboxListing) UnmarshalJSON(b []byte) error {
root := new(struct {
Data struct {
Things inboxThings `json:"children"`
After string `json:"after"`
} `json:"data"`
})
err := json.Unmarshal(b, root)
if err != nil {
return err
}
l.inboxThings = root.Data.Things
l.after = root.Data.After
return nil
}
type inboxThings struct {
Comments []*Message
Messages []*Message
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *inboxThings) UnmarshalJSON(b []byte) error {
var things []inboxThing
if err := json.Unmarshal(b, &things); err != nil {
return err
}
t.add(things...)
return nil
}
func (t *inboxThings) add(things ...inboxThing) {
for _, thing := range things {
switch thing.Kind {
case kindComment:
t.Comments = append(t.Comments, thing.Data)
case kindMessage:
t.Messages = append(t.Messages, thing.Data)
}
}
}
// SendMessageRequest represents a request to send a message.
type SendMessageRequest struct {
// Username, or /r/name for that subreddit's moderators.
To string `url:"to"`
Subject string `url:"subject"`
Text string `url:"text"`
// Optional. If specified, the message will look like it came from the subreddit.
FromSubreddit string `url:"from_sr,omitempty"`
}
// ReadAll marks all messages/comments as read. It queues up the task on Reddit's end.
// A successful response returns 202 to acknowledge acceptance of the request.
// This endpoint is heavily rate limited.
func (s *MessageService) ReadAll(ctx context.Context) (*Response, error) {
path := "api/read_all_messages"
req, err := s.client.NewRequest(http.MethodPost, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Read marks a message/comment as read via its full ID.
func (s *MessageService) Read(ctx context.Context, ids ...string) (*Response, error) {
if len(ids) == 0 {
return nil, errors.New("must provide at least 1 id")
}
path := "api/read_message"
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Unread marks a message/comment as unread via its full ID.
func (s *MessageService) Unread(ctx context.Context, ids ...string) (*Response, error) {
if len(ids) == 0 {
return nil, errors.New("must provide at least 1 id")
}
path := "api/unread_message"
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Block the author of a post, comment or message via its full ID.
func (s *MessageService) Block(ctx context.Context, id string) (*Response, error) {
path := "api/block"
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Collapse messages.
func (s *MessageService) Collapse(ctx context.Context, ids ...string) (*Response, error) {
if len(ids) == 0 {
return nil, errors.New("must provide at least 1 id")
}
path := "api/collapse_message"
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Uncollapse messages.
func (s *MessageService) Uncollapse(ctx context.Context, ids ...string) (*Response, error) {
if len(ids) == 0 {
return nil, errors.New("must provide at least 1 id")
}
path := "api/uncollapse_message"
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Delete a message.
func (s *MessageService) Delete(ctx context.Context, id string) (*Response, error) {
path := "api/del_msg"
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Send a message.
func (s *MessageService) Send(ctx context.Context, sendRequest *SendMessageRequest) (*Response, error) {
if sendRequest == nil {
return nil, errors.New("*SendMessageRequest: cannot be nil")
}
path := "api/compose"
form, err := query.Values(sendRequest)
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)
}
// Inbox returns comments and messages that appear in your inbox, respectively.
func (s *MessageService) Inbox(ctx context.Context, opts *ListOptions) ([]*Message, []*Message, *Response, error) {
root, resp, err := s.inbox(ctx, "message/inbox", opts)
if err != nil {
return nil, nil, resp, err
}
return root.Comments, root.Messages, resp, nil
}
// InboxUnread returns unread comments and messages that appear in your inbox, respectively.
func (s *MessageService) InboxUnread(ctx context.Context, opts *ListOptions) ([]*Message, []*Message, *Response, error) {
root, resp, err := s.inbox(ctx, "message/unread", opts)
if err != nil {
return nil, nil, resp, err
}
return root.Comments, root.Messages, resp, nil
}
// Sent returns messages that you've sent.
func (s *MessageService) Sent(ctx context.Context, opts *ListOptions) ([]*Message, *Response, error) {
root, resp, err := s.inbox(ctx, "message/sent", opts)
if err != nil {
return nil, resp, err
}
return root.Messages, resp, nil
}
func (s *MessageService) inbox(ctx context.Context, path string, opts *ListOptions) (*inboxListing, *Response, error) {
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(inboxListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err
}
return root, resp, nil
}