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';
|
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';
|
2023-11-02 13:28:49 +00:00
|
|
|
import { generateCacheKey } from './generateCacheKey';
|
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-11-02 13:28:49 +00:00
|
|
|
const readSyncs = new Map<string, ReadSyncAttributesType>();
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
function remove(sync: ReadSyncAttributesType): void {
|
2023-11-02 13:28:49 +00:00
|
|
|
readSyncs.delete(
|
|
|
|
generateCacheKey({
|
|
|
|
sender: sync.senderId,
|
|
|
|
timestamp: sync.timestamp,
|
2023-11-20 21:25:49 +00:00
|
|
|
type: 'readsync',
|
2023-11-02 13:28:49 +00:00
|
|
|
})
|
|
|
|
);
|
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
|
|
|
);
|
|
|
|
|
2023-09-28 22:05:24 +00:00
|
|
|
if (
|
|
|
|
!readReaction ||
|
|
|
|
readReaction?.targetAuthorAci !== window.storage.user.getCheckedAci()
|
|
|
|
) {
|
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-09-28 22:05:24 +00:00
|
|
|
log.info(
|
|
|
|
`${logId} read reaction sync found:`,
|
|
|
|
readReaction.conversationId,
|
|
|
|
sync.senderId,
|
|
|
|
sync.sender,
|
|
|
|
sync.senderAci
|
|
|
|
);
|
|
|
|
|
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-11-02 13:28:49 +00:00
|
|
|
readSyncs.set(
|
|
|
|
generateCacheKey({
|
|
|
|
sender: sync.senderId,
|
|
|
|
timestamp: sync.timestamp,
|
2023-11-20 21:25:49 +00:00
|
|
|
type: 'readsync',
|
2023-11-02 13:28:49 +00:00
|
|
|
}),
|
|
|
|
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-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) {
|
|
|
|
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'
|
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
const readAt = Math.min(sync.readAt, Date.now());
|
2023-09-13 14:42:33 +00:00
|
|
|
const newestSentAt = sync.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.
|
2023-09-27 02:42:40 +00:00
|
|
|
drop(conversation.onReadMessage(message, 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
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
remove(sync);
|
|
|
|
} catch (error) {
|
|
|
|
remove(sync);
|
|
|
|
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|