signal-desktop/js/expiring_tap_to_view_messages.js

97 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2019-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2019-06-26 19:33:13 +00:00
/* global
_,
MessageController,
Whisper
*/
// eslint-disable-next-line func-names
(function () {
2019-06-26 19:33:13 +00:00
window.Whisper = window.Whisper || {};
async function eraseTapToViewMessages() {
try {
window.log.info('eraseTapToViewMessages: Loading messages...');
const messages = await window.Signal.Data.getTapToViewMessagesNeedingErase(
{
MessageCollection: Whisper.MessageCollection,
}
);
await Promise.all(
messages.map(async fromDB => {
const message = MessageController.register(fromDB.id, fromDB);
window.log.info(
'eraseTapToViewMessages: message data erased',
message.idForLogging()
);
await message.eraseContents();
})
);
} catch (error) {
window.log.error(
'eraseTapToViewMessages: Error erasing messages',
error && error.stack ? error.stack : error
);
}
window.log.info('eraseTapToViewMessages: complete');
}
let timeout;
async function checkTapToViewMessages() {
const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const THIRTY_DAYS = 30 * 24 * HOUR;
const receivedAt = await window.Signal.Data.getNextTapToViewMessageTimestampToAgeOut();
if (!receivedAt) {
2019-06-26 19:33:13 +00:00
return;
}
2021-03-04 21:44:57 +00:00
const nextCheck = receivedAt + THIRTY_DAYS;
2019-06-26 19:33:13 +00:00
Whisper.TapToViewMessagesListener.nextCheck = nextCheck;
window.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;
}
clearTimeout(timeout);
timeout = setTimeout(async () => {
await eraseTapToViewMessages();
checkTapToViewMessages();
}, wait);
}
2019-09-26 19:56:31 +00:00
const debouncedCheckTapToViewMessages = _.debounce(
2019-06-26 19:33:13 +00:00
checkTapToViewMessages,
1000
);
Whisper.TapToViewMessagesListener = {
nextCheck: null,
init(events) {
checkTapToViewMessages();
2019-09-26 19:56:31 +00:00
events.on('timetravel', debouncedCheckTapToViewMessages);
2019-06-26 19:33:13 +00:00
},
2019-09-26 19:56:31 +00:00
update: debouncedCheckTapToViewMessages,
2019-06-26 19:33:13 +00:00
};
})();