signal-desktop/ts/messageModifiers/ReadSyncs.ts

158 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2017 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable max-classes-per-file */
import { Collection, Model } from 'backbone';
import type { MessageModel } from '../models/messages';
import { isIncoming } from '../state/selectors/message';
import { isMessageUnread } from '../util/isMessageUnread';
2021-09-23 18:16:09 +00:00
import { notificationService } from '../services/notifications';
import * as log from '../logging/log';
import * as Errors from '../types/errors';
2023-01-13 00:24:59 +00:00
import { StartupQueue } from '../util/StartupQueue';
2023-04-11 03:54:43 +00:00
import { queueUpdateMessage } from '../util/messageBatcher';
export type ReadSyncAttributesType = {
senderId: string;
sender?: string;
senderUuid: string;
timestamp: number;
readAt: number;
};
class ReadSyncModel extends Model<ReadSyncAttributesType> {}
let singleton: ReadSyncs | undefined;
async function maybeItIsAReactionReadSync(sync: ReadSyncModel): Promise<void> {
const readReaction = await window.Signal.Data.markReactionAsRead(
sync.get('senderUuid'),
Number(sync.get('timestamp'))
);
if (!readReaction) {
log.info(
'Nothing found for read sync',
sync.get('senderId'),
sync.get('sender'),
sync.get('senderUuid'),
sync.get('timestamp')
);
return;
}
2021-09-23 18:16:09 +00:00
notificationService.removeBy({
conversationId: readReaction.conversationId,
emoji: readReaction.emoji,
targetAuthorUuid: readReaction.targetAuthorUuid,
targetTimestamp: readReaction.targetTimestamp,
});
}
export class ReadSyncs extends Collection {
static getSingleton(): ReadSyncs {
if (!singleton) {
singleton = new ReadSyncs();
}
return singleton;
}
forMessage(message: MessageModel): ReadSyncModel | null {
const sender = window.ConversationController.lookupOrCreate({
e164: message.get('source'),
uuid: message.get('sourceUuid'),
reason: 'ReadSyncs.forMessage',
});
const sync = this.find(item => {
return (
item.get('senderId') === sender?.id &&
item.get('timestamp') === message.get('sent_at')
);
});
if (sync) {
log.info(`Found early read sync for message ${sync.get('timestamp')}`);
this.remove(sync);
return sync;
}
return null;
}
async onSync(sync: ReadSyncModel): Promise<void> {
try {
2023-03-27 23:48:57 +00:00
const messages =
await window.Signal.Data.getMessagesIncludingEditedBySentAt(
sync.get('timestamp')
);
const found = messages.find(item => {
const sender = window.ConversationController.lookupOrCreate({
e164: item.source,
uuid: item.sourceUuid,
reason: 'ReadSyncs.onSync',
});
return isIncoming(item) && sender?.id === sync.get('senderId');
});
if (!found) {
await maybeItIsAReactionReadSync(sync);
return;
}
2021-09-23 18:16:09 +00:00
notificationService.removeBy({ messageId: found.id });
const message = window.MessageController.register(found.id, found);
2021-08-17 00:16:00 +00:00
const readAt = Math.min(sync.get('readAt'), Date.now());
// 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 (isMessageUnread(message.attributes)) {
// TODO DESKTOP-1509: use MessageUpdater.markRead once this is TS
message.markRead(readAt, { skipSave: true });
2023-02-24 19:03:17 +00:00
const updateConversation = async () => {
// 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.
void message.getConversation()?.onReadMessage(message, readAt);
};
2023-02-24 19:03:17 +00:00
// only available during initialization
if (StartupQueue.isAvailable()) {
const conversation = message.getConversation();
if (conversation) {
2023-01-13 00:24:59 +00:00
StartupQueue.add(
conversation.get('id'),
2021-08-17 00:16:00 +00:00
message.get('sent_at'),
updateConversation
);
}
} else {
2023-02-24 19:03:17 +00:00
// not awaiting since we don't want to block work happening in the
// eventHandlerQueue
void updateConversation();
}
} else {
const now = Date.now();
const existingTimestamp = message.get('expirationStartTimestamp');
const expirationStartTimestamp = Math.min(
now,
Math.min(existingTimestamp || now, readAt || now)
);
message.set({ expirationStartTimestamp });
}
2023-04-11 03:54:43 +00:00
queueUpdateMessage(message.attributes);
this.remove(sync);
} catch (error) {
log.error('ReadSyncs.onSync error:', Errors.toLogFormat(error));
}
}
}