Skip to content

Commit

Permalink
Update conversation lastMessage from database
Browse files Browse the repository at this point in the history
Don't set lastMessage, let it update itself as needed, such as when
first rendering a conversation list item, and when its messages are
sent, received, or destroyed.
  • Loading branch information
liliakai committed Jan 26, 2017
1 parent 6253269 commit 9ef61d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
22 changes: 12 additions & 10 deletions js/models/conversations.js
Expand Up @@ -59,7 +59,6 @@
received_at : timestamp,
key_changed : id
});
this.set({ lastMessage: message.getNotificationText() });
message.save().then(this.trigger.bind(this,'newmessage', message));
},

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions js/models/messages.js
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions js/views/conversation_list_item_view.js
Expand Up @@ -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() {
Expand Down

0 comments on commit 9ef61d4

Please sign in to comment.