Fix out of order edit message read syncs

This commit is contained in:
Josh Perez 2023-09-01 16:27:18 -04:00 committed by GitHub
parent cf28e2dc2c
commit 372d9c2198
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 28 deletions

View file

@ -6,6 +6,7 @@ import type { MessageModel } from '../models/messages';
import * as Errors from '../types/errors'; import * as Errors from '../types/errors';
import * as log from '../logging/log'; import * as log from '../logging/log';
import { StartupQueue } from '../util/StartupQueue'; import { StartupQueue } from '../util/StartupQueue';
import { drop } from '../util/drop';
import { getMessageIdForLogging } from '../util/idForLogging'; import { getMessageIdForLogging } from '../util/idForLogging';
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp'; import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
import { isIncoming } from '../state/selectors/message'; import { isIncoming } from '../state/selectors/message';
@ -41,13 +42,10 @@ async function maybeItIsAReactionReadSync(
); );
if (!readReaction) { if (!readReaction) {
log.info(`${logId}: ReadSync-3 ${sync.envelopeId}`);
log.info(`${logId} not found:`, sync.senderId, sync.sender, sync.senderAci); log.info(`${logId} not found:`, sync.senderId, sync.sender, sync.senderAci);
return; return;
} }
log.info(`${logId}: ReadSync-4 ${sync.envelopeId}`);
remove(sync); remove(sync);
notificationService.removeBy({ notificationService.removeBy({
@ -93,8 +91,6 @@ export async function onSync(sync: ReadSyncAttributesType): Promise<void> {
const logId = `ReadSyncs.onSync(timestamp=${sync.timestamp})`; const logId = `ReadSyncs.onSync(timestamp=${sync.timestamp})`;
log.info(`${logId}: ReadSync-1 ${sync.envelopeId}`);
try { try {
const messages = await window.Signal.Data.getMessagesBySentAt( const messages = await window.Signal.Data.getMessagesBySentAt(
sync.timestamp sync.timestamp
@ -111,13 +107,10 @@ export async function onSync(sync: ReadSyncAttributesType): Promise<void> {
}); });
if (!found) { if (!found) {
log.info(`${logId}: ReadSync-2 ${sync.envelopeId}`);
await maybeItIsAReactionReadSync(sync); await maybeItIsAReactionReadSync(sync);
return; return;
} }
log.info(`${logId}: ReadSync-5 ${sync.envelopeId}`);
notificationService.removeBy({ messageId: found.id }); notificationService.removeBy({ messageId: found.id });
const message = window.MessageController.register(found.id, found); const message = window.MessageController.register(found.id, found);
@ -127,24 +120,20 @@ export async function onSync(sync: ReadSyncAttributesType): Promise<void> {
// timer to the time specified by the read sync if it's earlier than // timer to the time specified by the read sync if it's earlier than
// the previous read time. // the previous read time.
if (isMessageUnread(message.attributes)) { if (isMessageUnread(message.attributes)) {
log.info(`${logId}: ReadSync-6 ${sync.envelopeId}`);
// TODO DESKTOP-1509: use MessageUpdater.markRead once this is TS // TODO DESKTOP-1509: use MessageUpdater.markRead once this is TS
message.markRead(readAt, { skipSave: true }); message.markRead(readAt, { skipSave: true });
const updateConversation = async () => { const updateConversation = async () => {
log.info(`${logId}: ReadSync-7 ${sync.envelopeId}`);
// onReadMessage may result in messages older than this one being // onReadMessage may result in messages older than this one being
// marked read. We want those messages to have the same expire timer // marked read. We want those messages to have the same expire timer
// start time as this one, so we pass the readAt value through. // start time as this one, so we pass the readAt value through.
void message.getConversation()?.onReadMessage(message, readAt); drop(message.getConversation()?.onReadMessage(message, readAt));
}; };
// only available during initialization // only available during initialization
if (StartupQueue.isAvailable()) { if (StartupQueue.isAvailable()) {
log.info(`${logId}: ReadSync-8 ${sync.envelopeId}`);
const conversation = message.getConversation(); const conversation = message.getConversation();
if (conversation) { if (conversation) {
log.info(`${logId}: ReadSync-9 ${sync.envelopeId}`);
StartupQueue.add( StartupQueue.add(
conversation.get('id'), conversation.get('id'),
message.get('sent_at'), message.get('sent_at'),
@ -152,13 +141,11 @@ export async function onSync(sync: ReadSyncAttributesType): Promise<void> {
); );
} }
} else { } else {
log.info(`${logId}: ReadSync-10 ${sync.envelopeId}`);
// not awaiting since we don't want to block work happening in the // not awaiting since we don't want to block work happening in the
// eventHandlerQueue // eventHandlerQueue
void updateConversation(); drop(updateConversation());
} }
} else { } else {
log.info(`${logId}: ReadSync-11 ${sync.envelopeId}`);
const now = Date.now(); const now = Date.now();
const existingTimestamp = message.get('expirationStartTimestamp'); const existingTimestamp = message.get('expirationStartTimestamp');
const expirationStartTimestamp = Math.min( const expirationStartTimestamp = Math.min(
@ -168,12 +155,10 @@ export async function onSync(sync: ReadSyncAttributesType): Promise<void> {
message.set({ expirationStartTimestamp }); message.set({ expirationStartTimestamp });
} }
log.info(`${logId}: ReadSync-12 ${sync.envelopeId}`);
queueUpdateMessage(message.attributes); queueUpdateMessage(message.attributes);
remove(sync); remove(sync);
} catch (error) { } catch (error) {
log.info(`${logId}: ReadSync-13 ${sync.envelopeId}`);
remove(sync); remove(sync);
log.error(`${logId} error:`, Errors.toLogFormat(error)); log.error(`${logId} error:`, Errors.toLogFormat(error));
} }

View file

@ -2915,6 +2915,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
return modifyTargetMessage(this, conversation, { return modifyTargetMessage(this, conversation, {
isFirstRun, isFirstRun,
skipEdits: false, skipEdits: false,
skipSave: false,
}); });
} }

View file

@ -326,8 +326,9 @@ export async function handleEditMessage(
drop(mainMessageConversation.updateLastMessage()); drop(mainMessageConversation.updateLastMessage());
// Apply any other operations, excluding edits that target this message // Apply any other operations, excluding edits that target this message
await modifyTargetMessage(mainMessageModel, mainMessageConversation, { await modifyTargetMessage(mainMessageModel, mainMessageConversation, {
isFirstRun: true, isFirstRun: false,
skipEdits: true, skipEdits: true,
skipSave: true,
}); });
} }

View file

@ -34,9 +34,13 @@ import { strictAssert } from './assert';
export async function modifyTargetMessage( export async function modifyTargetMessage(
message: MessageModel, message: MessageModel,
conversation: ConversationModel, conversation: ConversationModel,
options?: { isFirstRun: boolean; skipEdits: boolean } options?: { isFirstRun: boolean; skipEdits: boolean; skipSave: boolean }
): Promise<void> { ): Promise<void> {
const { isFirstRun = false, skipEdits = false } = options ?? {}; const {
isFirstRun = false,
skipEdits = false,
skipSave = false,
} = options ?? {};
const logId = `modifyTargetMessage/${message.idForLogging()}`; const logId = `modifyTargetMessage/${message.idForLogging()}`;
const type = message.get('type'); const type = message.get('type');
@ -111,13 +115,10 @@ export async function modifyTargetMessage(
const viewSyncs = ViewSyncs.forMessage(message); const viewSyncs = ViewSyncs.forMessage(message);
log.info(`${logId}: ReadSync-1`, { length: readSyncs.length });
const isGroupStoryReply = const isGroupStoryReply =
isGroup(conversation.attributes) && message.get('storyId'); isGroup(conversation.attributes) && message.get('storyId');
if (readSyncs.length !== 0 || viewSyncs.length !== 0) { if (readSyncs.length !== 0 || viewSyncs.length !== 0) {
log.info(`${logId}: ReadSync-2`);
const markReadAt = Math.min( const markReadAt = Math.min(
Date.now(), Date.now(),
...readSyncs.map(sync => sync.readAt), ...readSyncs.map(sync => sync.readAt),
@ -152,7 +153,6 @@ export async function modifyTargetMessage(
}); });
changed = true; changed = true;
log.info(`${logId}: ReadSync-3`);
message.setPendingMarkRead( message.setPendingMarkRead(
Math.min(message.getPendingMarkRead() ?? Date.now(), markReadAt) Math.min(message.getPendingMarkRead() ?? Date.now(), markReadAt)
); );
@ -161,12 +161,10 @@ export async function modifyTargetMessage(
!isGroupStoryReply && !isGroupStoryReply &&
canConversationBeUnarchived(conversation.attributes) canConversationBeUnarchived(conversation.attributes)
) { ) {
log.info(`${logId}: ReadSync-4`);
conversation.setArchived(false); conversation.setArchived(false);
} }
if (!isFirstRun && message.getPendingMarkRead()) { if (!isFirstRun && message.getPendingMarkRead()) {
log.info(`${logId}: ReadSync-5`);
const markReadAt = message.getPendingMarkRead(); const markReadAt = message.getPendingMarkRead();
message.setPendingMarkRead(undefined); message.setPendingMarkRead(undefined);
@ -263,7 +261,7 @@ export async function modifyTargetMessage(
); );
} }
if (changed && !isFirstRun) { if (!skipSave && changed && !isFirstRun) {
log.info(`${logId}: Changes in second run; saving.`); log.info(`${logId}: Changes in second run; saving.`);
await window.Signal.Data.saveMessage(message.attributes, { await window.Signal.Data.saveMessage(message.attributes, {
ourAci, ourAci,