2016-09-21 00:19:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
|
|
|
*/
|
|
|
|
;(function() {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
2017-02-21 23:32:40 +00:00
|
|
|
|
|
|
|
function destroyExpiredMessages() {
|
|
|
|
// Load messages that have expired and destroy them
|
|
|
|
var expired = new Whisper.MessageCollection();
|
|
|
|
expired.on('add', function(message) {
|
|
|
|
console.log('message', message.get('sent_at'), 'expired');
|
|
|
|
message.destroy();
|
|
|
|
message.getConversation().trigger('expired', message);
|
|
|
|
});
|
|
|
|
expired.on('reset', checkExpiringMessages);
|
|
|
|
|
|
|
|
expired.fetchExpired();
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
var wait = expires_at - Date.now();
|
|
|
|
if (wait < 0) { wait = 0; }
|
|
|
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(destroyExpiredMessages, wait);
|
|
|
|
});
|
|
|
|
expiring.fetchNextExpiring();
|
|
|
|
}
|
|
|
|
|
|
|
|
window.ExpiringMessagesListener = {
|
2017-03-01 21:11:40 +00:00
|
|
|
init: function() {
|
|
|
|
checkExpiringMessages();
|
|
|
|
window.events.on('timetravel', checkExpiringMessages);
|
|
|
|
},
|
2017-02-21 23:32:40 +00:00
|
|
|
update: checkExpiringMessages
|
|
|
|
};
|
2016-09-28 23:47:57 +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;
|
|
|
|
}
|
|
|
|
var o = this.findWhere({seconds: seconds});
|
|
|
|
if (o) { return o.getName(); }
|
2017-02-07 02:22:09 +00:00
|
|
|
else {
|
|
|
|
return [seconds, 'seconds'].join(' ');
|
|
|
|
}
|
2016-09-28 23:47:57 +00:00
|
|
|
},
|
|
|
|
getAbbreviated: function(seconds) {
|
|
|
|
if (!seconds) {
|
|
|
|
seconds = 0;
|
|
|
|
}
|
|
|
|
var o = this.findWhere({seconds: seconds});
|
|
|
|
if (o) { return o.getAbbreviated(); }
|
2017-02-07 02:22:09 +00:00
|
|
|
else {
|
|
|
|
return [seconds, 's'].join('');
|
|
|
|
}
|
2016-09-28 23:47:57 +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' ],
|
|
|
|
].map(function(o) {
|
|
|
|
var duration = moment.duration(o[0], o[1]); // 5, 'seconds'
|
|
|
|
return {
|
|
|
|
time: o[0],
|
|
|
|
unit: o[1],
|
|
|
|
seconds: duration.asSeconds()
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
|
2016-09-21 00:19:51 +00:00
|
|
|
})();
|