Skip to content

Commit

Permalink
Unload conversations and old messages every half-hour
Browse files Browse the repository at this point in the history
FREEBIE
  • Loading branch information
scottnonnenberg committed Aug 4, 2017
1 parent 4ea457a commit 65283d2
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 29 deletions.
9 changes: 9 additions & 0 deletions js/conversation_controller.js
Expand Up @@ -19,6 +19,7 @@
this.on('add remove change:unreadCount',
_.debounce(this.updateUnreadCount.bind(this), 1000)
);
this.startPruning();
},
comparator: function(m1, m2) {
var timestamp1 = m1.get('timestamp');
Expand Down Expand Up @@ -65,6 +66,14 @@
if (newUnreadCount === 0) {
window.clearAttention();
}
},
startPruning: function() {
var halfHour = 30 * 60 * 1000;
this.interval = setInterval(function() {
this.forEach(function(conversation) {
conversation.trigger('prune');
});
}.bind(this), halfHour);
}
}))();

Expand Down
1 change: 1 addition & 0 deletions js/models/messages.js
Expand Up @@ -13,6 +13,7 @@
this.on('destroy', this.revokeImageUrl);
this.on('change:expirationStartTimestamp', this.setToExpire);
this.on('change:expireTimer', this.setToExpire);
this.on('unload', this.revokeImageUrl);
this.setToExpire();
},
idForLogging: function() {
Expand Down
23 changes: 19 additions & 4 deletions js/views/attachment_view.js
Expand Up @@ -81,6 +81,21 @@
events: {
'click': 'onclick'
},
unload: function() {
this.blob = null;

if (this.lightBoxView) {
this.lightBoxView.remove();
}
if (this.fileView) {
this.fileView.remove();
}
if (this.view) {
this.view.remove();
}

this.remove();
},
getFileType: function() {
switch(this.model.contentType) {
case 'video/quicktime': return 'mov';
Expand All @@ -89,10 +104,10 @@
},
onclick: function(e) {
if (this.isImage()) {
var view = new Whisper.LightboxView({ model: this });
view.render();
view.$el.appendTo(this.el);
view.$el.trigger('show');
this.lightBoxView = new Whisper.LightboxView({ model: this });
this.lightBoxView.render();
this.lightBoxView.$el.appendTo(this.el);
this.lightBoxView.$el.trigger('show');

} else {
this.saveFile();
Expand Down
120 changes: 106 additions & 14 deletions js/views/conversation_view.js
Expand Up @@ -109,6 +109,7 @@
this.listenTo(this.model, 'delivered', this.updateMessage);
this.listenTo(this.model, 'opened', this.onOpened);
this.listenTo(this.model, 'expired', this.onExpired);
this.listenTo(this.model, 'prune', this.onPrune);
this.listenTo(this.model.messageCollection, 'expired', this.onExpiredCollection);

this.lazyUpdateVerified = _.debounce(
Expand All @@ -126,7 +127,7 @@
this.loadingScreen.render();
this.loadingScreen.$el.prependTo(this.el);

new TimerMenuView({ el: this.$('.timer-menu'), model: this.model });
this.timerMenu = new TimerMenuView({ el: this.$('.timer-menu'), model: this.model });

emoji_util.parse(this.$('.conversation-name'));

Expand All @@ -151,15 +152,15 @@

this.$messageField = this.$('.send-message');

var onResize = this.forceUpdateMessageFieldSize.bind(this);
this.window.addEventListener('resize', onResize);
this.onResize = this.forceUpdateMessageFieldSize.bind(this);
this.window.addEventListener('resize', this.onResize);

var onFocus = function() {
this.onFocus = function() {
if (this.$el.css('display') !== 'none') {
this.markRead();
}
}.bind(this);
this.window.addEventListener('focus', onFocus);
this.window.addEventListener('focus', this.onFocus);

extension.windows.onClosed(function () {
this.window.removeEventListener('resize', onResize);
Expand Down Expand Up @@ -207,6 +208,83 @@
'show-identity': 'showSafetyNumber'
},

onPrune: function() {
if (!this.model.messageCollection.length || !this.lastActivity) {
return;
}

var oneHourAgo = Date.now() - 60 * 60 * 1000;
if (this.isHidden() && this.lastActivity < oneHourAgo) {
this.unload();
} else if (this.view.atBottom()) {
this.trim();
}
},

unload: function() {
console.log('unloading conversation', this.model.id, 'due to inactivity');

this.timerMenu.remove();
this.fileInput.remove();
this.titleView.remove();

if (this.captureAudioView) {
this.captureAudioView.remove();
}
if (this.banner) {
this.banner.remove();
}
if (this.lastSeenIndicator) {
this.lastSeenIndicator.remove();
}
if (this.scrollDownButton) {
this.scrollDownButton.remove();
}
if (this.panels && this.panels.length) {
for (var i = 0, max = this.panels.length; i < max; i += 1) {
var panel = this.panels[i];
panel.remove();
}
}

this.window.removeEventListener('resize', this.onResize);
this.window.removeEventListener('focus', this.onFocus);

this.view.remove();

this.remove();

this.model.messageCollection.forEach(function(model) {
model.trigger('unload');
});
this.model.messageCollection.reset([]);
this.model.revokeAvatarUrl();
},

trim: function() {
var MAX = 100;
var toRemove = this.model.messageCollection.length - MAX;
if (toRemove <= 0) {
return;
}

var models = [];
for (var i = 0; i < toRemove; i += 1) {
var model = this.model.messageCollection.at(i);
models.push(model);
}

if (!models.length) {
return;
}

console.log('trimming conversation', this.model.id, 'of', models.length, 'old messages');

this.model.messageCollection.remove(models);
_.forEach(models, function(model) {
model.trigger('unload');
});
},

markAllAsVerifiedDefault: function(unverified) {
return Promise.all(unverified.map(function(contact) {
Expand Down Expand Up @@ -292,10 +370,18 @@
},
captureAudio: function(e) {
e.preventDefault();
var view = new Whisper.RecorderView().render();

if (this.captureAudioView) {
this.captureAudioView.remove();
this.captureAudioView = null;
}

var view = this.captureAudioView = new Whisper.RecorderView();
view.render();
view.on('send', this.handleAudioCapture.bind(this));
view.on('closed', this.endCaptureAudio.bind(this));
view.$el.appendTo(this.$('.capture-audio'));

this.$('.send-message').attr('disabled','disabled');
this.$('.microphone').hide();
},
Expand All @@ -308,6 +394,7 @@
endCaptureAudio: function() {
this.$('.send-message').removeAttr('disabled');
this.$('.microphone').show();
this.captureAudioView = null;
},

unfocusBottomBar: function() {
Expand All @@ -322,6 +409,7 @@
// of messages are added to the DOM, one by one, changing window size and
// generating scroll events.
if (!this.isHidden() && window.isFocused() && !this.inProgressFetch) {
this.lastActivity = Date.now();
this.markRead();
}
},
Expand All @@ -340,6 +428,8 @@
},

onOpened: function() {
this.lastActivity = Date.now();

this.statusFetch = this.throttledGetProfiles().then(function() {
this.model.updateVerified().then(function() {
this.onVerifiedChange();
Expand Down Expand Up @@ -664,6 +754,16 @@
view.render();
},

// not currently in use
newGroupUpdate: function() {
var view = new Whisper.NewGroupUpdateView({
model: this.model,
window: this.window
});
view.render();
this.listenBack(view);
},

listenBack: function(view) {
this.panels = this.panels || [];
if (this.panels.length > 0) {
Expand Down Expand Up @@ -715,14 +815,6 @@
this.$('.conversation-menu .menu-list').toggle();
},

newGroupUpdate: function() {
this.newGroupUpdateView = new Whisper.NewGroupUpdateView({
model: this.model,
window: this.window
});
this.listenBack(this.newGroupUpdateView);
},

destroyMessages: function(e) {
this.confirm(i18n('deleteConversationConfirmation')).then(function() {
this.model.destroyMessages();
Expand Down

0 comments on commit 65283d2

Please sign in to comment.