Refactor new message notification and frontend updates

Create a cleaner seperation between generating notifications
and updating frontend conversation views. The former is now
handled by `conversation.notify` while the latter is achieved
by triggering an event on the conversation model, which will
only be acted on if there are any views listening for it.

Additionally, instead of re-fetching the entire message history,
which is overkill, just add or update the new/modified message.
This will help speed up the newmessage event handler and also
help avoid unnecessary re-rendering when resolving key conflicts.

// FREEBIE
This commit is contained in:
lilia 2015-11-07 14:11:13 -08:00
parent 055fc4f685
commit a569e34b33
5 changed files with 53 additions and 57 deletions

View file

@ -211,7 +211,8 @@
active_at: Date.now(),
unreadCount: conversation.get('unreadCount') + 1
});
notifyConversation(message);
conversation.trigger('newmessage', message);
conversation.notify(message);
});
});
return;
@ -223,13 +224,6 @@
// lazy hack
window.receipts = new Backbone.Collection();
function updateConversation(conversationId) {
var conversation = ConversationController.get(conversationId);
if (conversation) {
conversation.trigger('newmessages');
}
}
function onDeliveryReceipt(ev) {
var pushMessage = ev.proto;
var timestamp = pushMessage.timestamp.toNumber();
@ -238,20 +232,24 @@
console.log('delivery receipt', pushMessage.source, timestamp);
messages.fetchSentAt(timestamp).then(function() {
groups.fetchGroups(pushMessage.source).then(function() {
for (var i in messages.where({type: 'outgoing'})) {
var message = messages.at(i);
var found = false;
messages.where({type: 'outgoing'}).forEach(function(message) {
var deliveries = message.get('delivered') || 0;
var conversationId = message.get('conversationId');
if (conversationId === pushMessage.source || groups.get(conversationId)) {
message.save({delivered: deliveries + 1, sent: true}).then(
message.save({delivered: deliveries + 1}).then(function() {
// notify frontend listeners
updateConversation.bind(null, conversationId)
);
return;
var conversation = ConversationController.get(conversationId);
if (conversation) {
conversation.trigger('newmessage', message);
}
});
found = true;
// TODO: consider keeping a list of numbers we've
// successfully delivered to?
}
}
});
if (found) { return; }
// if we get here, we didn't find a matching message.
// keep the receipt in memory in case it shows up later
// as a sync message.

View file

@ -314,6 +314,31 @@
}.bind(this));
}.bind(this));
},
notify: function(message) {
if (!message.isIncoming()) {
this.markRead();
return;
}
if (window.isOpen() && window.isFocused()) {
return;
}
window.drawAttention();
var sender = ConversationController.create({
id: message.get('source'), type: 'private'
});
var conversationId = this.id;
sender.fetch().then(function() {
sender.getNotificationIcon().then(function(iconUrl) {
Whisper.Notifications.add({
title : sender.getTitle(),
message : message.getNotificationText(),
iconUrl : iconUrl,
imageUrl : message.getImageUrl(),
conversationId : conversationId
});
});
});
},
hashCode: function() {
if (this.hash === undefined) {
var string = this.getTitle() || '';

View file

@ -319,11 +319,8 @@
conversation.save().then(function() {
message.save().then(function() {
if (message.isIncoming()) {
notifyConversation(message);
} else {
conversation.trigger('newmessages');
}
conversation.trigger('newmessage', message);
conversation.notify(message);
});
});
});

View file

@ -17,41 +17,16 @@
}
};
window.notifyConversation = function(message) {
var conversationId = message.get('conversationId');
var conversation = ConversationController.get(conversationId);
if (!conversation) {
conversation = ConversationController.create({id: conversationId});
conversation.fetch();
}
window.isFocused = function() {
return inboxFocused;
};
window.isOpen = function() {
return inboxOpened;
};
if (inboxOpened) {
conversation.trigger('newmessages');
if (inboxFocused) {
return;
}
if (inboxOpened) {
extension.windows.drawAttention(inboxWindowId);
}
}
if (Whisper.Notifications.isEnabled()) {
var sender = ConversationController.create({id: message.get('source')});
conversation.fetch().then(function() {
sender.fetch().then(function() {
sender.getNotificationIcon().then(function(iconUrl) {
Whisper.Notifications.add({
title : sender.getTitle(),
message : message.getNotificationText(),
iconUrl : iconUrl,
imageUrl : message.getImageUrl(),
conversationId: conversation.id
});
});
});
});
} else {
openConversation(conversation);
window.drawAttention = function() {
if (inboxOpened && !inboxFocused) {
extension.windows.drawAttention(inboxWindowId);
}
};

View file

@ -23,7 +23,7 @@
initialize: function(options) {
this.listenTo(this.model, 'destroy', this.stopListening);
this.listenTo(this.model, 'change:name', this.updateTitle);
this.listenTo(this.model, 'newmessages', this.fetchMessages);
this.listenTo(this.model, 'newmessage', this.addMessage);
this.listenTo(this.model, 'change:unreadCount', this.onUnread);
this.listenTo(this.model, 'opened', this.focusMessageField);
@ -87,8 +87,9 @@
focusMessageField: function() {
this.$messageField.focus();
},
fetchMessages: function() {
this.model.fetchMessages();
addMessage: function(message) {
this.model.messageCollection.add(message, {merge: true});
},
viewMembers: function() {