2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2021-06-17 17:15:10 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
import type { MessageAttributesType } from '../model-types.d';
|
2024-04-29 21:20:20 +00:00
|
|
|
import { getAuthorId } from '../messages/helpers';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2022-07-13 23:09:18 +00:00
|
|
|
import { deleteForEveryone } from '../util/deleteForEveryone';
|
2022-12-21 18:41:48 +00:00
|
|
|
import { drop } from '../util/drop';
|
2023-05-16 17:37:12 +00:00
|
|
|
import { getMessageSentTimestampSet } from '../util/getMessageSentTimestampSet';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type DeleteAttributesType = {
|
2023-08-07 16:36:37 +00:00
|
|
|
envelopeId: string;
|
2021-06-17 17:15:10 +00:00
|
|
|
targetSentTimestamp: number;
|
|
|
|
serverTimestamp: number;
|
|
|
|
fromId: string;
|
2023-08-07 16:36:37 +00:00
|
|
|
removeFromMessageReceiverCache: () => unknown;
|
2021-06-17 17:15:10 +00:00
|
|
|
};
|
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
const deletes = new Map<string, DeleteAttributesType>();
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
function remove(del: DeleteAttributesType): void {
|
|
|
|
del.removeFromMessageReceiverCache();
|
|
|
|
deletes.delete(del.envelopeId);
|
|
|
|
}
|
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
export function forMessage(
|
|
|
|
messageAttributes: MessageAttributesType
|
|
|
|
): Array<DeleteAttributesType> {
|
|
|
|
const sentTimestamps = getMessageSentTimestampSet(messageAttributes);
|
2023-08-21 20:08:27 +00:00
|
|
|
const deleteValues = Array.from(deletes.values());
|
|
|
|
|
|
|
|
const matchingDeletes = deleteValues.filter(item => {
|
2023-08-07 16:36:37 +00:00
|
|
|
return (
|
2024-04-29 21:20:20 +00:00
|
|
|
item.fromId === getAuthorId(messageAttributes) &&
|
2023-08-07 16:36:37 +00:00
|
|
|
sentTimestamps.has(item.targetSentTimestamp)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (!matchingDeletes.length) {
|
|
|
|
return [];
|
2023-08-07 16:36:37 +00:00
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
log.info('Found early DOE for message');
|
|
|
|
matchingDeletes.forEach(del => {
|
|
|
|
remove(del);
|
|
|
|
});
|
|
|
|
return matchingDeletes;
|
2023-08-07 16:36:37 +00:00
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
export async function onDelete(del: DeleteAttributesType): Promise<void> {
|
|
|
|
deletes.set(del.envelopeId, del);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
const logId = `Deletes.onDelete(timestamp=${del.targetSentTimestamp})`;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
try {
|
|
|
|
// The conversation the deleted message was in; we have to find it in the database
|
|
|
|
// to to figure that out.
|
|
|
|
const targetConversation =
|
|
|
|
await window.ConversationController.getConversationForTargetMessage(
|
|
|
|
del.fromId,
|
|
|
|
del.targetSentTimestamp
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
if (!targetConversation) {
|
|
|
|
log.info(`${logId}: No message for DOE`);
|
|
|
|
return;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
// Do not await, since this can deadlock the queue
|
|
|
|
drop(
|
|
|
|
targetConversation.queueJob('Deletes.onDelete', async () => {
|
|
|
|
log.info(`${logId}: Handling DOE`);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
del.targetSentTimestamp
|
2021-11-11 22:43:05 +00:00
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
const targetMessage = messages.find(
|
2024-04-29 21:20:20 +00:00
|
|
|
m => del.fromId === getAuthorId(m) && !m.deletedForEveryone
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
if (!targetMessage) {
|
|
|
|
log.info(`${logId}: No message for DOE 2`);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$register(
|
2023-08-07 16:36:37 +00:00
|
|
|
targetMessage.id,
|
2023-10-04 00:12:57 +00:00
|
|
|
targetMessage,
|
|
|
|
'Deletes.onDelete'
|
2023-08-07 16:36:37 +00:00
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
await deleteForEveryone(message, del);
|
2022-12-21 18:41:48 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
remove(del);
|
2023-08-07 16:36:37 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (error) {
|
2023-08-21 20:08:27 +00:00
|
|
|
remove(del);
|
2023-08-07 16:36:37 +00:00
|
|
|
log.error(`${logId}: error`, Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|