2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2022-05-31 23:53:14 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { debounce } from 'lodash';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader } from '../sql/Client';
|
2022-05-31 23:53:14 +00:00
|
|
|
import { clearTimeoutIfNecessary } from '../util/clearTimeoutIfNecessary';
|
2024-10-15 22:43:06 +00:00
|
|
|
import { getMessageQueueTime } from '../util/getMessageQueueTime';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2022-05-31 23:53:14 +00:00
|
|
|
|
|
|
|
async function eraseTapToViewMessages() {
|
|
|
|
try {
|
|
|
|
window.SignalContext.log.info(
|
|
|
|
'eraseTapToViewMessages: Loading messages...'
|
|
|
|
);
|
2024-10-15 22:43:06 +00:00
|
|
|
const maxTimestamp = Date.now() - getMessageQueueTime();
|
|
|
|
const messages =
|
|
|
|
await DataReader.getTapToViewMessagesNeedingErase(maxTimestamp);
|
2022-05-31 23:53:14 +00:00
|
|
|
await Promise.all(
|
|
|
|
messages.map(async fromDB => {
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$register(
|
|
|
|
fromDB.id,
|
|
|
|
fromDB,
|
|
|
|
'eraseTapToViewMessages'
|
|
|
|
);
|
2022-05-31 23:53:14 +00:00
|
|
|
|
|
|
|
window.SignalContext.log.info(
|
|
|
|
'eraseTapToViewMessages: erasing message contents',
|
|
|
|
message.idForLogging()
|
|
|
|
);
|
|
|
|
|
2022-06-23 00:21:38 +00:00
|
|
|
// We do this to update the UI, if this message is being displayed somewhere
|
|
|
|
message.trigger('expired');
|
2022-12-20 17:50:23 +00:00
|
|
|
window.reduxActions.conversations.messageExpired(message.id);
|
2022-06-23 00:21:38 +00:00
|
|
|
|
2022-05-31 23:53:14 +00:00
|
|
|
await message.eraseContents();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
window.SignalContext.log.error(
|
|
|
|
'eraseTapToViewMessages: Error erasing messages',
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2022-05-31 23:53:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.SignalContext.log.info('eraseTapToViewMessages: complete');
|
|
|
|
}
|
|
|
|
|
|
|
|
class TapToViewMessagesDeletionService {
|
|
|
|
public update: typeof this.checkTapToViewMessages;
|
|
|
|
|
|
|
|
private timeout?: ReturnType<typeof setTimeout>;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.update = debounce(this.checkTapToViewMessages, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async checkTapToViewMessages() {
|
|
|
|
const receivedAt =
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataReader.getNextTapToViewMessageTimestampToAgeOut();
|
2022-05-31 23:53:14 +00:00
|
|
|
if (!receivedAt) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-15 22:43:06 +00:00
|
|
|
const nextCheck = receivedAt + getMessageQueueTime();
|
2022-05-31 23:53:14 +00:00
|
|
|
window.SignalContext.log.info(
|
|
|
|
'checkTapToViewMessages: next check at',
|
|
|
|
new Date(nextCheck).toISOString()
|
|
|
|
);
|
|
|
|
|
|
|
|
let wait = nextCheck - Date.now();
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeoutIfNecessary(this.timeout);
|
|
|
|
this.timeout = setTimeout(async () => {
|
|
|
|
await eraseTapToViewMessages();
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.update();
|
2022-05-31 23:53:14 +00:00
|
|
|
}, wait);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const tapToViewMessagesDeletionService =
|
|
|
|
new TapToViewMessagesDeletionService();
|