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: 68f1a73765134781e929613fa457e587de1b586e (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
/*
 * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * 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 orderBy from 'lodash/fp/orderBy'
import sortedUniqBy from 'lodash/fp/sortedUniqBy'
import Vue from 'vue'

import {buildMailboxHierarchy} from '../imap/MailboxHierarchy'
import {havePrefix} from '../imap/MailboxPrefix'
import {normalizedFolderId, normalizedMessageId, normalizedEnvelopeListId} from './normalization'
import {sortMailboxes} from '../imap/MailboxSorter'
import {UNIFIED_ACCOUNT_ID} from './constants'

const addFolderToState = (state, account) => (folder) => {
	const id = normalizedFolderId(account.id, folder.id)
	folder.accountId = account.id
	folder.envelopeLists = {}
	Vue.set(state.folders, id, folder)
	return id
}

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

export default {
	savePreference(state, {key, value}) {
		Vue.set(state.preferences, key, value)
	},
	addAccount(state, account) {
		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 folders to the store, but only keep IDs in the account's folder list
		const folders = buildMailboxHierarchy(sortMailboxes(account.folders || []), havePrefix(account.folders))
		Vue.set(account, 'folders', [])
		const addToState = addFolderToState(state, account)
		folders.forEach((folder) => {
			// Add all folders (including subfolders to state, but only toplevel to account
			const id = addToState(folder)
			Vue.set(folder, 'folders', folder.folders.map(addToState))

			account.folders.push(id)
		})
	},
	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
	},
	addFolder(state, {account, folder}) {
		// Flatten the existing ones before updating the hierarchy
		const existing = account.folders.map((id) => state.folders[id])
		existing.forEach((folder) => {
			if (!folder.folders) {
				return
			}
			folder.folders.map((id) => existing.push(state.folders[id]))
			folder.folders = []
		})
		// Save the folders to the store, but only keep IDs in the account's folder list
		existing.push(folder)
		const folders = buildMailboxHierarchy(sortMailboxes(existing), havePrefix(existing))
		Vue.set(account, 'folders', [])
		const addToState = addFolderToState(state, account)
		folders.forEach((folder) => {
			// Add all folders (including subfolders to state, but only toplevel to account
			const id = addToState(folder)
			Vue.set(folder, 'folders', folder.folders.map(addToState))

			account.folders.push(id)
		})
	},
	addEnvelope(state, {accountId, folderId, query, envelope}) {
		const folder = state.folders[normalizedFolderId(accountId, folderId)]
		Vue.set(state.envelopes, envelope.uuid, envelope)
		const listId = normalizedEnvelopeListId(query)
		const existing = folder.envelopeLists[listId] || []
		const uuidToDateInt = (uuid) => state.envelopes[uuid].dateInt
		const sortedUniqByDateInt = sortedUniqBy(uuidToDateInt)
		const orderByDateInt = orderBy(uuidToDateInt, 'desc')
		Vue.set(folder.envelopeLists, listId, sortedUniqByDateInt(orderByDateInt(existing.concat([envelope.uuid]))))

		const unifiedAccount = state.accounts[UNIFIED_ACCOUNT_ID]
		unifiedAccount.folders
			.map((fId) => state.folders[fId])
			.filter((f) => f.specialRole && f.specialRole === folder.specialRole)
			.forEach((folder) => {
				const existing = folder.envelopeLists[listId] || []
				Vue.set(
					folder.envelopeLists,
					listId,
					sortedUniqByDateInt(orderByDateInt(existing.concat([envelope.uuid])))
				)
			})
	},
	updateEnvelope(state, {envelope}) {
		const existing = state.envelopes[envelope.uid]
		if (!existing) {
			return
		}
		Vue.set(existing, 'flags', envelope.flags)
	},
	flagEnvelope(state, {envelope, flag, value}) {
		envelope.flags[flag] = value
	},
	removeEnvelope(state, {accountId, folderId, uid}) {
		const folder = state.folders[normalizedFolderId(accountId, folderId)]
		for (const listId in folder.envelopeLists) {
			if (!Object.hasOwnProperty.call(folder.envelopeLists, listId)) {
				continue
			}
			const list = folder.envelopeLists[listId]
			const idx = list.indexOf(normalizedMessageId(accountId, folderId, uid))
			if (idx < 0) {
				continue
			}
			console.debug('envelope removed from mailbox', accountId, folder.id, uid, listId)
			list.splice(idx, 1)
		}

		state.accounts[UNIFIED_ACCOUNT_ID].folders
			.map((fId) => state.folders[fId])
			.filter((f) => f.specialRole && f.specialRole === folder.specialRole)
			.forEach((folder) => {
				for (const listId in folder.envelopeLists) {
					if (!Object.hasOwnProperty.call(folder.envelopeLists, listId)) {
						continue
					}
					const list = folder.envelopeLists[listId]
					const idx = list.indexOf(normalizedMessageId(accountId, folderId, uid))
					if (idx < 0) {
						console.warn(
							'envelope does not exist in unified mailbox',
							accountId,
							folder.id,
							uid,
							listId,
							list
						)
						continue
					}
					console.debug('envelope removed from unified mailbox', accountId, folder.id, uid, listId)
					list.splice(idx, 1)
				}
			})
	},
	addMessage(state, {accountId, folderId, message}) {
		const uuid = normalizedMessageId(accountId, folderId, message.uid)
		message.accountId = accountId
		message.folderId = folderId
		message.uuid = uuid
		Vue.set(state.messages, uuid, message)
	},
	updateDraft(state, {draft, data, newUid}) {
		// Update draft's UID
		const oldUid = draft.uid
		const uid = normalizedMessageId(draft.accountId, draft.folderId, newUid)
		console.debug('saving draft as UID ' + uid)
		draft.uid = uid

		// TODO: strategy to keep the full draft object in sync, not just the visible
		//       changes
		draft.subject = data.subject

		// Update ref in folder's envelope list
		const envs = state.folders[normalizedFolderId(draft.accountId, draft.folderId)].envelopes
		const idx = envs.indexOf(oldUid)
		if (idx < 0) {
			console.warn('not replacing draft ' + oldUid + ' in envelope list because it did not exist')
		} else {
			envs[idx] = uid
		}

		// Move message/envelope objects to new keys
		Vue.delete(state.envelopes, oldUid)
		Vue.delete(state.messages, oldUid)
		Vue.set(state.envelopes, uid, draft)
		Vue.set(state.messages, uid, draft)
	},
	removeMessage(state, {accountId, folderId, uid}) {
		Vue.delete(state.messages, normalizedMessageId(accountId, folderId, uid))
	},
	createAlias(state, {account, alias}) {
		account.aliases.push(alias)
	},
	deleteAlias(state, {account, alias}) {
		account.aliases.splice(account.aliases.indexOf(alias), 1)
	},
}