Blame view

sources/apps/contacts/js/storage.js 19.6 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
4
  OC.Contacts = OC.Contacts || {};
  
  (function(window, $, OC) {
  	'use strict';
6d9380f96   Cédric Dupont   Update sources OC...
5
  	var JSONResponse = function(jqXHR) {
d1bafeea1   Kload   [fix] Upgrade to ...
6
7
8
  		this.getAllResponseHeaders = jqXHR.getAllResponseHeaders;
  		this.getResponseHeader = jqXHR.getResponseHeader;
  		this.statusCode = jqXHR.status;
6d9380f96   Cédric Dupont   Update sources OC...
9
  		var response = jqXHR.responseJSON;
d1bafeea1   Kload   [fix] Upgrade to ...
10
  		this.error = false;
6d9380f96   Cédric Dupont   Update sources OC...
11
12
13
14
15
  		console.log('jqXHR', jqXHR);
  		if (!response) {
  			// 204 == No content
  			// 304 == Not modified
  			if ([204, 304].indexOf(this.statusCode) === -1) {
d1bafeea1   Kload   [fix] Upgrade to ...
16
  				this.error = true;
d1bafeea1   Kload   [fix] Upgrade to ...
17
  			}
6d9380f96   Cédric Dupont   Update sources OC...
18
  			this.message = jqXHR.statusText;
d1bafeea1   Kload   [fix] Upgrade to ...
19
20
21
22
  		} else {
  			// We need to allow for both the 'old' success/error status property
  			// with the body in the data property, and the newer where we rely
  			// on the status code, and the entire body is used.
6d9380f96   Cédric Dupont   Update sources OC...
23
  			if (response.status === 'error'|| this.statusCode >= 400) {
d1bafeea1   Kload   [fix] Upgrade to ...
24
  				this.error = true;
6d9380f96   Cédric Dupont   Update sources OC...
25
26
27
28
  				if (!response.data || !response.data.message) {
  					this.message = t('contacts', 'Server error! Please inform system administator');
  				} else {
  					console.log('JSONResponse', response);
d1bafeea1   Kload   [fix] Upgrade to ...
29
30
31
  					this.message = (response.data && response.data.message)
  						? response.data.message
  						: response;
d1bafeea1   Kload   [fix] Upgrade to ...
32
33
  				}
  			} else {
6d9380f96   Cédric Dupont   Update sources OC...
34
35
36
37
38
  				this.data = response.data || response;
  				// Kind of a hack
  				if (response.metadata) {
  					this.metadata = response.metadata;
  				}
d1bafeea1   Kload   [fix] Upgrade to ...
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
  			}
  		}
  	};
  
  	/**
  	* An object for saving contact data to backends
  	*
  	* All methods returns a jQuery.Deferred object which resolves
  	* to either the requested response or an error object:
  	* {
  	*	error: true,
  	*	message: The error message
  	* }
  	*
  	* @param string user The user to query for. Defaults to current user
  	*/
  	var Storage = function(user) {
  		this.user = user ? user : OC.currentUser;
  	};
  
  	/**
  	 * Test if localStorage is working
  	 *
  	 * @return bool
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
64
65
66
  	Storage.prototype.hasLocalStorage = function() {
  		if (Modernizr && !Modernizr.localStorage) {
  			return false;
d1bafeea1   Kload   [fix] Upgrade to ...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  		}
  		// Some browsers report support but doesn't have it
  		// e.g. Safari in private browsing mode.
  		try {
  			OC.localStorage.setItem('Hello', 'World');
  			OC.localStorage.removeItem('Hello');
  		} catch (e) {
  			return false;
  		}
  		return true;
  	};
  
  	/**
  	 * When the response isn't returned from requestRoute(), you can
  	 * wrap it in a JSONResponse so that it's parsable by other objects.
  	 *
d1bafeea1   Kload   [fix] Upgrade to ...
83
84
  	 * @param XMLHTTPRequest http://api.jquery.com/jQuery.ajax/#jqXHR
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
85
86
  	Storage.prototype.formatResponse = function(jqXHR) {
  		return new JSONResponse(jqXHR);
d1bafeea1   Kload   [fix] Upgrade to ...
87
88
89
90
91
92
93
  	};
  
  	/**
  	 * Get all address books registered for this user.
  	 *
  	 * @return An array containing object of address book metadata e.g.:
  	 * {
6d9380f96   Cédric Dupont   Update sources OC...
94
95
96
97
  	 *    backend:'local',
  	 *    id:'1234'
  	 *    permissions:31,
  	 *    displayname:'Contacts'
d1bafeea1   Kload   [fix] Upgrade to ...
98
99
100
101
  	 * }
  	 */
  	Storage.prototype.getAddressBooksForUser = function() {
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
102
  			'addressbooks/',
d1bafeea1   Kload   [fix] Upgrade to ...
103
104
105
106
107
108
109
110
111
112
113
114
  			'GET',
  			{}
  		);
  	};
  
  	/**
  	 * Add an address book to a specific backend
  	 *
  	 * @param string backend - currently defaults to 'local'
  	 * @param object params An object {displayname:"My contacts", description:""}
  	 * @return An array containing contact data e.g.:
  	 * {
6d9380f96   Cédric Dupont   Update sources OC...
115
116
117
118
119
120
121
  	 * metadata:
  	 * {
  	 *     id:'1234'
  	 *     permissions:31,
  	 *     displayname:'My contacts',
  	 *     lastmodified: (unix timestamp),
  	 *     owner: 'joye',
d1bafeea1   Kload   [fix] Upgrade to ...
122
123
124
125
126
  	 * }
  	 */
  	Storage.prototype.addAddressBook = function(backend, parameters) {
  		console.log('Storage.addAddressBook', backend);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
127
  			'addressbook/{backend}/add',
d1bafeea1   Kload   [fix] Upgrade to ...
128
  			'POST',
6d9380f96   Cédric Dupont   Update sources OC...
129
  			{backend: backend},
d1bafeea1   Kload   [fix] Upgrade to ...
130
131
132
133
134
135
136
137
138
139
140
141
  			JSON.stringify(parameters)
  		);
  	};
  
  	/**
  	 * Update an address book in a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param object params An object {displayname:"My contacts", description:""}
  	 * @return An array containing contact data e.g.:
  	 * {
6d9380f96   Cédric Dupont   Update sources OC...
142
143
144
145
146
147
148
  	 * metadata:
  	 * {
  	 *     id:'1234'
  	 *     permissions:31,
  	 *     displayname:'My contacts',
  	 *     lastmodified: (unix timestamp),
  	 *     owner: 'joye',
d1bafeea1   Kload   [fix] Upgrade to ...
149
150
151
  	 * }
  	 */
  	Storage.prototype.updateAddressBook = function(backend, addressBookId, properties) {
6d9380f96   Cédric Dupont   Update sources OC...
152
  		console.log('Storage.updateAddressBook', backend, addressBookId, properties);
d1bafeea1   Kload   [fix] Upgrade to ...
153
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
154
  			'addressbook/{backend}/{addressBookId}',
d1bafeea1   Kload   [fix] Upgrade to ...
155
156
157
158
159
160
161
162
163
164
165
166
167
  			'POST',
  			{backend: backend, addressBookId: addressBookId},
  			JSON.stringify(properties)
  		);
  	};
  
  	/**
  	 * Delete an address book from a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 */
  	Storage.prototype.deleteAddressBook = function(backend, addressBookId) {
6d9380f96   Cédric Dupont   Update sources OC...
168
169
170
171
172
  		var key = 'contacts::' + backend + '::' + addressBookId;
  
  		if(this.hasLocalStorage() && OC.localStorage.hasItem(key)) {
  			OC.localStorage.removeItem(key);
  		}
d1bafeea1   Kload   [fix] Upgrade to ...
173
174
  		console.log('Storage.deleteAddressBook', backend, addressBookId);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
175
  			'addressbook/{backend}/{addressBookId}',
d1bafeea1   Kload   [fix] Upgrade to ...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  			'DELETE',
  			{backend: backend, addressBookId: addressBookId}
  		);
  	};
  
  	/**
  	 * (De)active an address book from a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param bool state
  	 */
  	Storage.prototype.activateAddressBook = function(backend, addressBookId, state) {
  		console.log('Storage.activateAddressBook', backend, addressBookId, state);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
191
  			'addressbook/{backend}/{addressBookId}/activate',
d1bafeea1   Kload   [fix] Upgrade to ...
192
193
194
195
196
  			'POST',
  			{backend: backend, addressBookId: addressBookId},
  			JSON.stringify({state: state})
  		);
  	};
