Update conversation lastMessage from database

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.
This commit is contained in:
lilia 2017-01-25 00:42:06 -08:00
parent 6253269d19
commit 9ef61d43f4
3 changed files with 15 additions and 14 deletions

View file

@ -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) {

View file

@ -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);

View file

@ -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() {