2020-01-17 22:23:19 +00:00
|
|
|
/* global
|
|
|
|
Backbone,
|
|
|
|
Whisper,
|
2020-03-20 19:07:44 +00:00
|
|
|
MessageController,
|
|
|
|
ConversationController
|
2020-01-17 22:23:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
Whisper.Reactions = new (Backbone.Collection.extend({
|
|
|
|
forMessage(message) {
|
|
|
|
if (message.isOutgoing()) {
|
2020-03-20 19:07:44 +00:00
|
|
|
const outgoingReactions = this.filter({
|
2020-01-17 22:23:19 +00:00
|
|
|
targetTimestamp: message.get('sent_at'),
|
|
|
|
});
|
|
|
|
|
2020-03-20 19:07:44 +00:00
|
|
|
if (outgoingReactions.length > 0) {
|
2020-01-17 22:23:19 +00:00
|
|
|
window.log.info('Found early reaction for outgoing message');
|
2020-03-20 19:07:44 +00:00
|
|
|
this.remove(outgoingReactions);
|
|
|
|
return outgoingReactions;
|
2020-01-17 22:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 19:07:44 +00:00
|
|
|
const reactionsBySource = this.filter(re => {
|
|
|
|
const mcid = message.get('conversationId');
|
|
|
|
const recid = ConversationController.getConversationId(
|
|
|
|
re.get('targetAuthorE164') || re.get('targetAuthorUuid')
|
|
|
|
);
|
2020-03-24 20:30:43 +00:00
|
|
|
const mTime = message.get('sent_at');
|
|
|
|
const rTime = re.get('targetTimestamp');
|
|
|
|
return mcid === recid && mTime === rTime;
|
2020-01-17 22:23:19 +00:00
|
|
|
});
|
|
|
|
|
2020-03-20 19:07:44 +00:00
|
|
|
if (reactionsBySource.length > 0) {
|
2020-01-17 22:23:19 +00:00
|
|
|
window.log.info('Found early reaction for message');
|
2020-03-20 19:07:44 +00:00
|
|
|
this.remove(reactionsBySource);
|
|
|
|
return reactionsBySource;
|
2020-01-17 22:23:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 19:07:44 +00:00
|
|
|
return [];
|
2020-01-17 22:23:19 +00:00
|
|
|
},
|
|
|
|
async onReaction(reaction) {
|
|
|
|
try {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
reaction.get('targetTimestamp'),
|
|
|
|
{
|
|
|
|
MessageCollection: Whisper.MessageCollection,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-24 20:30:43 +00:00
|
|
|
const targetMessage = messages.find(m => {
|
2020-03-25 18:05:57 +00:00
|
|
|
const contact = m.getContact();
|
|
|
|
|
|
|
|
if (!contact) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mcid = contact.get('id');
|
2020-03-24 20:30:43 +00:00
|
|
|
const recid = ConversationController.getConversationId(
|
|
|
|
reaction.get('targetAuthorE164') || reaction.get('targetAuthorUuid')
|
|
|
|
);
|
|
|
|
return mcid === recid;
|
|
|
|
});
|
2020-01-17 22:23:19 +00:00
|
|
|
|
|
|
|
if (!targetMessage) {
|
|
|
|
window.log.info(
|
|
|
|
'No message for reaction',
|
|
|
|
reaction.get('targetAuthorE164'),
|
|
|
|
reaction.get('targetAuthorUuid'),
|
|
|
|
reaction.get('targetTimestamp')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Since we haven't received the message for which we are removing a
|
|
|
|
// reaction, we can just remove those pending reaction
|
|
|
|
if (reaction.get('remove')) {
|
|
|
|
this.remove(reaction);
|
|
|
|
const oldReaction = this.where({
|
|
|
|
targetAuthorE164: reaction.get('targetAuthorE164'),
|
|
|
|
targetAuthorUuid: reaction.get('targetAuthorUuid'),
|
|
|
|
targetTimestamp: reaction.get('targetTimestamp'),
|
|
|
|
emoji: reaction.get('emoji'),
|
|
|
|
});
|
|
|
|
oldReaction.forEach(r => this.remove(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = MessageController.register(
|
|
|
|
targetMessage.id,
|
|
|
|
targetMessage
|
|
|
|
);
|
|
|
|
|
|
|
|
await message.handleReaction(reaction);
|
|
|
|
|
|
|
|
this.remove(reaction);
|
|
|
|
} catch (error) {
|
|
|
|
window.log.error(
|
|
|
|
'Reactions.onReaction error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}))();
|
|
|
|
})();
|