summaryrefslogtreecommitdiffstats
path: root/src/models/contact.js
blob: c4eebecc8506782afef8c885676c257e151a7213 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/**
 * @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 uuid from 'uuid'
import ICAL from 'ical.js'

import store from '../store'

/**
 * Check if the given value is an empty array or an empty string
 *
 * @param {string|Array} value the value to check
 * @returns {boolean}
 */
const isEmpty = value => {
	return (Array.isArray(value) && value.join('') === '') || (!Array.isArray(value) && value === '')
}

/**
 * Parse a jCal and update the global designset
 * if any grouped property is found
 *
 * @param {Array} jCal the contact ICAL.js jCal
 * @returns {Boolean}
 */
const updateDesignSet = jCal => {
	let result = false
	jCal[1].forEach(prop => {
		const propGroup = prop[0].split('.')

		// if this is a grouped property, update the designSet
		if (propGroup.length === 2 && (
			ICAL.design.vcard.property[propGroup[1]]
			|| ICAL.design.vcard3.property[propGroup[1]]
		)) {
			// force update the main design sets
			if (ICAL.design.vcard.property[propGroup[1]]) {
				ICAL.design.vcard.property[prop[0]]
					= ICAL.design.vcard.property[propGroup[1]]
				result = true
			}
			if (ICAL.design.vcard3.property[propGroup[1]]) {
				ICAL.design.vcard3.property[prop[0]]
					= ICAL.design.vcard3.property[propGroup[1]]

				result = true
			}
		}
	})
	return result
}

export default class Contact {

	/**
	 * Creates an instance of Contact
	 *
	 * @param {string} vcard the vcard data as string with proper new lines
	 * @param {object} addressbook the addressbook which the contat belongs to
	 * @memberof Contact
	 */
	constructor(vcard, addressbook) {
		if (typeof vcard !== 'string' || vcard.length === 0) {
			throw new Error('Invalid vCard')
		}

		let jCal = ICAL.parse(vcard)
		if (jCal[0] !== 'vcard') {
			throw new Error('Only one contact is allowed in the vcard data')
		}

		// add grouped properties to the design set
		// if any found, refresh the contact jCal
		if (updateDesignSet(jCal)) {
			jCal = ICAL.parse(vcard)
		}

		this.jCal = jCal
		this.addressbook = addressbook
		this.vCard = new ICAL.Component(this.jCal)

		// used to state a contact is not up to date with
		// the server and cannot be pushed (etag)
		this.conflict = false

		// if no uid set, create one
		if (!this.vCard.hasProperty('uid')) {
			console.info('This contact did not have a proper uid. Setting a new one for ', this)
			this.vCard.addPropertyWithValue('uid', uuid())
		}

		// if no rev set, init one
		if (!this.vCard.hasProperty('rev')) {
			const rev = new ICAL.VCardTime(null, null, 'date-time')
			rev.fromUnixTime(Date.now() / 1000)
			this.vCard.addPropertyWithValue('rev', rev)

		}
	}

	/**
	 * Update internal data of this contact
	 *
	 * @param {jCal} jCal jCal object from ICAL.js
	 * @memberof Contact
	 */
	updateContact(jCal) {
		this.jCal = jCal
		this.vCard = new ICAL.Component(this.jCal)
	}

	/**
	 * Update linked addressbook of this contact
	 *
	 * @param {Object} addressbook the addressbook
	 * @memberof Contact
	 */
	updateAddressbook(addressbook) {
		this.addressbook = addressbook
	}

	/**
	 * Ensure we're normalizing the possible arrays
	 * into a string by taking the first element
	 * e.g. ORG:ABC\, Inc.; will output an array because of the semi-colon
	 *
	 * @param {Array|string} data the data to normalize
	 * @returns {string}
	 * @memberof Contact
	 */
	firstIfArray(data) {
		return Array.isArray(data) ? data[0] : data
	}

	/**
	 * Return the url
	 *
	 * @readonly
	 * @memberof Contact
	 */
	get url() {
		if (this.dav) {
			return this.dav.url
		}
		return ''
	}

	/**
	 * Return the uid
	 *
	 * @readonly
	 * @memberof Contact
	 */
	get uid() {
		return this.vCard.getFirstPropertyValue('uid')
	}

	/**
	 * Set the uid
	 *
	 * @param {string} uid the uid to set
	 * @memberof Contact
	 */
	set uid(uid) {
		this.vCard.updatePropertyWithValue('uid', uid)
		return true
	}

	/**
	 * Return the rev
	 *
	 * @readonly
	 * @memberof Contact
	 */
	get rev() {
		return this.vCard.getFirstPropertyValue('rev')
	}

	/**
	 * Set the rev
	 *
	 * @param {string} rev the rev to set
	 * @memberof Contact
	 */
	set rev(rev) {
		this.vCard.updatePropertyWithValue('rev', rev)
		return true
	}

	/**
	 * Return the key
	 *
	 * @readonly
	 * @memberof Contact
	 */
	get key() {
		return this.uid + '~' + this.addressbook.id
	}

	/**
	 * Return the photo
	 *
	 * @readonly
	 * @memberof Contact
	 */
	get photo() {
		return this.vCard.getFirstPropertyValue('photo')
	}

	/**
	 * Set the photo
	 *
	 * @param {string} photo the photo to set
	 * @memberof Contact
	 */
	set photo(photo) {
		this.vCard.updatePropertyWithValue('photo', photo)
		return true
	}

	/**
	 * Return the groups
	 *
	 * @readonly
	 * @memberof Contact
	 */
	get groups() {
		let groupsProp = this.vCard.getFirstProperty('categories')
		if (groupsProp) {
			return groupsProp.getValues()
				.filter(group => group.trim() !== '')
		}
		return []
	}

	/**
	 * Set the groups
	 *
	 * @param {Array} groups the groups to set
	 * @memberof Contact
	 */
	set groups(groups) {
		// delete the title if empty
		if (isEmpty(groups)) {
			this.vCard.removeProperty('categories')
			return true
		}

		if (Array.isArray(groups)) {
			let property = this.vCard.getFirstProperty('categories')
			if (!property) {
				// Init with empty group since we set everything afterwise
				property = this.vCard.addPropertyWithValue('categories', '')
			}
			property.setValues(groups)
		} else {
			throw new Error('groups data is not an Array')
		}
	}

	/**<