Refactor delivery receipt tracking

Move code for matching receipts to messages (and vice versa) to its own
file.

// FREEBIE
This commit is contained in:
lilia 2016-04-11 15:06:06 -07:00
parent 18012688ea
commit d1e9534542
5 changed files with 80 additions and 56 deletions

View file

@ -394,6 +394,7 @@
<script type='text/javascript' src='js/libtextsecure.js'></script>
<script type='text/javascript' src='js/notifications.js'></script>
<script type='text/javascript' src='js/delivery_receipts.js'></script>
<script type='text/javascript' src='js/libphonenumber-util.js'></script>
<script type='text/javascript' src='js/models/messages.js'></script>
<script type='text/javascript' src='js/models/conversations.js'></script>

View file

@ -278,43 +278,14 @@
function onDeliveryReceipt(ev) {
var pushMessage = ev.proto;
var timestamp = pushMessage.timestamp.toNumber();
var messages = new Whisper.MessageCollection();
var groups = new Whisper.ConversationCollection();
console.log(
'delivery receipt from',
pushMessage.source + '.' + pushMessage.sourceDevice,
timestamp
);
groups.fetchGroups(pushMessage.source).then(function() {
messages.fetchSentAt(timestamp).then(function() {
var ids = groups.pluck('id');
ids.push(pushMessage.source);
var message = messages.find(function(message) {
return (!message.isIncoming() &&
_.contains(ids, message.get('conversationId')));
});
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.
// keep the receipt in memory in case it shows up later
// as a sync message.
receipts.add({ timestamp: timestamp, source: pushMessage.source });
return;
});
Whisper.DeliveryReceipts.add({
timestamp: timestamp, source: pushMessage.source
});
}
})();

58
js/delivery_receipts.js Normal file
View file

@ -0,0 +1,58 @@
/*
* vim: ts=4:sw=4:expandtab
*/
;(function() {
'use strict';
window.Whisper = window.Whisper || {};
Whisper.DeliveryReceipts = new (Backbone.Collection.extend({
initialize: function() {
this.on('add', this.onReceipt);
},
forMessage: function(conversation, message) {
var recipients;
if (conversation.isPrivate()) {
recipients = [ conversation.id ];
} else {
recipients = conversation.get('members') || [];
}
var receipts = this.filter(function(receipt) {
return (receipt.get('timestamp') === message.get('sent_at')) &&
(recipients.indexOf(receipt.get('source')) > -1);
});
this.remove(receipts);
return receipts;
},
onReceipt: function(receipt) {
var messages = new Whisper.MessageCollection();
var groups = new Whisper.ConversationCollection();
Promise.all([
groups.fetchGroups(receipt.get('source')),
messages.fetchSentAt(receipt.get('timestamp'))
]).then(function() {
var ids = groups.pluck('id');
ids.push(receipt.get('source'));
var message = messages.find(function(message) {
return (!message.isIncoming() &&
_.contains(ids, message.get('conversationId')));
});
if (message) {
this.remove(receipt);
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?
}
}.bind(this));
}
}))();
})();

View file

@ -509,12 +509,14 @@
},
fetchGroups: function(number) {
return this.fetch({
index: {
name: 'group',
only: number
}
});
return new Promise(function(resolve) {
this.fetch({
index: {
name: 'group',
only: number
}
}).always(resolve);
}.bind(this));
},
fetchActive: function() {

View file

@ -317,18 +317,8 @@
}
}
if (type === 'outgoing') {
// lazy hack - check for receipts that arrived early.
var recipients;
if (dataMessage.group && dataMessage.group.id) { // group sync
recipients = conversation.get('members') || [];
} else {
recipients = [ conversation.id ];
}
window.receipts.filter(function(receipt) {
return (receipt.get('timestamp') === timestamp) &&
(recipients.indexOf(receipt.get('source')) > -1);
}).forEach(function(receipt) {
window.receipts.remove(receipt);
var receipts = Whisper.DeliveryReceipts.forMessage(conversation, message);
receipts.forEach(function(receipt) {
message.set({
delivered: (message.get('delivered') || 0) + 1
});
@ -402,13 +392,15 @@
},
fetchSentAt: function(timestamp) {
return this.fetch({
index: {
// 'receipt' index on sent_at
name: 'receipt',
only: timestamp
}
});
return new Promise(function(resolve) {
return this.fetch({
index: {
// 'receipt' index on sent_at
name: 'receipt',
only: timestamp
}
}).always(resolve);
}.bind(this));
},
fetchConversation: function(conversationId, limit) {