Avoid querying groups on delivery receipts if possible

This commit is contained in:
lilia 2017-02-22 13:19:44 -08:00
parent c8a0fe5a92
commit b7fac17ec8

View file

@ -41,17 +41,23 @@
}, },
onReceipt: function(receipt) { onReceipt: function(receipt) {
var messages = new Whisper.MessageCollection(); var messages = new Whisper.MessageCollection();
messages.fetchSentAt(receipt.get('timestamp')).then(function() {
if (messages.length === 0) { return; }
var message = messages.find(function(message) {
return (!message.isIncoming() && receipt.get('source') === message.get('conversationId'));
});
if (message) { return message; }
var groups = new GroupCollection(); var groups = new GroupCollection();
Promise.all([ return groups.fetchGroups(receipt.get('source')).then(function() {
groups.fetchGroups(receipt.get('source')),
messages.fetchSentAt(receipt.get('timestamp'))
]).then(function() {
var ids = groups.pluck('id'); var ids = groups.pluck('id');
ids.push(receipt.get('source')); ids.push(receipt.get('source'));
var message = messages.find(function(message) { return messages.find(function(message) {
return (!message.isIncoming() && return (!message.isIncoming() &&
_.contains(ids, message.get('conversationId'))); _.contains(ids, message.get('conversationId')));
}); });
});
}).then(function(message) {
if (message) { if (message) {
this.remove(receipt); this.remove(receipt);
var deliveries = message.get('delivered') || 0; var deliveries = message.get('delivered') || 0;