2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { DeleteModel } from '../messageModifiers/Deletes';
|
|
|
|
import type { MessageModel } from '../models/messages';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-03-04 19:22:31 +00:00
|
|
|
import { DAY } from './durations';
|
2022-11-30 19:21:13 +00:00
|
|
|
import { isMe } from './whatTypeOfConversation';
|
2022-08-05 16:05:30 +00:00
|
|
|
import { getContactId } from '../messages/helpers';
|
2022-11-30 19:21:13 +00:00
|
|
|
import { isStory } from '../state/selectors/message';
|
2020-07-27 18:15:32 +00:00
|
|
|
|
|
|
|
export async function deleteForEveryone(
|
2020-09-24 20:57:54 +00:00
|
|
|
message: MessageModel,
|
2021-06-17 17:15:10 +00:00
|
|
|
doe: DeleteModel,
|
2020-09-14 21:56:35 +00:00
|
|
|
shouldPersist = true
|
2020-07-27 18:15:32 +00:00
|
|
|
): Promise<void> {
|
2022-08-05 16:05:30 +00:00
|
|
|
if (isDeletionByMe(message, doe)) {
|
2022-11-30 19:21:13 +00:00
|
|
|
const conversation = message.getConversation();
|
|
|
|
|
|
|
|
// Our 1:1 stories are deleted through ts/util/onStoryRecipientUpdate.ts
|
|
|
|
if (
|
|
|
|
isStory(message.attributes) &&
|
|
|
|
conversation &&
|
|
|
|
isMe(conversation.attributes)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-05 16:05:30 +00:00
|
|
|
await message.handleDeleteForEveryone(doe, shouldPersist);
|
|
|
|
return;
|
|
|
|
}
|
2021-08-13 16:02:28 +00:00
|
|
|
|
2022-08-05 16:05:30 +00:00
|
|
|
if (isDeletionTooOld(message, doe)) {
|
|
|
|
log.warn('Received late DOE. Dropping.', {
|
2020-07-27 18:15:32 +00:00
|
|
|
fromId: doe.get('fromId'),
|
|
|
|
targetSentTimestamp: doe.get('targetSentTimestamp'),
|
|
|
|
messageServerTimestamp: message.get('serverTimestamp'),
|
2021-08-13 16:02:28 +00:00
|
|
|
messageSentAt: message.get('sent_at'),
|
2020-07-27 18:15:32 +00:00
|
|
|
deleteServerTimestamp: doe.get('serverTimestamp'),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await message.handleDeleteForEveryone(doe, shouldPersist);
|
|
|
|
}
|
2022-08-05 16:05:30 +00:00
|
|
|
|
|
|
|
function isDeletionByMe(
|
|
|
|
message: Readonly<MessageModel>,
|
|
|
|
doe: Readonly<DeleteModel>
|
|
|
|
): boolean {
|
|
|
|
const ourConversationId =
|
|
|
|
window.ConversationController.getOurConversationIdOrThrow();
|
|
|
|
return (
|
|
|
|
getContactId(message.attributes) === ourConversationId &&
|
|
|
|
doe.get('fromId') === ourConversationId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDeletionTooOld(
|
|
|
|
message: Readonly<MessageModel>,
|
|
|
|
doe: Readonly<DeleteModel>
|
|
|
|
): boolean {
|
|
|
|
const messageTimestamp =
|
|
|
|
message.get('serverTimestamp') || message.get('sent_at') || 0;
|
|
|
|
const delta = Math.abs(doe.get('serverTimestamp') - messageTimestamp);
|
|
|
|
return delta > DAY;
|
|
|
|
}
|