Refactor delivery receipt processing

Replace the where, forEach, and found pattern with a find.

// FREEBIE
This commit is contained in:
lilia 2016-02-11 11:40:41 -08:00
parent b602533084
commit cd7bc78b1d

View file

@ -241,24 +241,27 @@
groups.fetchGroups(pushMessage.source).then(function() { groups.fetchGroups(pushMessage.source).then(function() {
messages.fetchSentAt(timestamp).then(function() { messages.fetchSentAt(timestamp).then(function() {
var found = false; var ids = groups.pluck('id');
messages.where({type: 'outgoing'}).forEach(function(message) { ids.push(pushMessage.source);
var deliveries = message.get('delivered') || 0; var message = messages.find(function(message) {
var conversationId = message.get('conversationId'); return (message.get('type') === 'outgoing' &&
if (conversationId === pushMessage.source || groups.get(conversationId)) { _.contains(ids, message.get('conversationId')));
message.save({delivered: deliveries + 1}).then(function() {
// notify frontend listeners
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 (message) {
var deliveries = message.get('delivered') || 0;
message.save({delivered: deliveries + 1}).then(function() {
// notify frontend listeners
var conversation = ConversationController.get(
message.get('conversationId')
);
if (conversation) {
conversation.trigger('newmessage', message);
}
});
// TODO: consider keeping a list of numbers we've
// successfully delivered to?
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.