mirror of
https://github.com/owncloudarchive/contacts.git
synced 2024-11-29 11:24:11 +01:00
Cache address books in localStorage.
This commit is contained in:
parent
8e7ab166fe
commit
8c3303c760
155
js/contacts.js
155
js/contacts.js
@ -1871,11 +1871,11 @@ OC.Contacts = OC.Contacts || {};
|
||||
console.log('status.addressbook.imported', data);
|
||||
var addressBook = data.addressbook;
|
||||
self.purgeFromAddressbook(addressBook);
|
||||
$.when(self.loadContacts(addressBook.getBackend(), addressBook.getId()))
|
||||
.then(function() {
|
||||
self.setSortOrder();
|
||||
$(document).trigger('request.groups.reload');
|
||||
});
|
||||
$.when(self.loadContacts(addressBook.getBackend(), addressBook.getId(), true))
|
||||
.then(function() {
|
||||
self.setSortOrder();
|
||||
$(document).trigger('request.groups.reload');
|
||||
});
|
||||
});
|
||||
$(document).bind('status.addressbook.activated', function(e, data) {
|
||||
console.log('status.addressbook.activated', data);
|
||||
@ -1886,7 +1886,7 @@ OC.Contacts = OC.Contacts || {};
|
||||
numcontacts: self.length
|
||||
});
|
||||
} else {
|
||||
$.when(self.loadContacts(addressBook.getBackend(), addressBook.getId(), true))
|
||||
$.when(self.loadContacts(addressBook.getBackend(), addressBook.getId(), true))
|
||||
.then(function() {
|
||||
self.setSortOrder();
|
||||
$(document).trigger('request.groups.reload');
|
||||
@ -2381,6 +2381,37 @@ OC.Contacts = OC.Contacts || {};
|
||||
}
|
||||
};
|
||||
|
||||
ContactList.prototype.insertContacts = function(contacts) {
|
||||
var self = this, items = [];
|
||||
$.each(contacts, function(c, contact) {
|
||||
var id = String(contact.metadata.id);
|
||||
self.contacts[id]
|
||||
= new Contact(
|
||||
self,
|
||||
id,
|
||||
contact.metadata,
|
||||
contact.data,
|
||||
self.$contactListItemTemplate,
|
||||
self.$contactDragItemTemplate,
|
||||
self.$contactFullTemplate,
|
||||
self.contactDetailTemplates
|
||||
);
|
||||
self.length +=1;
|
||||
var $item = self.contacts[id].renderListItem();
|
||||
if(!$item) {
|
||||
console.warn('Contact', contact, 'could not be rendered!');
|
||||
return true; // continue
|
||||
}
|
||||
items.push($item.get(0));
|
||||
});
|
||||
if(items.length > 0) {
|
||||
self.$contactList.append(items);
|
||||
}
|
||||
$(document).trigger('status.contacts.count', {
|
||||
count: self.length
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Load contacts
|
||||
* @param string backend Name of the backend ('local', 'ldap' etc.)
|
||||
@ -2392,53 +2423,75 @@ OC.Contacts = OC.Contacts || {};
|
||||
}
|
||||
var self = this,
|
||||
contacts,
|
||||
key = 'contacts::' + backend + '::' + addressBookId;
|
||||
var defer = $.when(this.storage.getAddressBook(backend, addressBookId)).then(function(response) {
|
||||
//console.log('ContactList.loadContacts', response);
|
||||
if(!response.error) {
|
||||
if(response.statusCode === 304 && OC.localStorage.hasItem(key)) {
|
||||
contacts = OC.localStorage.getItem(key);
|
||||
} else {
|
||||
contacts = response.data.contacts;
|
||||
OC.localStorage.setItem(key, contacts);
|
||||
}
|
||||
var items = [];
|
||||
$.each(contacts, function(c, contact) {
|
||||
var id = String(contact.metadata.id);
|
||||
contact.metadata.backend = backend;
|
||||
self.contacts[id]
|
||||
= new Contact(
|
||||
self,
|
||||
id,
|
||||
contact.metadata,
|
||||
contact.data,
|
||||
self.$contactListItemTemplate,
|
||||
self.$contactDragItemTemplate,
|
||||
self.$contactFullTemplate,
|
||||
self.contactDetailTemplates
|
||||
);
|
||||
self.length +=1;
|
||||
var $item = self.contacts[id].renderListItem();
|
||||
if(!$item) {
|
||||
console.warn('Contact', contact, 'could not be rendered!');
|
||||
return true; // continue
|
||||
key = 'contacts::' + backend + '::' + addressBookId,
|
||||
defer = $.Deferred();
|
||||
|
||||
// Local function to get the contacts if they're not cached or invalid.
|
||||
var fetchContacts = function(backend, addressBookId) {
|
||||
return $.when(self.storage.getAddressBook(backend, addressBookId, false))
|
||||
.then(function(response) {
|
||||
console.log('ContactList.loadContacts - fetching', response);
|
||||
if(!response.error) {
|
||||
if(response.data) {
|
||||
response.data.Etag = response.getResponseHeader('Etag');
|
||||
OC.localStorage.setItem(key, response.data);
|
||||
self.insertContacts(response.data.contacts);
|
||||
defer.resolve();
|
||||
}
|
||||
items.push($item.get(0));
|
||||
});
|
||||
if(items.length > 0) {
|
||||
self.$contactList.append(items);
|
||||
} else {
|
||||
console.warn('ContactList.loadContacts - no data!!');
|
||||
}
|
||||
$(document).trigger('status.contacts.count', {
|
||||
count: self.length
|
||||
});
|
||||
} else {
|
||||
defer.reject(response);
|
||||
}
|
||||
})
|
||||
.fail(function(response) {
|
||||
console.warn('Request Failed:', response.message);
|
||||
this.reject({error: true, message: response.message});
|
||||
});
|
||||
})
|
||||
.fail(function(response) {
|
||||
console.warn('Request Failed:', response.message);
|
||||
defer.reject({error: true, message: response.message});
|
||||
});
|
||||
return defer;
|
||||
};
|
||||
|
||||
// First check if we have a valid local cache
|
||||
if(OC.localStorage.hasItem(key)) {
|
||||
$.when(this.storage.getAddressBook(backend, addressBookId, true))
|
||||
.then(function(response) {
|
||||
var data = OC.localStorage.getItem(key);
|
||||
console.log('Local data', data);
|
||||
var etag = response.getResponseHeader('Etag');
|
||||
console.log('HEAD response', response);
|
||||
console.log('HEAD Etag', etag);
|
||||
console.log('Saved Etag', data.Etag);
|
||||
if(etag === data.Etag) {
|
||||
console.log('Returning saved data');
|
||||
self.insertContacts(data.contacts);
|
||||
defer.resolve();
|
||||
} else {
|
||||
console.log('Local cache invalid');
|
||||
$.when(fetchContacts(backend, addressBookId))
|
||||
.then(function(response) {
|
||||
console.log('ContactList.loadContacts on invalid', response);
|
||||
defer.resolve();
|
||||
})
|
||||
.fail(function(response) {
|
||||
console.warn('Request Failed:', response.message);
|
||||
defer.reject();
|
||||
});
|
||||
}
|
||||
})
|
||||
.fail(function(response) {
|
||||
console.warn('Request Failed:', response.message);
|
||||
defer.reject();
|
||||
});
|
||||
} else {
|
||||
console.log('No local cache');
|
||||
$.when(fetchContacts(backend, addressBookId))
|
||||
.then(function(response) {
|
||||
console.log('ContactList.loadContacts on no cache', response);
|
||||
defer.resolve();
|
||||
})
|
||||
.fail(function(response) {
|
||||
console.warn('Request Failed:', response.message);
|
||||
defer.reject();
|
||||
});
|
||||
}
|
||||
return defer;
|
||||
};
|
||||
|
||||
|
@ -29,7 +29,7 @@ OC.Contacts = OC.Contacts || {};
|
||||
console.log('group click', $(this));
|
||||
if($(event.target).is('.action.delete')) {
|
||||
$('.tipsy').remove();
|
||||
$(this).addClass('loading');
|
||||
$(this).addClass('loading').removeClass('active');
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
var id = $(event.target).parents('li').first().data('id');
|
||||
|
@ -175,12 +175,12 @@ OC.Contacts = OC.Contacts || {};
|
||||
* 'export'
|
||||
* A stream of vCards separated by "\r\n\r\n"
|
||||
*/
|
||||
Storage.prototype.getAddressBook = function(backend, addressbookid, params) {
|
||||
Storage.prototype.getAddressBook = function(backend, addressbookid, onlyHeaders) {
|
||||
var verb = onlyHeaders ? 'HEAD' : 'GET';
|
||||
return this.requestRoute(
|
||||
'contacts_address_book',
|
||||
'GET',
|
||||
{backend: backend, addressbookid: addressbookid},
|
||||
params
|
||||
verb,
|
||||
{backend: backend, addressbookid: addressbookid}
|
||||
);
|
||||
}
|
||||
|
||||
@ -558,7 +558,7 @@ OC.Contacts = OC.Contacts || {};
|
||||
);
|
||||
}
|
||||
|
||||
Storage.prototype.requestRoute = function(route, type, routeParams, params) {
|
||||
Storage.prototype.requestRoute = function(route, type, routeParams, params, dontCache) {
|
||||
var isJSON = (typeof params === 'string');
|
||||
var contentType = isJSON ? 'application/json' : 'application/x-www-form-urlencoded';
|
||||
var processData = !isJSON;
|
||||
@ -569,7 +569,8 @@ OC.Contacts = OC.Contacts || {};
|
||||
type: type,
|
||||
url: url,
|
||||
dataType: 'json',
|
||||
ifModified: true,
|
||||
//ifModified: true,
|
||||
cache: dontCache ? false : true,
|
||||
contentType: contentType,
|
||||
processData: processData,
|
||||
data: params
|
||||
|
@ -55,16 +55,16 @@ class AddressBookController extends BaseController {
|
||||
$response = new JSONResponse();
|
||||
|
||||
if(!is_null($lastModified)) {
|
||||
$response->addHeader('Cache-Control', 'private, must-revalidate');
|
||||
//$response->addHeader('Cache-Control', 'private, must-revalidate');
|
||||
$response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null);
|
||||
$response->setETag(md5($lastModified));
|
||||
}
|
||||
|
||||
$response->debug('method: ' . $this->request->method);
|
||||
if($this->request->method === 'GET') {
|
||||
$contacts = array();
|
||||
foreach($addressBook->getChildren() as $i => $contact) {
|
||||
$result = JSONSerializer::serializeContact($contact);
|
||||
//\OCP\Util::writeLog('contacts', __METHOD__.' contact: '.print_r($result, true), \OCP\Util::DEBUG);
|
||||
if($result !== null) {
|
||||
$contacts[] = $result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user