diff --git a/background.html b/background.html index a715e64e8..4fde50b82 100644 --- a/background.html +++ b/background.html @@ -395,6 +395,7 @@ + diff --git a/js/background.js b/js/background.js index 828131dea..18d1b2cfd 100644 --- a/js/background.js +++ b/js/background.js @@ -245,31 +245,8 @@ function onReadReceipt(ev) { var timestamp = ev.timestamp.toNumber(); var sender = ev.sender; - var messages = new Whisper.MessageCollection(); - var groups = new Whisper.ConversationCollection(); console.log('read receipt ', sender, timestamp); - groups.fetchGroups(sender).then(function() { - messages.fetchSentAt(timestamp).then(function() { - var ids = groups.pluck('id'); - ids.push(sender); - var message = messages.find(function(message) { - return (message.isIncoming() && message.isUnread() && - _.contains(ids, message.get('conversationId'))); - }); - if (message) { - message.markRead().then(function() { - var conversation = ConversationController.get({ - id: message.get('conversationId') - }); - - if (conversation) { - // notify frontend listeners - conversation.trigger('read', message); - } - }); - } - }); - }); + Whisper.ReadReceipts.add({sender: sender, timestamp: timestamp}); } // lazy hack diff --git a/js/models/messages.js b/js/models/messages.js index 6e6b4fe9f..2c39b390e 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -326,7 +326,12 @@ } attributes.active_at = now; if (type === 'incoming') { - attributes.unreadCount = conversation.get('unreadCount') + 1; + // experimental + if (Whisper.ReadReceipts.forMessage(message)) { + message.unset('unread'); + } else { + attributes.unreadCount = conversation.get('unreadCount') + 1; + } } conversation.set(attributes); diff --git a/js/read_receipts.js b/js/read_receipts.js new file mode 100644 index 000000000..2473aba06 --- /dev/null +++ b/js/read_receipts.js @@ -0,0 +1,47 @@ +/* + * vim: ts=4:sw=4:expandtab + */ +;(function() { + 'use strict'; + window.Whisper = window.Whisper || {}; + Whisper.ReadReceipts = new (Backbone.Collection.extend({ + initialize: function() { + this.on('add', this.onReceipt); + }, + forMessage: function(message) { + var receipt = this.findWhere({ + sender: message.get('source'), + timestamp: message.get('sent_at') + }); + if (receipt) { + console.log('Found early read receipt for message'); + this.remove(receipt); + return receipt; + } + }, + onReceipt: function(receipt) { + var messages = new Whisper.MessageCollection(); + messages.fetchSentAt(receipt.get('timestamp')).then(function() { + var message = messages.find(function(message) { + return (message.isIncoming() && message.isUnread() && + message.get('source') === receipt.get('sender')); + }); + if (message) { + this.remove(receipt); + message.markRead().then(function() { + var conversation = ConversationController.get({ + id: message.get('conversationId') + }); + + if (conversation) { + // notify frontend listeners + conversation.trigger('read', message); + } + }); + } else { + console.log('No message for read receipt'); + } + }.bind(this)); + } + }))(); +})();