6d9380f96   Cédric Dupont   Update sources OC...
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
  	
  	/**
  	 * Update an address book in a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param object params An object {displayname:"My contacts", description:""}
  	 * @return An array containing contact data e.g.:
  	 * {
  	 * metadata:
  	 * {
  	 *     id:'1234'
  	 *     permissions:31,
  	 *     displayname:'My contacts',
  	 *     lastmodified: (unix timestamp),
  	 *     owner: 'joye',
  	 * }
  	 */
  	Storage.prototype.getConnectors = function(backend) {
  		console.log('Storage.getConnectors', backend);
  		return this.requestRoute(
  			'connectors/{backend}',
  			'GET',
  			{backend: backend}
  		);
  	};
  
  	/**
  	 * Get metadata from an address book from a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @return
  	 *
  	 * metadata:
  	 * {
  	 *     id:'1234'
  	 *     permissions:31,
  	 *     displayname:'Contacts',
  	 *     lastmodified: (unix timestamp),
  	 *     owner: 'joye'
  	 * }
  	 */
  	Storage.prototype.getAddressBook = function(backend, addressBookId) {
  		var defer = $.Deferred();
  
  		$.when(this.requestRoute(
  			'addressbook/{backend}/{addressBookId}',
  			'GET',
  			{backend: backend, addressBookId: addressBookId},
  			''
  		))
  		.then(function(response) {
  			console.log('response', response);
  			defer.resolve(response);
  		})
  		.fail(function(response) {
  			console.warn('Request Failed:', response.message);
  			defer.reject(response);
  		});
  		return defer;
  	};
