2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
2016-11-17 19:10:45 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.DeliveryReceipts = new (Backbone.Collection.extend({
|
|
|
|
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();
|
|
|
|
return 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;
|
|
|
|
}
|
2017-02-22 21:19:44 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
var groups = new Whisper.GroupCollection();
|
|
|
|
return groups.fetchGroups(receipt.get('source')).then(function() {
|
|
|
|
var ids = groups.pluck('id');
|
|
|
|
ids.push(receipt.get('source'));
|
|
|
|
return messages.find(function(message) {
|
|
|
|
return (
|
|
|
|
!message.isIncoming() &&
|
|
|
|
_.contains(ids, message.get('conversationId'))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
function(message) {
|
|
|
|
if (message) {
|
|
|
|
var deliveries = message.get('delivered') || 0;
|
|
|
|
var delivered_to = message.get('delivered_to') || [];
|
|
|
|
return new Promise(
|
|
|
|
function(resolve, reject) {
|
|
|
|
message
|
|
|
|
.save({
|
|
|
|
delivered_to: _.union(delivered_to, [
|
2017-08-18 00:34:27 +00:00
|
|
|
receipt.get('source'),
|
2018-04-27 21:25:04 +00:00
|
|
|
]),
|
|
|
|
delivered: deliveries + 1,
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
function() {
|
|
|
|
// notify frontend listeners
|
|
|
|
var conversation = ConversationController.get(
|
|
|
|
message.get('conversationId')
|
|
|
|
);
|
|
|
|
if (conversation) {
|
|
|
|
conversation.trigger('delivered', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.remove(receipt);
|
|
|
|
resolve();
|
|
|
|
}.bind(this),
|
|
|
|
reject
|
2017-08-18 00:34:27 +00:00
|
|
|
);
|
2018-04-27 21:25:04 +00:00
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
// TODO: consider keeping a list of numbers we've
|
|
|
|
// successfully delivered to?
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
'No message for delivery receipt',
|
|
|
|
receipt.get('source'),
|
|
|
|
receipt.get('timestamp')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
)
|
|
|
|
.catch(function(error) {
|
|
|
|
console.log(
|
|
|
|
'DeliveryReceipts.onReceipt error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}))();
|
2016-04-11 22:06:06 +00:00
|
|
|
})();
|