2021-05-07 01:15:25 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { groupBy, map } from 'lodash';
|
|
|
|
import { ConversationAttributesType } from '../model-types.d';
|
|
|
|
import { getSendOptions } from './getSendOptions';
|
|
|
|
import { handleMessageSend } from './handleMessageSend';
|
|
|
|
import { isConversationAccepted } from './isConversationAccepted';
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
type ReceiptSpecType = {
|
|
|
|
messageId: string;
|
|
|
|
senderE164?: string;
|
|
|
|
senderUuid?: string;
|
|
|
|
senderId?: string;
|
|
|
|
timestamp: number;
|
|
|
|
hasErrors: boolean;
|
|
|
|
};
|
|
|
|
|
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 (
|
|
|
|
window.storage.get('read-receipt-setting') &&
|
|
|
|
isConversationAccepted(conversationAttrs)
|
|
|
|
) {
|
|
|
|
window.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) => {
|
2021-07-15 23:48:09 +00:00
|
|
|
const timestamps = map(receipts, item => item.timestamp);
|
|
|
|
const messageIds = map(receipts, item => item.messageId);
|
2021-05-07 01:15:25 +00:00
|
|
|
const conversation = window.ConversationController.get(senderId);
|
|
|
|
|
|
|
|
if (conversation) {
|
|
|
|
await handleMessageSend(
|
2021-07-02 18:34:17 +00:00
|
|
|
window.textsecure.messaging.sendReadReceipts({
|
2021-05-07 01:15:25 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2021-07-02 18:34:17 +00:00
|
|
|
senderE164: conversation.get('e164')!,
|
2021-05-07 01:15:25 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2021-07-02 18:34:17 +00:00
|
|
|
senderUuid: conversation.get('uuid')!,
|
2021-05-07 01:15:25 +00:00
|
|
|
timestamps,
|
2021-07-02 18:34:17 +00:00
|
|
|
options: sendOptions,
|
2021-07-15 23:48:09 +00:00
|
|
|
}),
|
|
|
|
{ messageIds, sendType: 'readReceipt' }
|
2021-05-07 01:15:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|