2018-07-07 00:48:14 +00:00
|
|
|
/* global Backbone: false */
|
|
|
|
/* global Whisper: false */
|
|
|
|
/* global ConversationController: false */
|
|
|
|
/* global _: false */
|
|
|
|
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
2018-07-07 00:48:14 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
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({
|
2018-07-07 00:48:14 +00:00
|
|
|
forMessage(conversation, message) {
|
|
|
|
let recipients;
|
2018-04-27 21:25:04 +00:00
|
|
|
if (conversation.isPrivate()) {
|
|
|
|
recipients = [conversation.id];
|
|
|
|
} else {
|
|
|
|
recipients = conversation.get('members') || [];
|
|
|
|
}
|
2018-07-07 00:48:14 +00:00
|
|
|
const receipts = this.filter(
|
|
|
|
receipt =>
|
2018-04-27 21:25:04 +00:00
|
|
|
receipt.get('timestamp') === message.get('sent_at') &&
|
|
|
|
recipients.indexOf(receipt.get('source')) > -1
|
2018-07-07 00:48:14 +00:00
|
|
|
);
|
2018-04-27 21:25:04 +00:00
|
|
|
this.remove(receipts);
|
|
|
|
return receipts;
|
|
|
|
},
|
2018-07-25 22:02:27 +00:00
|
|
|
async getTargetMessage(source, messages) {
|
|
|
|
if (messages.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const message = messages.find(
|
|
|
|
item => !item.isIncoming() && source === item.get('conversationId')
|
|
|
|
);
|
|
|
|
if (message) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
const groups = new Whisper.GroupCollection();
|
|
|
|
await groups.fetchGroups(source);
|
2017-02-22 21:19:44 +00:00
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
const ids = groups.pluck('id');
|
|
|
|
ids.push(source);
|
2018-04-27 21:25:04 +00:00
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
return messages.find(
|
|
|
|
item =>
|
|
|
|
!item.isIncoming() && _.contains(ids, item.get('conversationId'))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
async onReceipt(receipt) {
|
|
|
|
try {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
receipt.get('timestamp'),
|
|
|
|
{
|
|
|
|
MessageCollection: Whisper.MessageCollection,
|
2018-07-07 00:48:14 +00:00
|
|
|
}
|
2018-07-25 22:02:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const message = await this.getTargetMessage(
|
|
|
|
receipt.get('source'),
|
|
|
|
messages
|
|
|
|
);
|
|
|
|
if (!message) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info(
|
2018-07-07 00:48:14 +00:00
|
|
|
'No message for delivery receipt',
|
|
|
|
receipt.get('source'),
|
|
|
|
receipt.get('timestamp')
|
|
|
|
);
|
2018-07-25 22:02:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-07 00:48:14 +00:00
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
const deliveries = message.get('delivered') || 0;
|
|
|
|
const deliveredTo = message.get('delivered_to') || [];
|
2018-08-07 19:33:56 +00:00
|
|
|
const expirationStartTimestamp = message.get(
|
|
|
|
'expirationStartTimestamp'
|
|
|
|
);
|
2018-07-25 22:02:27 +00:00
|
|
|
message.set({
|
|
|
|
delivered_to: _.union(deliveredTo, [receipt.get('source')]),
|
|
|
|
delivered: deliveries + 1,
|
2018-08-07 19:33:56 +00:00
|
|
|
expirationStartTimestamp: expirationStartTimestamp || Date.now(),
|
|
|
|
sent: true,
|
2018-07-25 22:02:27 +00:00
|
|
|
});
|
|
|
|
|
2018-08-07 19:33:56 +00:00
|
|
|
if (message.isExpiring() && !expirationStartTimestamp) {
|
|
|
|
// This will save the message for us while starting the timer
|
|
|
|
await message.setToExpire();
|
|
|
|
} else {
|
|
|
|
await window.Signal.Data.saveMessage(message.attributes, {
|
|
|
|
Message: Whisper.Message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
// notify frontend listeners
|
|
|
|
const conversation = ConversationController.get(
|
|
|
|
message.get('conversationId')
|
|
|
|
);
|
|
|
|
if (conversation) {
|
|
|
|
conversation.trigger('delivered', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.remove(receipt);
|
|
|
|
} catch (error) {
|
|
|
|
window.log.error(
|
|
|
|
'DeliveryReceipts.onReceipt error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
|
|
|
}))();
|
2016-04-11 22:06:06 +00:00
|
|
|
})();
|