Skip to content

Commit

Permalink
Bulletproofing of markRead and findNewestVisibleUnread
Browse files Browse the repository at this point in the history
FREEBIE
  • Loading branch information
scottnonnenberg committed Jun 1, 2017
1 parent 1775e97 commit ec22445
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
27 changes: 9 additions & 18 deletions js/models/conversations.js
Expand Up @@ -71,7 +71,7 @@
}.bind(this));
},

getUnread: function(newestUnreadDate) {
getUnread: function() {
var conversationId = this.id;
var unreadMessages = new Whisper.MessageCollection();
return new Promise(function(resolve) {
Expand All @@ -83,15 +83,7 @@
upper : [conversationId, Number.MAX_VALUE],
}
}).always(function() {
if (!newestUnreadDate) {
return resolve(unreadMessages);
}

// TODO: look into an index which would allow us to efficiently get the
// set of unread messages before a certain date.
resolve(unreadMessages.filter(function(message) {
return message.get('received_at') <= newestUnreadDate;
}));
resolve(unreadMessages);
});
});

Expand Down Expand Up @@ -306,8 +298,12 @@
conversationId: conversationId
}));

this.getUnread(newestUnreadDate).then(function(unreadMessages) {
var read = unreadMessages.map(function(m) {
this.getUnread().then(function(unreadMessages) {
var oldUnread = unreadMessages.filter(function(message) {
return message.get('received_at') <= newestUnreadDate;
});

var read = _.map(oldUnread, function(m) {
if (this.messageCollection.get(m.id)) {
m = this.messageCollection.get(m.id);
} else {
Expand All @@ -322,12 +318,7 @@
}.bind(this));

if (read.length > 0) {
var unreadCount = this.get('unreadCount');
unreadCount = unreadCount - read.length;
if (unreadCount < 0) {
console.log('conversation unreadCount went below zero!');
unreadCount = 0;
}
var unreadCount = unreadMessages.length - read.length;
this.save({ unreadCount: unreadCount });

console.log('Sending', read.length, 'read receipts');
Expand Down
2 changes: 1 addition & 1 deletion js/views/conversation_view.js
Expand Up @@ -427,7 +427,7 @@
var viewportBottom = this.view.outerHeight;
var unreadCount = this.model.get('unreadCount');

if (unreadCount < 1) {
if (!unreadCount || unreadCount < 1) {
return;
}

Expand Down

0 comments on commit ec22445

Please sign in to comment.