Recursively fetch messages until we've loaded all unread

FREEBIE
This commit is contained in:
Scott Nonnenberg 2017-05-19 14:06:56 -07:00
parent 533ec52d0c
commit 30b7bf23db
2 changed files with 20 additions and 2 deletions

View file

@ -320,7 +320,7 @@
fetchMessages: function() { fetchMessages: function() {
if (!this.id) { return false; } if (!this.id) { return false; }
return this.messageCollection.fetchConversation(this.id); return this.messageCollection.fetchConversation(this.id, null, this.get('unreadCount'));
}, },
fetchContacts: function(options) { fetchContacts: function(options) {

View file

@ -524,10 +524,23 @@
}.bind(this)); }.bind(this));
}, },
fetchConversation: function(conversationId, limit) { getLoadedUnreadCount: function() {
return this.models.reduce(function(total, model) {
var count = model.get('unread');
if (count === undefined) {
count = 0;
}
return total + count;
}, 0);
},
fetchConversation: function(conversationId, limit, unreadCount) {
if (typeof limit !== 'number') { if (typeof limit !== 'number') {
limit = 100; limit = 100;
} }
if (typeof unreadCount !== 'number') {
unreadCount = 0;
}
return new Promise(function(resolve) { return new Promise(function(resolve) {
var upper; var upper;
if (this.length === 0) { if (this.length === 0) {
@ -548,6 +561,11 @@
// received_at DESC // received_at DESC
}; };
this.fetch(options).then(resolve); this.fetch(options).then(resolve);
}.bind(this)).then(function() {
var loadedUnread = this.getLoadedUnreadCount();
if (loadedUnread < unreadCount) {
return this.fetchConversation(conversationId, limit, unreadCount);
}
}.bind(this)); }.bind(this));
}, },