mirror of
https://github.com/moparisthebest/mail
synced 2024-12-22 15:28:49 -05:00
sort emails by sent date when fetching them from the data-store
This commit is contained in:
parent
34a84a6b9d
commit
5b98cd692a
@ -62,6 +62,9 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) {
|
|||||||
// parse to backbone model collection
|
// parse to backbone model collection
|
||||||
collection = new app.model.EmailCollection(decryptedList);
|
collection = new app.model.EmailCollection(decryptedList);
|
||||||
|
|
||||||
|
// reverse order so that mails with the most recent dat will be displayed first
|
||||||
|
collection.models.reverse();
|
||||||
|
|
||||||
// cache collection in folder memory
|
// cache collection in folder memory
|
||||||
if (decryptedList.length > 0) {
|
if (decryptedList.length > 0) {
|
||||||
folder = new app.model.Folder({name: folderName});
|
folder = new app.model.Folder({name: folderName});
|
||||||
|
@ -60,32 +60,43 @@ app.dao.LawnchairDAO = function(window) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if num is null, list all items
|
|
||||||
num = (num !== null) ? num : matchingKeys.length;
|
|
||||||
|
|
||||||
// set window of items to fetch
|
|
||||||
if (offset + num < matchingKeys.length) {
|
|
||||||
matchingKeys = matchingKeys.splice(matchingKeys.length - offset - num, num);
|
|
||||||
} else if (offset + num >= matchingKeys.length && offset < matchingKeys.length) {
|
|
||||||
matchingKeys = matchingKeys.splice(0, matchingKeys.length - offset);
|
|
||||||
} else {
|
|
||||||
matchingKeys = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// return if there are no matching keys
|
// return if there are no matching keys
|
||||||
if (matchingKeys.length === 0) {
|
if (matchingKeys.length === 0) {
|
||||||
callback(list);
|
callback(list);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get matching objects from data-store
|
// fetch all items from data-store with matching key
|
||||||
self.get(matchingKeys, function(matchingList) {
|
self.get(matchingKeys, function(matchingList) {
|
||||||
for (i = 0; i < matchingList.length; i++) {
|
for (i = 0; i < matchingList.length; i++) {
|
||||||
list.push(matchingList[i].object);
|
list.push(matchingList[i].object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sort items by date
|
||||||
|
if (list[0].sentDate) {
|
||||||
|
list = _.sortBy(list, function(item) {
|
||||||
|
var parts = item.sentDate.match(/(\d+)/g);
|
||||||
|
var date = new Date(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]);
|
||||||
|
return date.getTime();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// if num is null, list all items
|
||||||
|
num = (num !== null) ? num : list.length;
|
||||||
|
|
||||||
|
// set window of items to fetch
|
||||||
|
if (offset + num < list.length) {
|
||||||
|
list = list.splice(list.length - offset - num, num);
|
||||||
|
} else if (offset + num >= list.length && offset < list.length) {
|
||||||
|
list = list.splice(0, list.length - offset);
|
||||||
|
} else {
|
||||||
|
list = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// return only the interval between offset and num
|
||||||
callback(list);
|
callback(list);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -53,10 +53,11 @@ app.view.MessageListView = Backbone.View.extend({
|
|||||||
listItemArgs;
|
listItemArgs;
|
||||||
|
|
||||||
$.mobile.loading('show', { text: 'decrypting...', textVisible: true });
|
$.mobile.loading('show', { text: 'decrypting...', textVisible: true });
|
||||||
this.dao.listItems(this.folder, 0, 20, function(collection) {
|
this.dao.listItems(this.folder, 0, 10, function(collection) {
|
||||||
// clear list
|
// clear list
|
||||||
list.html('');
|
list.html('');
|
||||||
|
|
||||||
|
// append items to list
|
||||||
_.each(collection.models, function(email) {
|
_.each(collection.models, function(email) {
|
||||||
listItemArgs = {account: self.options.account, folder: self.folder, model: email};
|
listItemArgs = {account: self.options.account, folder: self.folder, model: email};
|
||||||
list.append(new app.view.MessageListItemView(listItemArgs).render().el);
|
list.append(new app.view.MessageListItemView(listItemArgs).render().el);
|
||||||
|
@ -13,7 +13,7 @@ var TestData = function() {
|
|||||||
from:'john@from.com',
|
from:'john@from.com',
|
||||||
to:['jack@to.com'],
|
to:['jack@to.com'],
|
||||||
subject: 'Important stuff ' + i,
|
subject: 'Important stuff ' + i,
|
||||||
sentDate: '15:00',
|
sentDate: (1900 + i) + '-03-13 18:17:53',
|
||||||
body: bigAssString
|
body: bigAssString
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ asyncTest("Init", 2, function() {
|
|||||||
emaildao_test.emailDao = new app.dao.EmailDAO(_, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub);
|
emaildao_test.emailDao = new app.dao.EmailDAO(_, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub);
|
||||||
|
|
||||||
// generate test data
|
// generate test data
|
||||||
emaildao_test.list = new TestData().getEmailCollection(10);
|
emaildao_test.list = new TestData().getEmailCollection(100);
|
||||||
|
|
||||||
var account = new app.model.Account({
|
var account = new app.model.Account({
|
||||||
emailAddress: emaildao_test.user,
|
emailAddress: emaildao_test.user,
|
||||||
@ -44,6 +44,11 @@ asyncTest("Persist test emails", 2, function() {
|
|||||||
emaildao_test.crypto.aesEncryptListForUser(emaildao_test.list.toJSON(), function(encryptedList) {
|
emaildao_test.crypto.aesEncryptListForUser(emaildao_test.list.toJSON(), function(encryptedList) {
|
||||||
equal(encryptedList.length, emaildao_test.list.length, 'Encrypt list');
|
equal(encryptedList.length, emaildao_test.list.length, 'Encrypt list');
|
||||||
|
|
||||||
|
// add sent date to encrypted items
|
||||||
|
for (var i = 0; i < encryptedList.length; i++) {
|
||||||
|
encryptedList[i].sentDate = emaildao_test.list.at(i).get('sentDate');
|
||||||
|
}
|
||||||
|
|
||||||
emaildao_test.storage.storeEcryptedList(encryptedList, 'email_inbox', function() {
|
emaildao_test.storage.storeEcryptedList(encryptedList, 'email_inbox', function() {
|
||||||
ok(true, 'Store encrypted list');
|
ok(true, 'Store encrypted list');
|
||||||
|
|
||||||
@ -55,9 +60,9 @@ asyncTest("Persist test emails", 2, function() {
|
|||||||
asyncTest("List Email models", 1, function() {
|
asyncTest("List Email models", 1, function() {
|
||||||
emaildao_test.emailDao.listItems('inbox', 0, emaildao_test.list.length, function(collection) {
|
emaildao_test.emailDao.listItems('inbox', 0, emaildao_test.list.length, function(collection) {
|
||||||
var gotten = collection.toJSON(),
|
var gotten = collection.toJSON(),
|
||||||
reference = emaildao_test.list.toJSON();
|
reference = emaildao_test.list.toJSON().reverse();
|
||||||
|
|
||||||
deepEqual(gotten, reference, 'Collection length');
|
deepEqual(gotten, reference, 'Compare collection');
|
||||||
|
|
||||||
start();
|
start();
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user