Do not confirm messages until we have handled them
This commit is contained in:
parent
29aa188c0f
commit
04f716986c
16 changed files with 990 additions and 960 deletions
|
@ -1,10 +1,7 @@
|
|||
// Copyright 2016 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';
|
||||
|
||||
import type { MessageModel } from '../models/messages';
|
||||
import type { MessageAttributesType } from '../model-types.d';
|
||||
|
@ -26,6 +23,7 @@ import * as log from '../logging/log';
|
|||
import { getSourceServiceId } from '../messages/helpers';
|
||||
import { queueUpdateMessage } from '../util/messageBatcher';
|
||||
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
|
||||
import { getMessageIdForLogging } from '../util/idForLogging';
|
||||
|
||||
const { deleteSentProtoRecipient } = dataInterface;
|
||||
|
||||
|
@ -36,18 +34,18 @@ export enum MessageReceiptType {
|
|||
}
|
||||
|
||||
export type MessageReceiptAttributesType = {
|
||||
envelopeId: string;
|
||||
messageSentAt: number;
|
||||
receiptTimestamp: number;
|
||||
sourceServiceId: ServiceIdString;
|
||||
removeFromMessageReceiverCache: () => unknown;
|
||||
sourceConversationId: string;
|
||||
sourceDevice: number;
|
||||
sourceServiceId: ServiceIdString;
|
||||
type: MessageReceiptType;
|
||||
wasSentEncrypted: boolean;
|
||||
};
|
||||
|
||||
class MessageReceiptModel extends Model<MessageReceiptAttributesType> {}
|
||||
|
||||
let singleton: MessageReceipts | undefined;
|
||||
const receipts = new Map<string, MessageReceiptAttributesType>();
|
||||
|
||||
const deleteSentProtoBatcher = createWaitBatcher({
|
||||
name: 'deleteSentProtoBatcher',
|
||||
|
@ -79,6 +77,11 @@ const deleteSentProtoBatcher = createWaitBatcher({
|
|||
},
|
||||
});
|
||||
|
||||
function remove(receipt: MessageReceiptAttributesType): void {
|
||||
receipts.delete(receipt.envelopeId);
|
||||
receipt.removeFromMessageReceiverCache();
|
||||
}
|
||||
|
||||
async function getTargetMessage(
|
||||
sourceId: string,
|
||||
serviceId: ServiceIdString,
|
||||
|
@ -124,10 +127,10 @@ const wasDeliveredWithSealedSender = (
|
|||
);
|
||||
|
||||
const shouldDropReceipt = (
|
||||
receipt: MessageReceiptModel,
|
||||
receipt: MessageReceiptAttributesType,
|
||||
message: MessageModel
|
||||
): boolean => {
|
||||
const type = receipt.get('type');
|
||||
const { type } = receipt;
|
||||
switch (type) {
|
||||
case MessageReceiptType.Delivery:
|
||||
return false;
|
||||
|
@ -143,248 +146,245 @@ const shouldDropReceipt = (
|
|||
}
|
||||
};
|
||||
|
||||
export class MessageReceipts extends Collection<MessageReceiptModel> {
|
||||
static getSingleton(): MessageReceipts {
|
||||
if (!singleton) {
|
||||
singleton = new MessageReceipts();
|
||||
}
|
||||
|
||||
return singleton;
|
||||
export function forMessage(
|
||||
message: MessageModel
|
||||
): Array<MessageReceiptAttributesType> {
|
||||
if (!isOutgoing(message.attributes) && !isStory(message.attributes)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
forMessage(message: MessageModel): Array<MessageReceiptModel> {
|
||||
if (!isOutgoing(message.attributes) && !isStory(message.attributes)) {
|
||||
return [];
|
||||
}
|
||||
const logId = `MessageReceipts.forMessage(${getMessageIdForLogging(
|
||||
message.attributes
|
||||
)})`;
|
||||
|
||||
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
||||
const sourceServiceId = getSourceServiceId(message.attributes);
|
||||
if (ourAci !== sourceServiceId) {
|
||||
return [];
|
||||
}
|
||||
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
||||
const sourceServiceId = getSourceServiceId(message.attributes);
|
||||
if (ourAci !== sourceServiceId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const sentAt = getMessageSentTimestamp(message.attributes, { log });
|
||||
const receipts = this.filter(
|
||||
receipt => receipt.get('messageSentAt') === sentAt
|
||||
);
|
||||
if (receipts.length) {
|
||||
log.info(`MessageReceipts: found early receipts for message ${sentAt}`);
|
||||
this.remove(receipts);
|
||||
}
|
||||
return receipts.filter(receipt => {
|
||||
if (shouldDropReceipt(receipt, message)) {
|
||||
log.info(
|
||||
`MessageReceipts: Dropping an early receipt ${receipt.get('type')} ` +
|
||||
`for message ${sentAt}`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
const receiptValues = Array.from(receipts.values());
|
||||
|
||||
return true;
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
private getNewSendStateByConversationId(
|
||||
oldSendStateByConversationId: SendStateByConversationId,
|
||||
receipt: MessageReceiptModel
|
||||
): SendStateByConversationId {
|
||||
const receiptTimestamp = receipt.get('receiptTimestamp');
|
||||
const sourceConversationId = receipt.get('sourceConversationId');
|
||||
const type = receipt.get('type');
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
const newSendState = sendStateReducer(oldSendState, {
|
||||
type: sendActionType,
|
||||
updatedAt: receiptTimestamp,
|
||||
});
|
||||
|
||||
return {
|
||||
...oldSendStateByConversationId,
|
||||
[sourceConversationId]: newSendState,
|
||||
};
|
||||
}
|
||||
|
||||
private async updateMessageSendState(
|
||||
receipt: MessageReceiptModel,
|
||||
message: MessageModel
|
||||
): Promise<void> {
|
||||
const messageSentAt = receipt.get('messageSentAt');
|
||||
|
||||
return result.filter(receipt => {
|
||||
if (shouldDropReceipt(receipt, message)) {
|
||||
log.info(
|
||||
`MessageReceipts: Dropping a receipt ${receipt.get('type')} ` +
|
||||
`for message ${messageSentAt}`
|
||||
`${logId}: Dropping an early receipt ${receipt.type} for message ${sentAt}`
|
||||
);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
let hasChanges = false;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
const editHistory = message.get('editHistory') ?? [];
|
||||
const newEditHistory = editHistory?.map(edit => {
|
||||
if (messageSentAt !== edit.timestamp) {
|
||||
return edit;
|
||||
}
|
||||
function getNewSendStateByConversationId(
|
||||
oldSendStateByConversationId: SendStateByConversationId,
|
||||
receipt: MessageReceiptAttributesType
|
||||
): SendStateByConversationId {
|
||||
const { receiptTimestamp, sourceConversationId, type } = receipt;
|
||||
|
||||
const oldSendStateByConversationId = edit.sendStateByConversationId ?? {};
|
||||
const newSendStateByConversationId = this.getNewSendStateByConversationId(
|
||||
oldSendStateByConversationId,
|
||||
receipt
|
||||
);
|
||||
const oldSendState = getOwn(
|
||||
oldSendStateByConversationId,
|
||||
sourceConversationId
|
||||
) ?? { status: SendStatus.Sent, updatedAt: undefined };
|
||||
|
||||
return {
|
||||
...edit,
|
||||
sendStateByConversationId: newSendStateByConversationId,
|
||||
};
|
||||
});
|
||||
if (!isEqual(newEditHistory, editHistory)) {
|
||||
message.set('editHistory', newEditHistory);
|
||||
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);
|
||||
}
|
||||
|
||||
const newSendState = sendStateReducer(oldSendState, {
|
||||
type: sendActionType,
|
||||
updatedAt: receiptTimestamp,
|
||||
});
|
||||
|
||||
return {
|
||||
...oldSendStateByConversationId,
|
||||
[sourceConversationId]: newSendState,
|
||||
};
|
||||
}
|
||||
|
||||
async function updateMessageSendState(
|
||||
receipt: MessageReceiptAttributesType,
|
||||
message: MessageModel
|
||||
): Promise<void> {
|
||||
const { messageSentAt } = receipt;
|
||||
const logId = `MessageReceipts.updateMessageSendState(sentAt=${receipt.messageSentAt})`;
|
||||
|
||||
if (shouldDropReceipt(receipt, message)) {
|
||||
log.info(
|
||||
`${logId}: Dropping a receipt ${receipt.type} for message ${messageSentAt}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let hasChanges = false;
|
||||
|
||||
const editHistory = message.get('editHistory') ?? [];
|
||||
const newEditHistory = editHistory?.map(edit => {
|
||||
if (messageSentAt !== edit.timestamp) {
|
||||
return edit;
|
||||
}
|
||||
|
||||
const oldSendStateByConversationId = edit.sendStateByConversationId ?? {};
|
||||
const newSendStateByConversationId = getNewSendStateByConversationId(
|
||||
oldSendStateByConversationId,
|
||||
receipt
|
||||
);
|
||||
|
||||
return {
|
||||
...edit,
|
||||
sendStateByConversationId: newSendStateByConversationId,
|
||||
};
|
||||
});
|
||||
if (!isEqual(newEditHistory, editHistory)) {
|
||||
message.set('editHistory', newEditHistory);
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
const editMessageTimestamp = message.get('editMessageTimestamp');
|
||||
if (
|
||||
messageSentAt === message.get('timestamp') ||
|
||||
messageSentAt === editMessageTimestamp
|
||||
) {
|
||||
const oldSendStateByConversationId =
|
||||
message.get('sendStateByConversationId') ?? {};
|
||||
const newSendStateByConversationId = getNewSendStateByConversationId(
|
||||
oldSendStateByConversationId,
|
||||
receipt
|
||||
);
|
||||
|
||||
// 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);
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
const editMessageTimestamp = message.get('editMessageTimestamp');
|
||||
if (
|
||||
messageSentAt === message.get('timestamp') ||
|
||||
messageSentAt === editMessageTimestamp
|
||||
) {
|
||||
const oldSendStateByConversationId =
|
||||
message.get('sendStateByConversationId') ?? {};
|
||||
const newSendStateByConversationId = this.getNewSendStateByConversationId(
|
||||
oldSendStateByConversationId,
|
||||
receipt
|
||||
);
|
||||
|
||||
// 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);
|
||||
hasChanges = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasChanges) {
|
||||
queueUpdateMessage(message.attributes);
|
||||
|
||||
// notify frontend listeners
|
||||
const conversation = window.ConversationController.get(
|
||||
message.get('conversationId')
|
||||
);
|
||||
const updateLeftPane = conversation
|
||||
? conversation.debouncedUpdateLastMessage
|
||||
: undefined;
|
||||
if (updateLeftPane) {
|
||||
updateLeftPane();
|
||||
}
|
||||
}
|
||||
|
||||
const sourceConversationId = receipt.get('sourceConversationId');
|
||||
const type = receipt.get('type');
|
||||
|
||||
if (
|
||||
(type === MessageReceiptType.Delivery &&
|
||||
wasDeliveredWithSealedSender(sourceConversationId, message) &&
|
||||
receipt.get('wasSentEncrypted')) ||
|
||||
type === MessageReceiptType.Read
|
||||
) {
|
||||
const recipient = window.ConversationController.get(sourceConversationId);
|
||||
const recipientServiceId = recipient?.getServiceId();
|
||||
const deviceId = receipt.get('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(
|
||||
`MessageReceipts.onReceipt: Missing serviceId or deviceId for deliveredTo ${sourceConversationId}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async onReceipt(receipt: MessageReceiptModel): Promise<void> {
|
||||
const messageSentAt = receipt.get('messageSentAt');
|
||||
const sourceConversationId = receipt.get('sourceConversationId');
|
||||
const sourceServiceId = receipt.get('sourceServiceId');
|
||||
const type = receipt.get('type');
|
||||
if (hasChanges) {
|
||||
queueUpdateMessage(message.attributes);
|
||||
|
||||
try {
|
||||
const messages = await window.Signal.Data.getMessagesBySentAt(
|
||||
messageSentAt
|
||||
// notify frontend listeners
|
||||
const conversation = window.ConversationController.get(
|
||||
message.get('conversationId')
|
||||
);
|
||||
const updateLeftPane = conversation
|
||||
? conversation.debouncedUpdateLastMessage
|
||||
: undefined;
|
||||
if (updateLeftPane) {
|
||||
updateLeftPane();
|
||||
}
|
||||
}
|
||||
|
||||
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}`
|
||||
);
|
||||
|
||||
const message = await getTargetMessage(
|
||||
sourceConversationId,
|
||||
sourceServiceId,
|
||||
messages
|
||||
);
|
||||
|
||||
if (message) {
|
||||
await this.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(
|
||||
'MessageReceipts: No message for receipt',
|
||||
type,
|
||||
sourceConversationId,
|
||||
sourceServiceId,
|
||||
messageSentAt
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
targetMessages.map(msg => {
|
||||
const model = window.MessageController.register(msg.id, msg);
|
||||
return this.updateMessageSendState(receipt, model);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
this.remove(receipt);
|
||||
} catch (error) {
|
||||
log.error('MessageReceipts.onReceipt error:', Errors.toLogFormat(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function onReceipt(
|
||||
receipt: MessageReceiptAttributesType
|
||||
): Promise<void> {
|
||||
receipts.set(receipt.envelopeId, receipt);
|
||||
|
||||
const { messageSentAt, sourceConversationId, sourceServiceId, type } =
|
||||
receipt;
|
||||
|
||||
const logId = `MessageReceipts.onReceipt(sentAt=${receipt.messageSentAt})`;
|
||||
|
||||
try {
|
||||
const messages = await window.Signal.Data.getMessagesBySentAt(
|
||||
messageSentAt
|
||||
);
|
||||
|
||||
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
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
targetMessages.map(msg => {
|
||||
const model = window.MessageController.register(msg.id, msg);
|
||||
return updateMessageSendState(receipt, model);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
remove(receipt);
|
||||
} catch (error) {
|
||||
remove(receipt);
|
||||
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue