-
Notifications
You must be signed in to change notification settings - Fork 17
/
message.go
343 lines (269 loc) · 8.28 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
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
package proton
import (
"bytes"
"context"
"fmt"
"runtime"
"strconv"
"github.com/ProtonMail/gluon/async"
"github.com/bradenaw/juniper/parallel"
"github.com/bradenaw/juniper/xslices"
"github.com/go-resty/resty/v2"
)
const maxMessageIDs = 1000
func (c *Client) GetFullMessage(ctx context.Context, messageID string, scheduler Scheduler, storageProvider AttachmentAllocator) (FullMessage, error) {
message, err := c.GetMessage(ctx, messageID)
if err != nil {
return FullMessage{}, err
}
attDataBuffers, err := scheduler.Schedule(ctx, xslices.Map(message.Attachments, func(att Attachment) string {
return att.ID
}), storageProvider, func(ctx context.Context, s string, buffer *bytes.Buffer) error {
return c.GetAttachmentInto(ctx, s, buffer)
})
if err != nil {
return FullMessage{}, err
}
return FullMessage{
Message: message,
AttData: xslices.Map(attDataBuffers, func(b *bytes.Buffer) []byte {
return b.Bytes()
}),
}, nil
}
func (c *Client) GetMessage(ctx context.Context, messageID string) (Message, error) {
var res struct {
Message Message
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).Get("/mail/v4/messages/" + messageID)
}); err != nil {
return Message{}, err
}
return res.Message, nil
}
func (c *Client) CountMessages(ctx context.Context) (int, error) {
return c.countMessages(ctx, MessageFilter{})
}
func (c *Client) GetMessageMetadata(ctx context.Context, filter MessageFilter) ([]MessageMetadata, error) {
count, err := c.countMessages(ctx, filter)
if err != nil {
return nil, err
}
return fetchPaged(ctx, count, maxPageSize, c, func(ctx context.Context, page, pageSize int) ([]MessageMetadata, error) {
return c.GetMessageMetadataPage(ctx, page, pageSize, filter)
})
}
func (c *Client) GetAllMessageIDs(ctx context.Context, afterID string) ([]string, error) {
var messageIDs []string
for ; ; afterID = messageIDs[len(messageIDs)-1] {
page, err := c.GetMessageIDs(ctx, afterID, maxMessageIDs)
if err != nil {
return nil, err
}
if len(page) == 0 {
return messageIDs, nil
}
messageIDs = append(messageIDs, page...)
}
}
func (c *Client) DeleteMessage(ctx context.Context, messageIDs ...string) error {
pages := xslices.Chunk(messageIDs, maxPageSize)
return parallel.DoContext(ctx, runtime.NumCPU(), len(pages), func(ctx context.Context, idx int) error {
defer async.HandlePanic(c.m.panicHandler)
return c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(MessageActionReq{IDs: pages[idx]}).Put("/mail/v4/messages/delete")
})
})
}
func (c *Client) MarkMessagesRead(ctx context.Context, messageIDs ...string) error {
for _, page := range xslices.Chunk(messageIDs, maxPageSize) {
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(MessageActionReq{IDs: page}).Put("/mail/v4/messages/read")
}); err != nil {
return err
}
}
return nil
}
func (c *Client) MarkMessagesForwarded(ctx context.Context, messageIDs ...string) error {
type subResponse struct {
Response APIError
ID string
}
var response struct {
Code int
Responses []subResponse
}
for _, page := range xslices.Chunk(messageIDs, maxPageSize) {
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(MessageActionReq{IDs: page}).SetResult(&response).Put("/mail/v4/messages/forward")
}); err != nil {
return err
}
for _, r := range response.Responses {
if r.Response.Code != SuccessCode {
return fmt.Errorf("failed to mark message %v as forwarded", r.ID)
}
}
}
return nil
}
func (c *Client) MarkMessagesUnForwarded(ctx context.Context, messageIDs ...string) error {
type subResponse struct {
Response APIError
ID string
}
var response struct {
Code int
Responses []subResponse
}
for _, page := range xslices.Chunk(messageIDs, maxPageSize) {
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(MessageActionReq{IDs: page}).SetResult(&response).Put("/mail/v4/messages/unforward")
}); err != nil {
return err
}
for _, r := range response.Responses {
if r.Response.Code != SuccessCode {
return fmt.Errorf("failed to unmark message %v as forwarded", r.ID)
}
}
}
return nil
}
func (c *Client) MarkMessagesUnread(ctx context.Context, messageIDs ...string) error {
for _, page := range xslices.Chunk(messageIDs, maxPageSize) {
req := MessageActionReq{IDs: page}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(req).Put("/mail/v4/messages/unread")
}); err != nil {
return err
}
}
return nil
}
func (c *Client) LabelMessages(ctx context.Context, messageIDs []string, labelID string) error {
var results []LabelMessagesRes
for _, chunk := range xslices.Chunk(messageIDs, maxPageSize) {
var res LabelMessagesRes
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(LabelMessagesReq{
LabelID: labelID,
IDs: chunk,
}).SetResult(&res).Put("/mail/v4/messages/label")
}); err != nil {
return err
}
if ok, errStr := res.ok(); !ok {
tokens := xslices.Map(results, func(res LabelMessagesRes) UndoToken {
return res.UndoToken
})
if _, undoErr := c.UndoActions(ctx, tokens...); undoErr != nil {
return fmt.Errorf("failed to undo label actions (undo reason: %v): %w", errStr, undoErr)
}
return fmt.Errorf("failed to label messages: %v", errStr)
}
results = append(results, res)
}
return nil
}
func (c *Client) UnlabelMessages(ctx context.Context, messageIDs []string, labelID string) error {
var results []LabelMessagesRes
for _, chunk := range xslices.Chunk(messageIDs, maxPageSize) {
var res LabelMessagesRes
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(LabelMessagesReq{
LabelID: labelID,
IDs: chunk,
}).SetResult(&res).Put("/mail/v4/messages/unlabel")
}); err != nil {
return err
}
if ok, errStr := res.ok(); !ok {
tokens := xslices.Map(results, func(res LabelMessagesRes) UndoToken {
return res.UndoToken
})
if _, undoErr := c.UndoActions(ctx, tokens...); undoErr != nil {
return fmt.Errorf("failed to undo unlabel actions (undo reason: %v): %w", errStr, undoErr)
}
return fmt.Errorf("failed to unlabel messages: %v", errStr)
}
results = append(results, res)
}
return nil
}
func (c *Client) GetMessageIDs(ctx context.Context, afterID string, limit int) ([]string, error) {
if limit > maxMessageIDs {
limit = maxMessageIDs
}
var res struct {
IDs []string
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
if afterID != "" {
r = r.SetQueryParam("AfterID", afterID)
}
return r.SetQueryParam("Limit", strconv.Itoa(limit)).SetResult(&res).Get("/mail/v4/messages/ids")
}); err != nil {
return nil, err
}
return res.IDs, nil
}
func (c *Client) countMessages(ctx context.Context, filter MessageFilter) (int, error) {
var res struct {
Total int
}
req := struct {
MessageFilter
Limit int `json:",,string"`
}{
MessageFilter: filter,
Limit: 0,
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(req).SetResult(&res).SetHeader("X-HTTP-Method-Override", "GET").Post("/mail/v4/messages")
}); err != nil {
return 0, err
}
return res.Total, nil
}
func (c *Client) GetMessageMetadataPage(ctx context.Context, page, pageSize int, filter MessageFilter) ([]MessageMetadata, error) {
var res struct {
Messages []MessageMetadata
Stale Bool
}
req := struct {
MessageFilter
Page int
PageSize int
Sort string
}{
MessageFilter: filter,
Page: page,
PageSize: pageSize,
Sort: "ID",
}
for {
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(req).SetResult(&res).SetHeader("X-HTTP-Method-Override", "GET").Post("/mail/v4/messages")
}); err != nil {
return nil, err
}
if !res.Stale {
break
}
}
return res.Messages, nil
}
func (c *Client) GetGroupedMessageCount(ctx context.Context) ([]MessageGroupCount, error) {
var res struct {
Counts []MessageGroupCount
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).Get("/mail/v4/messages/count")
}); err != nil {
return nil, err
}
return res.Counts, nil
}