From 02ea4f24752e98778b73f14f32e15b3d3307dda0 Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 21 Sep 2016 15:06:31 -0700 Subject: [PATCH] Use read receipt envelope to infer startExpirationTime Avoids display of phantom messages that are only received and marked read locally long after they have expired on another linked device. --- js/background.js | 11 ++++++++--- js/libtextsecure.js | 11 +++++++---- js/models/messages.js | 4 ++-- js/read_receipts.js | 2 +- libtextsecure/message_receiver.js | 11 +++++++---- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/js/background.js b/js/background.js index c8f34aeb..693943bc 100644 --- a/js/background.js +++ b/js/background.js @@ -247,10 +247,15 @@ } function onReadReceipt(ev) { - var timestamp = ev.timestamp.toNumber(); - var sender = ev.sender; + var read_at = ev.timestamp; + var timestamp = ev.read.timestamp; + var sender = ev.read.sender; console.log('read receipt ', sender, timestamp); - Whisper.ReadReceipts.add({sender: sender, timestamp: timestamp}); + Whisper.ReadReceipts.add({ + sender : sender, + timestamp : timestamp, + read_at : read_at + }); } // lazy hack diff --git a/js/libtextsecure.js b/js/libtextsecure.js index a8409ce6..6a5b4882 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38325,16 +38325,19 @@ MessageReceiver.prototype.extend({ } else if (syncMessage.read) { console.log('read messages', 'from', envelope.source + '.' + envelope.sourceDevice); - this.handleRead(syncMessage.read); + this.handleRead(syncMessage.read, envelope.timestamp); } else { throw new Error('Got empty SyncMessage'); } }, - handleRead: function(read) { + handleRead: function(read, timestamp) { for (var i = 0; i < read.length; ++i) { var ev = new Event('read'); - ev.timestamp = read[i].timestamp; - ev.sender = read[i].sender; + ev.timestamp = timestamp.toNumber(); + ev.read = { + timestamp : read[i].timestamp.toNumber(), + sender : read[i].sender + } this.dispatchEvent(ev); } }, diff --git a/js/models/messages.js b/js/models/messages.js index aaa83e12..e96f6829 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -374,10 +374,10 @@ }); }); }, - markRead: function() { + markRead: function(read_at) { this.unset('unread'); if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) { - this.set('expirationStartTimestamp', Date.now()); + this.set('expirationStartTimestamp', read_at || Date.now()); } Whisper.Notifications.remove(Whisper.Notifications.where({ messageId: this.id diff --git a/js/read_receipts.js b/js/read_receipts.js index 2473aba0..ca324da5 100644 --- a/js/read_receipts.js +++ b/js/read_receipts.js @@ -28,7 +28,7 @@ }); if (message) { this.remove(receipt); - message.markRead().then(function() { + message.markRead(receipt.get('read_at')).then(function() { var conversation = ConversationController.get({ id: message.get('conversationId') }); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 30015aa5..fd7fd618 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -254,16 +254,19 @@ MessageReceiver.prototype.extend({ } else if (syncMessage.read) { console.log('read messages', 'from', envelope.source + '.' + envelope.sourceDevice); - this.handleRead(syncMessage.read); + this.handleRead(syncMessage.read, envelope.timestamp); } else { throw new Error('Got empty SyncMessage'); } }, - handleRead: function(read) { + handleRead: function(read, timestamp) { for (var i = 0; i < read.length; ++i) { var ev = new Event('read'); - ev.timestamp = read[i].timestamp; - ev.sender = read[i].sender; + ev.timestamp = timestamp.toNumber(); + ev.read = { + timestamp : read[i].timestamp.toNumber(), + sender : read[i].sender + } this.dispatchEvent(ev); } },