summaryrefslogtreecommitdiffstats
path: root/src/store/contacts.js
blob: 7b5774678045b8817c77745177f82197af6749bd (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
/**
 * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @author John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @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 Vue from 'vue'
import ICAL from 'ical.js'
import Contact from '../models/contact'

const state = {
	// Using objects for performance
	// https://jsperf.com/ensure-unique-id-objects-vs-array
	contacts: {},
	sortedContacts: [],
	orderKey: 'displayName'
}

const mutations = {

	/**
	 * Store contacts into state
	 *
	 * @param {Object} state Default state
	 * @param {Array} contacts Contacts
	 */
	appendContacts(state, contacts = []) {
		state.contacts = contacts.reduce(function(list, contact) {
			if (contact instanceof Contact) {
				Vue.set(list, contact.key, contact)
			} else {
				console.error('Wrong contact object', contact)
			}
			return list
		}, state.contacts)
	},

	/**
	 * Delete a contact from the global contacts list
	 *
	 * @param {Object} state
	 * @param {Contact} contact
	 */
	deleteContact(state, contact) {
		if (state.contacts[contact.key] && contact instanceof Contact) {

			let index = state.sortedContacts.findIndex(search => search.key === contact.key)
			state.sortedContacts.splice(index, 1)
			Vue.delete(state.contacts, contact.key)

		} else {
			console.error('Error while deleting the following contact', contact)
		}
	},

	/**
	 * Insert new contact into sorted array
	 *
	 * @param {Object} state
	 * @param {Contact} contact
	 */
	addContact(state, contact) {
		if (contact instanceof Contact) {

			let sortedContact = {
				key: contact.key,
				value: contact[state.orderKey]
			}

			// Not using sort, splice has far better performances
			// https://jsperf.com/sort-vs-splice-in-array
			for (var i = 0, len = state.sortedContacts.length; i < len; i++) {
				var nameA = state.sortedContacts[i].value.toUpperCase()	// ignore upper and lowercase
				var nameB = sortedContact.value.toUpperCase()			// ignore upper and lowercase
				if (nameA.localeCompare(nameB) >= 0) {
					state.sortedContacts.splice(i, 0, sortedContact)
					break
				} else if (i + 1 === len) {
					// we reached the end insert it now
					state.sortedContacts.push(sortedContact)
				}
			}

			// sortedContact is empty, just push it
			if (state.sortedContacts.length === 0) {
				state.sortedContacts.push(sortedContact)
			}

			// default contacts list
			Vue.set(state.contacts, contact.key, contact)

		} else {
			console.error('Error while adding the following contact', contact)
		}
	},

	/**
	 * Update a contact
	 *
	 * @param {Object} state
	 * @param {Contact} contact
	 */
	updateContact(state, contact) {
		if (state.contacts[contact.key] && contact instanceof Contact) {

			// replace contact object data
			state.contacts[contact.key].updateContact(contact.jCal)
			let sortedContact = state.sortedContacts.find(search => search.key === contact.key)

			// has the sort key changed for this contact ?
			let hasChanged = sortedContact.value !== contact[state.orderKey]
			if (hasChanged) {
				// then update the new data
				sortedContact.value = contact[state.orderKey]
				// and then we sort again
				state.sortedContacts
					.sort((a, b) => {
						var nameA = a.value.toUpperCase() // ignore upper and lowercase
						var nameB = b.value.toUpperCase() // ignore upper and lowercase
						return nameA.localeCompare(nameB)
					})
			}

		} else {
			console.error('Error while replacing the following contact', contact)
		}
	},

	/**
	 * Update a contact
	 *
	 * @param {Object} state
	 * @param {Contact} contact
	 */
	updateContactAddressbook(state, { contact, addressbook }) {
		if (state.contacts[contact.key] && contact instanceof Contact) {

			// replace contact object data
			state.contacts[contact.key].updateAddressbook(addressbook)

		} else {
			console.error('Error while replacing the addressbook of following contact', contact)
		}
	},

	/**
	 * Order the contacts list. Filters have terrible performances.
	 * We do not want to run the sorting function every time.
	 * Let's only run it on additions and create an index
	 *
	 * @param {Object} state
	 */
	sortContacts(state) {
		state.sortedContacts = Object.values(state.contacts)
			// exclude groups
			.filter(contact => contact.kind !== 'group')
			.map(contact => { return { key: contact.key, value: contact[state.orderKey] } })
			.sort((a, b) => {
				var nameA = a.value.toUpperCase() // ignore upper and lowercase
				var nameB = b.value.toUpperCase() // ignore upper and lowercase
				let score = nameA.localeCompare(nameB)
				// if equal, fallback to the key
				return score !== 0
					? score
					: a.key.localeCompare(b.key)
			})
	},

	/**
	 * Set the order key
	 *
	 * @param {Object} state
	 * @param {string} [orderKey='displayName']
	 */
	setOrder(state, orderKey = 'displayName') {
		state.orderKey = orderKey
	}
}

const getters = {
	getContacts: state => state.contacts,
	getSortedContacts: state => state.sortedContacts,
	getContact: (state) => (uid) => state.contacts[uid],
	getOrderKey: state => state.orderKey
}

const actions = {

	/**
	 * Delete a contact from the list and from the associated addressbook
	 *
	 * @param {Object} context
	 * @param {Contact} contact the contact to delete
	 */
	async deleteContact(context, contact) {
		if (contact.dav) {
			await contact.dav.delete()
				.catch((error) => {
					console.error(error)
					OC.Notification.showTemporary(t('contacts', 'An error occurred'))
				})
		}
		context.commit('deleteContact', contact)
		context.commit('deleteContactFromAddressbook', contact)
	},

	/**
	 * Add a contact to the list and to the associated addressbook
	 *
	 * @param {Object} context
	 * @param {Contact} contact the contact to delete
	 */
	async addContact(context, contact) {
		await context.commit('addContact', contact)
		await context.commit('addContactToAddressbook', contact)
	},

	/**
	 * Replac a contact by this new object
	 *
	 * @param {Object} context
	 * @param {Contact} contact the contact to update
	 */
	async updateContact(context, contact) {
		let vData = ICAL.stringify(contact.vCard.jCal)

		// if no dav key, contact does not exists on server
		if (!contact.dav) {
			// create contact
			await contact.addressbook.dav.createVCard(vData)
				.then((response) => {
					contact.dav = response
				})
				.catch((error) => { throw error })
		}

		contact.dav.data = vData
		return contact.dav.update()
			.then((response) => context.commit('updateContact', contact))
			.catch((error) => { throw error })
	},

	/**
	 * Fetch the full vCard from the dav server
	 *
	 * @param {Object} context
	 * @param {Contact} contact the contact to fetch
	 */
	fetchFullContact(context