Bulletproofing of markRead and findNewestVisibleUnread
FREEBIE
This commit is contained in:
parent
1775e97bcd
commit
ec22445f75
2 changed files with 10 additions and 19 deletions
|
@ -71,7 +71,7 @@
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
getUnread: function(newestUnreadDate) {
|
getUnread: function() {
|
||||||
var conversationId = this.id;
|
var conversationId = this.id;
|
||||||
var unreadMessages = new Whisper.MessageCollection();
|
var unreadMessages = new Whisper.MessageCollection();
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
|
@ -83,15 +83,7 @@
|
||||||
upper : [conversationId, Number.MAX_VALUE],
|
upper : [conversationId, Number.MAX_VALUE],
|
||||||
}
|
}
|
||||||
}).always(function() {
|
}).always(function() {
|
||||||
if (!newestUnreadDate) {
|
resolve(unreadMessages);
|
||||||
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;
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -306,8 +298,12 @@
|
||||||
conversationId: conversationId
|
conversationId: conversationId
|
||||||
}));
|
}));
|
||||||
|
|
||||||
this.getUnread(newestUnreadDate).then(function(unreadMessages) {
|
this.getUnread().then(function(unreadMessages) {
|
||||||
var read = unreadMessages.map(function(m) {
|
var oldUnread = unreadMessages.filter(function(message) {
|
||||||
|
return message.get('received_at') <= newestUnreadDate;
|
||||||
|
});
|
||||||
|
|
||||||
|
var read = _.map(oldUnread, function(m) {
|
||||||
if (this.messageCollection.get(m.id)) {
|
if (this.messageCollection.get(m.id)) {
|
||||||
m = this.messageCollection.get(m.id);
|
m = this.messageCollection.get(m.id);
|
||||||
} else {
|
} else {
|
||||||
|
@ -322,12 +318,7 @@
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
if (read.length > 0) {
|
if (read.length > 0) {
|
||||||
var unreadCount = this.get('unreadCount');
|
var unreadCount = unreadMessages.length - read.length;
|
||||||
unreadCount = unreadCount - read.length;
|
|
||||||
if (unreadCount < 0) {
|
|
||||||
console.log('conversation unreadCount went below zero!');
|
|
||||||
unreadCount = 0;
|
|
||||||
}
|
|
||||||
this.save({ unreadCount: unreadCount });
|
this.save({ unreadCount: unreadCount });
|
||||||
|
|
||||||
console.log('Sending', read.length, 'read receipts');
|
console.log('Sending', read.length, 'read receipts');
|
||||||
|
|
|
@ -427,7 +427,7 @@
|
||||||
var viewportBottom = this.view.outerHeight;
|
var viewportBottom = this.view.outerHeight;
|
||||||
var unreadCount = this.model.get('unreadCount');
|
var unreadCount = this.model.get('unreadCount');
|
||||||
|
|
||||||
if (unreadCount < 1) {
|
if (!unreadCount || unreadCount < 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue