2022-02-22 20:34:57 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-12-07 22:41:40 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { chunk } from 'lodash';
|
|
|
|
import type { LoggerType } from '../types/Logging';
|
|
|
|
import type { Receipt } from '../types/Receipt';
|
|
|
|
import { ReceiptType } from '../types/Receipt';
|
|
|
|
import { getSendOptions } from './getSendOptions';
|
|
|
|
import { handleMessageSend } from './handleMessageSend';
|
|
|
|
import { isConversationAccepted } from './isConversationAccepted';
|
2022-02-22 20:34:57 +00:00
|
|
|
import { isConversationUnregistered } from './isConversationUnregistered';
|
2021-12-07 22:41:40 +00:00
|
|
|
import { map } from './iterables';
|
|
|
|
import { missingCaseError } from './missingCaseError';
|
|
|
|
|
|
|
|
const CHUNK_SIZE = 100;
|
|
|
|
|
|
|
|
export async function sendReceipts({
|
|
|
|
log,
|
|
|
|
receipts,
|
|
|
|
type,
|
|
|
|
}: Readonly<{
|
|
|
|
log: LoggerType;
|
|
|
|
receipts: ReadonlyArray<Receipt>;
|
|
|
|
type: ReceiptType;
|
|
|
|
}>): Promise<void> {
|
|
|
|
let requiresUserSetting: boolean;
|
|
|
|
let methodName:
|
|
|
|
| 'sendDeliveryReceipt'
|
|
|
|
| 'sendReadReceipt'
|
|
|
|
| 'sendViewedReceipt';
|
|
|
|
switch (type) {
|
|
|
|
case ReceiptType.Delivery:
|
|
|
|
requiresUserSetting = false;
|
|
|
|
methodName = 'sendDeliveryReceipt';
|
|
|
|
break;
|
|
|
|
case ReceiptType.Read:
|
|
|
|
requiresUserSetting = true;
|
|
|
|
methodName = 'sendReadReceipt';
|
|
|
|
break;
|
|
|
|
case ReceiptType.Viewed:
|
|
|
|
requiresUserSetting = true;
|
|
|
|
methodName = 'sendViewedReceipt';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw missingCaseError(type);
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
if (!messaging) {
|
|
|
|
throw new Error('messaging is not available!');
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:41:40 +00:00
|
|
|
if (requiresUserSetting && !window.storage.get('read-receipt-setting')) {
|
|
|
|
log.info('requires user setting. Not sending these receipts');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const receiptsBySenderId: Map<string, Array<Receipt>> = receipts.reduce(
|
|
|
|
(result, receipt) => {
|
|
|
|
const { senderE164, senderUuid } = receipt;
|
|
|
|
if (!senderE164 && !senderUuid) {
|
|
|
|
log.error('no sender E164 or UUID. Skipping this receipt');
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
const sender = window.ConversationController.lookupOrCreate({
|
2021-12-07 22:41:40 +00:00
|
|
|
e164: senderE164,
|
|
|
|
uuid: senderUuid,
|
|
|
|
});
|
2022-08-09 21:39:00 +00:00
|
|
|
if (!sender) {
|
2021-12-07 22:41:40 +00:00
|
|
|
throw new Error(
|
|
|
|
'no conversation found with that E164/UUID. Cannot send this receipt'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
const existingGroup = result.get(sender.id);
|
2021-12-07 22:41:40 +00:00
|
|
|
if (existingGroup) {
|
|
|
|
existingGroup.push(receipt);
|
|
|
|
} else {
|
2022-08-09 21:39:00 +00:00
|
|
|
result.set(sender.id, [receipt]);
|
2021-12-07 22:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
new Map()
|
|
|
|
);
|
|
|
|
|
2021-12-16 17:45:56 +00:00
|
|
|
await window.ConversationController.load();
|
|
|
|
|
2021-12-07 22:41:40 +00:00
|
|
|
await Promise.all(
|
|
|
|
map(receiptsBySenderId, async ([senderId, receiptsForSender]) => {
|
|
|
|
const sender = window.ConversationController.get(senderId);
|
|
|
|
if (!sender) {
|
|
|
|
throw new Error(
|
|
|
|
'despite having a conversation ID, no conversation was found'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isConversationAccepted(sender.attributes)) {
|
2022-02-22 20:34:57 +00:00
|
|
|
log.info(
|
|
|
|
`conversation ${sender.idForLogging()} is not accepted; refusing to send`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isConversationUnregistered(sender.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${sender.idForLogging()} is unregistered; refusing to send`
|
|
|
|
);
|
2021-12-07 22:41:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 02:40:56 +00:00
|
|
|
if (sender.isBlocked()) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${sender.idForLogging()} is blocked; refusing to send`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-07 22:41:40 +00:00
|
|
|
|
|
|
|
const sendOptions = await getSendOptions(sender.attributes);
|
|
|
|
|
|
|
|
const batches = chunk(receiptsForSender, CHUNK_SIZE);
|
|
|
|
await Promise.all(
|
|
|
|
map(batches, async batch => {
|
|
|
|
const timestamps = batch.map(receipt => receipt.timestamp);
|
|
|
|
const messageIds = batch.map(receipt => receipt.messageId);
|
2022-08-15 21:53:33 +00:00
|
|
|
const isDirectConversation = batch.some(
|
|
|
|
receipt => receipt.isDirectConversation
|
|
|
|
);
|
2021-12-07 22:41:40 +00:00
|
|
|
|
|
|
|
await handleMessageSend(
|
2022-06-13 21:39:35 +00:00
|
|
|
messaging[methodName]({
|
2021-12-07 22:41:40 +00:00
|
|
|
senderE164: sender.get('e164'),
|
|
|
|
senderUuid: sender.get('uuid'),
|
2022-08-15 21:53:33 +00:00
|
|
|
isDirectConversation,
|
2021-12-07 22:41:40 +00:00
|
|
|
timestamps,
|
|
|
|
options: sendOptions,
|
|
|
|
}),
|
|
|
|
{ messageIds, sendType: type }
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|