signal-desktop/js/expiring_messages.js

131 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-06-25 22:17:49 +00:00
/* global _: false */
/* global Backbone: false */
/* global i18n: false */
/* global moment: false */
/* global Whisper: false */
/* global wrapDeferred: false */
// eslint-disable-next-line func-names
2018-04-27 21:25:04 +00:00
(function() {
'use strict';
2018-06-25 22:17:49 +00:00
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
function destroyExpiredMessages() {
// Load messages that have expired and destroy them
2018-06-25 22:17:49 +00:00
const expired = new Whisper.MessageCollection();
expired.on('add', async message => {
console.log('Message expired', {
sentAt: message.get('sent_at'),
});
2018-06-25 22:17:49 +00:00
const conversation = message.getConversation();
2018-04-27 21:25:04 +00:00
if (conversation) {
conversation.trigger('expired', message);
}
2018-04-27 21:25:04 +00:00
// We delete after the trigger to allow the conversation time to process
// the expiration before the message is removed from the database.
await wrapDeferred(message.destroy());
if (conversation) {
conversation.updateLastMessage();
}
2018-04-27 21:25:04 +00:00
});
expired.on('reset', throttledCheckExpiringMessages);
2018-04-27 21:25:04 +00:00
expired.fetchExpired();
}
2018-06-25 22:17:49 +00:00
let timeout;
2018-04-27 21:25:04 +00:00
function checkExpiringMessages() {
// Look up the next expiring message and set a timer to destroy it
2018-06-25 22:17:49 +00:00
const expiring = new Whisper.MessageCollection();
expiring.once('add', next => {
const expiresAt = next.get('expires_at');
console.log('next message expires', new Date(expiresAt).toISOString());
2018-06-25 22:17:49 +00:00
let wait = expiresAt - Date.now();
2018-04-27 21:25:04 +00:00
// In the past
if (wait < 0) {
wait = 0;
}
2018-04-27 21:25:04 +00:00
// Too far in the future, since it's limited to a 32-bit value
if (wait > 2147483647) {
wait = 2147483647;
}
2018-04-27 21:25:04 +00:00
clearTimeout(timeout);
timeout = setTimeout(destroyExpiredMessages, wait);
});
expiring.fetchNextExpiring();
}
2018-06-25 22:17:49 +00:00
const throttledCheckExpiringMessages = _.throttle(
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();
events.on('timetravel', throttledCheckExpiringMessages);
},
update: throttledCheckExpiringMessages,
};
2018-06-25 22:17:49 +00:00
const TimerOption = Backbone.Model.extend({
getName() {
2018-04-27 21:25:04 +00:00
return (
i18n(['timerOption', this.get('time'), this.get('unit')].join('_')) ||
moment.duration(this.get('time'), this.get('unit')).humanize()
);
},
2018-06-25 22:17:49 +00:00
getAbbreviated() {
2018-04-27 21:25:04 +00:00
return i18n(
['timerOption', this.get('time'), this.get('unit'), 'abbreviated'].join(
'_'
)
);
},
});
Whisper.ExpirationTimerOptions = new (Backbone.Collection.extend({
model: TimerOption,
2018-06-25 22:17:49 +00:00
getName(seconds = 0) {
const o = this.findWhere({ seconds });
2018-04-27 21:25:04 +00:00
if (o) {
return o.getName();
}
2018-06-25 22:17:49 +00:00
return [seconds, 'seconds'].join(' ');
2018-04-27 21:25:04 +00:00
},
2018-06-25 22:17:49 +00:00
getAbbreviated(seconds = 0) {
const o = this.findWhere({ seconds });
2018-04-27 21:25:04 +00:00
if (o) {
return o.getAbbreviated();
}
2018-06-25 22:17:49 +00:00
return [seconds, 's'].join('');
2018-04-27 21:25:04 +00:00
},
}))(
[
[0, 'seconds'],
[5, 'seconds'],
[10, 'seconds'],
[30, 'seconds'],
[1, 'minute'],
[5, 'minutes'],
[30, 'minutes'],
[1, 'hour'],
[6, 'hours'],
[12, 'hours'],
[1, 'day'],
[1, 'week'],
2018-06-25 22:17:49 +00:00
].map(o => {
const duration = moment.duration(o[0], o[1]); // 5, 'seconds'
return {
time: o[0],
unit: o[1],
2018-04-27 21:25:04 +00:00
seconds: duration.asSeconds(),
};
2018-04-27 21:25:04 +00:00
})
);
})();