2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2016 Signal Messenger, LLC
|
2021-07-20 20:17:25 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
import { z } from 'zod';
|
2023-12-15 18:33:02 +00:00
|
|
|
import { groupBy } from 'lodash';
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2024-07-25 23:29:49 +00:00
|
|
|
import type {
|
|
|
|
MessageAttributesType,
|
|
|
|
ReadonlyMessageAttributesType,
|
|
|
|
} from '../model-types.d';
|
2023-08-15 23:24:19 +00:00
|
|
|
import type { SendStateByConversationId } from '../messages/MessageSendState';
|
2022-04-21 00:29:37 +00:00
|
|
|
import { isOutgoing, isStory } from '../state/selectors/message';
|
2021-07-20 20:17:25 +00:00
|
|
|
import { getOwn } from '../util/getOwn';
|
|
|
|
import { missingCaseError } from '../util/missingCaseError';
|
2021-08-31 21:35:01 +00:00
|
|
|
import { createWaitBatcher } from '../util/waitBatcher';
|
2024-06-17 19:24:39 +00:00
|
|
|
import { isServiceIdString } from '../types/ServiceId';
|
2021-07-28 18:53:18 +00:00
|
|
|
import {
|
|
|
|
SendActionType,
|
|
|
|
SendStatus,
|
2023-12-08 20:35:31 +00:00
|
|
|
UNDELIVERED_SEND_STATUSES,
|
2021-07-28 18:53:18 +00:00
|
|
|
sendStateReducer,
|
|
|
|
} from '../messages/MessageSendState';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader, DataWriter } from '../sql/Client';
|
2021-08-31 21:35:01 +00:00
|
|
|
import type { DeleteSentProtoRecipientOptionsType } from '../sql/Interface';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2023-08-16 20:54:39 +00:00
|
|
|
import { getSourceServiceId } from '../messages/helpers';
|
2023-05-16 17:37:12 +00:00
|
|
|
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
|
2023-08-21 20:08:27 +00:00
|
|
|
import { getMessageIdForLogging } from '../util/idForLogging';
|
2023-12-08 20:35:31 +00:00
|
|
|
import { getPropForTimestamp } from '../util/editHelpers';
|
2023-12-15 18:33:02 +00:00
|
|
|
import {
|
|
|
|
DELETE_SENT_PROTO_BATCHER_WAIT_MS,
|
|
|
|
RECEIPT_BATCHER_WAIT_MS,
|
|
|
|
} from '../types/Receipt';
|
|
|
|
import { drop } from '../util/drop';
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const { deleteSentProtoRecipient, removeSyncTaskById } = DataWriter;
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
export const messageReceiptTypeSchema = z.enum(['Delivery', 'Read', 'View']);
|
|
|
|
|
|
|
|
export type MessageReceiptType = z.infer<typeof messageReceiptTypeSchema>;
|
|
|
|
|
|
|
|
export const receiptSyncTaskSchema = z.object({
|
|
|
|
messageSentAt: z.number(),
|
|
|
|
receiptTimestamp: z.number(),
|
|
|
|
sourceConversationId: z.string(),
|
|
|
|
sourceDevice: z.number(),
|
|
|
|
sourceServiceId: z.string().refine(isServiceIdString),
|
|
|
|
type: messageReceiptTypeSchema,
|
|
|
|
wasSentEncrypted: z.boolean(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type ReceiptSyncTaskType = z.infer<typeof receiptSyncTaskSchema>;
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type MessageReceiptAttributesType = {
|
2023-08-21 20:08:27 +00:00
|
|
|
envelopeId: string;
|
2024-06-17 19:24:39 +00:00
|
|
|
syncTaskId: string;
|
|
|
|
receiptSync: ReceiptSyncTaskType;
|
2021-07-20 20:17:25 +00:00
|
|
|
};
|
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
const cachedReceipts = new Map<string, MessageReceiptAttributesType>();
|
|
|
|
|
|
|
|
const processReceiptBatcher = createWaitBatcher({
|
|
|
|
name: 'processReceiptBatcher',
|
|
|
|
wait: RECEIPT_BATCHER_WAIT_MS,
|
|
|
|
maxSize: 250,
|
|
|
|
async processBatch(receipts: Array<MessageReceiptAttributesType>) {
|
|
|
|
// First group by sentAt, so that we can find the target message
|
|
|
|
const receiptsByMessageSentAt = groupBy(
|
|
|
|
receipts,
|
2024-06-17 19:24:39 +00:00
|
|
|
receipt => receipt.receiptSync.messageSentAt
|
2023-12-15 18:33:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Once we find the message, we'll group them by messageId to process
|
|
|
|
// all receipts for a given message
|
|
|
|
const receiptsByMessageId: Map<
|
|
|
|
string,
|
|
|
|
Array<MessageReceiptAttributesType>
|
|
|
|
> = new Map();
|
|
|
|
|
|
|
|
function addReceiptAndTargetMessage(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
receipt: MessageReceiptAttributesType
|
|
|
|
): void {
|
|
|
|
const existing = receiptsByMessageId.get(message.id);
|
|
|
|
if (!existing) {
|
|
|
|
window.MessageCache.toMessageAttributes(message);
|
|
|
|
receiptsByMessageId.set(message.id, [receipt]);
|
|
|
|
} else {
|
|
|
|
existing.push(receipt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const receiptsForMessageSentAt of Object.values(
|
|
|
|
receiptsByMessageSentAt
|
|
|
|
)) {
|
|
|
|
if (!receiptsForMessageSentAt.length) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// All receipts have the same sentAt, so we can grab it from the first
|
2024-06-17 19:24:39 +00:00
|
|
|
const sentAt = receiptsForMessageSentAt[0].receiptSync.messageSentAt;
|
2023-12-15 18:33:02 +00:00
|
|
|
|
|
|
|
const messagesMatchingTimestamp =
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataReader.getMessagesBySentAt(sentAt);
|
2023-12-15 18:33:02 +00:00
|
|
|
|
2023-12-19 14:57:15 +00:00
|
|
|
if (messagesMatchingTimestamp.length === 0) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2024-07-22 18:16:33 +00:00
|
|
|
const reaction = await DataReader.getReactionByTimestamp(
|
2023-12-19 14:57:15 +00:00
|
|
|
window.ConversationController.getOurConversationIdOrThrow(),
|
|
|
|
sentAt
|
|
|
|
);
|
|
|
|
|
|
|
|
if (reaction) {
|
|
|
|
for (const receipt of receiptsForMessageSentAt) {
|
2024-06-17 19:24:39 +00:00
|
|
|
const { receiptSync } = receipt;
|
2023-12-19 14:57:15 +00:00
|
|
|
log.info(
|
|
|
|
'MesageReceipts.processReceiptBatcher: Got receipt for reaction',
|
2024-06-17 19:24:39 +00:00
|
|
|
receiptSync.messageSentAt,
|
|
|
|
receiptSync.type,
|
|
|
|
receiptSync.sourceConversationId,
|
|
|
|
receiptSync.sourceServiceId
|
2023-12-19 14:57:15 +00:00
|
|
|
);
|
2024-06-17 19:24:39 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await remove(receipt);
|
2023-12-19 14:57:15 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
for (const receipt of receiptsForMessageSentAt) {
|
|
|
|
const targetMessage = getTargetMessage({
|
2024-06-17 19:24:39 +00:00
|
|
|
sourceConversationId: receipt.receiptSync.sourceConversationId,
|
2023-12-15 18:33:02 +00:00
|
|
|
targetTimestamp: sentAt,
|
|
|
|
messagesMatchingTimestamp,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (targetMessage) {
|
|
|
|
addReceiptAndTargetMessage(targetMessage, receipt);
|
|
|
|
} else {
|
|
|
|
// We didn't find any messages but maybe it's a story sent message
|
|
|
|
const targetMessages = messagesMatchingTimestamp.filter(
|
|
|
|
item =>
|
|
|
|
item.storyDistributionListId &&
|
|
|
|
item.sendStateByConversationId &&
|
|
|
|
!item.deletedForEveryone &&
|
|
|
|
Boolean(
|
2024-06-17 19:24:39 +00:00
|
|
|
item.sendStateByConversationId[
|
|
|
|
receipt.receiptSync.sourceConversationId
|
|
|
|
]
|
2023-12-15 18:33:02 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (targetMessages.length) {
|
|
|
|
targetMessages.forEach(msg =>
|
|
|
|
addReceiptAndTargetMessage(msg, receipt)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Nope, no target message was found
|
2024-06-17 19:24:39 +00:00
|
|
|
const { receiptSync } = receipt;
|
2023-12-15 18:33:02 +00:00
|
|
|
log.info(
|
|
|
|
'MessageReceipts.processReceiptBatcher: No message for receipt',
|
2024-06-17 19:24:39 +00:00
|
|
|
receiptSync.messageSentAt,
|
|
|
|
receiptSync.type,
|
|
|
|
receiptSync.sourceConversationId,
|
|
|
|
receiptSync.sourceServiceId
|
2023-12-15 18:33:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:27:18 +00:00
|
|
|
await Promise.all(
|
|
|
|
[...receiptsByMessageId.entries()].map(
|
|
|
|
([messageId, receiptsForMessage]) => {
|
|
|
|
return processReceiptsForMessage(messageId, receiptsForMessage);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2023-12-15 18:33:02 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
async function processReceiptsForMessage(
|
|
|
|
messageId: string,
|
|
|
|
receipts: Array<MessageReceiptAttributesType>
|
|
|
|
) {
|
|
|
|
if (!receipts.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get message from cache or DB
|
|
|
|
const message = await window.MessageCache.resolveAttributes(
|
|
|
|
'processReceiptsForMessage',
|
|
|
|
messageId
|
|
|
|
);
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
const { updatedMessage, validReceipts } = await updateMessageWithReceipts(
|
2023-12-15 18:33:02 +00:00
|
|
|
message,
|
|
|
|
receipts
|
|
|
|
);
|
|
|
|
|
|
|
|
// Save it to cache & to DB
|
|
|
|
await window.MessageCache.setAttributes({
|
|
|
|
messageId,
|
|
|
|
messageAttributes: updatedMessage,
|
|
|
|
skipSaveToDatabase: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Confirm/remove receipts, and delete sent protos
|
|
|
|
for (const receipt of validReceipts) {
|
2024-06-17 19:24:39 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await remove(receipt);
|
2023-12-15 18:33:02 +00:00
|
|
|
drop(addToDeleteSentProtoBatcher(receipt, updatedMessage));
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify frontend listeners
|
|
|
|
const conversation = window.ConversationController.get(
|
|
|
|
message.conversationId
|
|
|
|
);
|
|
|
|
conversation?.debouncedUpdateLastMessage?.();
|
|
|
|
}
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
async function updateMessageWithReceipts(
|
2023-12-15 18:33:02 +00:00
|
|
|
message: MessageAttributesType,
|
|
|
|
receipts: Array<MessageReceiptAttributesType>
|
2024-06-17 19:24:39 +00:00
|
|
|
): Promise<{
|
2023-12-15 18:33:02 +00:00
|
|
|
updatedMessage: MessageAttributesType;
|
|
|
|
validReceipts: Array<MessageReceiptAttributesType>;
|
2024-06-17 19:24:39 +00:00
|
|
|
}> {
|
2023-12-15 18:33:02 +00:00
|
|
|
const logId = `updateMessageWithReceipts(timestamp=${message.timestamp})`;
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
const toRemove: Array<MessageReceiptAttributesType> = [];
|
2023-12-15 18:33:02 +00:00
|
|
|
const receiptsToProcess = receipts.filter(receipt => {
|
|
|
|
if (shouldDropReceipt(receipt, message)) {
|
2024-06-17 19:24:39 +00:00
|
|
|
const { receiptSync } = receipt;
|
2023-12-15 18:33:02 +00:00
|
|
|
log.info(
|
2024-06-17 19:24:39 +00:00
|
|
|
`${logId}: Dropping a receipt ${receiptSync.type} for sentAt=${receiptSync.messageSentAt}`
|
2023-12-15 18:33:02 +00:00
|
|
|
);
|
2024-06-17 19:24:39 +00:00
|
|
|
toRemove.push(receipt);
|
2023-12-15 18:33:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
if (!cachedReceipts.has(receipt.syncTaskId)) {
|
2023-12-15 18:33:02 +00:00
|
|
|
// Between the time it was received and now, this receipt has already been handled!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
await Promise.all(toRemove.map(remove));
|
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
log.info(
|
|
|
|
`${logId}: batch processing ${receipts.length}` +
|
|
|
|
` receipt${receipts.length === 1 ? '' : 's'}`
|
|
|
|
);
|
|
|
|
|
|
|
|
// Generate the updated message synchronously
|
|
|
|
let updatedMessage: MessageAttributesType = { ...message };
|
|
|
|
for (const receipt of receiptsToProcess) {
|
|
|
|
updatedMessage = {
|
|
|
|
...updatedMessage,
|
2023-12-18 20:27:18 +00:00
|
|
|
...updateMessageSendStateWithReceipt(updatedMessage, receipt),
|
2023-12-15 18:33:02 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return { updatedMessage, validReceipts: receiptsToProcess };
|
|
|
|
}
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2021-08-31 21:35:01 +00:00
|
|
|
const deleteSentProtoBatcher = createWaitBatcher({
|
|
|
|
name: 'deleteSentProtoBatcher',
|
2023-12-15 18:33:02 +00:00
|
|
|
wait: DELETE_SENT_PROTO_BATCHER_WAIT_MS,
|
2021-08-31 21:35:01 +00:00
|
|
|
maxSize: 30,
|
|
|
|
async processBatch(items: Array<DeleteSentProtoRecipientOptionsType>) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-31 21:35:01 +00:00
|
|
|
`MessageReceipts: Batching ${items.length} sent proto recipients deletes`
|
|
|
|
);
|
2024-07-24 00:31:40 +00:00
|
|
|
const { successfulPhoneNumberShares } =
|
|
|
|
await deleteSentProtoRecipient(items);
|
2022-08-15 21:53:33 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
for (const serviceId of successfulPhoneNumberShares) {
|
|
|
|
const convo = window.ConversationController.get(serviceId);
|
2022-08-15 21:53:33 +00:00
|
|
|
if (!convo) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'MessageReceipts: unsetting shareMyPhoneNumber ' +
|
|
|
|
`for ${convo.idForLogging()}`
|
|
|
|
);
|
|
|
|
|
|
|
|
// `deleteSentProtoRecipient` has already updated the database so there
|
|
|
|
// is no need in calling `updateConversation`
|
|
|
|
convo.unset('shareMyPhoneNumber');
|
|
|
|
}
|
2021-08-31 21:35:01 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
async function remove(receipt: MessageReceiptAttributesType): Promise<void> {
|
|
|
|
const { syncTaskId } = receipt;
|
|
|
|
cachedReceipts.delete(syncTaskId);
|
|
|
|
await removeSyncTaskById(syncTaskId);
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 20:35:31 +00:00
|
|
|
function getTargetMessage({
|
|
|
|
sourceConversationId,
|
2023-12-15 18:33:02 +00:00
|
|
|
messagesMatchingTimestamp,
|
2023-12-08 20:35:31 +00:00
|
|
|
targetTimestamp,
|
|
|
|
}: {
|
|
|
|
sourceConversationId: string;
|
2023-12-15 18:33:02 +00:00
|
|
|
messagesMatchingTimestamp: ReadonlyArray<MessageAttributesType>;
|
2023-12-08 20:35:31 +00:00
|
|
|
targetTimestamp: number;
|
2023-12-15 18:33:02 +00:00
|
|
|
}): MessageAttributesType | null {
|
|
|
|
if (messagesMatchingTimestamp.length === 0) {
|
2021-07-20 20:17:25 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
const matchingMessages = messagesMatchingTimestamp
|
2023-12-08 20:35:31 +00:00
|
|
|
.filter(msg => isOutgoing(msg) || isStory(msg))
|
|
|
|
.filter(msg => {
|
|
|
|
const sendStateByConversationId = getPropForTimestamp({
|
|
|
|
message: msg,
|
|
|
|
prop: 'sendStateByConversationId',
|
|
|
|
targetTimestamp,
|
|
|
|
log,
|
|
|
|
});
|
|
|
|
|
|
|
|
const isRecipient = Object.hasOwn(
|
|
|
|
sendStateByConversationId ?? {},
|
|
|
|
sourceConversationId
|
|
|
|
);
|
|
|
|
if (!isRecipient) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2023-12-08 20:35:31 +00:00
|
|
|
const sendStatus =
|
|
|
|
sendStateByConversationId?.[sourceConversationId]?.status;
|
|
|
|
|
|
|
|
if (
|
|
|
|
sendStatus === undefined ||
|
|
|
|
UNDELIVERED_SEND_STATUSES.includes(sendStatus)
|
|
|
|
) {
|
2023-12-14 21:09:11 +00:00
|
|
|
log.warn(
|
|
|
|
'MessageReceipts.getTargetMessage: received receipt for undelivered message, ' +
|
|
|
|
`status: ${sendStatus}, ` +
|
|
|
|
`sourceConversationId: ${sourceConversationId}, ` +
|
|
|
|
`message: ${getMessageIdForLogging(msg)}.`
|
|
|
|
);
|
2023-12-08 20:35:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2023-12-08 20:35:31 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (matchingMessages.length === 0) {
|
2021-07-20 20:17:25 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-12-08 20:35:31 +00:00
|
|
|
if (matchingMessages.length > 1) {
|
|
|
|
log.warn(`
|
|
|
|
MessageReceipts.getTargetMessage: multiple (${matchingMessages.length})
|
|
|
|
matching messages for receipt,
|
|
|
|
sentAt=${targetTimestamp},
|
|
|
|
sourceConversationId=${sourceConversationId}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = matchingMessages[0];
|
2023-12-15 18:33:02 +00:00
|
|
|
return window.MessageCache.toMessageAttributes(message);
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
|
|
|
const wasDeliveredWithSealedSender = (
|
|
|
|
conversationId: string,
|
2023-12-15 18:33:02 +00:00
|
|
|
message: MessageAttributesType
|
2021-07-20 20:17:25 +00:00
|
|
|
): boolean =>
|
2023-12-15 18:33:02 +00:00
|
|
|
(message.unidentifiedDeliveries || []).some(
|
2021-07-20 20:17:25 +00:00
|
|
|
identifier =>
|
|
|
|
window.ConversationController.getConversationId(identifier) ===
|
|
|
|
conversationId
|
|
|
|
);
|
|
|
|
|
2022-10-25 22:18:42 +00:00
|
|
|
const shouldDropReceipt = (
|
2023-08-21 20:08:27 +00:00
|
|
|
receipt: MessageReceiptAttributesType,
|
2024-07-25 23:29:49 +00:00
|
|
|
message: ReadonlyMessageAttributesType
|
2022-10-25 22:18:42 +00:00
|
|
|
): boolean => {
|
2024-06-17 19:24:39 +00:00
|
|
|
const { type } = receipt.receiptSync;
|
2022-10-25 22:18:42 +00:00
|
|
|
switch (type) {
|
2024-06-17 19:24:39 +00:00
|
|
|
case messageReceiptTypeSchema.Enum.Delivery:
|
2022-10-25 22:18:42 +00:00
|
|
|
return false;
|
2024-06-17 19:24:39 +00:00
|
|
|
case messageReceiptTypeSchema.Enum.Read:
|
2022-10-25 22:18:42 +00:00
|
|
|
return !window.storage.get('read-receipt-setting');
|
2024-06-17 19:24:39 +00:00
|
|
|
case messageReceiptTypeSchema.Enum.View:
|
2023-12-15 18:33:02 +00:00
|
|
|
if (isStory(message)) {
|
2022-10-25 22:18:42 +00:00
|
|
|
return !window.Events.getStoryViewReceiptsEnabled();
|
|
|
|
}
|
|
|
|
return !window.storage.get('read-receipt-setting');
|
|
|
|
default:
|
|
|
|
throw missingCaseError(type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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<Array<MessageReceiptAttributesType>> {
|
2024-07-25 23:29:49 +00:00
|
|
|
if (!isOutgoing(message) && !isStory(message)) {
|
2023-08-21 20:08:27 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const logId = `MessageReceipts.forMessage(${getMessageIdForLogging(
|
2024-07-25 23:29:49 +00:00
|
|
|
message
|
2023-08-21 20:08:27 +00:00
|
|
|
)})`;
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
2024-07-25 23:29:49 +00:00
|
|
|
const sourceServiceId = getSourceServiceId(message);
|
2023-08-21 20:08:27 +00:00
|
|
|
if (ourAci !== sourceServiceId) {
|
|
|
|
return [];
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
const receiptValues = Array.from(cachedReceipts.values());
|
2023-01-11 22:54:06 +00:00
|
|
|
|
2024-07-25 23:29:49 +00:00
|
|
|
const sentAt = getMessageSentTimestamp(message, { log });
|
2024-06-17 19:24:39 +00:00
|
|
|
const result = receiptValues.filter(
|
|
|
|
item => item.receiptSync.messageSentAt === sentAt
|
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
if (result.length > 0) {
|
|
|
|
log.info(`${logId}: found early receipts for message ${sentAt}`);
|
2024-06-17 19:24:39 +00:00
|
|
|
await Promise.all(
|
|
|
|
result.map(async receipt => {
|
|
|
|
await remove(receipt);
|
|
|
|
})
|
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2023-01-11 22:54:06 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return result.filter(receipt => {
|
2024-07-25 23:29:49 +00:00
|
|
|
if (shouldDropReceipt(receipt, message)) {
|
2023-08-21 20:08:27 +00:00
|
|
|
log.info(
|
2024-06-17 19:24:39 +00:00
|
|
|
`${logId}: Dropping an early receipt ${receipt.receiptSync.type} for message ${sentAt}`
|
2023-08-21 20:08:27 +00:00
|
|
|
);
|
|
|
|
return false;
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
2022-10-25 22:18:42 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNewSendStateByConversationId(
|
|
|
|
oldSendStateByConversationId: SendStateByConversationId,
|
|
|
|
receipt: MessageReceiptAttributesType
|
|
|
|
): SendStateByConversationId {
|
2024-06-17 19:24:39 +00:00
|
|
|
const { receiptTimestamp, sourceConversationId, type } = receipt.receiptSync;
|
2023-08-21 20:08:27 +00:00
|
|
|
const oldSendState = getOwn(
|
|
|
|
oldSendStateByConversationId,
|
|
|
|
sourceConversationId
|
|
|
|
) ?? { status: SendStatus.Sent, updatedAt: undefined };
|
|
|
|
|
|
|
|
let sendActionType: SendActionType;
|
|
|
|
switch (type) {
|
2024-06-17 19:24:39 +00:00
|
|
|
case messageReceiptTypeSchema.enum.Delivery:
|
2023-08-21 20:08:27 +00:00
|
|
|
sendActionType = SendActionType.GotDeliveryReceipt;
|
|
|
|
break;
|
2024-06-17 19:24:39 +00:00
|
|
|
case messageReceiptTypeSchema.enum.Read:
|
2023-08-21 20:08:27 +00:00
|
|
|
sendActionType = SendActionType.GotReadReceipt;
|
|
|
|
break;
|
2024-06-17 19:24:39 +00:00
|
|
|
case messageReceiptTypeSchema.enum.View:
|
2023-08-21 20:08:27 +00:00
|
|
|
sendActionType = SendActionType.GotViewedReceipt;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw missingCaseError(type);
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
const newSendState = sendStateReducer(oldSendState, {
|
|
|
|
type: sendActionType,
|
|
|
|
updatedAt: receiptTimestamp,
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
...oldSendStateByConversationId,
|
|
|
|
[sourceConversationId]: newSendState,
|
|
|
|
};
|
|
|
|
}
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
function updateMessageSendStateWithReceipt(
|
2023-12-18 20:27:18 +00:00
|
|
|
message: MessageAttributesType,
|
2023-12-15 18:33:02 +00:00
|
|
|
receipt: MessageReceiptAttributesType
|
|
|
|
): Partial<MessageAttributesType> {
|
2024-06-17 19:24:39 +00:00
|
|
|
const { messageSentAt } = receipt.receiptSync;
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
const newAttributes: Partial<MessageAttributesType> = {};
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
const newEditHistory = (message.editHistory ?? []).map(edit => {
|
2023-08-21 20:08:27 +00:00
|
|
|
if (messageSentAt !== edit.timestamp) {
|
|
|
|
return edit;
|
2023-08-15 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const newSendStateByConversationId = getNewSendStateByConversationId(
|
2023-12-15 18:33:02 +00:00
|
|
|
edit.sendStateByConversationId ?? {},
|
2023-08-21 20:08:27 +00:00
|
|
|
receipt
|
|
|
|
);
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return {
|
|
|
|
...edit,
|
|
|
|
sendStateByConversationId: newSendStateByConversationId,
|
|
|
|
};
|
|
|
|
});
|
2023-12-15 18:33:02 +00:00
|
|
|
|
|
|
|
if (message.editHistory?.length) {
|
|
|
|
newAttributes.editHistory = newEditHistory;
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
const { editMessageTimestamp, timestamp } = message;
|
2023-08-21 20:08:27 +00:00
|
|
|
if (
|
2023-12-15 18:33:02 +00:00
|
|
|
(!editMessageTimestamp && messageSentAt === timestamp) ||
|
2023-08-21 20:08:27 +00:00
|
|
|
messageSentAt === editMessageTimestamp
|
|
|
|
) {
|
|
|
|
const newSendStateByConversationId = getNewSendStateByConversationId(
|
2023-12-15 18:33:02 +00:00
|
|
|
message.sendStateByConversationId ?? {},
|
2023-08-21 20:08:27 +00:00
|
|
|
receipt
|
|
|
|
);
|
2023-12-15 18:33:02 +00:00
|
|
|
newAttributes.sendStateByConversationId = newSendStateByConversationId;
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
return newAttributes;
|
|
|
|
}
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-12-15 18:33:02 +00:00
|
|
|
async function addToDeleteSentProtoBatcher(
|
|
|
|
receipt: MessageReceiptAttributesType,
|
|
|
|
message: MessageAttributesType
|
|
|
|
) {
|
2024-06-17 19:24:39 +00:00
|
|
|
const { receiptSync } = receipt;
|
|
|
|
const {
|
|
|
|
sourceConversationId,
|
|
|
|
type,
|
|
|
|
wasSentEncrypted,
|
|
|
|
messageSentAt,
|
|
|
|
sourceDevice,
|
|
|
|
} = receiptSync;
|
2023-08-21 20:08:27 +00:00
|
|
|
|
|
|
|
if (
|
2024-06-17 19:24:39 +00:00
|
|
|
(type === messageReceiptTypeSchema.enum.Delivery &&
|
2023-08-21 20:08:27 +00:00
|
|
|
wasDeliveredWithSealedSender(sourceConversationId, message) &&
|
2024-06-17 19:24:39 +00:00
|
|
|
wasSentEncrypted) ||
|
|
|
|
type === messageReceiptTypeSchema.enum.Read
|
2023-08-21 20:08:27 +00:00
|
|
|
) {
|
|
|
|
const recipient = window.ConversationController.get(sourceConversationId);
|
|
|
|
const recipientServiceId = recipient?.getServiceId();
|
2024-06-17 19:24:39 +00:00
|
|
|
const deviceId = sourceDevice;
|
2023-08-21 20:08:27 +00:00
|
|
|
|
|
|
|
if (recipientServiceId && deviceId) {
|
2023-12-15 18:33:02 +00:00
|
|
|
await deleteSentProtoBatcher.add({
|
2024-06-17 19:24:39 +00:00
|
|
|
timestamp: messageSentAt,
|
2023-12-15 18:33:02 +00:00
|
|
|
recipientServiceId,
|
|
|
|
deviceId,
|
|
|
|
});
|
2023-08-21 20:08:27 +00:00
|
|
|
} else {
|
|
|
|
log.warn(
|
2024-06-17 19:24:39 +00:00
|
|
|
`MessageReceipts.deleteSentProto(sentAt=${messageSentAt}): ` +
|
2023-12-15 18:33:02 +00:00
|
|
|
`Missing serviceId or deviceId for deliveredTo ${sourceConversationId}`
|
2022-07-01 00:52:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export async function onReceipt(
|
|
|
|
receipt: MessageReceiptAttributesType
|
|
|
|
): Promise<void> {
|
2024-06-17 19:24:39 +00:00
|
|
|
cachedReceipts.set(receipt.syncTaskId, receipt);
|
2023-12-15 18:33:02 +00:00
|
|
|
await processReceiptBatcher.add(receipt);
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|