2013-03-20 18:57:58 +01:00
|
|
|
OC.Contacts = OC.Contacts || {};
|
|
|
|
|
|
|
|
(function(window, $, OC) {
|
|
|
|
'use strict';
|
2013-03-28 03:19:01 +01:00
|
|
|
|
2014-03-23 14:14:36 +01:00
|
|
|
var JSONResponse = function(jqXHR) {
|
2013-05-03 05:56:55 +02:00
|
|
|
this.getAllResponseHeaders = jqXHR.getAllResponseHeaders;
|
|
|
|
this.getResponseHeader = jqXHR.getResponseHeader;
|
2013-06-02 15:34:57 +02:00
|
|
|
this.statusCode = jqXHR.status;
|
2014-03-23 14:14:36 +01:00
|
|
|
var response = jqXHR.responseJSON;
|
2013-10-18 15:29:16 +02:00
|
|
|
this.error = false;
|
2014-03-24 21:23:11 +01:00
|
|
|
console.log('jqXHR', jqXHR);
|
|
|
|
if (!response) {
|
|
|
|
// 204 == No content
|
|
|
|
// 304 == Not modified
|
|
|
|
if ([204, 304].indexOf(this.statusCode) === -1) {
|
2013-09-03 19:25:29 +02:00
|
|
|
this.error = true;
|
|
|
|
}
|
2014-03-24 21:23:11 +01:00
|
|
|
this.message = jqXHR.statusText;
|
2013-03-28 03:19:01 +01:00
|
|
|
} else {
|
2013-09-27 16:38:22 +02:00
|
|
|
// 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.
|
2014-03-24 21:23:11 +01:00
|
|
|
if (response.status === 'error'|| this.statusCode >= 400) {
|
2013-05-24 02:54:33 +02:00
|
|
|
this.error = true;
|
2014-03-24 21:23:11 +01:00
|
|
|
if (!response.data || !response.data.message) {
|
|
|
|
this.message = t('contacts', 'Server error! Please inform system administator');
|
|
|
|
} else {
|
2014-03-20 15:59:07 +01:00
|
|
|
console.log('JSONResponse', response);
|
2013-09-27 16:38:22 +02:00
|
|
|
this.message = (response.data && response.data.message)
|
|
|
|
? response.data.message
|
|
|
|
: response;
|
|
|
|
}
|
2013-04-04 02:52:27 +02:00
|
|
|
} else {
|
2014-03-20 15:59:07 +01:00
|
|
|
this.data = response.data || response;
|
2014-03-20 21:53:13 +01:00
|
|
|
// Kind of a hack
|
|
|
|
if (response.metadata) {
|
|
|
|
this.metadata = response.metadata;
|
|
|
|
}
|
2013-03-28 03:19:01 +01:00
|
|
|
}
|
|
|
|
}
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-28 03:19:01 +01:00
|
|
|
|
2013-03-20 18:57:58 +01:00
|
|
|
/**
|
|
|
|
* An object for saving contact data to backends
|
2013-03-22 14:51:26 +01:00
|
|
|
*
|
|
|
|
* All methods returns a jQuery.Deferred object which resolves
|
|
|
|
* to either the requested response or an error object:
|
|
|
|
* {
|
2013-05-03 00:23:56 +02:00
|
|
|
* error: true,
|
2013-03-22 14:51:26 +01:00
|
|
|
* message: The error message
|
|
|
|
* }
|
|
|
|
*
|
2013-03-23 02:08:07 +01:00
|
|
|
* @param string user The user to query for. Defaults to current user
|
2013-03-20 18:57:58 +01:00
|
|
|
*/
|
2013-03-22 14:51:26 +01:00
|
|
|
var Storage = function(user) {
|
|
|
|
this.user = user ? user : OC.currentUser;
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
|
|
|
|
2013-09-27 16:38:22 +02:00
|
|
|
/**
|
|
|
|
* When the response isn't returned from requestRoute(), you can
|
|
|
|
* wrap it in a JSONResponse so that it's parsable by other objects.
|
|
|
|
*
|
|
|
|
* @param XMLHTTPRequest http://api.jquery.com/jQuery.ajax/#jqXHR
|
|
|
|
*/
|
2014-03-23 14:14:36 +01:00
|
|
|
Storage.prototype.formatResponse = function(jqXHR) {
|
|
|
|
return new JSONResponse(jqXHR);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-20 18:57:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all address books registered for this user.
|
|
|
|
*
|
2013-03-23 02:08:07 +01:00
|
|
|
* @return An array containing object of address book metadata e.g.:
|
2013-03-20 18:57:58 +01:00
|
|
|
* {
|
2014-03-23 13:41:04 +01:00
|
|
|
* backend:'local',
|
|
|
|
* id:'1234'
|
|
|
|
* permissions:31,
|
|
|
|
* displayname:'Contacts'
|
2013-03-20 18:57:58 +01:00
|
|
|
* }
|
|
|
|
*/
|
2013-03-22 14:51:26 +01:00
|
|
|
Storage.prototype.getAddressBooksForUser = function() {
|
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbooks/',
|
2013-03-22 14:51:26 +01:00
|
|
|
'GET',
|
2013-04-25 04:40:12 +02:00
|
|
|
{}
|
2013-03-22 14:51:26 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-22 14:51:26 +01:00
|
|
|
|
2013-03-23 02:05:22 +01:00
|
|
|
/**
|
2013-03-25 17:08:47 +01:00
|
|
|
* Add an address book to a specific backend
|
2013-03-23 02:05:22 +01:00
|
|
|
*
|
2013-04-07 22:33:40 +02:00
|
|
|
* @param string backend - currently defaults to 'local'
|
2013-03-25 17:08:47 +01:00
|
|
|
* @param object params An object {displayname:"My contacts", description:""}
|
|
|
|
* @return An array containing contact data e.g.:
|
2013-03-23 02:05:22 +01:00
|
|
|
* {
|
2014-03-23 13:41:04 +01:00
|
|
|
* metadata:
|
|
|
|
* {
|
|
|
|
* id:'1234'
|
|
|
|
* permissions:31,
|
|
|
|
* displayname:'My contacts',
|
|
|
|
* lastmodified: (unix timestamp),
|
|
|
|
* owner: 'joye',
|
2013-03-23 02:05:22 +01:00
|
|
|
* }
|
|
|
|
*/
|
2013-03-25 17:08:47 +01:00
|
|
|
Storage.prototype.addAddressBook = function(backend, parameters) {
|
|
|
|
console.log('Storage.addAddressBook', backend);
|
2013-03-23 02:05:22 +01:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/add',
|
2013-03-25 17:08:47 +01:00
|
|
|
'POST',
|
2013-04-25 04:40:12 +02:00
|
|
|
{backend: 'local'},
|
2013-10-03 04:11:54 +02:00
|
|
|
JSON.stringify(parameters)
|
2013-03-25 17:08:47 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-25 17:08:47 +01:00
|
|
|
|
2013-05-07 19:16:06 +02:00
|
|
|
/**
|
|
|
|
* Update an address book in a specific backend
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
2013-05-07 19:16:06 +02:00
|
|
|
* @param object params An object {displayname:"My contacts", description:""}
|
|
|
|
* @return An array containing contact data e.g.:
|
|
|
|
* {
|
2014-03-23 13:41:04 +01:00
|
|
|
* metadata:
|
|
|
|
* {
|
|
|
|
* id:'1234'
|
|
|
|
* permissions:31,
|
|
|
|
* displayname:'My contacts',
|
|
|
|
* lastmodified: (unix timestamp),
|
|
|
|
* owner: 'joye',
|
2013-05-07 19:16:06 +02:00
|
|
|
* }
|
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.updateAddressBook = function(backend, addressBookId, properties) {
|
2013-05-07 19:16:06 +02:00
|
|
|
console.log('Storage.updateAddressBook', backend);
|
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}',
|
2013-05-07 19:16:06 +02:00
|
|
|
'POST',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId},
|
|
|
|
JSON.stringify(properties)
|
2013-05-07 19:16:06 +02:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-05-07 19:16:06 +02:00
|
|
|
|
2013-03-25 17:08:47 +01:00
|
|
|
/**
|
|
|
|
* Delete an address book from a specific backend
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
2013-03-25 17:08:47 +01:00
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.deleteAddressBook = function(backend, addressBookId) {
|
2014-03-11 12:58:57 +01:00
|
|
|
var key = 'contacts::' + backend + '::' + addressBookId;
|
2014-03-10 16:39:17 +01:00
|
|
|
|
|
|
|
if(OC.localStorage.hasItem(key)) {
|
|
|
|
OC.localStorage.removeItem(key);
|
|
|
|
}
|
|
|
|
|
2013-10-03 04:11:54 +02:00
|
|
|
console.log('Storage.deleteAddressBook', backend, addressBookId);
|
2013-03-25 17:08:47 +01:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}',
|
2013-04-25 04:50:45 +02:00
|
|
|
'DELETE',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId}
|
2013-03-23 02:05:22 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-23 02:05:22 +01:00
|
|
|
|
2013-09-05 22:07:40 +02:00
|
|
|
/**
|
|
|
|
* (De)active an address book from a specific backend
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
2013-09-05 22:07:40 +02:00
|
|
|
* @param bool state
|
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.activateAddressBook = function(backend, addressBookId, state) {
|
|
|
|
console.log('Storage.activateAddressBook', backend, addressBookId, state);
|
2013-09-05 22:07:40 +02:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/activate',
|
2013-09-05 22:07:40 +02:00
|
|
|
'POST',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId},
|
|
|
|
JSON.stringify({state: state})
|
2013-09-05 22:07:40 +02:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-09-05 22:07:40 +02:00
|
|
|
|
2013-03-22 14:51:26 +01:00
|
|
|
/**
|
|
|
|
* Get contacts from an address book from a specific backend
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
|
|
|
* @return
|
2013-05-24 20:35:23 +02:00
|
|
|
* An array containing contact data e.g.:
|
2013-03-22 14:51:26 +01:00
|
|
|
* {
|
2014-03-23 13:41:04 +01:00
|
|
|
* 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
|
2013-03-22 14:51:26 +01:00
|
|
|
* }
|
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.getAddressBook = function(backend, addressBookId) {
|
|
|
|
var headers = {},
|
|
|
|
data,
|
|
|
|
key = 'contacts::' + backend + '::' + addressBookId,
|
|
|
|
defer = $.Deferred();
|
|
|
|
|
|
|
|
if(OC.localStorage.hasItem(key)) {
|
|
|
|
data = OC.localStorage.getItem(key);
|
|
|
|
headers['If-None-Match'] = data.Etag;
|
|
|
|
}
|
|
|
|
$.when(this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}',
|
2013-10-03 04:11:54 +02:00
|
|
|
'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');
|
|
|
|
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);
|
2014-03-21 14:24:36 +01:00
|
|
|
defer.reject(response);
|
2013-10-03 04:11:54 +02:00
|
|
|
});
|
|
|
|
return defer;
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-25 17:08:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a contact to an address book from a specific backend
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
2013-03-25 17:08:47 +01:00
|
|
|
* @return An array containing contact data e.g.:
|
|
|
|
* {
|
2014-03-23 13:41:04 +01:00
|
|
|
* 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
|
2013-03-25 17:08:47 +01:00
|
|
|
* }
|
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.addContact = function(backend, addressBookId) {
|
|
|
|
console.log('Storage.addContact', backend, addressBookId);
|
2013-03-25 17:08:47 +01:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/contact/add',
|
2013-03-25 17:08:47 +01:00
|
|
|
'POST',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId}
|
2013-03-25 17:08:47 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-25 17:08:47 +01:00
|
|
|
|
2013-03-28 03:19:01 +01:00
|
|
|
/**
|
|
|
|
* Delete a contact from an address book from a specific backend
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
|
|
|
* @param string contactId Address book ID
|
2013-03-28 03:19:01 +01:00
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.deleteContact = function(backend, addressBookId, contactId) {
|
|
|
|
console.log('Storage.deleteContact', backend, addressBookId, contactId);
|
2013-03-28 03:19:01 +01:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/contact/{contactId}',
|
2013-04-25 04:50:45 +02:00
|
|
|
'DELETE',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId, contactId: contactId}
|
2013-03-28 03:19:01 +01:00
|
|
|
);
|
2014-03-08 09:03:28 +01:00
|
|
|
};
|
2013-03-28 03:19:01 +01:00
|
|
|
|
2013-09-03 14:08:12 +02:00
|
|
|
/**
|
|
|
|
* Delete a list of contacts from an address book from a specific backend
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
|
|
|
* @param array contactIds Address book ID
|
2013-09-03 14:08:12 +02:00
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.deleteContacts = function(backend, addressBookId, contactIds) {
|
|
|
|
console.log('Storage.deleteContacts', backend, addressBookId, contactIds);
|
2013-09-03 14:08:12 +02:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/deleteContacts',
|
2013-09-03 14:08:12 +02:00
|
|
|
'POST',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId},
|
|
|
|
JSON.stringify({contacts: contactIds})
|
2013-09-03 14:08:12 +02:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-09-03 14:08:12 +02:00
|
|
|
|
2013-05-06 01:49:10 +02:00
|
|
|
/**
|
|
|
|
* Move a contact to an address book from a specific backend
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
|
|
|
* @param string contactId Address book ID
|
2013-05-06 01:49:10 +02:00
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.moveContact = function(backend, addressBookId, contactId, target) {
|
|
|
|
console.log('Storage.moveContact', backend, addressBookId, contactId, target);
|
2013-05-06 01:49:10 +02:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/contact/{contactId}',
|
2013-05-06 01:49:10 +02:00
|
|
|
'POST',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId, contactId: contactId},
|
|
|
|
JSON.stringify(target)
|
2013-05-06 01:49:10 +02:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-05-06 01:49:10 +02:00
|
|
|
|
2013-04-05 00:30:42 +02:00
|
|
|
/**
|
|
|
|
* Get Image instance for a contacts profile picture
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
|
|
|
* @param string contactId Address book ID
|
2013-04-05 00:30:42 +02:00
|
|
|
* @return Image
|
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.getContactPhoto = function(backend, addressBookId, contactId) {
|
2013-04-05 00:30:42 +02:00
|
|
|
var photo = new Image();
|
2014-03-02 23:01:56 +01:00
|
|
|
var url = OC.generateUrl(
|
2014-03-02 23:19:01 +01:00
|
|
|
'apps/contacts/addressbook/{backend}/{addressBookId}/contact/{contactId}/photo',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId, contactId: contactId}
|
2013-04-08 22:37:37 +02:00
|
|
|
);
|
2013-04-05 00:30:42 +02:00
|
|
|
var defer = $.Deferred();
|
2014-03-08 09:03:28 +01:00
|
|
|
|
2013-04-05 00:30:42 +02:00
|
|
|
$.when(
|
2013-09-04 08:03:33 +02:00
|
|
|
$(photo).on('load', function() {
|
2013-04-05 00:30:42 +02:00
|
|
|
defer.resolve(photo);
|
|
|
|
})
|
|
|
|
.error(function() {
|
2014-03-08 09:03:28 +01:00
|
|
|
console.log('Error loading contact photo');
|
2013-05-15 01:02:38 +02:00
|
|
|
defer.reject();
|
2013-04-05 00:30:42 +02:00
|
|
|
})
|
2013-04-08 22:37:37 +02:00
|
|
|
.attr('src', url + '?refresh=' + Math.random())
|
2013-04-05 00:30:42 +02:00
|
|
|
)
|
|
|
|
.fail(function(jqxhr, textStatus, error) {
|
|
|
|
defer.reject();
|
|
|
|
var err = textStatus + ', ' + error;
|
2014-03-08 09:03:28 +01:00
|
|
|
console.warn('Request Failed:', + err);
|
2013-04-05 00:30:42 +02:00
|
|
|
$(document).trigger('status.contact.error', {
|
2014-03-08 09:05:52 +01:00
|
|
|
message: t('contacts', 'Failed loading photo: {error}', {error:err})
|
2013-04-05 00:30:42 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return defer.promise();
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-04-05 00:30:42 +02:00
|
|
|
|
2013-09-04 08:03:33 +02:00
|
|
|
/**
|
2014-03-08 09:03:28 +01:00
|
|
|
* Get Image instance for cropping contacts profile picture
|
2013-09-04 08:03:33 +02:00
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
|
|
|
* @param string contactId Address book ID
|
2013-09-04 08:03:33 +02:00
|
|
|
* @param string key The key to the cache where the photo is stored.
|
|
|
|
* @return Image
|
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.getTempContactPhoto = function(backend, addressBookId, contactId, key) {
|
2013-09-04 08:03:33 +02:00
|
|
|
var photo = new Image();
|
2014-03-02 23:01:56 +01:00
|
|
|
var url = OC.generateUrl(
|
2014-03-02 23:19:01 +01:00
|
|
|
'apps/contacts/addressbook/{backend}/{addressBookId}/contact/{contactId}/photo/{key}/tmp',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId, contactId: contactId, key: key, refresh: Math.random()}
|
2013-09-04 08:03:33 +02:00
|
|
|
);
|
|
|
|
console.log('url', url);
|
|
|
|
var defer = $.Deferred();
|
2014-03-08 06:30:29 +01:00
|
|
|
|
2013-09-04 08:03:33 +02:00
|
|
|
$.when(
|
|
|
|
$(photo).on('load', function() {
|
|
|
|
defer.resolve(photo);
|
|
|
|
})
|
|
|
|
.error(function(event) {
|
2014-03-23 19:02:16 +01:00
|
|
|
console.warn('Error loading temporary photo', event);
|
2013-09-04 08:03:33 +02:00
|
|
|
defer.reject();
|
|
|
|
})
|
|
|
|
.attr('src', url)
|
|
|
|
)
|
|
|
|
.fail(function(jqxhr, textStatus, error) {
|
|
|
|
defer.reject();
|
|
|
|
var err = textStatus + ', ' + error;
|
2014-03-08 09:05:52 +01:00
|
|
|
console.warn('Request Failed:', err);
|
2013-09-04 08:03:33 +02:00
|
|
|
$(document).trigger('status.contact.error', {
|
2014-03-08 09:05:52 +01:00
|
|
|
message: t('contacts', 'Failed loading photo: {error}', {error:err})
|
2013-09-04 08:03:33 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return defer.promise();
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-09-04 08:03:33 +02:00
|
|
|
|
2014-03-20 15:59:07 +01:00
|
|
|
/**
|
|
|
|
* Crop a contact phot.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2013-03-25 17:08:47 +01:00
|
|
|
/**
|
2013-10-03 04:11:54 +02:00
|
|
|
* Update a contact.
|
2013-03-25 17:08:47 +01:00
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
|
|
|
* @param string contactId Contact ID
|
2013-03-25 17:08:47 +01:00
|
|
|
* @param object params An object with the following properties:
|
|
|
|
* @param string name The name of the property e.g. EMAIL.
|
2013-10-03 04:16:28 +02:00
|
|
|
* @param string|array|null value The of the property
|
2013-03-25 17:08:47 +01:00
|
|
|
* @param array parameters Optional parameters for the property
|
|
|
|
* @param string checksum For non-singular properties such as email this must contain
|
2014-03-23 19:02:16 +01:00
|
|
|
* an 8 character md5 checksum of the serialized \Sabre\Property
|
2013-03-25 17:08:47 +01:00
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.patchContact = function(backend, addressBookId, contactId, params) {
|
2013-03-25 17:08:47 +01:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/contact/{contactId}',
|
2013-09-27 16:38:22 +02:00
|
|
|
'PATCH',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId, contactId: contactId},
|
2013-09-27 16:38:22 +02:00
|
|
|
JSON.stringify(params)
|
2013-03-25 17:08:47 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-25 17:08:47 +01:00
|
|
|
|
2013-04-03 16:43:18 +02:00
|
|
|
/**
|
|
|
|
* Save all properties. Used when merging contacts.
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param string addressBookId Address book ID
|
|
|
|
* @param string contactId Contact ID
|
2013-04-03 16:43:18 +02:00
|
|
|
* @param object params An object with the all properties:
|
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.saveAllProperties = function(backend, addressBookId, contactId, params) {
|
2013-04-03 16:43:18 +02:00
|
|
|
console.log('Storage.saveAllProperties', params);
|
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/contact/{contactId}/save',
|
2013-04-03 16:43:18 +02:00
|
|
|
'POST',
|
2013-10-03 04:11:54 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId, contactId: contactId},
|
|
|
|
JSON.stringify(params)
|
2013-04-03 16:43:18 +02:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-04-03 16:43:18 +02:00
|
|
|
|
2013-03-25 17:08:47 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* {
|
2014-03-23 13:41:04 +01:00
|
|
|
* 'categories': [{'id':1',Family'}, {...}],
|
|
|
|
* 'favorites': [123,456],
|
|
|
|
* 'shared': [],
|
|
|
|
* 'lastgroup':'1',
|
|
|
|
* 'sortorder':'3,2,4'
|
2013-03-25 17:08:47 +01:00
|
|
|
* }
|
|
|
|
*/
|
|
|
|
Storage.prototype.getGroupsForUser = function() {
|
|
|
|
console.log('getGroupsForUser');
|
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'groups/',
|
2013-03-25 17:08:47 +01:00
|
|
|
'GET',
|
2013-04-25 04:40:12 +02:00
|
|
|
{}
|
2013-03-25 17:08:47 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-25 17:08:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a group
|
|
|
|
*
|
|
|
|
* @param string name
|
|
|
|
* @return A JSON object containing the (maybe sanitized) group name and its ID:
|
|
|
|
* {
|
2014-03-23 13:41:04 +01:00
|
|
|
* 'id':1234,
|
|
|
|
* 'name':'My group'
|
2013-03-25 17:08:47 +01:00
|
|
|
* }
|
|
|
|
*/
|
|
|
|
Storage.prototype.addGroup = function(name) {
|
|
|
|
console.log('Storage.addGroup', name);
|
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'groups/add',
|
2013-03-25 17:08:47 +01:00
|
|
|
'POST',
|
2013-04-25 04:40:12 +02:00
|
|
|
{},
|
2013-10-03 04:11:54 +02:00
|
|
|
JSON.stringify({name: name})
|
2013-03-25 17:08:47 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-25 17:08:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a group
|
|
|
|
*
|
|
|
|
* @param string name
|
|
|
|
*/
|
|
|
|
Storage.prototype.deleteGroup = function(name) {
|
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'groups/delete',
|
2013-03-25 17:08:47 +01:00
|
|
|
'POST',
|
2013-04-25 04:40:12 +02:00
|
|
|
{},
|
2013-10-03 04:11:54 +02:00
|
|
|
JSON.stringify({name: name})
|
2013-03-25 17:08:47 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-25 17:08:47 +01:00
|
|
|
|
2013-05-21 23:39:50 +02:00
|
|
|
/**
|
|
|
|
* Rename a group
|
|
|
|
*
|
|
|
|
* @param string from
|
|
|
|
* @param string to
|
|
|
|
*/
|
|
|
|
Storage.prototype.renameGroup = function(from, to) {
|
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'groups/rename',
|
2013-05-21 23:39:50 +02:00
|
|
|
'POST',
|
|
|
|
{},
|
2013-10-03 04:11:54 +02:00
|
|
|
JSON.stringify({from: from, to: to})
|
2013-05-21 23:39:50 +02:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-05-21 23:39:50 +02:00
|
|
|
|
2013-03-29 06:51:16 +01:00
|
|
|
/**
|
|
|
|
* Add contacts to a group
|
|
|
|
*
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param array contactIds
|
2013-03-29 06:51:16 +01:00
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.addToGroup = function(contactIds, categoryId, categoryName) {
|
|
|
|
console.log('Storage.addToGroup', contactIds, categoryId);
|
2013-03-29 06:51:16 +01:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'groups/addto/{categoryId}',
|
2013-03-29 06:51:16 +01:00
|
|
|
'POST',
|
2013-10-03 04:11:54 +02:00
|
|
|
{categoryId: categoryId},
|
|
|
|
JSON.stringify({contactIds: contactIds, name: categoryName})
|
2013-03-29 06:51:16 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-29 06:51:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove contacts from a group
|
|
|
|
*
|
2013-10-03 04:11:54 +02:00
|
|
|
* @param array contactIds
|
2013-03-29 06:51:16 +01:00
|
|
|
*/
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.removeFromGroup = function(contactIds, categoryId, categoryName) {
|
|
|
|
console.log('Storage.removeFromGroup', contactIds, categoryId);
|
2013-03-29 06:51:16 +01:00
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'groups/removefrom/{categoryId}',
|
2013-03-29 06:51:16 +01:00
|
|
|
'POST',
|
2013-10-03 04:11:54 +02:00
|
|
|
{categoryId: categoryId},
|
|
|
|
JSON.stringify({contactIds: contactIds, name: categoryName})
|
2013-03-29 06:51:16 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-29 06:51:16 +01:00
|
|
|
|
2013-03-25 17:08:47 +01:00
|
|
|
/**
|
|
|
|
* Set a user preference
|
|
|
|
*
|
|
|
|
* @param string key
|
|
|
|
* @param string value
|
|
|
|
*/
|
|
|
|
Storage.prototype.setPreference = function(key, value) {
|
|
|
|
return this.requestRoute(
|
2014-03-02 23:01:56 +01:00
|
|
|
'preference/set',
|
2013-03-25 17:08:47 +01:00
|
|
|
'POST',
|
2013-04-25 04:40:12 +02:00
|
|
|
{},
|
2013-09-27 16:38:22 +02:00
|
|
|
JSON.stringify({key: key, value:value})
|
2013-03-22 14:51:26 +01:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
|
|
|
|
2013-09-20 16:24:29 +02:00
|
|
|
Storage.prototype.prepareImport = function(backend, addressBookId, importType, params) {
|
|
|
|
console.log('Storage.prepareImport', backend, addressBookId, importType);
|
2013-09-10 00:28:16 +02:00
|
|
|
return this.requestRoute(
|
2014-03-11 19:34:18 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/{importType}/import/prepare',
|
2013-09-10 00:28:16 +02:00
|
|
|
'POST',
|
2013-09-20 16:24:29 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId, importType: importType},
|
2013-10-03 04:11:54 +02:00
|
|
|
JSON.stringify(params)
|
2013-09-10 00:28:16 +02:00
|
|
|
);
|
|
|
|
};
|
2013-03-22 14:51:26 +01:00
|
|
|
|
2013-09-20 16:24:29 +02:00
|
|
|
Storage.prototype.startImport = function(backend, addressBookId, importType, params) {
|
|
|
|
console.log('Storage.startImport', backend, addressBookId, importType);
|
2013-05-02 20:41:26 +02:00
|
|
|
return this.requestRoute(
|
2014-03-11 19:34:18 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/{importType}/import/start',
|
2013-05-02 20:41:26 +02:00
|
|
|
'POST',
|
2013-09-20 16:24:29 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId, importType: importType},
|
2013-10-03 04:11:54 +02:00
|
|
|
JSON.stringify(params)
|
2013-05-02 20:41:26 +02:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-05-02 20:41:26 +02:00
|
|
|
|
2013-09-20 16:24:29 +02:00
|
|
|
Storage.prototype.importStatus = function(backend, addressBookId, importType, params) {
|
2013-05-02 20:41:26 +02:00
|
|
|
return this.requestRoute(
|
2014-03-11 19:34:18 +01:00
|
|
|
'addressbook/{backend}/{addressBookId}/{importType}/import/status',
|
2013-06-25 21:44:23 +02:00
|
|
|
'GET',
|
2013-09-20 16:24:29 +02:00
|
|
|
{backend: backend, addressBookId: addressBookId, importType: importType},
|
2013-10-24 14:36:11 +02:00
|
|
|
params
|
2013-05-02 20:41:26 +02:00
|
|
|
);
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-05-02 20:41:26 +02:00
|
|
|
|
2013-10-03 04:11:54 +02:00
|
|
|
Storage.prototype.requestRoute = function(route, type, routeParams, params, additionalHeaders) {
|
2013-04-03 16:49:54 +02:00
|
|
|
var isJSON = (typeof params === 'string');
|
2013-10-03 04:11:54 +02:00
|
|
|
var contentType = isJSON
|
|
|
|
? (type === 'PATCH' ? 'application/json-merge-patch' : 'application/json')
|
|
|
|
: 'application/x-www-form-urlencoded';
|
2013-04-03 16:49:54 +02:00
|
|
|
var processData = !isJSON;
|
|
|
|
contentType += '; charset=UTF-8';
|
2014-03-02 23:19:01 +01:00
|
|
|
var url = OC.generateUrl('apps/contacts/' + route, routeParams);
|
2013-10-03 04:11:54 +02:00
|
|
|
var headers = {
|
2014-03-02 23:01:56 +01:00
|
|
|
Accept : 'application/json; charset=utf-8'
|
2013-10-03 04:11:54 +02:00
|
|
|
};
|
|
|
|
if(typeof additionalHeaders === 'object') {
|
|
|
|
headers = $.extend(headers, additionalHeaders);
|
|
|
|
}
|
2013-04-03 16:49:54 +02:00
|
|
|
var ajaxParams = {
|
|
|
|
type: type,
|
|
|
|
url: url,
|
|
|
|
dataType: 'json',
|
2013-10-03 04:11:54 +02:00
|
|
|
headers: headers,
|
2013-04-03 16:49:54 +02:00
|
|
|
contentType: contentType,
|
|
|
|
processData: processData,
|
|
|
|
data: params
|
|
|
|
};
|
2013-05-03 00:23:56 +02:00
|
|
|
|
2013-03-28 03:19:01 +01:00
|
|
|
var defer = $.Deferred();
|
2013-04-11 17:35:17 +02:00
|
|
|
|
2014-03-23 19:02:16 +01:00
|
|
|
$.ajax(ajaxParams)
|
2013-05-03 05:56:55 +02:00
|
|
|
.done(function(response, textStatus, jqXHR) {
|
2014-03-23 14:14:36 +01:00
|
|
|
console.log(jqXHR);
|
|
|
|
defer.resolve(new JSONResponse(jqXHR));
|
2013-04-11 17:35:17 +02:00
|
|
|
})
|
2014-03-08 06:30:29 +01:00
|
|
|
.fail(function(jqXHR/*, textStatus, error*/) {
|
2013-05-18 05:45:48 +02:00
|
|
|
console.log(jqXHR);
|
2013-09-17 18:46:59 +02:00
|
|
|
var response = jqXHR.responseText ? $.parseJSON(jqXHR.responseText) : null;
|
2013-05-18 05:45:48 +02:00
|
|
|
console.log('response', response);
|
2014-03-23 14:14:36 +01:00
|
|
|
defer.reject(new JSONResponse(jqXHR));
|
2013-04-11 17:35:17 +02:00
|
|
|
});
|
|
|
|
|
2013-03-28 03:19:01 +01:00
|
|
|
return defer.promise();
|
2013-09-10 00:28:16 +02:00
|
|
|
};
|
2013-03-20 18:57:58 +01:00
|
|
|
|
|
|
|
OC.Contacts.Storage = Storage;
|
|
|
|
|
|
|
|
})(window, jQuery, OC);
|