diff --git a/js/models/conversations.js b/js/models/conversations.js index 7a32e78352..59e0aa60c8 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -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) { diff --git a/js/models/messages.js b/js/models/messages.js index 7952df6b11..46e9b1cb6d 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -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) { @@ -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)); },