signal-desktop/ts/messageModifiers/Edits.ts

152 lines
4 KiB
TypeScript
Raw Normal View History

2023-03-27 23:48:57 +00:00
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { MessageAttributesType } from '../model-types.d';
import * as Errors from '../types/errors';
import * as log from '../logging/log';
import { drop } from '../util/drop';
import { getAuthorId } from '../messages/helpers';
2023-03-27 23:48:57 +00:00
import { handleEditMessage } from '../util/handleEditMessage';
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
import {
isAttachmentDownloadQueueEmpty,
registerQueueEmptyCallback,
} from '../util/attachmentDownloadQueue';
2023-03-27 23:48:57 +00:00
export type EditAttributesType = {
conversationId: string;
envelopeId: string;
2023-03-27 23:48:57 +00:00
fromId: string;
fromDevice: number;
2023-03-27 23:48:57 +00:00
message: MessageAttributesType;
targetSentTimestamp: number;
removeFromMessageReceiverCache: () => unknown;
2023-03-27 23:48:57 +00:00
};
const edits = new Map<string, EditAttributesType>();
2023-03-27 23:48:57 +00:00
function remove(edit: EditAttributesType): void {
edits.delete(edit.envelopeId);
edit.removeFromMessageReceiverCache();
}
2023-08-08 16:26:22 +00:00
export function forMessage(
messageAttributes: Pick<
MessageAttributesType,
| 'editMessageTimestamp'
| 'sent_at'
| 'source'
2023-08-16 20:54:39 +00:00
| 'sourceServiceId'
2023-08-08 16:26:22 +00:00
| 'timestamp'
| 'type'
>
): Array<EditAttributesType> {
const sentAt = getMessageSentTimestamp(messageAttributes, { log });
const editValues = Array.from(edits.values());
if (!isAttachmentDownloadQueueEmpty()) {
log.info(
'Edits.forMessage attachmentDownloadQueue not empty, not processing edits'
);
registerQueueEmptyCallback(flushEdits);
return [];
}
const matchingEdits = editValues.filter(item => {
2023-03-27 23:48:57 +00:00
return (
item.targetSentTimestamp === sentAt &&
item.fromId === getAuthorId(messageAttributes)
2023-03-27 23:48:57 +00:00
);
});
if (matchingEdits.length > 0) {
const editsLogIds: Array<number> = [];
const result = matchingEdits.map(item => {
editsLogIds.push(item.message.sent_at);
remove(item);
return item;
});
log.info(
2023-08-08 16:26:22 +00:00
`Edits.forMessage(${messageAttributes.sent_at}): ` +
`Found early edits for message ${editsLogIds.join(', ')}`
);
return result;
2023-03-27 23:48:57 +00:00
}
return [];
}
export async function flushEdits(): Promise<void> {
log.info('Edits.flushEdits running');
return drop(
Promise.all(Array.from(edits.values()).map(edit => onEdit(edit)))
);
}
2023-03-27 23:48:57 +00:00
export async function onEdit(edit: EditAttributesType): Promise<void> {
edits.set(edit.envelopeId, edit);
2023-03-27 23:48:57 +00:00
2023-07-05 22:38:37 +00:00
const logId = `Edits.onEdit(timestamp=${edit.message.timestamp};target=${edit.targetSentTimestamp})`;
if (!isAttachmentDownloadQueueEmpty()) {
log.info(
`${logId}: attachmentDownloadQueue not empty, not processing edits`
);
registerQueueEmptyCallback(flushEdits);
return;
}
2023-03-27 23:48:57 +00:00
try {
// The conversation the edited message was in; we have to find it in the database
2023-03-27 23:48:57 +00:00
// to to figure that out.
const targetConversation =
await window.ConversationController.getConversationForTargetMessage(
edit.fromId,
edit.targetSentTimestamp
);
if (!targetConversation) {
2023-08-08 16:26:22 +00:00
log.info(`${logId}: No message found`);
2023-03-27 23:48:57 +00:00
return;
}
// Do not await, since this can deadlock the queue
drop(
targetConversation.queueJob('Edits.onEdit', async () => {
2023-07-05 22:38:37 +00:00
log.info(`${logId}: Handling edit`);
2023-03-27 23:48:57 +00:00
const messages = await window.Signal.Data.getMessagesBySentAt(
edit.targetSentTimestamp
);
// Verify authorship
const targetMessage = messages.find(
m =>
edit.conversationId === m.conversationId &&
edit.fromId === getAuthorId(m)
2023-03-27 23:48:57 +00:00
);
if (!targetMessage) {
2023-07-05 22:38:37 +00:00
log.info(`${logId}: No message`);
2023-03-27 23:48:57 +00:00
return;
}
const message = window.MessageCache.__DEPRECATED$register(
2023-03-27 23:48:57 +00:00
targetMessage.id,
targetMessage,
'Edits.onEdit'
2023-03-27 23:48:57 +00:00
);
await handleEditMessage(message.attributes, edit);
remove(edit);
2023-03-27 23:48:57 +00:00
})
);
} catch (error) {
remove(edit);
2023-07-05 22:38:37 +00:00
log.error(`${logId} error:`, Errors.toLogFormat(error));
2023-03-27 23:48:57 +00:00
}
}