d1bafeea1   Kload   [fix] Upgrade to ...
259
260
261
262
263
264
265
266
267
  
  	/**
  	 * Get contacts from an address book from a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @return
  	 * An array containing contact data e.g.:
  	 * {
6d9380f96   Cédric Dupont   Update sources OC...
268
269
270
271
272
273
274
275
276
  	 * metadata:
  	 * {
  	 *     id:'1234'
  	 *     permissions:31,
  	 *     displayname:'John Q. Public',
  	 *     lastmodified: (unix timestamp),
  	 *     owner: 'joye',
  	 *     parent: (id of the parent address book)
  	 *     data: //array of VCard data
d1bafeea1   Kload   [fix] Upgrade to ...
277
278
  	 * }
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
279
  	Storage.prototype.getContacts = function(backend, addressBookId) {
d1bafeea1   Kload   [fix] Upgrade to ...
280
281
282
283
284
285
286
287
288
289
290
  		var self = this,
  			headers = {},
  			data,
  			key = 'contacts::' + backend + '::' + addressBookId,
  			defer = $.Deferred();
  
  		if(this.hasLocalStorage() && OC.localStorage.hasItem(key)) {
  			data = OC.localStorage.getItem(key);
  			headers['If-None-Match'] = data.Etag;
  		}
  		$.when(this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
291
  			'addressbook/{backend}/{addressBookId}/contacts',
d1bafeea1   Kload   [fix] Upgrade to ...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  			'GET',
  			{backend: backend, addressBookId: addressBookId},
  			'',
  			headers
  		))
  		.then(function(response) {
  			console.log('response', response);
  			if(response.statusCode === 200) {
  				console.log('Returning fetched address book');
  				if(response.data) {
  					response.data.Etag = response.getResponseHeader('Etag');
  					if (!self.hasLocalStorage()) {
  						OC.localStorage.setItem(key, response.data);
  					}
  					defer.resolve(response);
  				}
  			} else if(response.statusCode === 304) {
  				console.log('Returning stored address book');
  				response.data = data;
  				defer.resolve(response);
  			}
  		})
  		.fail(function(response) {
  			console.warn('Request Failed:', response.message);
6d9380f96   Cédric Dupont   Update sources OC...
316
  			defer.reject(response);
d1bafeea1   Kload   [fix] Upgrade to ...
317
318
319
320
321
322
323
324
325
326
327
  		});
  		return defer;
  	};
  
  	/**
  	 * Add a contact to an address book from a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @return An array containing contact data e.g.:
  	 * {
6d9380f96   Cédric Dupont   Update sources OC...
328
329
330
331
332
333
334
335
336
  	 * metadata:
  	 *     {
  	 *     id:'1234'
  	 *     permissions:31,
  	 *     displayname:'John Q. Public',
  	 *     lastmodified: (unix timestamp),
  	 *     owner: 'joye',
  	 *     parent: (id of the parent address book)
  	 *     data: //array of VCard data
d1bafeea1   Kload   [fix] Upgrade to ...
337
338
339
340
341
  	 * }
  	 */
  	Storage.prototype.addContact = function(backend, addressBookId) {
  		console.log('Storage.addContact', backend, addressBookId);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
342
  			'addressbook/{backend}/{addressBookId}/contact/add',
d1bafeea1   Kload   [fix] Upgrade to ...
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  			'POST',
  			{backend: backend, addressBookId: addressBookId}
  		);
  	};
  
  	/**
  	 * Delete a contact from an address book from a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param string contactId Address book ID
  	 */
  	Storage.prototype.deleteContact = function(backend, addressBookId, contactId) {
  		console.log('Storage.deleteContact', backend, addressBookId, contactId);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
358
  			'addressbook/{backend}/{addressBookId}/contact/{contactId}',
d1bafeea1   Kload   [fix] Upgrade to ...
359
360
361
  			'DELETE',
  			{backend: backend, addressBookId: addressBookId, contactId: contactId}
  		);
