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
|
/**
* @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'
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
* @param {string} [url] the url of the contact
* @param {string} [etag] the current etag of the contact
* @memberof Contact
*/
constructor(vcard = '', addressbook, url = '', etag = '') {
let jCal = ICAL.parse(vcard)
if (jCal[0] !== 'vcard') {
throw new Error('Only one contact is allowed in the vcard data')
}
this.jCal = jCal
this.addressbook = addressbook
this.vCard = new ICAL.Component(this.jCal)
this.url = url
// if no uid set, create one
if (!this.vCard.hasProperty('uid')) {
console.debug('This contact did not have a proper uid. Setting a new one for ', this)
this.vCard.addPropertyWithValue('uid', uuid())
}
}
/**
* 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 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 key
*
* @readonly
* @memberof Contact
*/
get key() {
return this.uid + '@' + this.addressbook.id
}
/**
* Return the groups
*
* @readonly
* @memberof Contact
*/
get groups() {
let groupsProp = this.vCard.getFirstProperty('categories')
if (groupsProp) {
return groupsProp.getValues()
}
return []
}
/**
* Set the groups
*
* @param {Array} groups the groups to set
* @memberof Contact
*/
set groups(groups) {
if (Array.isArray(groups)) {
this.vCard.updatePropertyWithValue('uid', groups)
}
}
/**
* Return the groups
*
* @readonly
* @memberof Contact
*/
get kind() {
return this.firstIfArray(this.vCard.getFirstPropertyValue('kind'))
}
/**
* Return the first email
*
* @readonly
* @memberof Contact
*/
get email() {
return this.firstIfArray(this.vCard.getFirstPropertyValue('email'))
}
/**
* Return the first org
*
* @readonly
* @memberof Contact
*/
get org() {
return this.firstIfArray(this.vCard.getFirstPropertyValue('org'))
}
/**
* Set the org
*
* @param {string} org the org data
* @memberof Contact
*/
set org(org) {
return this.vCard.updatePropertyWithValue('org', org)
}
/**
* Return the first title
*
* @readonly
* @memberof Contact
*/
get title() {
return this.firstIfArray(this.vCard.getFirstPropertyValue('title'))
}
/**
* Set the title
*
* @param {string} title the title
* @memberof Contact
*/
set title(title) {
return this.vCard.updatePropertyWithValue('title', title)
}
/**
* Return the full name
*
* @readonly
* @memberof Contact
*/
get fullName() {
return this.vCard.getFirstPropertyValue('fn')
}
/**
* Set the full name
*
* @param {string} name the fn data
* @memberof Contact
*/
set fullName(name) {
return this.vCard.updatePropertyWithValue('fn', name)
}
/**
* Return the display name
*
* @readonly
* @memberof Contact
* @returns {string} the displayName
*/
get displayName() {
if (this.vCard.hasProperty('fn')) {
return this.vCard.getFirstPropertyValue('fn')
}
if (this.vCard.hasProperty('n')) {
// reverse and join
return this.vCard.getFirstPropertyValue('n')
.filter(function(part) {
return part
})
.join(' ')
}
return null
}
/**
* Return the first name if exists
* Returns the displayName otherwise
*
* @readonly
* @memberof Contact
* @returns {string} firstName|displayName
*/
get firstName() {
if (this.vCard.hasProperty('n')) {
// reverse and join
return this.vCard.getFirstPropertyValue('n')[1]
}
return this.displayName
}
/**
* Return the last name if exists
* Returns the displayName otherwise
*
* @readonly
* @memberof Contact
* @returns {string} lastName|displayName
*/
get lastName() {
if (this.vCard.hasProperty('n')) {
// reverse and join
return this.vCard.getFirstPropertyValue('n')[0]
}
return this.displayName
}
/**
* Return all the properties as Property objects
*
* @readonly
* @memberof Contact
* @returns {Property[]} http://mozilla-comm.github.io/ical.js/api/ICAL.Property.html
*/
get properties() {
return this.vCard.getAllProperties()
}
/**
* Add the contact to the group
*
* @param {string} group the group to add the contact to
* @memberof Contact
*/
addToGroup(group) {
if (this.groups.indexOf(group) === -1) {
if (this.groups.length > 0) {
this.vCard.getFirstProperty('categories').setValues(this.groups.concat(group))
} else {
this.vCard.updatePropertyWithValue('categories', [group])
}
}
}
}
|