2013-03-20 18:57:58 +01:00
|
|
|
OC.Contacts = OC.Contacts || {};
|
|
|
|
|
2013-03-22 14:51:26 +01:00
|
|
|
/**
|
|
|
|
* TODO: Use $.Deferred.
|
|
|
|
*/
|
2013-03-20 18:57:58 +01:00
|
|
|
|
|
|
|
(function(window, $, OC) {
|
|
|
|
'use strict';
|
2013-03-28 03:19:01 +01:00
|
|
|
|
|
|
|
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;
|
2013-04-04 02:52:27 +02:00
|
|
|
} else {
|
|
|
|
this.data = response;
|
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:
|
|
|
|
* {
|
|
|
|
* 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
|
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-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
|
|
|
* {
|
2013-04-07 22:33:40 +02:00
|
|
|
* backend:'local',
|
2013-03-20 18:57:58 +01:00
|
|
|
* id:'1234'
|
|
|
|
* permissions:31,
|
|
|
|
* displayname:'Contacts'
|
|
|
|
* }
|
|
|
|
*/
|
2013-03-22 14:51:26 +01:00
|
|
|
Storage.prototype.getAddressBooksForUser = function() {
|
|
|
|
return this.requestRoute(
|
|
|
|
'contacts_address_books_for_user',
|
|
|
|
'GET',
|
2013-04-25 04:40:12 +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
|
|
|
* {
|
2013-03-25 17:08:47 +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(
|
2013-03-25 17:08:47 +01:00
|
|
|
'contacts_address_book_add',
|
|
|
|
'POST',
|
2013-04-25 04:40:12 +02:00
|
|
|
{backend: 'local'},
|
2013-03-30 07:28:30 +01:00
|
|
|
parameters
|
2013-03-25 17:08:47 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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',
|
2013-04-25 04:40:12 +02:00
|
|
|
{backend: 'local', addressbookid: addressbookid}
|
2013-03-23 02:05:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-22 14:51:26 +01:00
|
|
|
/**
|
|
|
|
* Get contacts from an address book from a specific backend
|
|
|
|
*
|
|
|
|
* @param string backend
|
2013-03-25 17:08:47 +01:00
|
|
|
* @param string addressbookid Address book ID
|
2013-03-22 14:51:26 +01:00
|
|
|
* @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
|
|
|
|
* }
|
|
|
|
*/
|
2013-03-25 17:08:47 +01:00
|
|
|
Storage.prototype.getContacts = function(backend, addressbookid) {
|
2013-03-22 14:51:26 +01:00
|
|
|
return this.requestRoute(
|
|
|
|
'contacts_address_book_collection',
|
|
|
|
'GET',
|
2013-04-25 04:40:12 +02:00
|
|
|
{backend: backend, addressbookid: addressbookid}
|
2013-03-25 17:08:47 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-28 03:19:01 +01:00
|
|
|
/**
|
|
|
|
* 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}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-04-05 00:30:42 +02:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
var url = OC.Router.generate(
|
|
|
|
'contacts_contact_photo',
|
|
|
|
{user: this.user, 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();
|
|
|
|
$.when(
|
|
|
|
$(photo).load(function() {
|
|
|
|
defer.resolve(photo);
|
|
|
|
})
|
|
|
|
.error(function() {
|
|
|
|
console.log('Error loading default photo', arguments)
|
|
|
|
})
|
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;
|
|
|
|
console.log( "Request Failed: " + err);
|
|
|
|
$(document).trigger('status.contact.error', {
|
|
|
|
message: t('contacts', 'Failed loading photo: {error}', {error:err})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return defer.promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Image instance for default profile picture
|
|
|
|
*
|
|
|
|
* This method loads the default picture only once and caches it.
|
|
|
|
*
|
|
|
|
* @return Image
|
|
|
|
*/
|
|
|
|
Storage.prototype.getDefaultPhoto = function() {
|
|
|
|
console.log('Storage.getDefaultPhoto');
|
2013-04-25 00:59:03 +02:00
|
|
|
if(!this.defaultPhoto) {
|
|
|
|
var defer = $.Deferred();
|
2013-04-05 00:30:42 +02:00
|
|
|
var url = OC.imagePath('contacts', 'person_large.png');
|
2013-04-25 00:59:03 +02:00
|
|
|
this.defaultPhoto = new Image();
|
|
|
|
var self = this;
|
|
|
|
$(this.defaultPhoto)
|
|
|
|
.load(function() {
|
|
|
|
defer.resolve(this);
|
|
|
|
}).error(function(event) {
|
|
|
|
defer.reject();
|
|
|
|
}).attr('src', url)
|
|
|
|
return defer.promise();
|
|
|
|
} else {
|
|
|
|
return this.defaultPhoto;
|
|
|
|
}
|
2013-04-05 00:30:42 +02:00
|
|
|
}
|
|
|
|
|
2013-03-25 17:08:47 +01:00
|
|
|
/**
|
|
|
|
* 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',
|
2013-04-25 04:40:12 +02:00
|
|
|
{backend: backend, addressbookid: addressbookid, contactid: contactid},
|
2013-03-25 17:08:47 +01:00
|
|
|
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',
|
2013-04-25 04:40:12 +02:00
|
|
|
{backend: backend, addressbookid: addressbookid, contactid: contactid},
|
2013-03-25 17:08:47 +01:00
|
|
|
params
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-04-03 16:43:18 +02:00
|
|
|
/**
|
|
|
|
* 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',
|
2013-04-25 04:40:12 +02:00
|
|
|
{backend: backend, addressbookid: addressbookid, contactid: contactid},
|
2013-04-03 16:43:18 +02:00
|
|
|
params
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* {
|
|
|
|
* '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',
|
2013-04-25 04:40:12 +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:
|
|
|
|
* {
|
|
|
|
* 'id':1234,
|
|
|
|
* 'name':'My group'
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
Storage.prototype.addGroup = function(name) {
|
|
|
|
console.log('Storage.addGroup', name);
|
|
|
|
return this.requestRoute(
|
|
|
|
'contacts_categories_add',
|
|
|
|
'POST',
|
2013-04-25 04:40:12 +02:00
|
|
|
{},
|
2013-03-25 17:08:47 +01:00
|
|
|
{name: name}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a group
|
|
|
|
*
|
|
|
|
* @param string name
|
|
|
|
*/
|
|
|
|
Storage.prototype.deleteGroup = function(name) {
|
|
|
|
return this.requestRoute(
|
|
|
|
'contacts_categories_delete',
|
|
|
|
'POST',
|
2013-04-25 04:40:12 +02:00
|
|
|
{},
|
2013-03-25 17:08:47 +01:00
|
|
|
{name: name}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-29 06:51:16 +01:00
|
|
|
/**
|
|
|
|
* 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',
|
2013-04-25 04:40:12 +02:00
|
|
|
{categoryid: categoryid},
|
2013-03-29 06:51:16 +01:00
|
|
|
{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',
|
2013-04-25 04:40:12 +02:00
|
|
|
{categoryid: categoryid},
|
2013-03-29 06:51:16 +01:00
|
|
|
{contactids: contactids}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
'contacts_setpreference',
|
|
|
|
'POST',
|
2013-04-25 04:40:12 +02:00
|
|
|
{},
|
2013-03-25 17:08:47 +01:00
|
|
|
{key: key, value:value}
|
2013-03-22 14:51:26 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-25 17:08:47 +01:00
|
|
|
Storage.prototype.requestRoute = function(route, type, routeParams, params) {
|
2013-04-03 16:49:54 +02:00
|
|
|
var isJSON = (typeof params === 'string');
|
|
|
|
var contentType = isJSON ? 'application/json' : 'application/x-www-form-urlencoded';
|
|
|
|
var processData = !isJSON;
|
|
|
|
contentType += '; charset=UTF-8';
|
2013-03-22 14:51:26 +01:00
|
|
|
var self = this;
|
2013-03-25 17:08:47 +01:00
|
|
|
var url = OC.Router.generate(route, routeParams);
|
2013-04-03 16:49:54 +02:00
|
|
|
var ajaxParams = {
|
|
|
|
type: type,
|
|
|
|
url: url,
|
|
|
|
dataType: 'json',
|
|
|
|
contentType: contentType,
|
|
|
|
processData: processData,
|
|
|
|
data: params
|
|
|
|
};
|
2013-03-28 03:19:01 +01:00
|
|
|
var defer = $.Deferred();
|
2013-04-11 17:35:17 +02:00
|
|
|
/*$.when($.ajax(ajaxParams)).then(function(response) {
|
2013-03-28 03:19:01 +01:00
|
|
|
defer.resolve(new JSONResponse(response));
|
|
|
|
}).fail(function(jqxhr, textStatus, error) {
|
|
|
|
defer.reject(
|
|
|
|
new JSONResponse({
|
|
|
|
status:'error',
|
2013-04-09 23:19:22 +02:00
|
|
|
data:{message:t('contacts', 'Request failed: {error}', {error:textStatus + ', ' + error})}
|
2013-03-28 03:19:01 +01:00
|
|
|
})
|
|
|
|
);
|
2013-04-11 17:35:17 +02:00
|
|
|
});*/
|
|
|
|
|
|
|
|
var jqxhr = $.ajax(ajaxParams)
|
|
|
|
.done(function(response) {
|
|
|
|
defer.resolve(new JSONResponse(response));
|
|
|
|
})
|
|
|
|
.fail(function(jqxhr, textStatus, error) {
|
|
|
|
defer.reject(
|
|
|
|
new JSONResponse({
|
|
|
|
status:'error',
|
|
|
|
data:{message:t('contacts', 'Request failed: {error}', {error:textStatus + ', ' + error})}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2013-03-28 03:19:01 +01:00
|
|
|
return defer.promise();
|
2013-03-20 18:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OC.Contacts.Storage = Storage;
|
|
|
|
|
|
|
|
})(window, jQuery, OC);
|