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, received_at : timestamp,
key_changed : id key_changed : id
}); });
this.set({ lastMessage: message.getNotificationText() });
message.save().then(this.trigger.bind(this,'newmessage', message)); message.save().then(this.trigger.bind(this,'newmessage', message));
}, },
@ -185,15 +184,18 @@
}, },
updateLastMessage: function() { updateLastMessage: function() {
var lastMessage = this.messageCollection.at(this.messageCollection.length - 1); var collection = new Whisper.MessageCollection();
if (lastMessage) { return collection.fetchConversation(this.id, 1).then(function() {
this.save({ var lastMessage = collection.at(0);
lastMessage : lastMessage.getNotificationText(), if (lastMessage) {
timestamp : lastMessage.get('sent_at') this.save({
}); lastMessage : lastMessage.getNotificationText(),
} else { timestamp : lastMessage.get('sent_at')
this.save({ lastMessage: '', timestamp: null }); });
} } else {
this.save({ lastMessage: '', timestamp: null });
}
}.bind(this));
}, },
updateExpirationTimer: function(expireTimer, source, received_at) { updateExpirationTimer: function(expireTimer, source, received_at) {

View file

@ -424,10 +424,6 @@
timestamp: message.get('sent_at') timestamp: message.get('sent_at')
}); });
} }
conversation.set({
lastMessage: message.getNotificationText()
});
message.save().then(function() { message.save().then(function() {
conversation.save().then(function() { conversation.save().then(function() {
conversation.trigger('newmessage', message); conversation.trigger('newmessage', message);

View file

@ -22,8 +22,11 @@
this.listenTo(this.model, 'opened', this.markSelected); // auto update this.listenTo(this.model, 'opened', this.markSelected); // auto update
this.listenTo(this.model.messageCollection, 'add remove', this.listenTo(this.model.messageCollection, 'add remove',
_.debounce(this.model.updateLastMessage.bind(this.model), 1000)); _.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)); extension.windows.onClosed(this.stopListening.bind(this));
this.timeStampView = new Whisper.TimestampView({brief: true}); this.timeStampView = new Whisper.TimestampView({brief: true});
this.model.updateLastMessage();
}, },
markSelected: function() { markSelected: function() {