2021-07-20 20:17:25 +00:00
|
|
|
// Copyright 2016-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
|
|
|
|
import { isEqual } from 'lodash';
|
|
|
|
import { Collection, Model } from 'backbone';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationModel } from '../models/conversations';
|
|
|
|
import type { MessageModel } from '../models/messages';
|
2021-12-10 22:51:54 +00:00
|
|
|
import type { MessageAttributesType } from '../model-types.d';
|
2021-07-20 20:17:25 +00:00
|
|
|
import { isOutgoing } from '../state/selectors/message';
|
|
|
|
import { isDirectConversation } from '../util/whatTypeOfConversation';
|
|
|
|
import { getOwn } from '../util/getOwn';
|
|
|
|
import { missingCaseError } from '../util/missingCaseError';
|
2021-08-31 21:35:01 +00:00
|
|
|
import { createWaitBatcher } from '../util/waitBatcher';
|
2021-10-26 22:59:08 +00:00
|
|
|
import type { UUIDStringType } from '../types/UUID';
|
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';
|
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 = {
|
2021-07-20 20:17:25 +00:00
|
|
|
messageSentAt: number;
|
|
|
|
receiptTimestamp: number;
|
2021-10-26 22:59:08 +00:00
|
|
|
sourceUuid: UUIDStringType;
|
2021-07-20 20:17:25 +00:00
|
|
|
sourceConversationId: string;
|
|
|
|
sourceDevice: number;
|
|
|
|
type: MessageReceiptType;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MessageReceiptModel extends Model<MessageReceiptAttributesType> {}
|
|
|
|
|
|
|
|
let singleton: MessageReceipts | undefined;
|
|
|
|
|
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`
|
|
|
|
);
|
|
|
|
await deleteSentProtoRecipient(items);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-07-20 20:17:25 +00:00
|
|
|
async function getTargetMessage(
|
|
|
|
sourceId: string,
|
2021-10-26 22:59:08 +00:00
|
|
|
sourceUuid: UUIDStringType,
|
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(
|
2021-12-10 22:51:54 +00:00
|
|
|
item => isOutgoing(item) && sourceId === item.conversationId
|
2021-07-20 20:17:25 +00:00
|
|
|
);
|
|
|
|
if (message) {
|
|
|
|
return window.MessageController.register(message.id, message);
|
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
const groups = await window.Signal.Data.getAllGroupsInvolvingUuid(sourceUuid);
|
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(
|
2021-12-10 22:51:54 +00:00
|
|
|
item => isOutgoing(item) && ids.includes(item.conversationId)
|
2021-07-20 20:17:25 +00:00
|
|
|
);
|
|
|
|
if (!target) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.MessageController.register(target.id, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
const wasDeliveredWithSealedSender = (
|
|
|
|
conversationId: string,
|
|
|
|
message: MessageModel
|
|
|
|
): boolean =>
|
|
|
|
(message.get('unidentifiedDeliveries') || []).some(
|
|
|
|
identifier =>
|
|
|
|
window.ConversationController.getConversationId(identifier) ===
|
|
|
|
conversationId
|
|
|
|
);
|
|
|
|
|
|
|
|
export class MessageReceipts extends Collection<MessageReceiptModel> {
|
|
|
|
static getSingleton(): MessageReceipts {
|
|
|
|
if (!singleton) {
|
|
|
|
singleton = new MessageReceipts();
|
|
|
|
}
|
|
|
|
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
forMessage(
|
|
|
|
conversation: ConversationModel,
|
|
|
|
message: MessageModel
|
|
|
|
): Array<MessageReceiptModel> {
|
|
|
|
if (!isOutgoing(message.attributes)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
let ids: Array<string>;
|
|
|
|
if (isDirectConversation(conversation.attributes)) {
|
|
|
|
ids = [conversation.id];
|
|
|
|
} else {
|
|
|
|
ids = conversation.getMemberIds();
|
|
|
|
}
|
|
|
|
const receipts = this.filter(
|
|
|
|
receipt =>
|
|
|
|
receipt.get('messageSentAt') === message.get('sent_at') &&
|
|
|
|
ids.includes(receipt.get('sourceConversationId'))
|
|
|
|
);
|
|
|
|
if (receipts.length) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Found early receipts for message');
|
2021-07-20 20:17:25 +00:00
|
|
|
this.remove(receipts);
|
|
|
|
}
|
|
|
|
return receipts;
|
|
|
|
}
|
|
|
|
|
|
|
|
async onReceipt(receipt: MessageReceiptModel): Promise<void> {
|
|
|
|
const type = receipt.get('type');
|
|
|
|
const messageSentAt = receipt.get('messageSentAt');
|
2021-11-04 20:49:38 +00:00
|
|
|
const receiptTimestamp = receipt.get('receiptTimestamp');
|
2021-07-20 20:17:25 +00:00
|
|
|
const sourceConversationId = receipt.get('sourceConversationId');
|
2021-10-26 22:59:08 +00:00
|
|
|
const sourceUuid = receipt.get('sourceUuid');
|
2021-07-20 20:17:25 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
2021-12-10 22:51:54 +00:00
|
|
|
messageSentAt
|
2021-07-20 20:17:25 +00:00
|
|
|
);
|
|
|
|
|
2021-10-26 22:59:08 +00:00
|
|
|
const message = await getTargetMessage(
|
|
|
|
sourceConversationId,
|
|
|
|
sourceUuid,
|
|
|
|
messages
|
|
|
|
);
|
2021-07-20 20:17:25 +00:00
|
|
|
if (!message) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-07-20 20:17:25 +00:00
|
|
|
'No message for receipt',
|
|
|
|
type,
|
|
|
|
sourceConversationId,
|
|
|
|
messageSentAt
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const oldSendStateByConversationId =
|
|
|
|
message.get('sendStateByConversationId') || {};
|
|
|
|
const oldSendState = getOwn(
|
|
|
|
oldSendStateByConversationId,
|
|
|
|
sourceConversationId
|
2021-07-28 18:53:18 +00:00
|
|
|
) ?? { 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
|
|
|
|
2021-07-28 18:53:18 +00:00
|
|
|
const newSendState = sendStateReducer(oldSendState, {
|
|
|
|
type: sendActionType,
|
2021-11-04 20:49:38 +00:00
|
|
|
updatedAt: receiptTimestamp,
|
2021-07-28 18:53:18 +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(oldSendState, newSendState)) {
|
|
|
|
message.set('sendStateByConversationId', {
|
|
|
|
...oldSendStateByConversationId,
|
|
|
|
[sourceConversationId]: newSendState,
|
2021-07-20 20:17:25 +00:00
|
|
|
});
|
|
|
|
|
2021-07-28 18:53:18 +00:00
|
|
|
window.Signal.Util.queueUpdateMessage(message.attributes);
|
2021-07-20 20:17:25 +00:00
|
|
|
|
2021-07-28 18:53:18 +00:00
|
|
|
// notify frontend listeners
|
|
|
|
const conversation = window.ConversationController.get(
|
|
|
|
message.get('conversationId')
|
2021-07-20 20:17:25 +00:00
|
|
|
);
|
2021-07-28 18:53:18 +00:00
|
|
|
const updateLeftPane = conversation
|
|
|
|
? conversation.debouncedUpdateLastMessage
|
|
|
|
: undefined;
|
|
|
|
if (updateLeftPane) {
|
|
|
|
updateLeftPane();
|
|
|
|
}
|
2021-07-20 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
(type === MessageReceiptType.Delivery &&
|
|
|
|
wasDeliveredWithSealedSender(sourceConversationId, message)) ||
|
|
|
|
type === MessageReceiptType.Read
|
|
|
|
) {
|
2021-11-11 22:43:05 +00:00
|
|
|
const recipient =
|
|
|
|
window.ConversationController.get(sourceConversationId);
|
2021-07-20 20:17:25 +00:00
|
|
|
const recipientUuid = recipient?.get('uuid');
|
|
|
|
const deviceId = receipt.get('sourceDevice');
|
|
|
|
|
|
|
|
if (recipientUuid && deviceId) {
|
2021-08-31 21:35:01 +00:00
|
|
|
await deleteSentProtoBatcher.add({
|
2021-07-20 20:17:25 +00:00
|
|
|
timestamp: messageSentAt,
|
|
|
|
recipientUuid,
|
|
|
|
deviceId,
|
|
|
|
});
|
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-20 20:17:25 +00:00
|
|
|
`MessageReceipts.onReceipt: Missing uuid or deviceId for deliveredTo ${sourceConversationId}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.remove(receipt);
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-07-20 20:17:25 +00:00
|
|
|
'MessageReceipts.onReceipt error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|