diff --git a/js/models/conversations.js b/js/models/conversations.js index 7e70b4f5..519dcbf7 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -59,7 +59,6 @@ received_at : timestamp, key_changed : id }); - this.set({ lastMessage: message.getNotificationText() }); message.save().then(this.trigger.bind(this,'newmessage', message)); }, @@ -185,15 +184,18 @@ }, updateLastMessage: function() { - var lastMessage = this.messageCollection.at(this.messageCollection.length - 1); - if (lastMessage) { - this.save({ - lastMessage : lastMessage.getNotificationText(), - timestamp : lastMessage.get('sent_at') - }); - } else { - this.save({ lastMessage: '', timestamp: null }); - } + var collection = new Whisper.MessageCollection(); + return collection.fetchConversation(this.id, 1).then(function() { + var lastMessage = collection.at(0); + if (lastMessage) { + this.save({ + lastMessage : lastMessage.getNotificationText(), + timestamp : lastMessage.get('sent_at') + }); + } else { + this.save({ lastMessage: '', timestamp: null }); + } + }.bind(this)); }, updateExpirationTimer: function(expireTimer, source, received_at) { diff --git a/js/models/messages.js b/js/models/messages.js index bcb73964..da64f67d 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -424,10 +424,6 @@ timestamp: message.get('sent_at') }); } - conversation.set({ - lastMessage: message.getNotificationText() - }); - message.save().then(function() { conversation.save().then(function() { conversation.trigger('newmessage', message); diff --git a/js/views/conversation_list_item_view.js b/js/views/conversation_list_item_view.js index aad25bfc..ec34eda2 100644 --- a/js/views/conversation_list_item_view.js +++ b/js/views/conversation_list_item_view.js @@ -22,8 +22,11 @@ this.listenTo(this.model, 'opened', this.markSelected); // auto update this.listenTo(this.model.messageCollection, 'add remove', _.debounce(this.model.updateLastMessage.bind(this.model), 1000)); + this.listenTo(this.model, 'newmessage', + _.debounce(this.model.updateLastMessage.bind(this.model), 1000)); extension.windows.onClosed(this.stopListening.bind(this)); this.timeStampView = new Whisper.TimestampView({brief: true}); + this.model.updateLastMessage(); }, markSelected: function() {