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

mutations.js « store « src - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c63a15cd1e10e3003e3bb8aea0209b099e0b0a2 (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
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
/*
 * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author 2021 Richard Steinmetz <richard@steinmetz.cloud>
 *
 * @license AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import { curry } from 'ramda'
import escapeRegExp from 'lodash/fp/escapeRegExp'
import orderBy from 'lodash/fp/orderBy'
import uniq from 'lodash/fp/uniq'
import Vue from 'vue'

import { sortMailboxes } from '../imap/MailboxSorter'
import { normalizedEnvelopeListId } from './normalization'
import { UNIFIED_ACCOUNT_ID } from './constants'

const transformMailboxName = (account, mailbox) => {
	// Add all mailboxes (including submailboxes to state, but only toplevel to account
	const nameWithoutPrefix = account.personalNamespace
		? mailbox.name.replace(new RegExp(escapeRegExp(account.personalNamespace)), '')
		: mailbox.name
	if (nameWithoutPrefix.includes(mailbox.delimiter)) {
		/**
		 * Sub-mailbox, e.g. 'Archive.2020' or 'INBOX.Archive.2020'
		 */
		mailbox.displayName = mailbox.name.substr(mailbox.name.lastIndexOf(mailbox.delimiter) + 1)
		mailbox.path = mailbox.name.substr(0, mailbox.name.lastIndexOf(mailbox.delimiter))
	} else if (account.personalNamespace && mailbox.name.startsWith(account.personalNamespace)) {
		/**
		 * Top-level mailbox, but with a personal namespace, e.g. 'INBOX.Sent'
		 */
		mailbox.displayName = nameWithoutPrefix
		mailbox.path = account.personalNamespace
	} else {
		/**
		 * Top-level mailbox, e.g. 'INBOX' or 'Draft'
		 */
		mailbox.displayName = nameWithoutPrefix
		mailbox.path = ''
	}
}

const addMailboxToState = curry((state, account, mailbox) => {
	mailbox.accountId = account.id
	mailbox.mailboxes = []
	Vue.set(mailbox, 'envelopeLists', {})

	transformMailboxName(account, mailbox)

	Vue.set(state.mailboxes, mailbox.databaseId, mailbox)
	const parent = Object.values(state.mailboxes)
		.filter(mb => mb.accountId === account.id)
		.find(mb => mb.name === mailbox.path)
	if (mailbox.path === '' || !parent) {
		account.mailboxes.push(mailbox.databaseId)
	} else {
		parent.mailboxes.push(mailbox.databaseId)
	}

})

const sortAccounts = (accounts) => {
	accounts.sort((a1, a2) => a1.order - a2.order)
	return accounts
}

/**
 * Convert envelope tag objects to references and add new tags to global list.
 * @param {Object} state vuex state
 * @param {Object} envelope envelope with tag objects
 */
const normalizeTags = (state, envelope) => {
	if (Array.isArray(envelope.tags)) {
		// Tags have been normalized already
		return
	}

	const tags = Object
		.entries(envelope.tags ?? {})
		.map(([imapLabel, tag]) => {
			if (!state.tags[tag.id]) {
				Vue.set(state.tags, tag.id, tag)
			}
			if (!state.tagList.includes(tag.id)) {
				state.tagList.push(tag.id)
			}
			return tag.id
		})

	Vue.set(envelope, 'tags', tags)
}

/**
 * Append or replace an envelope id for an existing message list
 *
 * If the given thread root id exist the message is replaced
 * otherwise appended
 *
 * @param {Object} state vuex state
 * @param {Array} existing list of envelope ids for a message list
 * @param {Object} envelope envelope with tag objects
 * @returns {Array} list of envelope ids
 */
const appendOrReplaceEnvelopeId = (state, existing, envelope) => {
	const index = existing.findIndex((id) => state.envelopes[id].threadRootId === envelope.threadRootId)
	if (index === -1) {
		existing.push(envelope.databaseId)
	} else {
		existing[index] = envelope.databaseId
	}
	return existing
}

