From 06ff6c3087c9e5c48ccbb852490007400df04ea8 Mon Sep 17 00:00:00 2001 From: lilia Date: Sat, 7 Jun 2014 18:00:51 -0700 Subject: [PATCH] Let thread collection double as contacts db When a thread is 'destroyed' from the UI we delete its messages and mark the thread as inactive, (in other words, keep it around as contact info). Additionally, we only load active threads when initializing the UI, and reactivate threads when new messages are added to them. Conflicts: js/models/messages.js js/models/threads.js js/views/conversations/show.js --- js/models/messages.js | 12 +++++++++--- js/models/threads.js | 5 +++-- js/views/conversations/index.js | 8 ++++++-- js/views/conversations/show.js | 5 +++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/js/models/messages.js b/js/models/messages.js index 92d3f7b74..74c26151a 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -46,10 +46,11 @@ var Whisper = Whisper || {}; m.save(); if (decrypted.message.timestamp > thread.get('timestamp')) { - thread.set({timestamp: decrypted.message.timestamp}); - thread.set({unreadCount: thread.get('unreadCount') + 1}); - thread.save(); + thread.set('timestamp', decrypted.message.timestamp); } + thread.set('unreadCount', thread.get('unreadCount') + 1); + thread.set('active', true); + thread.save(); return m; }, @@ -61,6 +62,11 @@ var Whisper = Whisper || {}; timestamp: new Date().getTime() }); m.save(); + + thread.set('timestamp', new Date().getTime()); + thread.set('unreadCount', 0); + thread.set('active', true); + thread.save(); return m; } }))(); diff --git a/js/models/threads.js b/js/models/threads.js index d09259f05..5f5bdb785 100644 --- a/js/models/threads.js +++ b/js/models/threads.js @@ -1,4 +1,4 @@ -/* vim: ts=2:sw=2:expandtab: */ +// vim: ts=2:sw=2:expandtab: var Whisper = Whisper || {}; (function () { @@ -9,7 +9,8 @@ var Whisper = Whisper || {}; return { image: '/images/default.png', unreadCount: 0, - timestamp: new Date().getTime() + timestamp: new Date().getTime(), + active: true }; }, diff --git a/js/views/conversations/index.js b/js/views/conversations/index.js index d1b125cc7..cc29053a9 100644 --- a/js/views/conversations/index.js +++ b/js/views/conversations/index.js @@ -33,11 +33,15 @@ var Whisper = Whisper || {}; addAll: function() { this.$el.html(''); - this.threads.each(this.addThread, this); + _.each(this.threads.where({'active': true}), this.addThread, this); }, addMessage: function(message) { - message.thread().trigger('message', message); + var thread = message.thread(); + if (!_.has(this.views, thread.id)) { + this.addThread(thread); + } + thread.trigger('message', message); } }); })(); diff --git a/js/views/conversations/show.js b/js/views/conversations/show.js index e6785c057..3534997d1 100644 --- a/js/views/conversations/show.js +++ b/js/views/conversations/show.js @@ -12,9 +12,10 @@ var Whisper = Whisper || {}; }, destroy: function() { - this.model.messages().each(function(message) { message.destroy(); }); - this.model.destroy(); + this.model.set('active', false); + this.model.save(); + this.model.trigger('destroy'); } });