1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-01 13:24:10 +01:00
OwncloudContactsOfficial/js/storage.js

336 lines
15 KiB
JavaScript
Raw Normal View History

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
* }
*
2013-03-23 02:08:07 +01:00
* @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/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I/nepO53PSrbxSJos/WxgiGwL/AEVY51OhWlyFKKlMR4psvQyFDFYUKRJN6y2ql9b4DjTTaY5OjzNA3z7lZ83Ok5lG1h1i3zF2SWueYJDhiHW0093gtGqNzD7UcuTLS6VaFWJgx
}
/**
* 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.:
* {
* 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'},
2013-03-30 07:28:30 +01:00
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
);
}
/**
* 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);