-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathMessageService.js
293 lines (245 loc) Β· 5.95 KB
/
MessageService.js
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
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { generateUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import { curry } from 'ramda'
import { parseErrorResponse } from '../http/ErrorResponseParser.js'
import { convertAxiosError } from '../errors/convert.js'
import SyncIncompleteError from '../errors/SyncIncompleteError.js'
const amendEnvelopeWithIds = curry((accountId, envelope) => ({
accountId,
...envelope,
}))
export function fetchEnvelope(accountId, id) {
const url = generateUrl('/apps/mail/api/messages/{id}', {
id,
})
return axios
.get(url)
.then((resp) => resp.data)
.then(amendEnvelopeWithIds(accountId))
.catch((error) => {
if (error.response && error.response.status === 404) {
return undefined
}
return Promise.reject(parseErrorResponse(error.response))
})
}
export function fetchEnvelopes(accountId, mailboxId, query, cursor, limit) {
const url = generateUrl('/apps/mail/api/messages')
const params = {
mailboxId,
}
if (query) {
params.filter = query
}
if (limit) {
params.limit = limit
}
if (cursor) {
params.cursor = cursor
}
return axios
.get(url, {
params,
})
.then((resp) => resp.data)
.then(envelopes => envelopes.map(amendEnvelopeWithIds(accountId)))
.catch((error) => {
throw convertAxiosError(error)
})
}
export const fetchThread = async (id, knownIds) => {
const url = generateUrl('apps/mail/api/messages/{id}/thread', {
id,
})
const resp = await axios.post(url, {
knownIds,
})
// Thread is up to date
if (resp.status === 204) {
return []
}
return resp.data
}
export async function syncEnvelopes(accountId, id, ids, lastMessageTimestamp, query, init = false, sortOrder) {
const url = generateUrl('/apps/mail/api/mailboxes/{id}/sync', {
id,
})
try {
const response = await axios.post(url, {
ids,
lastMessageTimestamp,
init,
sortOrder,
query,
})
if (response.status === 202) {
throw new SyncIncompleteError()
}
const amend = amendEnvelopeWithIds(accountId)
return {
newMessages: response.data.newMessages.map(amend),
changedMessages: response.data.changedMessages.map(amend),
vanishedMessages: response.data.vanishedMessages,
stats: response.data.stats,
}
} catch (e) {
throw convertAxiosError(e)
}
}
export async function clearCache(accountId, id) {
const url = generateUrl('/apps/mail/api/mailboxes/{id}/sync', {
id,
})
try {
const response = await axios.delete(url)
if (response.status === 202) {
throw new SyncIncompleteError()
}
} catch (e) {
throw convertAxiosError(e)
}
}
/**
* Set flags for envelope
*
* @param {int} id
* @param {object} flags
*/
export async function setEnvelopeFlags(id, flags) {
const url = generateUrl('/apps/mail/api/messages/{id}/flags', {
id,
})
return await axios.put(url, {
flags,
})
}
export async function createEnvelopeTag(displayName, color) {
const url = generateUrl('/apps/mail/api/tags')
const { data } = await axios.post(url, { displayName, color })
return data
}
export async function setEnvelopeTag(id, imapLabel) {
const url = generateUrl('/apps/mail/api/messages/{id}/tags/{imapLabel}', {
id, imapLabel,
})
const { data } = await axios.put(url)
return data
}
export async function updateEnvelopeTag(id, displayName, color) {
const url = generateUrl('/apps/mail/api/tags/{id}', {
id,
})
await axios.put(url, { displayName, color })
}
export async function deleteTag(id, accountId) {
const url = generateUrl('/apps/mail/api/tags/{accountId}/delete/{id}', {
accountId, id,
})
await axios.delete(url)
}
export async function removeEnvelopeTag(id, imapLabel) {
const url = generateUrl('/apps/mail/api/messages/{id}/tags/{imapLabel}', {
id, imapLabel,
})
const { data } = await axios.delete(url)
return data
}
export async function fetchMessage(id) {
const url = generateUrl('/apps/mail/api/messages/{id}/body', {
id,
})
try {
const resp = await axios.get(url)
return resp.data
} catch (error) {
if (error.response && error.response.status === 404) {
return undefined
}
throw parseErrorResponse(error.response)
}
}
export async function fetchMessageItineraries(id) {
const url = generateUrl('/apps/mail/api/messages/{id}/itineraries', {
id,
})
try {
const resp = await axios.get(url)
return resp.data
} catch (error) {
if (error.response && error.response.status === 404) {
return undefined
}
throw parseErrorResponse(error.response)
}
}
export async function fetchMessageDkim(id) {
const url = generateUrl('/apps/mail/api/messages/{id}/dkim', {
id,
})
try {
const resp = await axios.get(url)
return resp.data
} catch (error) {
if (error.response && error.response.status === 404) {
return undefined
}
throw parseErrorResponse(error.response)
}
}
export async function saveDraft(accountId, data) {
const url = generateUrl('/apps/mail/api/accounts/{accountId}/draft', {
accountId,
})
try {
return (await axios.post(url, data)).data
} catch (e) {
throw convertAxiosError(e)
}
}
export async function deleteMessage(id) {
const url = generateUrl('/apps/mail/api/messages/{id}', {
id,
})
try {
return (await axios.delete(url)).data
} catch (e) {
throw convertAxiosError(e)
}
}
export function moveMessage(id, destFolderId) {
const url = generateUrl('/apps/mail/api/messages/{id}/move', {
id,
})
return axios.post(url, {
destFolderId,
})
}
export function snoozeMessage(id, unixTimestamp, destMailboxId) {
const url = generateUrl('/apps/mail/api/messages/{id}/snooze', {
id,
})
return axios.post(url, {
unixTimestamp,
destMailboxId,
})
}
export function unSnoozeMessage(id) {
const url = generateUrl('/apps/mail/api/messages/{id}/unsnooze', {
id,
})
return axios.post(url, {})
}
export async function sendMdn(id, data) {
const url = generateUrl('/apps/mail/api/messages/{id}/mdn', {
id,
})
try {
await axios.post(url, data)
} catch (e) {
throw convertAxiosError(e)
}
}