2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2016 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-09-07 20:07:07 +00:00
|
|
|
import { batch } from 'react-redux';
|
2022-05-31 23:53:14 +00:00
|
|
|
import { debounce } from 'lodash';
|
2022-06-20 18:55:34 +00:00
|
|
|
|
2022-05-31 23:53:14 +00:00
|
|
|
import { clearTimeoutIfNecessary } from '../util/clearTimeoutIfNecessary';
|
2022-06-20 18:55:34 +00:00
|
|
|
import { sleep } from '../util/sleep';
|
|
|
|
import { SECOND } from '../util/durations';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2024-06-17 19:24:39 +00:00
|
|
|
import * as log from '../logging/log';
|
|
|
|
|
|
|
|
import type { MessageModel } from '../models/messages';
|
|
|
|
import type { SingleProtoJobQueue } from '../jobs/singleProtoJobQueue';
|
2018-06-25 22:17:49 +00:00
|
|
|
|
2022-05-31 23:53:14 +00:00
|
|
|
class ExpiringMessagesDeletionService {
|
|
|
|
public update: typeof this.checkExpiringMessages;
|
2017-02-21 23:32:40 +00:00
|
|
|
|
2022-05-31 23:53:14 +00:00
|
|
|
private timeout?: ReturnType<typeof setTimeout>;
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
constructor(private readonly singleProtoJobQueue: SingleProtoJobQueue) {
|
2022-05-31 23:53:14 +00:00
|
|
|
this.update = debounce(this.checkExpiringMessages, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async destroyExpiredMessages() {
|
2018-08-03 04:12:27 +00:00
|
|
|
try {
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.info(
|
2021-09-17 18:27:53 +00:00
|
|
|
'destroyExpiredMessages: Loading messages...'
|
|
|
|
);
|
2022-05-31 23:53:14 +00:00
|
|
|
const messages = await window.Signal.Data.getExpiredMessages();
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.info(
|
2021-06-16 22:20:17 +00:00
|
|
|
`destroyExpiredMessages: found ${messages.length} messages to expire`
|
|
|
|
);
|
2018-08-03 04:12:27 +00:00
|
|
|
|
2022-05-31 23:53:14 +00:00
|
|
|
const messageIds: Array<string> = [];
|
|
|
|
const inMemoryMessages: Array<MessageModel> = [];
|
2021-03-04 21:44:57 +00:00
|
|
|
|
|
|
|
messages.forEach(dbMessage => {
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$register(
|
2022-05-31 23:53:14 +00:00
|
|
|
dbMessage.id,
|
2023-10-04 00:12:57 +00:00
|
|
|
dbMessage,
|
|
|
|
'destroyExpiredMessages'
|
2022-05-31 23:53:14 +00:00
|
|
|
);
|
2021-03-04 21:44:57 +00:00
|
|
|
messageIds.push(message.id);
|
|
|
|
inMemoryMessages.push(message);
|
|
|
|
});
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
await window.Signal.Data.removeMessages(messageIds, {
|
|
|
|
singleProtoJobQueue: this.singleProtoJobQueue,
|
|
|
|
});
|
2021-03-04 21:44:57 +00:00
|
|
|
|
2023-09-07 20:07:07 +00:00
|
|
|
batch(() => {
|
|
|
|
inMemoryMessages.forEach(message => {
|
|
|
|
window.SignalContext.log.info('Message expired', {
|
|
|
|
sentAt: message.get('sent_at'),
|
|
|
|
});
|
2021-03-04 21:44:57 +00:00
|
|
|
|
2023-09-07 20:07:07 +00:00
|
|
|
// We do this to update the UI, if this message is being displayed somewhere
|
|
|
|
message.trigger('expired');
|
|
|
|
window.reduxActions.conversations.messageExpired(message.id);
|
|
|
|
});
|
2021-03-04 21:44:57 +00:00
|
|
|
});
|
2018-08-03 04:12:27 +00:00
|
|
|
} catch (error) {
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.error(
|
2018-08-03 04:12:27 +00:00
|
|
|
'destroyExpiredMessages: Error deleting expired messages',
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2018-08-03 04:12:27 +00:00
|
|
|
);
|
2022-06-20 18:55:34 +00:00
|
|
|
window.SignalContext.log.info(
|
|
|
|
'destroyExpiredMessages: Waiting 30 seconds before trying again'
|
|
|
|
);
|
|
|
|
await sleep(30 * SECOND);
|
2018-08-03 04:12:27 +00:00
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
|
2022-06-20 18:55:34 +00:00
|
|
|
window.SignalContext.log.info(
|
|
|
|
'destroyExpiredMessages: done, scheduling another check'
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.update();
|
2018-04-27 21:25:04 +00:00
|
|
|
}
|
2017-02-21 23:32:40 +00:00
|
|
|
|
2022-05-31 23:53:14 +00:00
|
|
|
private async checkExpiringMessages() {
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.info(
|
2021-09-17 18:27:53 +00:00
|
|
|
'checkExpiringMessages: checking for expiring messages'
|
|
|
|
);
|
2017-02-21 23:32:40 +00:00
|
|
|
|
2021-06-16 22:20:17 +00:00
|
|
|
const soonestExpiry = await window.Signal.Data.getSoonestMessageExpiry();
|
|
|
|
if (!soonestExpiry) {
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.info(
|
2021-09-17 18:27:53 +00:00
|
|
|
'checkExpiringMessages: found no messages to expire'
|
|
|
|
);
|
2018-07-25 22:02:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-10 18:16:20 +00:00
|
|
|
|
2021-06-16 22:20:17 +00:00
|
|
|
let wait = soonestExpiry - Date.now();
|
2017-08-10 18:16:20 +00:00
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
// In the past
|
|
|
|
if (wait < 0) {
|
|
|
|
wait = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Too far in the future, since it's limited to a 32-bit value
|
|
|
|
if (wait > 2147483647) {
|
|
|
|
wait = 2147483647;
|
|
|
|
}
|
|
|
|
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.info(
|
2021-06-16 22:20:17 +00:00
|
|
|
`checkExpiringMessages: next message expires ${new Date(
|
|
|
|
soonestExpiry
|
|
|
|
).toISOString()}; waiting ${wait} ms before clearing`
|
|
|
|
);
|
|
|
|
|
2022-05-31 23:53:14 +00:00
|
|
|
clearTimeoutIfNecessary(this.timeout);
|
|
|
|
this.timeout = setTimeout(this.destroyExpiredMessages.bind(this), wait);
|
2018-04-27 21:25:04 +00:00
|
|
|
}
|
2022-05-31 23:53:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 19:24:39 +00:00
|
|
|
// Because this service is used inside of Client.ts, it can't directly reference
|
|
|
|
// SingleProtoJobQueue. Instead of direct access, it is provided once on startup.
|
|
|
|
export function initialize(singleProtoJobQueue: SingleProtoJobQueue): void {
|
|
|
|
if (instance) {
|
|
|
|
log.warn('Expiring Messages Deletion service is already initialized!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
instance = new ExpiringMessagesDeletionService(singleProtoJobQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function update(): Promise<void> {
|
|
|
|
if (!instance) {
|
|
|
|
throw new Error('Expiring Messages Deletion service not yet initialized!');
|
|
|
|
}
|
|
|
|
await instance.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
let instance: ExpiringMessagesDeletionService;
|