Skip to content

Commit

Permalink
Recursively fetch messages until we've loaded all unread
Browse files Browse the repository at this point in the history
FREEBIE
  • Loading branch information
scottnonnenberg committed May 23, 2017
1 parent 533ec52 commit 30b7bf2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion js/models/conversations.js
Expand Up @@ -320,7 +320,7 @@

fetchMessages: function() {
if (!this.id) { return false; }
return this.messageCollection.fetchConversation(this.id);
return this.messageCollection.fetchConversation(this.id, null, this.get('unreadCount'));
},

fetchContacts: function(options) {
Expand Down
20 changes: 19 additions & 1 deletion js/models/messages.js
Expand Up @@ -524,10 +524,23 @@
}.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') {
limit = 100;
}
if (typeof unreadCount !== 'number') {
unreadCount = 0;
}
return new Promise(function(resolve) {
var upper;
if (this.length === 0) {
Expand All @@ -548,6 +561,11 @@
// received_at DESC
};
this.fetch(options).then(resolve);
}.bind(this)).then(function() {
var loadedUnread = this.getLoadedUnreadCount();
if (loadedUnread < unreadCount) {
return this.fetchConversation(conversationId, limit, unreadCount);
}
}.bind(this));
},

Expand Down

0 comments on commit 30b7bf2

Please sign in to comment.