Move expiring message time earlier if read sync has earlier time

This commit is contained in:
Scott Nonnenberg 2018-08-06 11:31:41 -07:00
parent a01db40e27
commit 9ed1ee90f8
3 changed files with 38 additions and 20 deletions

View File

@ -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);
},

View File

@ -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;

View File

@ -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);
}
},
}))();
})();