6d9380f96   Cédric Dupont   Update sources OC...
362
  	};
d1bafeea1   Kload   [fix] Upgrade to ...
363
364
365
366
367
368
369
370
371
372
373
  
  	/**
  	 * Delete a list of contacts from an address book from a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param array contactIds Address book ID
  	 */
  	Storage.prototype.deleteContacts = function(backend, addressBookId, contactIds) {
  		console.log('Storage.deleteContacts', backend, addressBookId, contactIds);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
374
  			'addressbook/{backend}/{addressBookId}/deleteContacts',
d1bafeea1   Kload   [fix] Upgrade to ...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  			'POST',
  			{backend: backend, addressBookId: addressBookId},
  			JSON.stringify({contacts: contactIds})
  		);
  	};
  
  	/**
  	 * Move a contact to an address book from a specific backend
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param string contactId Address book ID
  	 */
  	Storage.prototype.moveContact = function(backend, addressBookId, contactId, target) {
  		console.log('Storage.moveContact', backend, addressBookId, contactId, target);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
391
  			'addressbook/{backend}/{addressBookId}/contact/{contactId}',
d1bafeea1   Kload   [fix] Upgrade to ...
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  			'POST',
  			{backend: backend, addressBookId: addressBookId, contactId: contactId},
  			JSON.stringify(target)
  		);
  	};
  
  	/**
  	 * Get Image instance for a contacts profile picture
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param string contactId Address book ID
  	 * @return Image
  	 */
  	Storage.prototype.getContactPhoto = function(backend, addressBookId, contactId) {
  		var photo = new Image();
6d9380f96   Cédric Dupont   Update sources OC...
408
409
  		var url = OC.generateUrl(
  			'apps/contacts/addressbook/{backend}/{addressBookId}/contact/{contactId}/photo',
d1bafeea1   Kload   [fix] Upgrade to ...
410
411
412
  			{backend: backend, addressBookId: addressBookId, contactId: contactId}
  		);
  		var defer = $.Deferred();
6d9380f96   Cédric Dupont   Update sources OC...
413

d1bafeea1   Kload   [fix] Upgrade to ...
414
415
416
417
418
  		$.when(
  			$(photo).on('load', function() {
  				defer.resolve(photo);
  			})
  			.error(function() {
6d9380f96   Cédric Dupont   Update sources OC...
419
  				console.log('Error loading contact photo');
d1bafeea1   Kload   [fix] Upgrade to ...
420
421
422
423
424
425
426
  				defer.reject();
  			})
  			.attr('src', url + '?refresh=' + Math.random())
  		)
  		.fail(function(jqxhr, textStatus, error) {
  			defer.reject();
  			var err = textStatus + ', ' + error;
6d9380f96   Cédric Dupont   Update sources OC...
427
  			console.warn('Request Failed:', + err);
d1bafeea1   Kload   [fix] Upgrade to ...
428
429
430
431
432
433
434
435
  			$(document).trigger('status.contact.error', {
  				message: t('contacts', 'Failed loading photo: {error}', {error:err})
  			});
  		});
  		return defer.promise();
  	};
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
436
  	 * Get Image instance for cropping contacts profile picture
