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
|
|
|
|
|
|
|
|
import { isEqual } from 'lodash';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MessageModel } from '../models/messages';
|
2021-12-10 22:51:54 +00:00
|
|
|
import type { MessageAttributesType } 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';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../types/ServiceId';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2021-07-28 18:53:18 +00:00
|
|
|
import {
|
|
|
|
SendActionType,
|
|
|
|
SendStatus,
|
|
|
|
sendStateReducer,
|
|
|
|
} from '../messages/MessageSendState';
|
2021-08-31 21:35:01 +00:00
|
|
|
import type { DeleteSentProtoRecipientOptionsType } from '../sql/Interface';
|
2021-07-20 20:17:25 +00:00
|
|
|
import dataInterface from '../sql/Client';
|
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-04-11 03:54:43 +00:00
|
|
|
import { queueUpdateMessage } from '../util/messageBatcher';
|
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-11-02 13:28:49 +00:00
|
|
|
import { generateCacheKey } from './generateCacheKey';
|
2021-07-20 20:17:25 +00:00
|
|
|
|
|
|
|
const { deleteSentProtoRecipient } = dataInterface;
|
|
|
|
|
|
|
|
export enum MessageReceiptType {
|
|
|
|
Delivery = 'Delivery',
|
|
|
|
Read = 'Read',
|
2021-07-27 15:42:25 +00:00
|
|
|
View = 'View',
|
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;
|
2021-07-20 20:17:25 +00:00
|
|
|
messageSentAt: number;
|
|
|
|
receiptTimestamp: number;
|
2023-08-21 20:08:27 +00:00
|
|
|
removeFromMessageReceiverCache: () => unknown;
|
2021-07-20 20:17:25 +00:00
|
|
|
sourceConversationId: string;
|
|
|
|
sourceDevice: number;
|
2023-08-21 20:08:27 +00:00
|
|
|
sourceServiceId: ServiceIdString;
|
2021-07-20 20:17:25 +00:00
|
|
|
type: MessageReceiptType;
|
2022-08-15 21:53:33 +00:00
|
|
|
wasSentEncrypted: boolean;
|
2021-07-20 20:17:25 +00:00
|
|
|
};
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const receipts = new Map<string, MessageReceiptAttributesType>();
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2021-08-31 21:35:01 +00:00
|
|
|
const deleteSentProtoBatcher = createWaitBatcher({
|
|
|
|
name: 'deleteSentProtoBatcher',
|
|
|
|
wait: 250,
|
|
|
|
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`
|
|
|
|
);
|
2022-08-15 21:53:33 +00:00
|
|
|
const { successfulPhoneNumberShares } = await deleteSentProtoRecipient(
|
|
|
|
items
|
|
|
|
);
|
|
|
|
|
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
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
function remove(receipt: MessageReceiptAttributesType): void {
|
2023-11-02 13:28:49 +00:00
|
|
|
receipts.delete(
|
|
|
|
generateCacheKey({
|
|
|
|
sender: receipt.sourceServiceId,
|
|
|
|
timestamp: receipt.messageSentAt,
|
2023-11-20 21:25:49 +00:00
|
|
|
type: receipt.type,
|
2023-11-02 13:28:49 +00:00
|
|
|
})
|
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
receipt.removeFromMessageReceiverCache();
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:17:25 +00:00
|
|
|
async function getTargetMessage(
|
|
|
|
sourceId: string,
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: ServiceIdString,
|
2021-12-10 22:51:54 +00:00
|
|
|
messages: ReadonlyArray<MessageAttributesType>
|
2021-07-20 20:17:25 +00:00
|
|
|
): Promise<MessageModel | null> {
|
|
|
|
if (messages.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const message = messages.find(
|
2022-04-21 00:29:37 +00:00
|
|
|
item =>
|
|
|
|
(isOutgoing(item) || isStory(item)) && sourceId === item.conversationId
|
2021-07-20 20:17:25 +00:00
|
|
|
);
|
|
|
|
if (message) {
|
2023-10-04 00:12:57 +00:00
|
|
|
return window.MessageCache.__DEPRECATED$register(
|
|
|
|
message.id,
|
|
|
|
message,
|
|
|
|
'MessageReceipts.getTargetMessage 1'
|
|
|
|
);
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const groups = await window.Signal.Data.getAllGroupsInvolvingServiceId(
|
|
|
|
serviceId
|
|
|
|
);
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
const ids = groups.map(item => item.id);
|
2021-07-20 20:17:25 +00:00
|
|
|
ids.push(sourceId);
|
|
|
|
|
|
|
|
const target = messages.find(
|
2022-04-21 00:29:37 +00:00
|
|
|
item =>
|
|
|
|
(isOutgoing(item) || isStory(item)) && ids.includes(item.conversationId)
|
2021-07-20 20:17:25 +00:00
|
|
|
);
|
|
|
|
if (!target) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
return window.MessageCache.__DEPRECATED$register(
|
|
|
|
target.id,
|
|
|
|
target,
|
|
|
|
'MessageReceipts.getTargetMessage 2'
|
|
|
|
);
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const wasDeliveredWithSealedSender = (
|
|
|
|
conversationId: string,
|
|
|
|
message: MessageModel
|
|
|
|
): boolean =>
|
|
|
|
(message.get('unidentifiedDeliveries') || []).some(
|
|
|
|
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,
|
2022-10-25 22:18:42 +00:00
|
|
|
message: MessageModel
|
|
|
|
): boolean => {
|
2023-08-21 20:08:27 +00:00
|
|
|
const { type } = receipt;
|
2022-10-25 22:18:42 +00:00
|
|
|
switch (type) {
|
|
|
|
case MessageReceiptType.Delivery:
|
|
|
|
return false;
|
|
|
|
case MessageReceiptType.Read:
|
|
|
|
return !window.storage.get('read-receipt-setting');
|
|
|
|
case MessageReceiptType.View:
|
|
|
|
if (isStory(message.attributes)) {
|
|
|
|
return !window.Events.getStoryViewReceiptsEnabled();
|
|
|
|
}
|
|
|
|
return !window.storage.get('read-receipt-setting');
|
|
|
|
default:
|
|
|
|
throw missingCaseError(type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export function forMessage(
|
|
|
|
message: MessageModel
|
|
|
|
): Array<MessageReceiptAttributesType> {
|
|
|
|
if (!isOutgoing(message.attributes) && !isStory(message.attributes)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const logId = `MessageReceipts.forMessage(${getMessageIdForLogging(
|
|
|
|
message.attributes
|
|
|
|
)})`;
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
|
|
|
const sourceServiceId = getSourceServiceId(message.attributes);
|
|
|
|
if (ourAci !== sourceServiceId) {
|
|
|
|
return [];
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const receiptValues = Array.from(receipts.values());
|
2023-01-11 22:54:06 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const sentAt = getMessageSentTimestamp(message.attributes, { log });
|
|
|
|
const result = receiptValues.filter(item => item.messageSentAt === sentAt);
|
|
|
|
if (result.length > 0) {
|
|
|
|
log.info(`${logId}: found early receipts for message ${sentAt}`);
|
|
|
|
result.forEach(receipt => {
|
|
|
|
remove(receipt);
|
|
|
|
});
|
|
|
|
}
|
2023-01-11 22:54:06 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return result.filter(receipt => {
|
|
|
|
if (shouldDropReceipt(receipt, message)) {
|
|
|
|
log.info(
|
|
|
|
`${logId}: Dropping an early receipt ${receipt.type} for message ${sentAt}`
|
|
|
|
);
|
|
|
|
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 {
|
|
|
|
const { receiptTimestamp, sourceConversationId, type } = receipt;
|
|
|
|
|
|
|
|
const oldSendState = getOwn(
|
|
|
|
oldSendStateByConversationId,
|
|
|
|
sourceConversationId
|
|
|
|
) ?? { status: SendStatus.Sent, updatedAt: undefined };
|
|
|
|
|
|
|
|
let sendActionType: SendActionType;
|
|
|
|
switch (type) {
|
|
|
|
case MessageReceiptType.Delivery:
|
|
|
|
sendActionType = SendActionType.GotDeliveryReceipt;
|
|
|
|
break;
|
|
|
|
case MessageReceiptType.Read:
|
|
|
|
sendActionType = SendActionType.GotReadReceipt;
|
|
|
|
break;
|
|
|
|
case MessageReceiptType.View:
|
|
|
|
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,
|
|
|
|
});
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return {
|
|
|
|
...oldSendStateByConversationId,
|
|
|
|
[sourceConversationId]: newSendState,
|
|
|
|
};
|
|
|
|
}
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
async function updateMessageSendState(
|
|
|
|
receipt: MessageReceiptAttributesType,
|
|
|
|
message: MessageModel
|
|
|
|
): Promise<void> {
|
|
|
|
const { messageSentAt } = receipt;
|
|
|
|
const logId = `MessageReceipts.updateMessageSendState(sentAt=${receipt.messageSentAt})`;
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (shouldDropReceipt(receipt, message)) {
|
|
|
|
log.info(
|
|
|
|
`${logId}: Dropping a receipt ${receipt.type} for message ${messageSentAt}`
|
|
|
|
);
|
|
|
|
return;
|
2023-08-15 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
let hasChanges = false;
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const editHistory = message.get('editHistory') ?? [];
|
|
|
|
const newEditHistory = editHistory?.map(edit => {
|
|
|
|
if (messageSentAt !== edit.timestamp) {
|
|
|
|
return edit;
|
2023-08-15 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const oldSendStateByConversationId = edit.sendStateByConversationId ?? {};
|
|
|
|
const newSendStateByConversationId = getNewSendStateByConversationId(
|
|
|
|
oldSendStateByConversationId,
|
|
|
|
receipt
|
|
|
|
);
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return {
|
|
|
|
...edit,
|
|
|
|
sendStateByConversationId: newSendStateByConversationId,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
if (!isEqual(newEditHistory, editHistory)) {
|
|
|
|
message.set('editHistory', newEditHistory);
|
|
|
|
hasChanges = true;
|
|
|
|
}
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const editMessageTimestamp = message.get('editMessageTimestamp');
|
|
|
|
if (
|
2023-11-02 00:16:20 +00:00
|
|
|
(!editMessageTimestamp && messageSentAt === message.get('timestamp')) ||
|
2023-08-21 20:08:27 +00:00
|
|
|
messageSentAt === editMessageTimestamp
|
|
|
|
) {
|
|
|
|
const oldSendStateByConversationId =
|
|
|
|
message.get('sendStateByConversationId') ?? {};
|
|
|
|
const newSendStateByConversationId = getNewSendStateByConversationId(
|
|
|
|
oldSendStateByConversationId,
|
|
|
|
receipt
|
|
|
|
);
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
// The send state may not change. For example, this can happen if we get a read
|
|
|
|
// receipt before a delivery receipt.
|
|
|
|
if (!isEqual(oldSendStateByConversationId, newSendStateByConversationId)) {
|
|
|
|
message.set('sendStateByConversationId', newSendStateByConversationId);
|
2023-08-15 23:24:19 +00:00
|
|
|
hasChanges = true;
|
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (hasChanges) {
|
|
|
|
queueUpdateMessage(message.attributes);
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
// notify frontend listeners
|
|
|
|
const conversation = window.ConversationController.get(
|
|
|
|
message.get('conversationId')
|
|
|
|
);
|
|
|
|
const updateLeftPane = conversation
|
|
|
|
? conversation.debouncedUpdateLastMessage
|
|
|
|
: undefined;
|
|
|
|
if (updateLeftPane) {
|
|
|
|
updateLeftPane();
|
2023-08-15 23:24:19 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2023-08-15 23:24:19 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const { sourceConversationId, type } = receipt;
|
|
|
|
|
|
|
|
if (
|
|
|
|
(type === MessageReceiptType.Delivery &&
|
|
|
|
wasDeliveredWithSealedSender(sourceConversationId, message) &&
|
|
|
|
receipt.wasSentEncrypted) ||
|
|
|
|
type === MessageReceiptType.Read
|
|
|
|
) {
|
|
|
|
const recipient = window.ConversationController.get(sourceConversationId);
|
|
|
|
const recipientServiceId = recipient?.getServiceId();
|
|
|
|
const deviceId = receipt.sourceDevice;
|
|
|
|
|
|
|
|
if (recipientServiceId && deviceId) {
|
|
|
|
await Promise.all([
|
|
|
|
deleteSentProtoBatcher.add({
|
|
|
|
timestamp: messageSentAt,
|
|
|
|
recipientServiceId,
|
|
|
|
deviceId,
|
|
|
|
}),
|
|
|
|
|
|
|
|
// We want the above call to not be delayed when testing with
|
|
|
|
// CI.
|
|
|
|
window.SignalCI
|
|
|
|
? deleteSentProtoBatcher.flushAndWait()
|
|
|
|
: Promise.resolve(),
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
log.warn(
|
|
|
|
`${logId}: 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> {
|
2023-11-02 13:28:49 +00:00
|
|
|
receipts.set(
|
|
|
|
generateCacheKey({
|
|
|
|
sender: receipt.sourceServiceId,
|
|
|
|
timestamp: receipt.messageSentAt,
|
2023-11-20 21:25:49 +00:00
|
|
|
type: receipt.type,
|
2023-11-02 13:28:49 +00:00
|
|
|
}),
|
|
|
|
receipt
|
|
|
|
);
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const { messageSentAt, sourceConversationId, sourceServiceId, type } =
|
|
|
|
receipt;
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const logId = `MessageReceipts.onReceipt(sentAt=${receipt.messageSentAt})`;
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
try {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
messageSentAt
|
|
|
|
);
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const message = await getTargetMessage(
|
|
|
|
sourceConversationId,
|
|
|
|
sourceServiceId,
|
|
|
|
messages
|
|
|
|
);
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
await updateMessageSendState(receipt, message);
|
|
|
|
} else {
|
|
|
|
// We didn't find any messages but maybe it's a story sent message
|
|
|
|
const targetMessages = messages.filter(
|
|
|
|
item =>
|
|
|
|
item.storyDistributionListId &&
|
|
|
|
item.sendStateByConversationId &&
|
|
|
|
!item.deletedForEveryone &&
|
|
|
|
Boolean(item.sendStateByConversationId[sourceConversationId])
|
|
|
|
);
|
|
|
|
|
|
|
|
// Nope, no target message was found
|
|
|
|
if (!targetMessages.length) {
|
|
|
|
log.info(
|
|
|
|
`${logId}: No message for receipt`,
|
|
|
|
type,
|
|
|
|
sourceConversationId,
|
|
|
|
sourceServiceId
|
2022-07-01 00:52:03 +00:00
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
return;
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
await Promise.all(
|
|
|
|
targetMessages.map(msg => {
|
2023-10-04 00:12:57 +00:00
|
|
|
const model = window.MessageCache.__DEPRECATED$register(
|
|
|
|
msg.id,
|
|
|
|
msg,
|
|
|
|
'MessageReceipts.onReceipt'
|
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
return updateMessageSendState(receipt, model);
|
|
|
|
})
|
|
|
|
);
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
|
|
|
|
remove(receipt);
|
|
|
|
} catch (error) {
|
|
|
|
remove(receipt);
|
|
|
|
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
|
|
|
}
|