Skip to content

Commit

Permalink
eslintify expiring_messages.js
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Jul 3, 2018
1 parent 12b5547 commit 6746477
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ts/**/*.js
!js/models/conversations.js
!js/models/messages.js
!js/notifications.js
!js/expiring_messages.js
!js/views/attachment_view.js
!js/views/backbone_wrapper_view.js
!js/views/conversation_search_view.js
Expand Down
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ module.exports = function(grunt) {
'!js/libsignal-protocol-worker.js',
'!js/libtextsecure.js',
'!js/logging.js',
'!js/expiring_messages.js',
'!js/modules/**/*.js',
'!js/Mp3LameEncoder.min.js',
'!js/signal_protocol_store.js',
Expand Down
64 changes: 34 additions & 30 deletions js/expiring_messages.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
/* global _: false */
/* global Backbone: false */
/* global i18n: false */
/* global moment: false */
/* global Whisper: false */
/* global wrapDeferred: false */

// eslint-disable-next-line func-names
(function() {
'use strict';

window.Whisper = window.Whisper || {};

function destroyExpiredMessages() {
// Load messages that have expired and destroy them
var expired = new Whisper.MessageCollection();
expired.on('add', async function(message) {
const expired = new Whisper.MessageCollection();
expired.on('add', async message => {
console.log('Message expired', {
sentAt: message.get('sent_at'),
});
var conversation = message.getConversation();
const conversation = message.getConversation();
if (conversation) {
conversation.trigger('expired', message);
}
Expand All @@ -26,15 +35,15 @@
expired.fetchExpired();
}

var timeout;
let 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());
const expiring = new Whisper.MessageCollection();
expiring.once('add', next => {
const expiresAt = next.get('expires_at');
console.log('next message expires', new Date(expiresAt).toISOString());

var wait = expires_at - Date.now();
let wait = expiresAt - Date.now();

// In the past
if (wait < 0) {
Expand All @@ -51,24 +60,27 @@
});
expiring.fetchNextExpiring();
}
var throttledCheckExpiringMessages = _.throttle(checkExpiringMessages, 1000);
const throttledCheckExpiringMessages = _.throttle(
checkExpiringMessages,
1000
);

Whisper.ExpiringMessagesListener = {
init: function(events) {
init(events) {
checkExpiringMessages();
events.on('timetravel', throttledCheckExpiringMessages);
},
update: throttledCheckExpiringMessages,
};

var TimerOption = Backbone.Model.extend({
getName: function() {
const TimerOption = Backbone.Model.extend({
getName() {
return (
i18n(['timerOption', this.get('time'), this.get('unit')].join('_')) ||
moment.duration(this.get('time'), this.get('unit')).humanize()
);
},
getAbbreviated: function() {
getAbbreviated() {
return i18n(
['timerOption', this.get('time'), this.get('unit'), 'abbreviated'].join(
'_'
Expand All @@ -78,27 +90,19 @@
});
Whisper.ExpirationTimerOptions = new (Backbone.Collection.extend({
model: TimerOption,
getName: function(seconds) {
if (!seconds) {
seconds = 0;
}
var o = this.findWhere({ seconds: seconds });
getName(seconds = 0) {
const o = this.findWhere({ seconds });
if (o) {
return o.getName();
} else {
return [seconds, 'seconds'].join(' ');
}
return [seconds, 'seconds'].join(' ');
},
getAbbreviated: function(seconds) {
if (!seconds) {
seconds = 0;
}
var o = this.findWhere({ seconds: seconds });
getAbbreviated(seconds = 0) {
const o = this.findWhere({ seconds });
if (o) {
return o.getAbbreviated();
} else {
return [seconds, 's'].join('');
}
return [seconds, 's'].join('');
},
}))(
[
Expand All @@ -114,8 +118,8 @@
[12, 'hours'],
[1, 'day'],
[1, 'week'],
].map(function(o) {
var duration = moment.duration(o[0], o[1]); // 5, 'seconds'
].map(o => {
const duration = moment.duration(o[0], o[1]); // 5, 'seconds'
return {
time: o[0],
unit: o[1],
Expand Down

0 comments on commit 6746477

Please sign in to comment.