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:
parent
055fc4f685
commit
a569e34b33
5 changed files with 53 additions and 57 deletions
|
@ -211,7 +211,8 @@
|
||||||
active_at: Date.now(),
|
active_at: Date.now(),
|
||||||
unreadCount: conversation.get('unreadCount') + 1
|
unreadCount: conversation.get('unreadCount') + 1
|
||||||
});
|
});
|
||||||
notifyConversation(message);
|
conversation.trigger('newmessage', message);
|
||||||
|
conversation.notify(message);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
@ -223,13 +224,6 @@
|
||||||
// lazy hack
|
// lazy hack
|
||||||
window.receipts = new Backbone.Collection();
|
window.receipts = new Backbone.Collection();
|
||||||
|
|
||||||
function updateConversation(conversationId) {
|
|
||||||
var conversation = ConversationController.get(conversationId);
|
|
||||||
if (conversation) {
|
|
||||||
conversation.trigger('newmessages');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function onDeliveryReceipt(ev) {
|
function onDeliveryReceipt(ev) {
|
||||||
var pushMessage = ev.proto;
|
var pushMessage = ev.proto;
|
||||||
var timestamp = pushMessage.timestamp.toNumber();
|
var timestamp = pushMessage.timestamp.toNumber();
|
||||||
|
@ -238,20 +232,24 @@
|
||||||
console.log('delivery receipt', pushMessage.source, timestamp);
|
console.log('delivery receipt', pushMessage.source, timestamp);
|
||||||
messages.fetchSentAt(timestamp).then(function() {
|
messages.fetchSentAt(timestamp).then(function() {
|
||||||
groups.fetchGroups(pushMessage.source).then(function() {
|
groups.fetchGroups(pushMessage.source).then(function() {
|
||||||
for (var i in messages.where({type: 'outgoing'})) {
|
var found = false;
|
||||||
var message = messages.at(i);
|
messages.where({type: 'outgoing'}).forEach(function(message) {
|
||||||
var deliveries = message.get('delivered') || 0;
|
var deliveries = message.get('delivered') || 0;
|
||||||
var conversationId = message.get('conversationId');
|
var conversationId = message.get('conversationId');
|
||||||
if (conversationId === pushMessage.source || groups.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
|
// notify frontend listeners
|
||||||
updateConversation.bind(null, conversationId)
|
var conversation = ConversationController.get(conversationId);
|
||||||
);
|
if (conversation) {
|
||||||
return;
|
conversation.trigger('newmessage', message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
found = true;
|
||||||
// TODO: consider keeping a list of numbers we've
|
// TODO: consider keeping a list of numbers we've
|
||||||
// successfully delivered to?
|
// successfully delivered to?
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
if (found) { return; }
|
||||||
// if we get here, we didn't find a matching message.
|
// if we get here, we didn't find a matching message.
|
||||||
// keep the receipt in memory in case it shows up later
|
// keep the receipt in memory in case it shows up later
|
||||||
// as a sync message.
|
// as a sync message.
|
||||||
|
|
|
@ -314,6 +314,31 @@
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}.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() {
|
hashCode: function() {
|
||||||
if (this.hash === undefined) {
|
if (this.hash === undefined) {
|
||||||
var string = this.getTitle() || '';
|
var string = this.getTitle() || '';
|
||||||
|
|
|
@ -319,11 +319,8 @@
|
||||||
|
|
||||||
conversation.save().then(function() {
|
conversation.save().then(function() {
|
||||||
message.save().then(function() {
|
message.save().then(function() {
|
||||||
if (message.isIncoming()) {
|
conversation.trigger('newmessage', message);
|
||||||
notifyConversation(message);
|
conversation.notify(message);
|
||||||
} else {
|
|
||||||
conversation.trigger('newmessages');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -17,41 +17,16 @@
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
window.notifyConversation = function(message) {
|
window.isFocused = function() {
|
||||||
var conversationId = message.get('conversationId');
|
return inboxFocused;
|
||||||
var conversation = ConversationController.get(conversationId);
|
};
|
||||||
if (!conversation) {
|
window.isOpen = function() {
|
||||||
conversation = ConversationController.create({id: conversationId});
|
return inboxOpened;
|
||||||
conversation.fetch();
|
};
|
||||||
}
|
|
||||||
|
|
||||||
if (inboxOpened) {
|
window.drawAttention = function() {
|
||||||
conversation.trigger('newmessages');
|
if (inboxOpened && !inboxFocused) {
|
||||||
if (inboxFocused) {
|
extension.windows.drawAttention(inboxWindowId);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
this.listenTo(this.model, 'destroy', this.stopListening);
|
this.listenTo(this.model, 'destroy', this.stopListening);
|
||||||
this.listenTo(this.model, 'change:name', this.updateTitle);
|
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, 'change:unreadCount', this.onUnread);
|
||||||
this.listenTo(this.model, 'opened', this.focusMessageField);
|
this.listenTo(this.model, 'opened', this.focusMessageField);
|
||||||
|
|
||||||
|
@ -87,8 +87,9 @@
|
||||||
focusMessageField: function() {
|
focusMessageField: function() {
|
||||||
this.$messageField.focus();
|
this.$messageField.focus();
|
||||||
},
|
},
|
||||||
fetchMessages: function() {
|
|
||||||
this.model.fetchMessages();
|
addMessage: function(message) {
|
||||||
|
this.model.messageCollection.add(message, {merge: true});
|
||||||
},
|
},
|
||||||
|
|
||||||
viewMembers: function() {
|
viewMembers: function() {
|
||||||
|
|
Loading…
Reference in a new issue