OC.Contacts = OC.Contacts || {}; /** * TODO: Use $.Deferred. */ (function(window, $, OC) { 'use strict'; var JSONResponse = function(response) { if(!response || !response.status || response.status === 'error') { this.error = true; this.message = response.data.message || 'Unknown error.'; } else { this.error = false; if(response.data) { this.data = response.data; } } } /** * 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: * { * status: 'error', * 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; this.contactPhoto = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAACBCAQAAACcYtfuAAAAAnNCSVQICFXsRgQAAAAJcEhZcwAADsQAAA7EAZUrDhsAABMnSURBVHjazZx7jFzVfcc/5947j32za3vt9foJGANtgDaQxm1ValEimqQBAoWQBEhaJWlDI6WkTWhS1Y3UVEmkqo+ghjSqSqGhxVJUBwgKUIGiVgWqRDxjMCb4sazXa3tfnp33vffXP+bOPefM3NnH7M7iOxp75z5/3/N7nt/vd66iA9tjg+4vqivYLRvViIywEZhUE3KSSQ7xQvDqB2dW/5lqNW+2z3n3kPcedS2/yna1HjfhlEDOcEyeVU9Vn//p9FfDcxLIj4bkdj7JZWpJ95SX+Bf14HXT5xiQR0fdD/A5tYvMMi4qy2F1r//Y74yfM0B+eKW6l19S6eVfKRX1QvhHH/jJOQDkmWz+D9Wfq6GkW6fw8EgBVXx8qkjSLab5q65v7y29o0Ae26S+rD5Bn703RReDrCeDi4MLBIQElDnDDEWqjbfJyf3y1x88+Y4BeXJz9R/V9fa+LCNspC/RZAEE5JhkgkYGyA/Cu1aiLSsAcqDPe4APKk/v6WYj28mgFritAEKZY0xSMPcHPOrfcUOuXWrcdi98pNu7h08p4/ohLmWUFA4qIrj16HkMcR4Finqfw25XPvLcv1fXFMg+Z8s96s+IueGxnUvpWSKDBVBkGUbIEXtFpd6rwl3//WNphyKnPSBXXS13SVrfZDcX4CUQLAghYfRX/aPhn89ugwRJy2evuro9ipz2tEPuZoO+x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NrA4uLg7p6JMhHY2gsiyZImmlaECFCmUq0SckiMurC0Iu8TaPq+8ePvRPVRbLR39jt/NebpRr6V7+2Goj6sSKqmd2YVx1bCtOKKin+M/wuS8dSjInLbZ9Ts+w+u3gZnUxW5Maa9Z0qzImr6vvq8fzp1r1PC4i4N/o87e6O9Ue2cOldEdl97UKfUPyFOQ153/l2eCIN/al3MIGfonbvk2Zy5xf5l1sk/PVcEcn8L6cUm9xnFf8F6ovfXWJq0uWOb5/01XJOl3+eepCdQm7ZBcDqosu6VLdZNuaC4eUpEBJFaTIHG+qN4LX1WFvNiymS19YVnvmigTlO6kT69wBr1/1MSD9alg2soGNdKmMZMmQIUuGjMqAlClTlrIqUVYlKVNkktNqUk6ps8xJzj8bzG2e+ky1fVr+H9CSOOo3Z6VHAAAAAElFTkSuQmCC'; } /** * Get all address books registered for this user. * * @return An array containing object of address book metadata e.g.: * { * backend:'database', * id:'1234' * permissions:31, * displayname:'Contacts' * } */ Storage.prototype.getAddressBooksForUser = function() { return this.requestRoute( 'contacts_address_books_for_user', 'GET', {user: this.user} ); } /** * Add an address book to a specific backend * * @param string backend - currently defaults to 'database' * @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.addAddressBook = function(backend, parameters) { console.log('Storage.addAddressBook', backend); return this.requestRoute( 'contacts_address_book_add', 'POST', {user: this.user, backend: 'database'}, parameters ); } /** * Delete an address book from a specific backend * * @param string backend * @param string addressbookid Address book ID */ Storage.prototype.deleteAddressBook = function(backend, addressbookid) { console.log('Storage.deleteAddressBook', backend, addressbookid); return this.requestRoute( 'contacts_address_book_delete', 'POST', {user: this.user, backend: 'database', addressbookid: addressbookid} ); } /** * 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.: * { * 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 * } */ Storage.prototype.getContacts = function(backend, addressbookid) { return this.requestRoute( 'contacts_address_book_collection', 'GET', {user: this.user, backend: backend, addressbookid: addressbookid} ); } /** * 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.: * { * 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 * } */ Storage.prototype.addContact = function(backend, addressbookid) { console.log('Storage.addContact', backend, addressbookid); return this.requestRoute( 'contacts_address_book_add_contact', 'POST', {user: this.user, 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( 'contacts_address_book_delete_contact', 'POST', {user: this.user, backend: backend, addressbookid: addressbookid, contactid: contactid} ); } /** * Delete a single property. * * @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 checksum For non-singular properties such as email this must contain * an 8 character md5 checksum of the serialized \Sabre\Property */ Storage.prototype.deleteProperty = function(backend, addressbookid, contactid, params) { return this.requestRoute( 'contacts_contact_delete_property', 'POST', {user: this.user, backend: backend, addressbookid: addressbookid, contactid: contactid}, params ); } /** * Save a property. * * @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 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 * an 8 character md5 checksum of the serialized \Sabre\Property */ Storage.prototype.saveProperty = function(backend, addressbookid, contactid, params) { return this.requestRoute( 'contacts_contact_save_property', 'POST', {user: this.user, backend: backend, addressbookid: addressbookid, contactid: contactid}, 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( 'contacts_contact_save_all', 'POST', {user: this.user, backend: backend, addressbookid: addressbookid, contactid: contactid}, 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. * { * 'categories': [{'id':1',Family'}, {...}], * 'favorites': [123,456], * 'shared': [], * 'lastgroup':'1', * 'sortorder':'3,2,4' * } */ Storage.prototype.getGroupsForUser = function() { console.log('getGroupsForUser'); return this.requestRoute( 'contacts_categories_list', 'GET', {user: this.user} ); } /** * Add a group * * @param string name * @return A JSON object containing the (maybe sanitized) group name and its ID: * { * 'id':1234, * 'name':'My group' * } */ Storage.prototype.addGroup = function(name) { console.log('Storage.addGroup', name); return this.requestRoute( 'contacts_categories_add', 'POST', {user: this.user}, {name: name} ); } /** * Delete a group * * @param string name */ Storage.prototype.deleteGroup = function(name) { return this.requestRoute( 'contacts_categories_delete', 'POST', {user: this.user}, {name: name} ); } /** * Add contacts to a group * * @param array contactids */ Storage.prototype.addToGroup = function(contactids, categoryid) { console.log('Storage.addToGroup', contactids, categoryid); return this.requestRoute( 'contacts_categories_addto', 'POST', {user: this.user, categoryid: categoryid}, {contactids: contactids} ); } /** * Remove contacts from a group * * @param array contactids */ Storage.prototype.removeFromGroup = function(contactids, categoryid) { console.log('Storage.addToGroup', contactids, categoryid); return this.requestRoute( 'contacts_categories_removefrom', 'POST', {user: this.user, categoryid: categoryid}, {contactids: contactids} ); } /** * Set a user preference * * @param string key * @param string value */ Storage.prototype.setPreference = function(key, value) { return this.requestRoute( 'contacts_setpreference', 'POST', {user: this.user}, {key: key, value:value} ); } Storage.prototype.requestRoute = function(route, type, routeParams, params) { var self = this; var url = OC.Router.generate(route, routeParams); var ajaxParams = {type: type, url: url, dataType: 'json'}; if(typeof params === 'object') { ajaxParams['data'] = params; } var defer = $.Deferred(); $.when($.ajax(ajaxParams)).then(function(response) { //console.log('response', response); defer.resolve(new JSONResponse(response)); }).fail(function(jqxhr, textStatus, error) { defer.reject( new JSONResponse({ status:'error', data:{message:t('contacts', 'Failed loading address books: {error}', {error:textStatus + ', ' + error})} }) ); }); return defer.promise(); } OC.Contacts.Storage = Storage; })(window, jQuery, OC);