2021-05-07 01:15:25 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-07-30 01:08:04 +00:00
|
|
|
import { chunk, groupBy, map } from 'lodash';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationAttributesType } from '../model-types.d';
|
2021-05-07 01:15:25 +00:00
|
|
|
import { getSendOptions } from './getSendOptions';
|
|
|
|
import { handleMessageSend } from './handleMessageSend';
|
|
|
|
import { isConversationAccepted } from './isConversationAccepted';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-05-07 01:15:25 +00:00
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
type ReceiptSpecType = {
|
|
|
|
messageId: string;
|
|
|
|
senderE164?: string;
|
|
|
|
senderUuid?: string;
|
|
|
|
senderId?: string;
|
|
|
|
timestamp: number;
|
|
|
|
hasErrors: boolean;
|
|
|
|
};
|
|
|
|
|
2021-07-30 01:08:04 +00:00
|
|
|
const CHUNK_SIZE = 100;
|
|
|
|
|
2021-05-07 01:15:25 +00:00
|
|
|
export async function sendReadReceiptsFor(
|
|
|
|
conversationAttrs: ConversationAttributesType,
|
2021-07-15 23:48:09 +00:00
|
|
|
items: Array<ReceiptSpecType>
|
2021-05-07 01:15:25 +00:00
|
|
|
): Promise<void> {
|
|
|
|
// Only send read receipts for accepted conversations
|
|
|
|
if (
|
2021-08-18 20:08:14 +00:00
|
|
|
window.Events.getReadReceiptSetting() &&
|
2021-05-07 01:15:25 +00:00
|
|
|
isConversationAccepted(conversationAttrs)
|
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Sending ${items.length} read receipts`);
|
2021-07-02 18:34:17 +00:00
|
|
|
const sendOptions = await getSendOptions(conversationAttrs);
|
2021-05-07 01:15:25 +00:00
|
|
|
const receiptsBySender = groupBy(items, 'senderId');
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
map(receiptsBySender, async (receipts, senderId) => {
|
|
|
|
const conversation = window.ConversationController.get(senderId);
|
|
|
|
|
2021-07-30 01:08:04 +00:00
|
|
|
if (!conversation) {
|
|
|
|
return;
|
2021-05-07 01:15:25 +00:00
|
|
|
}
|
2021-07-30 01:08:04 +00:00
|
|
|
|
|
|
|
const batches = chunk(receipts, CHUNK_SIZE);
|
|
|
|
await Promise.all(
|
|
|
|
batches.map(batch => {
|
|
|
|
const timestamps = map(batch, item => item.timestamp);
|
|
|
|
const messageIds = map(batch, item => item.messageId);
|
|
|
|
|
|
|
|
return handleMessageSend(
|
|
|
|
window.textsecure.messaging.sendReadReceipts({
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
senderE164: conversation.get('e164')!,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
senderUuid: conversation.get('uuid')!,
|
|
|
|
timestamps,
|
|
|
|
options: sendOptions,
|
|
|
|
}),
|
|
|
|
{ messageIds, sendType: 'readReceipt' }
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2021-05-07 01:15:25 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|