-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathaddressBook.js
More file actions
308 lines (274 loc) · 7.7 KB
/
Copy pathaddressBook.js
File metadata and controls
308 lines (274 loc) · 7.7 KB
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
/**
* CDAV Library
*
* This library is part of the Nextcloud project
*
* @author Georg Ehrke
* @copyright 2018 Georg Ehrke <oc.list@georgehrke.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { davCollectionShareable } from './davCollectionShareable.js'
import { DavCollection } from './davCollection.js'
import * as NS from '../utility/namespaceUtility.js'
import * as StringUtility from '../utility/stringUtility.js'
import * as XMLUtility from '../utility/xmlUtility.js'
import addressBookPropSet from '../propset/addressBookPropSet.js'
import { VCard } from './vcard.js'
import { debugFactory } from '../debug.js'
const debug = debugFactory('AddressBook')
/**
* This class represents an address book collection as specified in
* https://tools.ietf.org/html/rfc6352#section-5.2
*
* On top of all the properties provided by davCollectionShareable and DavCollection,
* It allows you access to the following list of properties:
* - description
* - enabled
* - readOnly
*
* The first two allowing read-write access
*
* @augments DavCollection
*/
export class AddressBook extends davCollectionShareable(DavCollection) {
/**
* @inheritDoc
*/
constructor(...args) {
super(...args)
super._registerObjectFactory('text/vcard', VCard)
super._registerPropSetFactory(addressBookPropSet)
super._exposeProperty('description', NS.IETF_CARDDAV, 'addressbook-description', true)
super._exposeProperty('enabled', NS.OWNCLOUD, 'enabled', true)
super._exposeProperty('readOnly', NS.OWNCLOUD, 'read-only')
}
/**
* finds all VCards in this address book
*
* @return {Promise<VCard[]>}
*/
findAllVCards() {
return super.findAllByFilter((elm) => elm instanceof VCard)
}
/**
* finds all contacts in an address-book, but with filtered data.
*
* Example use:
* findAllAndFilterBySimpleProperties(['EMAIL', 'UID', 'CATEGORIES', 'FN', 'TEL', 'NICKNAME', 'N'])
*
* @param {string[]} props
* @param offset
* @param limit
* @return {Promise<VCard[]>}
*/
async findAllAndFilterBySimpleProperties(props, offset = null, limit = 0) {
const children = []
props.forEach((prop) => {
children.push({
name: [NS.IETF_CARDDAV, 'prop'],
attributes: [['name', prop]],
})
})
return this.addressbookQuery(null, [{
name: [NS.DAV, 'getetag'],
}, {
name: [NS.DAV, 'getcontenttype'],
}, {
name: [NS.DAV, 'resourcetype'],
}, {
name: [NS.IETF_CARDDAV, 'address-data'],
children,
}, {
name: [NS.NEXTCLOUD, 'has-photo'],
}], null, 'anyof', offset, limit)
}
/**
* creates a new VCard object in this address book
*
* @param {string} data
* @return {Promise<VCard>}
*/
async createVCard(data) {
debug('creating VCard object')
const name = StringUtility.uid('', 'vcf')
const headers = {
'Content-Type': 'text/vcard; charset=utf-8',
}
return super.createObject(name, headers, data)
}
/**
* sends an addressbook query as defined in
* https://tools.ietf.org/html/rfc6352#section-8.6
*
* @param {object[]} filter
* @param {object[]} prop
* @param {number} limit
* @param {string} test Either anyof or allof
* @param {number} offset Offset for the query
* @param {number} queryLimit Limit for the query
* @return {Promise<VCard[]>}
*/
async addressbookQuery(filter, prop = null, limit = null, test = 'anyof', offset = null, queryLimit = null) {
debug('sending an addressbook-query request')
const [skeleton] = XMLUtility.getRootSkeleton(
[NS.IETF_CARDDAV, 'addressbook-query'],
)
if (!prop) {
skeleton.children.push({
name: [NS.DAV, 'prop'],
children: this._propFindList.map((p) => ({ name: p })),
})
} else {
skeleton.children.push({
name: [NS.DAV, 'prop'],
children: prop,
})
}
// According to the spec, every address-book query needs a filter,
// but Nextcloud just returns all elements without a filter.
if (filter) {
skeleton.children.push({
name: [NS.IETF_CARDDAV, 'filter'],
attributes: [
['test', test],
],
children: filter,
})
}
if (limit) {
skeleton.children.push({
name: [NS.IETF_CARDDAV, 'limit'],
children: [{
name: [NS.IETF_CARDDAV, 'nresults'],
value: limit,
}],
})
}
const headers = {
Depth: '1',
'X-NEXTCLOUD-Offset': offset,
'X-NEXTCLOUD-limit': queryLimit,
}
const body = XMLUtility.serialize(skeleton)
const response = await this._request.report(this.url, headers, body)
return super._handleMultiStatusResponse(response, AddressBook._isRetrievalPartial(prop))
}
/**
* sends an addressbook multiget query as defined in
* https://tools.ietf.org/html/rfc6352#section-8.7
*
* @param {string[]} hrefs
* @param {object[]} prop
* @return {Promise<VCard[]>}
*/
async addressbookMultiget(hrefs = [], prop) {
debug('sending an addressbook-multiget request')
if (hrefs.length === 0) {
return []
}
const headers = {
Depth: '1',
}
const body = this._buildMultiGetBody(hrefs, prop)
const response = await this._request.report(this.url, headers, body)
return super._handleMultiStatusResponse(response, AddressBook._isRetrievalPartial(prop))
}
/**
* sends an addressbook multiget query as defined in
* https://tools.ietf.org/html/rfc6352#section-8.7
* and requests a download of the result
*
* @param {string[]} hrefs
* @param {object[]} prop
* @return {Promise<{Object}>}
* @property {string | object} body
* @property {number} status
* @property {XMLHttpRequest} xhr
*/
async addressbookMultigetExport(hrefs = [], prop) {
debug('sending an addressbook-multiget request and request download')
if (hrefs.length === 0) {
return ''
}
const headers = {
Depth: '1',
}
const body = this._buildMultiGetBody(hrefs, prop)
return this._request.report(this.url + '?export', headers, body)
}
/**
*
* @param {string[]} hrefs
* @param {object[]} prop
* @return String
* @private
*/
_buildMultiGetBody(hrefs, prop) {
const [skeleton] = XMLUtility.getRootSkeleton(
[NS.IETF_CARDDAV, 'addressbook-multiget'],
)
if (!prop) {
skeleton.children.push({
name: [NS.DAV, 'prop'],
children: this._propFindList.map((p) => ({ name: p })),
})
} else {
skeleton.children.push({
name: [NS.DAV, 'prop'],
children: prop,
})
}
hrefs.forEach((href) => {
skeleton.children.push({
name: [NS.DAV, 'href'],
value: href,
})
})
return XMLUtility.serialize(skeleton)
}
/**
* @inheritDoc
*/
static getPropFindList() {
return super.getPropFindList().concat([
[NS.IETF_CARDDAV, 'addressbook-description'],
[NS.IETF_CARDDAV, 'supported-address-data'],
[NS.IETF_CARDDAV, 'max-resource-size'],
[NS.CALENDARSERVER, 'getctag'],
[NS.OWNCLOUD, 'enabled'],
[NS.OWNCLOUD, 'read-only'],
])
}
/**
* checks if the prop part of a report requested partial data
*
* @param {object[]} prop
* @return {boolean}
* @private
*/
static _isRetrievalPartial(prop) {
if (!prop) {
return false
}
const addressBookDataProperty = prop.find((p) => {
return p.name[0] === NS.IETF_CARDDAV && p.name[1] === 'address-data'
})
if (!addressBookDataProperty) {
return false
}
return !!addressBookDataProperty.children
}
}