2021-06-17 17:15:10 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
|
|
|
|
import { Collection, Model } from 'backbone';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MessageModel } from '../models/messages';
|
2021-12-10 22:51:54 +00:00
|
|
|
import { getContactId } from '../messages/helpers';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type DeleteAttributesType = {
|
2021-06-17 17:15:10 +00:00
|
|
|
targetSentTimestamp: number;
|
|
|
|
serverTimestamp: number;
|
|
|
|
fromId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class DeleteModel extends Model<DeleteAttributesType> {}
|
|
|
|
|
|
|
|
let singleton: Deletes | undefined;
|
|
|
|
|
|
|
|
export class Deletes extends Collection<DeleteModel> {
|
|
|
|
static getSingleton(): Deletes {
|
|
|
|
if (!singleton) {
|
|
|
|
singleton = new Deletes();
|
|
|
|
}
|
|
|
|
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
forMessage(message: MessageModel): Array<DeleteModel> {
|
|
|
|
const matchingDeletes = this.filter(item => {
|
|
|
|
return (
|
|
|
|
item.get('targetSentTimestamp') === message.get('sent_at') &&
|
2021-12-10 22:51:54 +00:00
|
|
|
item.get('fromId') === getContactId(message.attributes)
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (matchingDeletes.length > 0) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Found early DOE for message');
|
2021-06-17 17:15:10 +00:00
|
|
|
this.remove(matchingDeletes);
|
|
|
|
return matchingDeletes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async onDelete(del: DeleteModel): Promise<void> {
|
|
|
|
try {
|
|
|
|
// The conversation the deleted message was in; we have to find it in the database
|
|
|
|
// to to figure that out.
|
2021-11-11 22:43:05 +00:00
|
|
|
const targetConversation =
|
|
|
|
await window.ConversationController.getConversationForTargetMessage(
|
|
|
|
del.get('fromId'),
|
|
|
|
del.get('targetSentTimestamp')
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
if (!targetConversation) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
'No target conversation for DOE',
|
|
|
|
del.get('fromId'),
|
|
|
|
del.get('targetSentTimestamp')
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not await, since this can deadlock the queue
|
|
|
|
targetConversation.queueJob('Deletes.onDelete', async () => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Handling DOE for', del.get('targetSentTimestamp'));
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
2021-12-10 22:51:54 +00:00
|
|
|
del.get('targetSentTimestamp')
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const targetMessage = messages.find(
|
2021-12-10 22:51:54 +00:00
|
|
|
m => del.get('fromId') === getContactId(m)
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!targetMessage) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
'No message for DOE',
|
|
|
|
del.get('fromId'),
|
|
|
|
del.get('targetSentTimestamp')
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = window.MessageController.register(
|
|
|
|
targetMessage.id,
|
|
|
|
targetMessage
|
|
|
|
);
|
|
|
|
|
|
|
|
await window.Signal.Util.deleteForEveryone(message, del);
|
|
|
|
|
|
|
|
this.remove(del);
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-06-17 17:15:10 +00:00
|
|
|
'Deletes.onDelete error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|