Track and sync unread messages

// FREEBIE
This commit is contained in:
lilia 2016-02-21 21:37:47 -08:00
parent 1be45f3775
commit 1f897f32b7
4 changed files with 78 additions and 7 deletions

View file

@ -44,6 +44,31 @@
this.on('change:avatar', this.updateAvatarUrl);
this.on('destroy', this.revokeAvatarUrl);
this.on('read', this.countUnread);
},
countUnread: function() {
return this.getUnread().then(function(unreadMessages) {
this.save({unreadCount: unreadMessages.length});
}.bind(this));
},
getUnread: function() {
var conversationId = this.id;
var unreadMessages = new Whisper.MessageCollection();
return new Promise(function(resolve) {
return unreadMessages.fetch({
index: {
// 'unread' index
name: 'unread',
lower: [conversationId, true],
upper: [conversationId, true]
}
}).always(function() {
resolve(unreadMessages);
});
});
},
validate: function(attributes, options) {
@ -182,13 +207,22 @@
markRead: function() {
if (this.get('unreadCount') > 0) {
this.save({unreadCount: 0});
this.save({ unreadCount: 0 });
var conversationId = this.id;
Whisper.Notifications.remove(
Whisper.Notifications.models.filter(
function(model) {
return model.attributes.conversationId===conversationId;
}));
Whisper.Notifications.remove(Whisper.Notifications.where({
conversationId: conversationId
}));
var readReceipts = this.messageCollection.where({
type: 'incoming', unread: true
}).map(function(m) {
m.markRead();
return {
sender: m.get('source'),
timestamp: m.get('sent_at')
};
}.bind(this));
textsecure.messaging.sendReadReceipts(readReceipts);
}
},