From 9ed1ee90f84e11c891a9d287a7b6ec480c1200b7 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Mon, 6 Aug 2018 11:31:41 -0700 Subject: [PATCH] Move expiring message time earlier if read sync has earlier time --- js/models/conversations.js | 1 + js/models/messages.js | 4 +-- js/read_syncs.js | 53 +++++++++++++++++++++++++------------- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index aa8b9c3421ac..cba3f5e296f4 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -139,6 +139,7 @@ // Listening for out-of-band data updates this.on('delivered', this.updateAndMerge); this.on('read', this.updateAndMerge); + this.on('expiration-change', this.updateAndMerge); this.on('expired', this.onExpired); }, diff --git a/js/models/messages.js b/js/models/messages.js index 0c553b47250c..b8e485508990 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -1289,8 +1289,8 @@ } return msFromNow; }, - async setToExpire() { - if (this.isExpiring() && !this.get('expires_at')) { + async setToExpire(force = false) { + if (this.isExpiring() && (force || !this.get('expires_at'))) { const start = this.get('expirationStartTimestamp'); const delta = this.get('expireTimer') * 1000; const expiresAt = start + delta; diff --git a/js/read_syncs.js b/js/read_syncs.js index 81a0c0705b51..4732445c3509 100644 --- a/js/read_syncs.js +++ b/js/read_syncs.js @@ -1,4 +1,4 @@ -/* global Backbone, Whisper, ConversationController */ +/* global Backbone, Whisper */ /* eslint-disable more/no-then */ @@ -32,9 +32,7 @@ const message = messages.find( item => - item.isIncoming() && - item.isUnread() && - item.get('source') === receipt.get('sender') + item.isIncoming() && item.get('source') === receipt.get('sender') ); const notificationForMessage = message ? Whisper.Notifications.findWhere({ messageId: message.id }) @@ -59,11 +57,39 @@ return; } - await message.markRead(receipt.get('read_at')); - // This notification may result in messages older than this one being - // marked read. We want those messages to have the same expire timer - // start time as this one, so we pass the read_at value through. - this.notifyConversation(message, receipt.get('read_at')); + const readAt = receipt.get('read_at'); + + // If message is unread, we mark it read. Otherwise, we update the expiration + // timer to the time specified by the read sync if it's earlier than + // the previous read time. + if (message.isUnread()) { + await message.markRead(readAt); + + // onReadMessage may result in messages older than this one being + // marked read. We want those messages to have the same expire timer + // start time as this one, so we pass the readAt value through. + const conversation = message.getConversation(); + if (conversation) { + conversation.onReadMessage(message, readAt); + } + } else { + const now = Date.now(); + const existingTimestamp = message.get('expirationStartTimestamp'); + const expirationStartTimestamp = Math.min( + now, + Math.min(existingTimestamp || now, readAt || now) + ); + message.set({ expirationStartTimestamp }); + + const force = true; + await message.setToExpire(force); + + const conversation = message.getConversation(); + if (conversation) { + conversation.trigger('expiration-change', message); + } + } + this.remove(receipt); } catch (error) { window.log.error( @@ -72,14 +98,5 @@ ); } }, - notifyConversation(message, readAt) { - const conversation = ConversationController.get({ - id: message.get('conversationId'), - }); - - if (conversation) { - conversation.onReadMessage(message, readAt); - } - }, }))(); })();