Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MessageService.js « service « src - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0492272435a4b84a43356a00d87a69b93a6288b8 (plain)
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
import { generateUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import { curry } from 'ramda'

import { parseErrorResponse } from '../http/ErrorResponseParser'
import { convertAxiosError } from '../errors/convert'
import SyncIncompleteError from '../errors/SyncIncompleteError'

const amendEnvelopeWithIds = curry((accountId, envelope) => ({
	accountId,
	...envelope,
}))

export function fetchEnvelope(id) {
	const url = generateUrl('/apps/mail/api/messages/{id}', {
		id,
	})

	return axios
		.get(url)
		.then((resp) => resp.data)
		.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) => {
	const url = generateUrl('apps/mail/api/messages/{id}/thread', {
		id,
	})
	const resp = await axios.get(url)
	return resp.data
}

export async function syncEnvelopes(accountId, id, ids, query, init = false) {
	const url = generateUrl('/apps/mail/api/mailboxes/{id}/sync', {
		id,
	})

	try {
		const response = await axios.post(url, {
			ids,
			query,
			init,
		})

		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)
	}
}

export function setEnvelopeFlag(id, flag, value) {
	const url = generateUrl('/apps/mail/api/messages/{id}/flags', {
		id,
	})

	return axios
		.put(url, {
			flags: {
				[flag]: value,
			},
		})
}
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 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 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 sendMessage(accountId, data) {
	const url = generateUrl('/apps/mail/api/accounts/{accountId}/send', {
		accountId,
	})

	try {
		const resp = await axios.post(url, data)
		return resp.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 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)
	}
}