d1bafeea1   Kload   [fix] Upgrade to ...
437
438
439
440
441
442
443
444
445
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param string contactId Address book ID
  	 * @param string key The key to the cache where the photo is stored.
  	 * @return Image
  	 */
  	Storage.prototype.getTempContactPhoto = function(backend, addressBookId, contactId, key) {
  		var photo = new Image();
6d9380f96   Cédric Dupont   Update sources OC...
446
447
  		var url = OC.generateUrl(
  			'apps/contacts/addressbook/{backend}/{addressBookId}/contact/{contactId}/photo/{key}/tmp',
d1bafeea1   Kload   [fix] Upgrade to ...
448
449
450
451
  			{backend: backend, addressBookId: addressBookId, contactId: contactId, key: key, refresh: Math.random()}
  		);
  		console.log('url', url);
  		var defer = $.Deferred();
6d9380f96   Cédric Dupont   Update sources OC...
452

d1bafeea1   Kload   [fix] Upgrade to ...
453
454
455
456
457
  		$.when(
  			$(photo).on('load', function() {
  				defer.resolve(photo);
  			})
  			.error(function(event) {
6d9380f96   Cédric Dupont   Update sources OC...
458
  				console.warn('Error loading temporary photo', event);
d1bafeea1   Kload   [fix] Upgrade to ...
459
460
461
462
463
464
465
  				defer.reject();
  			})
  			.attr('src', url)
  		)
  		.fail(function(jqxhr, textStatus, error) {
  			defer.reject();
  			var err = textStatus + ', ' + error;
6d9380f96   Cédric Dupont   Update sources OC...
466
  			console.warn('Request Failed:', err);
d1bafeea1   Kload   [fix] Upgrade to ...
467
468
469
470
471
472
473
474
  			$(document).trigger('status.contact.error', {
  				message: t('contacts', 'Failed loading photo: {error}', {error:err})
  			});
  		});
  		return defer.promise();
  	};
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
475
  	 * Crop a contact phot.
d1bafeea1   Kload   [fix] Upgrade to ...
476
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
477
478
479
480
481
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param string contactId Contact ID
  	 * @param string key The key to the cache where the temporary image is saved.
  	 * @param object coords An object with the properties: x, y, w, h
