diff --git a/js/expiring_messages.js b/js/expiring_messages.js index 1b019efef..bf4d35592 100644 --- a/js/expiring_messages.js +++ b/js/expiring_messages.js @@ -5,7 +5,7 @@ function destroyExpiredMessages() { // Load messages that have expired and destroy them var expired = new Whisper.MessageCollection(); - expired.on('add', function(message) { + expired.on('add', async function(message) { console.log('Message expired', { sentAt: message.get('sent_at'), }); @@ -16,7 +16,10 @@ // We delete after the trigger to allow the conversation time to process // the expiration before the message is removed from the database. - message.destroy(); + await wrapDeferred(message.destroy()); + if (conversation) { + conversation.updateLastMessage(); + } }); expired.on('reset', throttledCheckExpiringMessages); diff --git a/js/models/conversations.js b/js/models/conversations.js index 9acfa9b06..26a63e6ba 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -107,12 +107,42 @@ this.on('change:profileAvatar', this.updateAvatarUrl); this.on('change:profileKey', this.onChangeProfileKey); this.on('destroy', this.revokeAvatarUrl); + + this.on('expired', this.onExpired); + this.listenTo( + this.messageCollection, + 'expired', + this.onExpiredCollection + ); }, isMe() { return this.id === this.ourNumber; }, + onExpired(message) { + const mine = this.messageCollection.get(message.id); + if (mine && mine.cid !== message.cid) { + mine.trigger('expired', mine); + } + }, + async onExpiredCollection(message) { + console.log('onExpiredCollection', message.attributes); + const removeMessage = () => { + console.log('Remove expired message from collection', { + sentAt: message.get('sent_at'), + }); + this.messageCollection.remove(message.id); + }; + + // If a fetch is in progress, then we need to wait until that's complete to + // do this removal. Otherwise we could remove from messageCollection, then + // the async database fetch could include the removed message. + + await this.inProgressFetch; + removeMessage(); + }, + addSingleMessage(message) { const model = this.messageCollection.add(message, { merge: true }); this.processQuotes(this.messageCollection); @@ -1462,12 +1492,15 @@ throw new Error('This conversation has no id!'); } - await this.messageCollection.fetchConversation( + this.inProgressFetch = this.messageCollection.fetchConversation( this.id, null, this.get('unreadCount') ); + await this.inProgressFetch; + this.inProgressFetch = null; + // We kick this process off, but don't wait for it. If async updates happen on a // given Message, 'change' will be triggered this.processQuotes(this.messageCollection); diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 11fa29e07..893c6f038 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -110,13 +110,7 @@ this.listenTo(this.model, 'delivered', this.updateMessage); this.listenTo(this.model, 'read', this.updateMessage); this.listenTo(this.model, 'opened', this.onOpened); - this.listenTo(this.model, 'expired', this.onExpired); this.listenTo(this.model, 'prune', this.onPrune); - this.listenTo( - this.model.messageCollection, - 'expired', - this.onExpiredCollection - ); this.listenTo( this.model.messageCollection, 'scroll-to-message', @@ -807,28 +801,6 @@ return this.inProgressFetch; }, - onExpired(message) { - const mine = this.model.messageCollection.get(message.id); - if (mine && mine.cid !== message.cid) { - mine.trigger('expired', mine); - } - }, - async onExpiredCollection(message) { - const removeMessage = () => { - console.log('Remove expired message from message collection', { - sentAt: message.get('sent_at'), - }); - this.model.messageCollection.remove(message.id); - }; - - // If a fetch is in progress, then we need to wait until that's complete to - // do this removal. Otherwise we could remove from messageCollection, then - // the async database fetch could include the removed message. - - await this.inProgressFetch; - removeMessage(); - }, - addMessage(message) { // This is debounced, so it won't hit the database too often. this.lazyUpdateVerified();