signal-desktop/js/delivery_receipts.js

123 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2016-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* global
Backbone,
Whisper,
ConversationController,
MessageController,
_
*/
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
(function () {
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
Whisper.DeliveryReceipts = new (Backbone.Collection.extend({
forMessage(conversation, message) {
let recipients;
2018-04-27 21:25:04 +00:00
if (conversation.isPrivate()) {
recipients = [conversation.id];
} else {
2020-09-09 02:25:05 +00:00
recipients = conversation.getMemberIds();
2018-04-27 21:25:04 +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('deliveredTo')) > -1
);
2018-04-27 21:25:04 +00:00
this.remove(receipts);
return receipts;
},
async getTargetMessage(sourceId, messages) {
if (messages.length === 0) {
return null;
}
const message = messages.find(
item => !item.isIncoming() && sourceId === item.get('conversationId')
);
if (message) {
return MessageController.register(message.id, message);
}
const groups = await window.Signal.Data.getAllGroupsInvolvingId(
sourceId,
{
ConversationCollection: Whisper.ConversationCollection,
}
);
const ids = groups.pluck('id');
ids.push(sourceId);
2018-04-27 21:25:04 +00:00
const target = messages.find(
item =>
!item.isIncoming() && _.contains(ids, item.get('conversationId'))
);
if (!target) {
return null;
}
return MessageController.register(target.id, target);
},
async onReceipt(receipt) {
try {
const messages = await window.Signal.Data.getMessagesBySentAt(
receipt.get('timestamp'),
{
MessageCollection: Whisper.MessageCollection,
}
);
const message = await this.getTargetMessage(
receipt.get('deliveredTo'),
messages
);
if (!message) {
window.log.info(
'No message for delivery receipt',
receipt.get('deliveredTo'),
receipt.get('timestamp')
);
return;
}
const deliveries = message.get('delivered') || 0;
const deliveredTo = message.get('delivered_to') || [];
const expirationStartTimestamp = message.get(
'expirationStartTimestamp'
);
message.set({
delivered_to: _.union(deliveredTo, [receipt.get('deliveredTo')]),
delivered: deliveries + 1,
expirationStartTimestamp: expirationStartTimestamp || Date.now(),
sent: true,
});
if (message.isExpiring() && !expirationStartTimestamp) {
2019-09-26 19:56:31 +00:00
await message.setToExpire(false, { skipSave: true });
}
window.Signal.Util.queueUpdateMessage(message.attributes);
2019-09-26 19:56:31 +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
},
}))();
})();