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
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
import { z } from 'zod';
|
|
|
|
|
2024-07-25 23:29:49 +00:00
|
|
|
import type { ReadonlyMessageAttributesType } from '../model-types.d';
|
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';
|
2023-09-01 20:27:18 +00:00
|
|
|
import { drop } from '../util/drop';
|
2023-08-21 20:08:27 +00:00
|
|
|
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';
|
2023-09-27 02:42:40 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2024-06-17 19:24:39 +00:00
|
|
|
import { isAciString } from '../util/isAciString';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader, DataWriter } from '../sql/Client';
|
2024-06-17 19:24:39 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const { removeSyncTaskById } = DataWriter;
|
2024-06-17 19:24:39 +00:00
|
|
|
|
|
|
|
export const readSyncTaskSchema = z.object({
|
|
|
|
type: z.literal('ReadSync').readonly(),
|
|
|
|
readAt: z.number(),
|
|
|
|
sender: z.string().optional(),
|
|
|
|
senderAci: z.string().refine(isAciString),
|
|
|
|
senderId: z.string(),
|
|
|
|
timestamp: z.number(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type ReadSyncTaskType = z.infer<typeof readSyncTaskSchema>;
|
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;
|
2024-06-17 19:24:39 +00:00
|
|
|
syncTaskId: string;
|
|
|
|
readSync: ReadSyncTaskType;
|
2021-06-17 17:15:10 +00:00
|
|
|
};
|
|
|
|
|
2023-11-02 13:28:49 +00:00
|
|
|
const readSyncs = new Map<string, ReadSyncAttributesType>();
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
async function remove(sync: ReadSyncAttributesType): Promise<void> {
|
|
|
|
const { syncTaskId } = sync;
|
|
|
|
readSyncs.delete(syncTaskId);
|
|
|
|
await removeSyncTaskById(syncTaskId);
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function maybeItIsAReactionReadSync(
|
|
|
|
sync: ReadSyncAttributesType
|
|
|
|
): Promise<void> {
|
2024-06-17 19:24:39 +00:00
|
|
|
const { readSync } = sync;
|
|
|
|
const logId = `ReadSyncs.onSync(timestamp=${readSync.timestamp})`;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const readReaction = await DataWriter.markReactionAsRead(
|
2024-06-17 19:24:39 +00:00
|
|
|
readSync.senderAci,
|
|
|
|
Number(readSync.timestamp)
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
|
2023-09-28 22:05:24 +00:00
|
|
|
if (
|
|
|
|
!readReaction ||
|
|
|
|
readReaction?.targetAuthorAci !== window.storage.user.getCheckedAci()
|
|
|
|
) {
|
2024-06-17 19:24:39 +00:00
|
|
|
log.info(
|
|
|
|
`${logId} not found:`,
|
|
|
|
readSync.senderId,
|
|
|
|
readSync.sender,
|
|
|
|
readSync.senderAci
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-28 22:05:24 +00:00
|
|
|
log.info(
|
|
|
|
`${logId} read reaction sync found:`,
|
|
|
|
readReaction.conversationId,
|
2024-06-17 19:24:39 +00:00
|
|
|
readSync.senderId,
|
|
|
|
readSync.sender,
|
|
|
|
readSync.senderAci
|
2023-09-28 22:05:24 +00:00
|
|
|
);
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
await remove(sync);
|
2023-08-21 20:08:27 +00:00
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
export async function forMessage(
|
2024-07-25 23:29:49 +00:00
|
|
|
message: ReadonlyMessageAttributesType
|
2024-06-17 19:24:39 +00:00
|
|
|
): Promise<ReadSyncAttributesType | null> {
|
2024-07-25 23:29:49 +00:00
|
|
|
const logId = `ReadSyncs.forMessage(${getMessageIdForLogging(message)})`;
|
2023-08-21 20:08:27 +00:00
|
|
|
|
|
|
|
const sender = window.ConversationController.lookupOrCreate({
|
2024-07-25 23:29:49 +00:00
|
|
|
e164: message.source,
|
|
|
|
serviceId: message.sourceServiceId,
|
2023-08-21 20:08:27 +00:00
|
|
|
reason: logId,
|
|
|
|
});
|
2024-07-25 23:29:49 +00:00
|
|
|
const messageTimestamp = getMessageSentTimestamp(message, {
|
2023-08-21 20:08:27 +00:00
|
|
|
log,
|
|
|
|
});
|
|
|
|
const readSyncValues = Array.from(readSyncs.values());
|
|
|
|
const foundSync = readSyncValues.find(item => {
|
2024-06-17 19:24:39 +00:00
|
|
|
const { readSync } = item;
|
|
|
|
return (
|
|
|
|
readSync.senderId === sender?.id &&
|
|
|
|
readSync.timestamp === messageTimestamp
|
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
});
|
|
|
|
if (foundSync) {
|
|
|
|
log.info(
|
2024-06-17 19:24:39 +00:00
|
|
|
`${logId}: Found early read sync for message ${foundSync.readSync.timestamp}`
|
2023-08-21 20:08:27 +00:00
|
|
|
);
|
2024-06-17 19:24:39 +00:00
|
|
|
await remove(foundSync);
|
2023-08-21 20:08:27 +00:00
|
|
|
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> {
|
2024-06-17 19:24:39 +00:00
|
|
|
const { readSync, syncTaskId } = sync;
|
|
|
|
|
|
|
|
readSyncs.set(syncTaskId, sync);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
const logId = `ReadSyncs.onSync(timestamp=${readSync.timestamp})`;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
try {
|
2024-07-22 18:16:33 +00:00
|
|
|
const messages = await DataReader.getMessagesBySentAt(readSync.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
|
|
|
});
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
return isIncoming(item) && sender?.id === readSync.senderId;
|
2023-08-21 20:08:27 +00:00
|
|
|
});
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (!found) {
|
|
|
|
await maybeItIsAReactionReadSync(sync);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationService.removeBy({ messageId: found.id });
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$register(
|
|
|
|
found.id,
|
|
|
|
found,
|
|
|
|
'ReadSyncs.onSync'
|
|
|
|
);
|
2024-06-17 19:24:39 +00:00
|
|
|
const readAt = Math.min(readSync.readAt, Date.now());
|
|
|
|
const newestSentAt = readSync.timestamp;
|
2023-08-21 20:08:27 +00:00
|
|
|
|
|
|
|
// 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 });
|
|
|
|
|
|
|
|
const updateConversation = async () => {
|
2023-09-27 02:42:40 +00:00
|
|
|
const conversation = message.getConversation();
|
|
|
|
strictAssert(conversation, `${logId}: conversation not found`);
|
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.
|
2024-07-25 23:29:49 +00:00
|
|
|
drop(
|
|
|
|
conversation.onReadMessage(message.attributes, readAt, newestSentAt)
|
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// only available during initialization
|
|
|
|
if (StartupQueue.isAvailable()) {
|
|
|
|
const conversation = message.getConversation();
|
2023-09-27 02:42:40 +00:00
|
|
|
strictAssert(
|
|
|
|
conversation,
|
|
|
|
`${logId}: conversation not found (StartupQueue)`
|
|
|
|
);
|
|
|
|
StartupQueue.add(
|
|
|
|
conversation.get('id'),
|
|
|
|
message.get('sent_at'),
|
|
|
|
updateConversation
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
} else {
|
2023-08-21 20:08:27 +00:00
|
|
|
// not awaiting since we don't want to block work happening in the
|
|
|
|
// eventHandlerQueue
|
2023-09-01 20:27:18 +00:00
|
|
|
drop(updateConversation());
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
} else {
|
2023-09-27 02:42:40 +00:00
|
|
|
log.info(`${logId}: updating expiration`);
|
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-21 20:08:27 +00:00
|
|
|
queueUpdateMessage(message.attributes);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
await remove(sync);
|
2023-08-21 20:08:27 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
2024-06-17 19:24:39 +00:00
|
|
|
await remove(sync);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|