2021-06-17 17:15:10 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
|
|
|
|
import { Collection, Model } from 'backbone';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MessageModel } from '../models/messages';
|
2021-06-17 17:15:10 +00:00
|
|
|
import { isOutgoing } from '../state/selectors/message';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ReactionAttributesType } from '../model-types.d';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
export class ReactionModel extends Model<ReactionAttributesType> {}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
let singleton: Reactions | undefined;
|
|
|
|
|
2021-10-13 16:29:15 +00:00
|
|
|
export class Reactions extends Collection<ReactionModel> {
|
2021-06-17 17:15:10 +00:00
|
|
|
static getSingleton(): Reactions {
|
|
|
|
if (!singleton) {
|
|
|
|
singleton = new Reactions();
|
|
|
|
}
|
|
|
|
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
forMessage(message: MessageModel): Array<ReactionModel> {
|
|
|
|
if (isOutgoing(message.attributes)) {
|
|
|
|
const outgoingReactions = this.filter(
|
|
|
|
item => item.get('targetTimestamp') === message.get('sent_at')
|
|
|
|
);
|
|
|
|
|
|
|
|
if (outgoingReactions.length > 0) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Found early reaction for outgoing message');
|
2021-06-17 17:15:10 +00:00
|
|
|
this.remove(outgoingReactions);
|
|
|
|
return outgoingReactions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const senderId = message.getContactId();
|
|
|
|
const sentAt = message.get('sent_at');
|
|
|
|
const reactionsBySource = this.filter(re => {
|
|
|
|
const targetSenderId = window.ConversationController.ensureContactIds({
|
|
|
|
uuid: re.get('targetAuthorUuid'),
|
|
|
|
});
|
|
|
|
const targetTimestamp = re.get('targetTimestamp');
|
|
|
|
return targetSenderId === senderId && targetTimestamp === sentAt;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (reactionsBySource.length > 0) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Found early reaction for message');
|
2021-06-17 17:15:10 +00:00
|
|
|
this.remove(reactionsBySource);
|
|
|
|
return reactionsBySource;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
async onReaction(reaction: ReactionModel): Promise<void> {
|
2021-06-17 17:15:10 +00:00
|
|
|
try {
|
|
|
|
// The conversation the target message was in; we have to find it in the database
|
|
|
|
// to to figure that out.
|
|
|
|
const targetConversationId = window.ConversationController.ensureContactIds(
|
|
|
|
{
|
|
|
|
uuid: reaction.get('targetAuthorUuid'),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (!targetConversationId) {
|
|
|
|
throw new Error(
|
|
|
|
'onReaction: No conversationId returned from ensureContactIds!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const targetConversation = await window.ConversationController.getConversationForTargetMessage(
|
|
|
|
targetConversationId,
|
|
|
|
reaction.get('targetTimestamp')
|
|
|
|
);
|
|
|
|
if (!targetConversation) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
'No target conversation for reaction',
|
|
|
|
reaction.get('targetAuthorUuid'),
|
|
|
|
reaction.get('targetTimestamp')
|
|
|
|
);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// awaiting is safe since `onReaction` is never called from inside the queue
|
2021-10-29 23:19:44 +00:00
|
|
|
await targetConversation.queueJob('Reactions.onReaction', async () => {
|
|
|
|
log.info('Handling reaction for', reaction.get('targetTimestamp'));
|
|
|
|
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
reaction.get('targetTimestamp'),
|
|
|
|
{
|
|
|
|
MessageCollection: window.Whisper.MessageCollection,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
// Message is fetched inside the conversation queue so we have the
|
|
|
|
// most recent data
|
|
|
|
const targetMessage = messages.find(m => {
|
|
|
|
const contact = m.getContact();
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
if (!contact) {
|
|
|
|
return false;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
const mcid = contact.get('id');
|
|
|
|
const recid = window.ConversationController.ensureContactIds({
|
|
|
|
uuid: reaction.get('targetAuthorUuid'),
|
|
|
|
});
|
|
|
|
return mcid === recid;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!targetMessage) {
|
|
|
|
log.info(
|
|
|
|
'No message for reaction',
|
|
|
|
reaction.get('targetAuthorUuid'),
|
|
|
|
reaction.get('targetTimestamp')
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
// Since we haven't received the message for which we are removing a
|
|
|
|
// reaction, we can just remove those pending reactions
|
|
|
|
if (reaction.get('remove')) {
|
|
|
|
this.remove(reaction);
|
|
|
|
const oldReaction = this.where({
|
|
|
|
targetAuthorUuid: reaction.get('targetAuthorUuid'),
|
|
|
|
targetTimestamp: reaction.get('targetTimestamp'),
|
|
|
|
emoji: reaction.get('emoji'),
|
|
|
|
});
|
|
|
|
oldReaction.forEach(r => this.remove(r));
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
return;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
2021-10-29 23:19:44 +00:00
|
|
|
|
|
|
|
const message = window.MessageController.register(
|
|
|
|
targetMessage.id,
|
|
|
|
targetMessage
|
|
|
|
);
|
|
|
|
|
|
|
|
await message.handleReaction(reaction);
|
|
|
|
|
|
|
|
this.remove(reaction);
|
|
|
|
});
|
2021-06-17 17:15:10 +00:00
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-06-17 17:15:10 +00:00
|
|
|
'Reactions.onReaction error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|