2021-08-12 18:15:55 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// 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 { GiftBadgeStates } from '../components/conversation/Message';
|
2021-08-12 18:15:55 +00:00
|
|
|
import { ReadStatus } from '../messages/MessageReadStatus';
|
2023-08-21 20:08:27 +00:00
|
|
|
import { getMessageIdForLogging } from '../util/idForLogging';
|
|
|
|
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
|
2022-08-23 17:38:13 +00:00
|
|
|
import { isDownloaded } from '../types/Attachment';
|
2022-07-12 16:39:18 +00:00
|
|
|
import { isIncoming } from '../state/selectors/message';
|
2023-08-21 20:08:27 +00:00
|
|
|
import { markViewed } from '../services/MessageUpdater';
|
2021-09-23 18:16:09 +00:00
|
|
|
import { notificationService } from '../services/notifications';
|
2022-08-23 17:38:13 +00:00
|
|
|
import { queueAttachmentDownloads } from '../util/queueAttachmentDownloads';
|
2023-04-11 03:54:43 +00:00
|
|
|
import { queueUpdateMessage } from '../util/messageBatcher';
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type ViewSyncAttributesType = {
|
2023-08-21 20:08:27 +00:00
|
|
|
envelopeId: string;
|
|
|
|
removeFromMessageReceiverCache: () => unknown;
|
2023-08-10 16:43:33 +00:00
|
|
|
senderAci: AciString;
|
2023-08-21 20:08:27 +00:00
|
|
|
senderE164?: string;
|
|
|
|
senderId: string;
|
2021-08-12 18:15:55 +00:00
|
|
|
timestamp: number;
|
|
|
|
viewedAt: number;
|
|
|
|
};
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const viewSyncs = new Map<string, ViewSyncAttributesType>();
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
function remove(sync: ViewSyncAttributesType): void {
|
|
|
|
viewSyncs.delete(sync.envelopeId);
|
|
|
|
sync.removeFromMessageReceiverCache();
|
|
|
|
}
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export function forMessage(
|
|
|
|
message: MessageModel
|
|
|
|
): Array<ViewSyncAttributesType> {
|
|
|
|
const logId = `ViewSyncs.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 viewSyncValues = Array.from(viewSyncs.values());
|
|
|
|
|
|
|
|
const matchingSyncs = viewSyncValues.filter(item => {
|
|
|
|
return item.senderId === sender?.id && item.timestamp === messageTimestamp;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (matchingSyncs.length > 0) {
|
|
|
|
log.info(
|
|
|
|
`${logId}: Found ${matchingSyncs.length} early view sync(s) for message ${messageTimestamp}`
|
|
|
|
);
|
2021-08-12 18:15:55 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
matchingSyncs.forEach(sync => {
|
|
|
|
remove(sync);
|
|
|
|
});
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return matchingSyncs;
|
|
|
|
}
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export async function onSync(sync: ViewSyncAttributesType): Promise<void> {
|
|
|
|
viewSyncs.set(sync.envelopeId, sync);
|
|
|
|
|
|
|
|
const logId = `ViewSyncs.onSync(timestamp=${sync.timestamp})`;
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
try {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
sync.timestamp
|
|
|
|
);
|
2021-08-12 18:15:55 +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-08-12 18:15:55 +00:00
|
|
|
});
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return sender?.id === sync.senderId;
|
|
|
|
});
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (!found) {
|
|
|
|
log.info(
|
|
|
|
`${logId}: nothing found`,
|
|
|
|
sync.senderId,
|
|
|
|
sync.senderE164,
|
|
|
|
sync.senderAci
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
notificationService.removeBy({ messageId: found.id });
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const message = window.MessageController.register(found.id, found);
|
|
|
|
let didChangeMessage = false;
|
2022-08-23 17:38:13 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (message.get('readStatus') !== ReadStatus.Viewed) {
|
|
|
|
didChangeMessage = true;
|
|
|
|
message.set(markViewed(message.attributes, sync.viewedAt));
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const attachments = message.get('attachments');
|
|
|
|
if (!attachments?.every(isDownloaded)) {
|
|
|
|
const updatedFields = await queueAttachmentDownloads(
|
|
|
|
message.attributes
|
|
|
|
);
|
|
|
|
if (updatedFields) {
|
|
|
|
message.set(updatedFields);
|
|
|
|
}
|
2022-05-11 20:59:58 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2022-05-11 20:59:58 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const giftBadge = message.get('giftBadge');
|
|
|
|
if (giftBadge) {
|
|
|
|
didChangeMessage = true;
|
|
|
|
message.set({
|
|
|
|
giftBadge: {
|
|
|
|
...giftBadge,
|
|
|
|
state: isIncoming(message.attributes)
|
|
|
|
? GiftBadgeStates.Redeemed
|
|
|
|
: GiftBadgeStates.Opened,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-12-19 19:11:04 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (didChangeMessage) {
|
|
|
|
queueUpdateMessage(message.attributes);
|
2021-08-12 18:15:55 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
|
|
|
|
remove(sync);
|
|
|
|
} catch (error) {
|
|
|
|
remove(sync);
|
|
|
|
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
2021-08-12 18:15:55 +00:00
|
|
|
}
|
|
|
|
}
|