d1bafeea1   Kload   [fix] Upgrade to ...
482
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
483
484
485
486
487
488
489
  	Storage.prototype.cropContactPhoto = function(backend, addressBookId, contactId, key, coords) {
  		return this.requestRoute(
  			'addressbook/{backend}/{addressBookId}/contact/{contactId}/photo/{key}/crop',
  			'POST',
  			{backend: backend, addressBookId: addressBookId, contactId: contactId, key: key},
  			JSON.stringify(coords)
  		);
d1bafeea1   Kload   [fix] Upgrade to ...
490
491
492
493
494
495
496
497
498
499
500
501
502
  	};
  
  	/**
  	 * Update a contact.
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param string contactId Contact ID
  	 * @param object params An object with the following properties:
  	 * @param string name The name of the property e.g. EMAIL.
  	 * @param string|array|null value The of the property
  	 * @param array parameters Optional parameters for the property
  	 * @param string checksum For non-singular properties such as email this must contain
6d9380f96   Cédric Dupont   Update sources OC...
503
  	 *               an 8 character md5 checksum of the serialized \Sabre\Property
d1bafeea1   Kload   [fix] Upgrade to ...
504
505
506
  	 */
  	Storage.prototype.patchContact = function(backend, addressBookId, contactId, params) {
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
507
  			'addressbook/{backend}/{addressBookId}/contact/{contactId}',
d1bafeea1   Kload   [fix] Upgrade to ...
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
  			'PATCH',
  			{backend: backend, addressBookId: addressBookId, contactId: contactId},
  			JSON.stringify(params)
  		);
  	};
  
  	/**
  	 * Save all properties. Used when merging contacts.
  	 *
  	 * @param string backend
  	 * @param string addressBookId Address book ID
  	 * @param string contactId Contact ID
  	 * @param object params An object with the all properties:
  	 */
  	Storage.prototype.saveAllProperties = function(backend, addressBookId, contactId, params) {
  		console.log('Storage.saveAllProperties', params);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
525
  			'addressbook/{backend}/{addressBookId}/contact/{contactId}/save',
d1bafeea1   Kload   [fix] Upgrade to ...
526
527
528
529
530
531
532
533
534
535
536
537
  			'POST',
  			{backend: backend, addressBookId: addressBookId, contactId: contactId},
  			JSON.stringify(params)
  		);
  	};
  
  	/**
  	 * Get all groups for this user.
  	 *
  	 * @return An array containing the groups, the favorites, any shared
  	 * address books, the last selected group and the sort order of the groups.
  	 * {
6d9380f96   Cédric Dupont   Update sources OC...
538
539
540
541
542
  	 *     'categories': [{'id':1',Family'}, {...}],
  	 *     'favorites': [123,456],
  	 *     'shared': [],
  	 *     'lastgroup':'1',
  	 *     'sortorder':'3,2,4'
d1bafeea1   Kload   [fix] Upgrade to ...
543
544
545
546
547
  	 * }
  	 */
  	Storage.prototype.getGroupsForUser = function() {
  		console.log('getGroupsForUser');
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
548
  			'groups/',
d1bafeea1   Kload   [fix] Upgrade to ...
549
550
551
552
553
554
555
556
557
558
559
  			'GET',
  			{}
  		);
  	};
  
  	/**
  	 * Add a group
  	 *
  	 * @param string name
  	 * @return A JSON object containing the (maybe sanitized) group name and its ID:
  	 * {
6d9380f96   Cédric Dupont   Update sources OC...
560
561
  	 *     'id':1234,
  	 *     'name':'My group'
d1bafeea1   Kload   [fix] Upgrade to ...
562
563
564
565
566
  	 * }
  	 */
  	Storage.prototype.addGroup = function(name) {
  		console.log('Storage.addGroup', name);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
567
  			'groups/add',
d1bafeea1   Kload   [fix] Upgrade to ...
568
569
570
571
572
573
574
575
576
577
578
579
580
  			'POST',
  			{},
  			JSON.stringify({name: name})
  		);
  	};
  
  	/**
  	 * Delete a group
  	 *
  	 * @param string name
  	 */
  	Storage.prototype.deleteGroup = function(name) {
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
581
  			'groups/delete',
d1bafeea1   Kload   [fix] Upgrade to ...
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  			'POST',
  			{},
  			JSON.stringify({name: name})
  		);
  	};
  
  	/**
  	 * Rename a group
  	 *
  	 * @param string from
  	 * @param string to
  	 */
  	Storage.prototype.renameGroup = function(from, to) {
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
596
  			'groups/rename',
d1bafeea1   Kload   [fix] Upgrade to ...
597
598
599
600
601
602
603
604
605
606
607
608
609
610
  			'POST',
  			{},
  			JSON.stringify({from: from, to: to})
  		);
  	};
  
  	/**
  	 * Add contacts to a group
  	 *
  	 * @param array contactIds
  	 */
  	Storage.prototype.addToGroup = function(contactIds, categoryId, categoryName) {
  		console.log('Storage.addToGroup', contactIds, categoryId);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
611
  			'groups/addto/{categoryId}',
d1bafeea1   Kload   [fix] Upgrade to ...
612
613
614
615
616
617
618
619
620
621
622
623
624
625
  			'POST',
  			{categoryId: categoryId},
  			JSON.stringify({contactIds: contactIds, name: categoryName})
  		);
  	};
  
  	/**
  	 * Remove contacts from a group
  	 *
  	 * @param array contactIds
  	 */
  	Storage.prototype.removeFromGroup = function(contactIds, categoryId, categoryName) {
  		console.log('Storage.removeFromGroup', contactIds, categoryId);
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
626
  			'groups/removefrom/{categoryId}',
d1bafeea1   Kload   [fix] Upgrade to ...
627
628
629
630
631
632
633
634
635
636
637
638
639
640
  			'POST',
  			{categoryId: categoryId},
  			JSON.stringify({contactIds: contactIds, name: categoryName})
  		);
  	};
  
  	/**
  	 * Set a user preference
  	 *
  	 * @param string key
  	 * @param string value
  	 */
  	Storage.prototype.setPreference = function(key, value) {
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
641
  			'preference/set',
d1bafeea1   Kload   [fix] Upgrade to ...
642
643
644
645
646
  			'POST',
  			{},
  			JSON.stringify({key: key, value:value})
  		);
  	};
