2022-05-31 23:53:14 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-05-07 01:15:25 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-04-29 23:42:47 +00:00
|
|
|
import { omit } from 'lodash';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationAttributesType } from '../model-types.d';
|
2021-06-17 17:15:10 +00:00
|
|
|
import { hasErrors } from '../state/selectors/message';
|
2021-12-07 22:41:40 +00:00
|
|
|
import { readReceiptsJobQueue } from '../jobs/readReceiptsJobQueue';
|
2021-07-23 22:02:36 +00:00
|
|
|
import { readSyncJobQueue } from '../jobs/readSyncJobQueue';
|
2021-09-23 18:16:09 +00:00
|
|
|
import { notificationService } from '../services/notifications';
|
2022-05-31 23:53:14 +00:00
|
|
|
import { expiringMessagesDeletionService } from '../services/expiringMessagesDeletion';
|
|
|
|
import { tapToViewMessagesDeletionService } from '../services/tapToViewMessagesDeletionService';
|
2022-08-15 21:53:33 +00:00
|
|
|
import { isGroup, isDirectConversation } from './whatTypeOfConversation';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-04-22 18:35:14 +00:00
|
|
|
import { getConversationIdForLogging } from './idForLogging';
|
2022-11-09 01:33:25 +00:00
|
|
|
import { isConversationAccepted } from './isConversationAccepted';
|
2022-04-29 23:42:47 +00:00
|
|
|
import { ReadStatus } from '../messages/MessageReadStatus';
|
2021-05-07 01:15:25 +00:00
|
|
|
|
|
|
|
export async function markConversationRead(
|
|
|
|
conversationAttrs: ConversationAttributesType,
|
2021-12-08 19:52:46 +00:00
|
|
|
newestUnreadAt: number,
|
2022-04-22 18:35:14 +00:00
|
|
|
options: {
|
|
|
|
readAt?: number;
|
|
|
|
sendReadReceipts: boolean;
|
|
|
|
newestSentAt?: number;
|
|
|
|
} = {
|
2021-05-07 01:15:25 +00:00
|
|
|
sendReadReceipts: true,
|
|
|
|
}
|
2021-05-10 18:49:13 +00:00
|
|
|
): Promise<boolean> {
|
2021-05-07 01:15:25 +00:00
|
|
|
const { id: conversationId } = conversationAttrs;
|
|
|
|
|
|
|
|
const [unreadMessages, unreadReactions] = await Promise.all([
|
2021-12-08 19:52:46 +00:00
|
|
|
window.Signal.Data.getUnreadByConversationAndMarkRead({
|
2021-05-07 01:15:25 +00:00
|
|
|
conversationId,
|
2021-12-08 19:52:46 +00:00
|
|
|
newestUnreadAt,
|
|
|
|
readAt: options.readAt,
|
2022-09-30 00:57:11 +00:00
|
|
|
includeStoryReplies: !isGroup(conversationAttrs),
|
2021-12-08 19:52:46 +00:00
|
|
|
}),
|
|
|
|
window.Signal.Data.getUnreadReactionsAndMarkRead({
|
2021-05-07 01:15:25 +00:00
|
|
|
conversationId,
|
2021-12-08 19:52:46 +00:00
|
|
|
newestUnreadAt,
|
|
|
|
}),
|
2021-05-07 01:15:25 +00:00
|
|
|
]);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('markConversationRead', {
|
2022-04-22 18:35:14 +00:00
|
|
|
conversationId: getConversationIdForLogging(conversationAttrs),
|
|
|
|
newestSentAt: options.newestSentAt,
|
2021-12-08 19:52:46 +00:00
|
|
|
newestUnreadAt,
|
2021-05-17 16:52:09 +00:00
|
|
|
unreadMessages: unreadMessages.length,
|
|
|
|
unreadReactions: unreadReactions.length,
|
|
|
|
});
|
|
|
|
|
2021-05-10 18:49:13 +00:00
|
|
|
if (!unreadMessages.length && !unreadReactions.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-23 18:16:09 +00:00
|
|
|
notificationService.removeBy({ conversationId });
|
2021-05-17 16:52:09 +00:00
|
|
|
|
2021-05-07 01:15:25 +00:00
|
|
|
const unreadReactionSyncData = new Map<
|
|
|
|
string,
|
|
|
|
{
|
2021-07-15 23:48:09 +00:00
|
|
|
messageId?: string;
|
2021-05-07 01:15:25 +00:00
|
|
|
senderUuid?: string;
|
|
|
|
senderE164?: string;
|
|
|
|
timestamp: number;
|
|
|
|
}
|
|
|
|
>();
|
|
|
|
unreadReactions.forEach(reaction => {
|
|
|
|
const targetKey = `${reaction.targetAuthorUuid}/${reaction.targetTimestamp}`;
|
|
|
|
if (unreadReactionSyncData.has(targetKey)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unreadReactionSyncData.set(targetKey, {
|
2021-07-15 23:48:09 +00:00
|
|
|
messageId: reaction.messageId,
|
2021-05-07 01:15:25 +00:00
|
|
|
senderE164: undefined,
|
|
|
|
senderUuid: reaction.targetAuthorUuid,
|
|
|
|
timestamp: reaction.targetTimestamp,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const allReadMessagesSync = unreadMessages.map(messageSyncData => {
|
|
|
|
const message = window.MessageController.getById(messageSyncData.id);
|
|
|
|
// we update the in-memory MessageModel with the fresh database call data
|
|
|
|
if (message) {
|
2022-04-29 23:42:47 +00:00
|
|
|
message.set(omit(messageSyncData, 'originalReadStatus'));
|
2021-05-07 01:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-07-15 23:48:09 +00:00
|
|
|
messageId: messageSyncData.id,
|
2022-04-29 23:42:47 +00:00
|
|
|
originalReadStatus: messageSyncData.originalReadStatus,
|
2021-05-07 01:15:25 +00:00
|
|
|
senderE164: messageSyncData.source,
|
|
|
|
senderUuid: messageSyncData.sourceUuid,
|
2022-08-09 21:39:00 +00:00
|
|
|
senderId: window.ConversationController.lookupOrCreate({
|
2021-05-07 01:15:25 +00:00
|
|
|
e164: messageSyncData.source,
|
|
|
|
uuid: messageSyncData.sourceUuid,
|
2022-08-09 21:39:00 +00:00
|
|
|
})?.id,
|
2021-05-07 01:15:25 +00:00
|
|
|
timestamp: messageSyncData.sent_at,
|
2022-08-15 21:53:33 +00:00
|
|
|
isDirectConversation: isDirectConversation(conversationAttrs),
|
2021-06-17 17:15:10 +00:00
|
|
|
hasErrors: message ? hasErrors(message.attributes) : false,
|
2021-05-07 01:15:25 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-04-29 23:42:47 +00:00
|
|
|
// Some messages we're marking read are local notifications with no sender or were just
|
|
|
|
// unseen and not unread.
|
|
|
|
// Also, if a message has errors, we don't want to send anything out about it:
|
2021-05-07 01:15:25 +00:00
|
|
|
// read syncs - let's wait for a client that really understands the message
|
|
|
|
// to mark it read. we'll mark our local error read locally, though.
|
|
|
|
// read receipts - here we can run into infinite loops, where each time the
|
|
|
|
// conversation is viewed, another error message shows up for the contact
|
2021-05-10 18:49:13 +00:00
|
|
|
const unreadMessagesSyncData = allReadMessagesSync.filter(
|
2022-04-29 23:42:47 +00:00
|
|
|
item =>
|
|
|
|
Boolean(item.senderId) &&
|
|
|
|
item.originalReadStatus === ReadStatus.Unread &&
|
|
|
|
!item.hasErrors
|
2021-05-07 01:15:25 +00:00
|
|
|
);
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
const readSyncs: Array<{
|
|
|
|
messageId?: string;
|
|
|
|
senderE164?: string;
|
|
|
|
senderUuid?: string;
|
|
|
|
senderId?: string;
|
|
|
|
timestamp: number;
|
|
|
|
hasErrors?: string;
|
2021-08-31 16:47:15 +00:00
|
|
|
}> = [...unreadMessagesSyncData, ...unreadReactionSyncData.values()];
|
2021-05-07 01:15:25 +00:00
|
|
|
|
|
|
|
if (readSyncs.length && options.sendReadReceipts) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Sending ${readSyncs.length} read syncs`);
|
2021-05-07 01:15:25 +00:00
|
|
|
// Because syncReadMessages sends to our other devices, and sendReadReceipts goes
|
|
|
|
// to a contact, we need accessKeys for both.
|
2021-07-15 23:48:09 +00:00
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
'markConversationRead: We are primary device; not sending read syncs'
|
|
|
|
);
|
|
|
|
} else {
|
2021-07-23 22:02:36 +00:00
|
|
|
readSyncJobQueue.add({ readSyncs });
|
2021-07-15 23:48:09 +00:00
|
|
|
}
|
2021-05-07 01:15:25 +00:00
|
|
|
|
2022-11-09 01:33:25 +00:00
|
|
|
if (isConversationAccepted(conversationAttrs)) {
|
|
|
|
await readReceiptsJobQueue.addIfAllowedByUser(
|
|
|
|
window.storage,
|
|
|
|
allReadMessagesSync
|
|
|
|
);
|
|
|
|
}
|
2021-05-07 01:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 23:53:14 +00:00
|
|
|
expiringMessagesDeletionService.update();
|
|
|
|
tapToViewMessagesDeletionService.update();
|
2021-07-19 20:45:18 +00:00
|
|
|
|
2021-05-10 18:49:13 +00:00
|
|
|
return true;
|
2021-05-07 01:15:25 +00:00
|
|
|
}
|