export default {
	savePreference(state, { key, value }) {
		Vue.set(state.preferences, key, value)
	},
	addAccount(state, account) {
		account.collapsed = account.collapsed ?? true
		Vue.set(state.accounts, account.id, account)
		Vue.set(
			state,
			'accountList',
			sortAccounts(state.accountList.concat([account.id]).map((id) => state.accounts[id])).map((a) => a.id)
		)

		// Save the mailboxes to the store, but only keep IDs in the account's mailboxes list
		const mailboxes = sortMailboxes(account.mailboxes || [])
		Vue.set(account, 'mailboxes', [])
		Vue.set(account, 'aliases', account.aliases ?? [])
		mailboxes.map(addMailboxToState(state, account))
	},
	editAccount(state, account) {
		Vue.set(state.accounts, account.id, Object.assign({}, state.accounts[account.id], account))
	},
	patchAccount(state, { account, data }) {
		Vue.set(state.accounts, account.id, Object.assign({}, state.accounts[account.id], data))
	},
	saveAccountsOrder(state, { account, order }) {
		Vue.set(account, 'order', order)
		Vue.set(
			state,
			'accountList',
			sortAccounts(state.accountList.map((id) => state.accounts[id])).map((a) => a.id)
		)
	},
	toggleAccountCollapsed(state, accountId) {
		state.accounts[accountId].collapsed = !state.accounts[accountId].collapsed
	},
	expandAccount(state, accountId) {
		state.accounts[accountId].collapsed = false
	},
	setAccountSetting(state, { accountId, key, value }) {
		const accountSettings = state.allAccountSettings.find(settings => settings.accountId === accountId)
		if (accountSettings) {
			accountSettings[key] = value
		} else {
			const newAccountSettings = { accountId }
			newAccountSettings[key] = value
			state.allAccountSettings.push(newAccountSettings)
		}
	},
	addMailbox(state, { account, mailbox }) {
		addMailboxToState(state, account, mailbox)
	},
	updateMailbox(state, { mailbox }) {
		const account = state.accounts[mailbox.accountId]
		transformMailboxName(account, mailbox)
		Vue.set(state.mailboxes, mailbox.databaseId, mailbox)
	},
	removeMailbox(state, { id }) {
		const mailbox = state.mailboxes[id]
		if (mailbox === undefined) {
			throw new Error(`Mailbox ${id} does not exist`)
		}
		const account = state.accounts[mailbox.accountId]
		if (account === undefined) {
			throw new Error(`Account ${mailbox.accountId} of mailbox ${id} is unknown`)
		}
		Vue.delete(state.mailboxes, id)

		// Travers through the account and the full mailbox tree to find any dangling pointers
		const removeRec = (parent) => {
			parent.mailboxes = parent.mailboxes.filter((mbId) => mbId !== id)
			parent.mailboxes.map(mbid => removeRec(state.mailboxes[mbid]))
		}
		removeRec(account)
	},
	showMessageComposer(state, { type, data, forwardedMessages, originalSendAt }) {
		Vue.set(state, 'newMessage', {
			type,
			data,
			options: {
				forwardedMessages,
				originalSendAt,
			},
		})
	},
	convertComposerMessageToOutbox(state, { message }) {
		Vue.set(state.newMessage, 'type', 'outbox')
		Vue.set(state.newMessage.data, 'id', message.id)
	},
	hideMessageComposer(state) {
		Vue.delete(state, 'newMessage')
	},
	addEnvelope(state, { query, envelope, addToUnifiedMailboxes = true }) {
		normalizeTags(state, envelope)
		const mailbox = state.mailboxes[envelope.mailboxId]
		Vue.set(state.envelopes, envelope.databaseId, Object.assign({}, state.envelopes[envelope.databaseId] || {}, envelope))
		Vue.set(envelope, 'accountId', mailbox.accountId)

		const idToDateInt = (id) => state.envelopes[id].dateInt
		const orderByDateInt = orderBy(idToDateInt, 'desc')

		const listId = normalizedEnvelopeListId(query)
		const existing = mailbox.envelopeLists[listId] || []
		Vue.set(mailbox.envelopeLists, listId, uniq(orderByDateInt(appendOrReplaceEnvelopeId(state, existing, envelope))))

		if (!addToUnifiedMailboxes) {
			return
		}
		const unifiedAccount = state.accounts[UNIFIED_ACCOUNT_ID]
		unifiedAccount.mailboxes
			.map((mbId) => state.mailboxes[mbId])
			.filter((mb) => mb.specialRole && mb.specialRole === mailbox.specialRole)
			.forEach((mailbox) => {
				const existing = mailbox.envelopeLists[listId] || []
				Vue.set(
					mailbox.envelopeLists,
					listId,
					uniq(orderByDateInt(appendOrReplaceEnvelopeId(state, existing, envelope)))
				)
			})
	},
	updateEnvelope(state, { envelope }) {
		const existing = state.envelopes[envelope.databaseId]
		if (!existing) {
			return
		}
		normalizeTags(state, envelope)
		Vue.set(existing, 'flags', envelope.flags)
		Vue.set(existing, 'tags', envelope.tags)
	},
	flagEnvelope(state, { envelope, flag, value }) {
		const mailbox = state.mailboxes[envelope.mailboxId]
		if (mailbox && flag === 'seen') {
			const unread = mailbox.unread ?? 0
			if (envelope.flags[flag] && !value) {
				Vue.set(mailbox, 'unread', unread + 1)
			} else if (!envelope.flags[flag] && value) {
				Vue.set(mailbox, 'unread', Math.max(unread - 1, 0))
			}
		}
		Vue.set(envelope.flags, flag, value)
	},
	addTag(state, { tag }) {
		Vue.set(state.tags, tag.id, tag)
		state.tagList.push(tag.id)
	},
	addEnvelopeTag(state, { envelope, tagId }) {
		Vue.set(envelope, 'tags', uniq([...envelope.tags, tagId]))
	},
	updateTag(state, { tag, displayName, color }) {
		tag.displayName = displayName
		tag.color = color
	},
	removeEnvelopeTag(state, { envelope, tagId }) {
		Vue.set(envelope, 'tags', envelope.tags.filter((id) => id !== tagId))
	},
	removeEnvelope(state, { id }) {
		const envelope = state.envelopes[id]
		if (!envelope) {
			console.warn('envelope ' + id + ' is unknown, can\'t remove it')
			return
		}
		const mailbox = state.mailboxes[envelope.mailboxId]
		for (const listId in mailbox.envelopeLists) {
			if (!Object.hasOwnProperty.call(mailbox.envelopeLists, listId)) {
				continue
			}
			const list = mailbox.envelopeLists[listId]
			const idx = list.indexOf(id)
			if (idx < 0) {
				continue
			}
			console.debug('envelope ' + id + ' removed from mailbox list ' + listId)
			list.splice(idx, 1)
		}

		if (!envelope.seen && mailbox.unread) {
			Vue.set(mailbox, 'unread', mailbox.unread - 1)
		}

		state.accounts[UNIFIED_ACCOUNT_ID].mailboxes
			.map((mailboxId) => state.mailboxes[mailboxId])
			.filter((mb) => mb.specialRole && mb.specialRole === mailbox.specialRole)
			.forEach((mailbox) => {
				for (const listId in mailbox.envelopeLists) {
					if (!Object.hasOwnProperty.call(mailbox.envelopeLists, listId)) {
						continue
					}
					const list = mailbox.envelopeLists[listId]
					const idx = list.indexOf(id)
					if (idx < 0) {
						console.warn(
							'envelope does not exist in unified mailbox',
							mailbox.databaseId,
							id,
							listId,
							list
						)
						continue
					}
					console.debug('envelope removed from unified mailbox', mailbox.databaseId, id)
					list.splice(idx, 1)
				}
			})

		// Delete references from other threads
		for (const [key, env] of Object.entries(state.envelopes)) {
			if (!env.thread) {
				continue
			}

			const thread = env.thread.filter(threadId => threadId !== id)
			Vue.set(state.envelopes[key], 'thread', thread)
		}

		Vue.delete(state.envelopes, id)
	},
	addMessage(state, { message }) {
		Vue.set(state.messages, message.databaseId, message)
	},
	addMessageItineraries(state, { id, itineraries }) {
		const message = state.messages[id]
		if (!message) {
			return
		}
		Vue.set(message, 'itineraries', itineraries)
	},
	addEnvelopeThread(state, { id, thread }) {
		// Store the envelopes, merge into any existing object if one exists
		thread.forEach(e => {
			normalizeTags(state, e)
			const mailbox = state.mailboxes[e.mailboxId]
			Vue.set(e, 'accountId', mailbox.accountId)
			Vue.set(state.envelopes, e.databaseId, Object.assign({}, state.envelopes[e.databaseId] || {}, e))
		})

		// Store the references
		Vue.set(state.envelopes[id], 'thread', thread.map(e => e.databaseId))
	},
	removeMessage(state, { id }) {
		Vue.delete(state.messages, id)
	},
	createAlias(state, { account, alias }) {
		account.aliases.push(alias)
	},
	deleteAlias(state, { account, aliasId }) {
		const index = account.aliases.findIndex(temp => aliasId === temp.id)
		if (index !== -1) {
			account.aliases.splice(index, 1)
		}
	},
	patchAlias(state, { account, aliasId, data }) {
		const index = account.aliases.findIndex(temp => aliasId === temp.id)
		if (index !== -1) {
			account.aliases[index] = Object.assign({}, account.aliases[index], data)
		}
	},
	setMailboxUnreadCount(state, { id, unread }) {
		Vue.set(state.mailboxes[id], 'unread', unread ?? 0)
	},
	setScheduledSendingDisabled(state, value) {
		state.isScheduledSendingDisabled = value
	},
}