6d9380f96   Cédric Dupont   Update sources OC...
647
648
  	Storage.prototype.prepareImport = function(backend, addressBookId, importType, params) {
  		console.log('Storage.prepareImport', backend, addressBookId, importType);
d1bafeea1   Kload   [fix] Upgrade to ...
649
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
650
  			'addressbook/{backend}/{addressBookId}/{importType}/import/prepare',
d1bafeea1   Kload   [fix] Upgrade to ...
651
  			'POST',
6d9380f96   Cédric Dupont   Update sources OC...
652
  			{backend: backend, addressBookId: addressBookId, importType: importType},
d1bafeea1   Kload   [fix] Upgrade to ...
653
654
655
  			JSON.stringify(params)
  		);
  	};
6d9380f96   Cédric Dupont   Update sources OC...
656
657
  	Storage.prototype.startImport = function(backend, addressBookId, importType, params) {
  		console.log('Storage.startImport', backend, addressBookId, importType);
d1bafeea1   Kload   [fix] Upgrade to ...
658
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
659
  			'addressbook/{backend}/{addressBookId}/{importType}/import/start',
d1bafeea1   Kload   [fix] Upgrade to ...
660
  			'POST',
6d9380f96   Cédric Dupont   Update sources OC...
661
  			{backend: backend, addressBookId: addressBookId, importType: importType},
d1bafeea1   Kload   [fix] Upgrade to ...
662
663
664
  			JSON.stringify(params)
  		);
  	};
6d9380f96   Cédric Dupont   Update sources OC...
665
  	Storage.prototype.importStatus = function(backend, addressBookId, importType, params) {
d1bafeea1   Kload   [fix] Upgrade to ...
666
  		return this.requestRoute(
6d9380f96   Cédric Dupont   Update sources OC...
667
  			'addressbook/{backend}/{addressBookId}/{importType}/import/status',
d1bafeea1   Kload   [fix] Upgrade to ...
668
  			'GET',
6d9380f96   Cédric Dupont   Update sources OC...
669
  			{backend: backend, addressBookId: addressBookId, importType: importType},
d1bafeea1   Kload   [fix] Upgrade to ...
670
671
672
  			params
  		);
  	};
6d9380f96   Cédric Dupont   Update sources OC...
673
  	
d1bafeea1   Kload   [fix] Upgrade to ...
674
675
676
677
678
679
680
  	Storage.prototype.requestRoute = function(route, type, routeParams, params, additionalHeaders) {
  		var isJSON = (typeof params === 'string');
  		var contentType = isJSON
  			? (type === 'PATCH' ? 'application/json-merge-patch' : 'application/json')
  			: 'application/x-www-form-urlencoded';
  		var processData = !isJSON;
  		contentType += '; charset=UTF-8';
6d9380f96   Cédric Dupont   Update sources OC...
681
  		var url = OC.generateUrl('apps/contacts/' + route, routeParams);
d1bafeea1   Kload   [fix] Upgrade to ...
682
  		var headers = {
6d9380f96   Cédric Dupont   Update sources OC...
683
  			Accept : 'application/json; charset=utf-8'
d1bafeea1   Kload   [fix] Upgrade to ...
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
  		};
  		if(typeof additionalHeaders === 'object') {
  			headers = $.extend(headers, additionalHeaders);
  		}
  		var ajaxParams = {
  			type: type,
  			url: url,
  			dataType: 'json',
  			headers: headers,
  			contentType: contentType,
  			processData: processData,
  			data: params
  		};
  
  		var defer = $.Deferred();
6d9380f96   Cédric Dupont   Update sources OC...
699
  		$.ajax(ajaxParams)
d1bafeea1   Kload   [fix] Upgrade to ...
700
  			.done(function(response, textStatus, jqXHR) {
6d9380f96   Cédric Dupont   Update sources OC...
701
702
  				console.log(jqXHR);
  				defer.resolve(new JSONResponse(jqXHR));
d1bafeea1   Kload   [fix] Upgrade to ...
703
  			})
6d9380f96   Cédric Dupont   Update sources OC...
704
  			.fail(function(jqXHR/*, textStatus, error*/) {
d1bafeea1   Kload   [fix] Upgrade to ...
705
706
707
  				console.log(jqXHR);
  				var response = jqXHR.responseText ? $.parseJSON(jqXHR.responseText) : null;
  				console.log('response', response);
6d9380f96   Cédric Dupont   Update sources OC...
708
  				defer.reject(new JSONResponse(jqXHR));
d1bafeea1   Kload   [fix] Upgrade to ...
709
710
711
712
713
714
715
716
  			});
  
  		return defer.promise();
  	};
  
  	OC.Contacts.Storage = Storage;
  
  })(window, jQuery, OC);