Refactor delivery receipt tracking
Move code for matching receipts to messages (and vice versa) to its own file. // FREEBIE
This commit is contained in:
parent
18012688ea
commit
d1e9534542
5 changed files with 80 additions and 56 deletions
|
@ -394,6 +394,7 @@
|
||||||
<script type='text/javascript' src='js/libtextsecure.js'></script>
|
<script type='text/javascript' src='js/libtextsecure.js'></script>
|
||||||
|
|
||||||
<script type='text/javascript' src='js/notifications.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/libphonenumber-util.js'></script>
|
||||||
<script type='text/javascript' src='js/models/messages.js'></script>
|
<script type='text/javascript' src='js/models/messages.js'></script>
|
||||||
<script type='text/javascript' src='js/models/conversations.js'></script>
|
<script type='text/javascript' src='js/models/conversations.js'></script>
|
||||||
|
|
|
@ -278,43 +278,14 @@
|
||||||
function onDeliveryReceipt(ev) {
|
function onDeliveryReceipt(ev) {
|
||||||
var pushMessage = ev.proto;
|
var pushMessage = ev.proto;
|
||||||
var timestamp = pushMessage.timestamp.toNumber();
|
var timestamp = pushMessage.timestamp.toNumber();
|
||||||
var messages = new Whisper.MessageCollection();
|
|
||||||
var groups = new Whisper.ConversationCollection();
|
|
||||||
console.log(
|
console.log(
|
||||||
'delivery receipt from',
|
'delivery receipt from',
|
||||||
pushMessage.source + '.' + pushMessage.sourceDevice,
|
pushMessage.source + '.' + pushMessage.sourceDevice,
|
||||||
timestamp
|
timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
groups.fetchGroups(pushMessage.source).then(function() {
|
Whisper.DeliveryReceipts.add({
|
||||||
messages.fetchSentAt(timestamp).then(function() {
|
timestamp: timestamp, source: pushMessage.source
|
||||||
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;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
58
js/delivery_receipts.js
Normal file
58
js/delivery_receipts.js
Normal 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));
|
||||||
|
}
|
||||||
|
}))();
|
||||||
|
})();
|
|
@ -509,12 +509,14 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchGroups: function(number) {
|
fetchGroups: function(number) {
|
||||||
return this.fetch({
|
return new Promise(function(resolve) {
|
||||||
index: {
|
this.fetch({
|
||||||
name: 'group',
|
index: {
|
||||||
only: number
|
name: 'group',
|
||||||
}
|
only: number
|
||||||
});
|
}
|
||||||
|
}).always(resolve);
|
||||||
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchActive: function() {
|
fetchActive: function() {
|
||||||
|
|
|
@ -317,18 +317,8 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (type === 'outgoing') {
|
if (type === 'outgoing') {
|
||||||
// lazy hack - check for receipts that arrived early.
|
var receipts = Whisper.DeliveryReceipts.forMessage(conversation, message);
|
||||||
var recipients;
|
receipts.forEach(function(receipt) {
|
||||||
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);
|
|
||||||
message.set({
|
message.set({
|
||||||
delivered: (message.get('delivered') || 0) + 1
|
delivered: (message.get('delivered') || 0) + 1
|
||||||
});
|
});
|
||||||
|
@ -402,13 +392,15 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchSentAt: function(timestamp) {
|
fetchSentAt: function(timestamp) {
|
||||||
return this.fetch({
|
return new Promise(function(resolve) {
|
||||||
index: {
|
return this.fetch({
|
||||||
// 'receipt' index on sent_at
|
index: {
|
||||||
name: 'receipt',
|
// 'receipt' index on sent_at
|
||||||
only: timestamp
|
name: 'receipt',
|
||||||
}
|
only: timestamp
|
||||||
});
|
}
|
||||||
|
}).always(resolve);
|
||||||
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchConversation: function(conversationId, limit) {
|
fetchConversation: function(conversationId, limit) {
|
||||||
|
|
Loading…
Reference in a new issue