2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2017 Signal Messenger, LLC
|
2021-06-17 17:15:10 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
import type { AciString } from '../types/ServiceId';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MessageModel } from '../models/messages';
|
2023-08-21 20:08:27 +00:00
|
|
|
import * as Errors from '../types/errors';
|
|
|
|
import * as log from '../logging/log';
|
|
|
|
import { StartupQueue } from '../util/StartupQueue';
|
|
|
|
import { getMessageIdForLogging } from '../util/idForLogging';
|
|
|
|
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
|
2021-06-17 17:15:10 +00:00
|
|
|
import { isIncoming } from '../state/selectors/message';
|
2021-07-29 14:29:07 +00:00
|
|
|
import { isMessageUnread } from '../util/isMessageUnread';
|
2021-09-23 18:16:09 +00:00
|
|
|
import { notificationService } from '../services/notifications';
|
2023-04-11 03:54:43 +00:00
|
|
|
import { queueUpdateMessage } from '../util/messageBatcher';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type ReadSyncAttributesType = {
|
2023-08-21 20:08:27 +00:00
|
|
|
envelopeId: string;
|
|
|
|
readAt: number;
|
|
|
|
removeFromMessageReceiverCache: () => unknown;
|
2022-01-04 15:27:16 +00:00
|
|
|
sender?: string;
|
2023-08-10 16:43:33 +00:00
|
|
|
senderAci: AciString;
|
2023-08-21 20:08:27 +00:00
|
|
|
senderId: string;
|
2021-06-17 17:15:10 +00:00
|
|
|
timestamp: number;
|
|
|
|
};
|
|
|
|
|
2023-08-29 21:51:24 +00:00
|
|
|
const readSyncs = new Map<number, ReadSyncAttributesType>();
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
function remove(sync: ReadSyncAttributesType): void {
|
2023-08-29 21:51:24 +00:00
|
|
|
readSyncs.delete(sync.timestamp);
|
2023-08-21 20:08:27 +00:00
|
|
|
sync.removeFromMessageReceiverCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function maybeItIsAReactionReadSync(
|
|
|
|
sync: ReadSyncAttributesType
|
|
|
|
): Promise<void> {
|
|
|
|
const logId = `ReadSyncs.onSync(timestamp=${sync.timestamp})`;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
const readReaction = await window.Signal.Data.markReactionAsRead(
|
2023-08-21 20:08:27 +00:00
|
|
|
sync.senderAci,
|
|
|
|
Number(sync.timestamp)
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!readReaction) {
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-3 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
log.info(`${logId} not found:`, sync.senderId, sync.sender, sync.senderAci);
|
2021-06-17 17:15:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-4 ${sync.envelopeId}`);
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
remove(sync);
|
|
|
|
|
2021-09-23 18:16:09 +00:00
|
|
|
notificationService.removeBy({
|
2021-06-17 17:15:10 +00:00
|
|
|
conversationId: readReaction.conversationId,
|
|
|
|
emoji: readReaction.emoji,
|
2023-08-16 20:54:39 +00:00
|
|
|
targetAuthorAci: readReaction.targetAuthorAci,
|
2021-06-17 17:15:10 +00:00
|
|
|
targetTimestamp: readReaction.targetTimestamp,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export function forMessage(
|
|
|
|
message: MessageModel
|
|
|
|
): ReadSyncAttributesType | null {
|
|
|
|
const logId = `ReadSyncs.forMessage(${getMessageIdForLogging(
|
|
|
|
message.attributes
|
|
|
|
)})`;
|
|
|
|
|
|
|
|
const sender = window.ConversationController.lookupOrCreate({
|
|
|
|
e164: message.get('source'),
|
|
|
|
serviceId: message.get('sourceServiceId'),
|
|
|
|
reason: logId,
|
|
|
|
});
|
|
|
|
const messageTimestamp = getMessageSentTimestamp(message.attributes, {
|
|
|
|
log,
|
|
|
|
});
|
|
|
|
const readSyncValues = Array.from(readSyncs.values());
|
|
|
|
const foundSync = readSyncValues.find(item => {
|
|
|
|
return item.senderId === sender?.id && item.timestamp === messageTimestamp;
|
|
|
|
});
|
|
|
|
if (foundSync) {
|
|
|
|
log.info(
|
|
|
|
`${logId}: Found early read sync for message ${foundSync.timestamp}`
|
|
|
|
);
|
|
|
|
remove(foundSync);
|
|
|
|
return foundSync;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export async function onSync(sync: ReadSyncAttributesType): Promise<void> {
|
2023-08-29 21:51:24 +00:00
|
|
|
readSyncs.set(sync.timestamp, sync);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const logId = `ReadSyncs.onSync(timestamp=${sync.timestamp})`;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-1 ${sync.envelopeId}`);
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
try {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
sync.timestamp
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const found = messages.find(item => {
|
|
|
|
const sender = window.ConversationController.lookupOrCreate({
|
|
|
|
e164: item.source,
|
|
|
|
serviceId: item.sourceServiceId,
|
|
|
|
reason: logId,
|
2021-06-17 17:15:10 +00:00
|
|
|
});
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return isIncoming(item) && sender?.id === sync.senderId;
|
|
|
|
});
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (!found) {
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-2 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
await maybeItIsAReactionReadSync(sync);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-5 ${sync.envelopeId}`);
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
notificationService.removeBy({ messageId: found.id });
|
|
|
|
|
|
|
|
const message = window.MessageController.register(found.id, found);
|
|
|
|
const readAt = Math.min(sync.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)) {
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-6 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
// TODO DESKTOP-1509: use MessageUpdater.markRead once this is TS
|
|
|
|
message.markRead(readAt, { skipSave: true });
|
|
|
|
|
|
|
|
const updateConversation = async () => {
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-7 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
|
|
|
// only available during initialization
|
|
|
|
if (StartupQueue.isAvailable()) {
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-8 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
const conversation = message.getConversation();
|
|
|
|
if (conversation) {
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-9 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
StartupQueue.add(
|
|
|
|
conversation.get('id'),
|
|
|
|
message.get('sent_at'),
|
|
|
|
updateConversation
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-10 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
// not awaiting since we don't want to block work happening in the
|
|
|
|
// eventHandlerQueue
|
|
|
|
void updateConversation();
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
} else {
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-11 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
const now = Date.now();
|
|
|
|
const existingTimestamp = message.get('expirationStartTimestamp');
|
|
|
|
const expirationStartTimestamp = Math.min(
|
|
|
|
now,
|
|
|
|
Math.min(existingTimestamp || now, readAt || now)
|
|
|
|
);
|
|
|
|
message.set({ expirationStartTimestamp });
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-12 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
queueUpdateMessage(message.attributes);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
remove(sync);
|
|
|
|
} catch (error) {
|
2023-08-29 21:51:24 +00:00
|
|
|
log.info(`${logId}: ReadSync-13 ${sync.envelopeId}`);
|
2023-08-21 20:08:27 +00:00
|
|
|
remove(sync);
|
|
|
|
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|