2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
2017-02-21 23:32:40 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
function destroyExpiredMessages() {
|
|
|
|
// Load messages that have expired and destroy them
|
|
|
|
var expired = new Whisper.MessageCollection();
|
2018-06-25 21:51:06 +00:00
|
|
|
expired.on('add', async function(message) {
|
2018-05-03 17:10:44 +00:00
|
|
|
console.log('Message expired', {
|
|
|
|
sentAt: message.get('sent_at'),
|
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
var conversation = message.getConversation();
|
|
|
|
if (conversation) {
|
|
|
|
conversation.trigger('expired', message);
|
|
|
|
}
|
2017-08-04 18:45:49 +00:00
|
|
|
|
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.
|
2018-06-25 21:51:06 +00:00
|
|
|
await wrapDeferred(message.destroy());
|
|
|
|
if (conversation) {
|
|
|
|
conversation.updateLastMessage();
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
|
|
|
expired.on('reset', throttledCheckExpiringMessages);
|
2017-02-21 23:32:40 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
expired.fetchExpired();
|
|
|
|
}
|
2017-02-21 23:32:40 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
var timeout;
|
|
|
|
function checkExpiringMessages() {
|
|
|
|
// Look up the next expiring message and set a timer to destroy it
|
|
|
|
var expiring = new Whisper.MessageCollection();
|
|
|
|
expiring.once('add', function(next) {
|
|
|
|
var expires_at = next.get('expires_at');
|
|
|
|
console.log('next message expires', new Date(expires_at).toISOString());
|
2017-02-21 23:32:40 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
var wait = expires_at - Date.now();
|
2017-08-10 18:16:20 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
// In the past
|
|
|
|
if (wait < 0) {
|
|
|
|
wait = 0;
|
|
|
|
}
|
2017-02-21 23:32:40 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2017-08-10 18:16:20 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(destroyExpiredMessages, wait);
|
|
|
|
});
|
|
|
|
expiring.fetchNextExpiring();
|
|
|
|
}
|
|
|
|
var throttledCheckExpiringMessages = _.throttle(checkExpiringMessages, 1000);
|
2017-02-21 23:32:40 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.ExpiringMessagesListener = {
|
|
|
|
init: function(events) {
|
|
|
|
checkExpiringMessages();
|
|
|
|
events.on('timetravel', throttledCheckExpiringMessages);
|
|
|
|
},
|
|
|
|
update: throttledCheckExpiringMessages,
|
|
|
|
};
|
2016-09-28 23:47:57 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
var TimerOption = Backbone.Model.extend({
|
|
|
|
getName: function() {
|
|
|
|
return (
|
|
|
|
i18n(['timerOption', this.get('time'), this.get('unit')].join('_')) ||
|
|
|
|
moment.duration(this.get('time'), this.get('unit')).humanize()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
getAbbreviated: function() {
|
|
|
|
return i18n(
|
|
|
|
['timerOption', this.get('time'), this.get('unit'), 'abbreviated'].join(
|
|
|
|
'_'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Whisper.ExpirationTimerOptions = new (Backbone.Collection.extend({
|
|
|
|
model: TimerOption,
|
|
|
|
getName: function(seconds) {
|
|
|
|
if (!seconds) {
|
|
|
|
seconds = 0;
|
2016-09-28 23:47:57 +00:00
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
var o = this.findWhere({ seconds: seconds });
|
|
|
|
if (o) {
|
|
|
|
return o.getName();
|
|
|
|
} else {
|
|
|
|
return [seconds, 'seconds'].join(' ');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getAbbreviated: function(seconds) {
|
|
|
|
if (!seconds) {
|
|
|
|
seconds = 0;
|
2016-09-28 23:47:57 +00:00
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
var o = this.findWhere({ seconds: seconds });
|
|
|
|
if (o) {
|
|
|
|
return o.getAbbreviated();
|
|
|
|
} else {
|
|
|
|
return [seconds, 's'].join('');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}))(
|
|
|
|
[
|
|
|
|
[0, 'seconds'],
|
|
|
|
[5, 'seconds'],
|
|
|
|
[10, 'seconds'],
|
|
|
|
[30, 'seconds'],
|
|
|
|
[1, 'minute'],
|
|
|
|
[5, 'minutes'],
|
|
|
|
[30, 'minutes'],
|
|
|
|
[1, 'hour'],
|
|
|
|
[6, 'hours'],
|
|
|
|
[12, 'hours'],
|
|
|
|
[1, 'day'],
|
|
|
|
[1, 'week'],
|
2016-09-28 23:47:57 +00:00
|
|
|
].map(function(o) {
|
|
|
|
var 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(),
|
2016-09-28 23:47:57 +00:00
|
|
|
};
|
2018-04-27 21:25:04 +00:00
|
|
|
})
|
|
|
|
);
|
2016-09-21 00:19:51 +00:00
|
|
|
})();
|