signal-desktop/js/expiring_messages.js

107 lines
3 KiB
JavaScript
Raw Normal View History

2021-06-16 22:20:17 +00:00
// Copyright 2016-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
/* global
_,
MessageController,
Whisper
*/
2018-06-25 22:17:49 +00:00
// eslint-disable-next-line func-names
(function () {
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
async function destroyExpiredMessages() {
try {
window.log.info('destroyExpiredMessages: Loading messages...');
const messages = await window.Signal.Data.getExpiredMessages({
MessageCollection: Whisper.MessageCollection,
});
2021-06-16 22:20:17 +00:00
window.log.info(
`destroyExpiredMessages: found ${messages.length} messages to expire`
);
2021-03-04 21:44:57 +00:00
const messageIds = [];
const inMemoryMessages = [];
const messageCleanup = [];
messages.forEach(dbMessage => {
const message = MessageController.register(dbMessage.id, dbMessage);
messageIds.push(message.id);
inMemoryMessages.push(message);
messageCleanup.push(message.cleanup());
});
// We delete after the trigger to allow the conversation time to process
// the expiration before the message is removed from the database.
await window.Signal.Data.removeMessages(messageIds);
await Promise.all(messageCleanup);
inMemoryMessages.forEach(message => {
window.log.info('Message expired', {
sentAt: message.get('sent_at'),
});
const conversation = message.getConversation();
if (conversation) {
// An expired message only counts as decrementing the message count, not
// the sent message count
conversation.decrementMessageCount();
2021-03-04 21:44:57 +00:00
}
});
} catch (error) {
window.log.error(
'destroyExpiredMessages: Error deleting expired messages',
error && error.stack ? error.stack : error
);
}
window.log.info('destroyExpiredMessages: complete');
checkExpiringMessages();
2018-04-27 21:25:04 +00:00
}
2018-06-25 22:17:49 +00:00
let timeout;
async function checkExpiringMessages() {
2021-06-16 22:20:17 +00:00
window.log.info('checkExpiringMessages: checking for expiring messages');
2021-06-16 22:20:17 +00:00
const soonestExpiry = await window.Signal.Data.getSoonestMessageExpiry();
if (!soonestExpiry) {
window.log.info('checkExpiringMessages: found no messages to expire');
return;
}
2021-06-16 22:20:17 +00:00
let wait = soonestExpiry - 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;
}
2021-06-16 22:20:17 +00:00
window.log.info(
`checkExpiringMessages: next message expires ${new Date(
soonestExpiry
).toISOString()}; waiting ${wait} ms before clearing`
);
clearTimeout(timeout);
timeout = setTimeout(destroyExpiredMessages, wait);
2018-04-27 21:25:04 +00:00
}
2019-09-26 19:56:31 +00:00
const debouncedCheckExpiringMessages = _.debounce(
2018-06-25 22:17:49 +00:00
checkExpiringMessages,
1000
);
2018-04-27 21:25:04 +00:00
Whisper.ExpiringMessagesListener = {
2018-06-25 22:17:49 +00:00
init(events) {
2018-04-27 21:25:04 +00:00
checkExpiringMessages();
2019-09-26 19:56:31 +00:00
events.on('timetravel', debouncedCheckExpiringMessages);
2018-04-27 21:25:04 +00:00
},
2019-09-26 19:56:31 +00:00
update: debouncedCheckExpiringMessages,
2018-04-27 21:25:04 +00:00
};
})();