2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2021-06-17 17:15:10 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
import type { AciString } from '../types/ServiceId';
|
2022-07-11 18:35:55 +00:00
|
|
|
import type { ConversationModel } from '../models/conversations';
|
2023-08-21 20:08:27 +00:00
|
|
|
import type { MessageAttributesType } from '../model-types.d';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MessageModel } from '../models/messages';
|
2023-08-21 20:08:27 +00:00
|
|
|
import type { ReactionSource } from '../reactions/ReactionSource';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2022-07-11 18:35:55 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-05-10 19:02:21 +00:00
|
|
|
import { getContactId, getContact } from '../messages/helpers';
|
2023-08-21 20:08:27 +00:00
|
|
|
import { getMessageIdForLogging } from '../util/idForLogging';
|
|
|
|
import { getMessageSentTimestampSet } from '../util/getMessageSentTimestampSet';
|
2022-07-11 18:35:55 +00:00
|
|
|
import { isDirectConversation, isMe } from '../util/whatTypeOfConversation';
|
2022-05-10 19:02:21 +00:00
|
|
|
import { isOutgoing, isStory } from '../state/selectors/message';
|
2023-01-11 22:54:06 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export type ReactionAttributesType = {
|
|
|
|
emoji: string;
|
|
|
|
envelopeId: string;
|
|
|
|
fromId: string;
|
|
|
|
remove?: boolean;
|
|
|
|
removeFromMessageReceiverCache: () => unknown;
|
|
|
|
source: ReactionSource;
|
|
|
|
// Necessary to put 1:1 story replies into the right conversation - not the same
|
|
|
|
// conversation as the target message!
|
|
|
|
storyReactionMessage?: MessageModel;
|
|
|
|
targetAuthorAci: AciString;
|
|
|
|
targetTimestamp: number;
|
|
|
|
timestamp: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
const reactions = new Map<string, ReactionAttributesType>();
|
|
|
|
|
|
|
|
function remove(reaction: ReactionAttributesType): void {
|
|
|
|
reactions.delete(reaction.envelopeId);
|
|
|
|
reaction.removeFromMessageReceiverCache();
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export function forMessage(
|
|
|
|
message: MessageModel
|
|
|
|
): Array<ReactionAttributesType> {
|
|
|
|
const logId = `Reactions.forMessage(${getMessageIdForLogging(
|
|
|
|
message.attributes
|
|
|
|
)})`;
|
|
|
|
|
|
|
|
const reactionValues = Array.from(reactions.values());
|
|
|
|
const sentTimestamps = getMessageSentTimestampSet(message.attributes);
|
|
|
|
if (isOutgoing(message.attributes)) {
|
|
|
|
const outgoingReactions = reactionValues.filter(item =>
|
|
|
|
sentTimestamps.has(item.targetTimestamp)
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (outgoingReactions.length > 0) {
|
|
|
|
log.info(`${logId}: Found early reaction for outgoing message`);
|
|
|
|
outgoingReactions.forEach(item => {
|
|
|
|
remove(item);
|
|
|
|
});
|
|
|
|
return outgoingReactions;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const senderId = getContactId(message.attributes);
|
|
|
|
const reactionsBySource = reactionValues.filter(re => {
|
|
|
|
const targetSender = window.ConversationController.lookupOrCreate({
|
|
|
|
serviceId: re.targetAuthorAci,
|
|
|
|
reason: logId,
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
targetSender?.id === senderId && sentTimestamps.has(re.targetTimestamp)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (reactionsBySource.length > 0) {
|
|
|
|
log.info(`${logId}: Found early reaction for message`);
|
|
|
|
reactionsBySource.forEach(item => {
|
|
|
|
remove(item);
|
|
|
|
item.removeFromMessageReceiverCache();
|
|
|
|
});
|
|
|
|
return reactionsBySource;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return [];
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
async function findMessage(
|
|
|
|
targetTimestamp: number,
|
|
|
|
targetConversationId: string
|
|
|
|
): Promise<MessageAttributesType | undefined> {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
targetTimestamp
|
|
|
|
);
|
|
|
|
|
|
|
|
return messages.find(m => {
|
|
|
|
const contact = getContact(m);
|
|
|
|
|
|
|
|
if (!contact) {
|
|
|
|
return false;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const mcid = contact.get('id');
|
|
|
|
return mcid === targetConversationId;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function onReaction(
|
|
|
|
reaction: ReactionAttributesType
|
|
|
|
): Promise<void> {
|
|
|
|
reactions.set(reaction.envelopeId, reaction);
|
|
|
|
|
|
|
|
const logId = `Reactions.onReaction(timestamp=${reaction.timestamp};target=${reaction.targetTimestamp})`;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// The conversation the target message was in; we have to find it in the database
|
|
|
|
// to to figure that out.
|
|
|
|
const targetAuthorConversation =
|
|
|
|
window.ConversationController.lookupOrCreate({
|
|
|
|
serviceId: reaction.targetAuthorAci,
|
|
|
|
reason: logId,
|
2021-06-17 17:15:10 +00:00
|
|
|
});
|
2023-08-21 20:08:27 +00:00
|
|
|
const targetConversationId = targetAuthorConversation?.id;
|
|
|
|
if (!targetConversationId) {
|
|
|
|
throw new Error(
|
|
|
|
`${logId} Error: No conversationId returned from lookupOrCreate!`
|
2023-05-16 17:37:12 +00:00
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const generatedMessage = reaction.storyReactionMessage;
|
|
|
|
strictAssert(
|
|
|
|
generatedMessage,
|
|
|
|
`${logId} strictAssert: Story reactions must provide storyReactionMessage`
|
|
|
|
);
|
|
|
|
const fromConversation = window.ConversationController.get(
|
|
|
|
generatedMessage.get('conversationId')
|
2022-07-11 18:35:55 +00:00
|
|
|
);
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
let targetConversation: ConversationModel | undefined | null;
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const targetMessageCheck = await findMessage(
|
|
|
|
reaction.targetTimestamp,
|
|
|
|
targetConversationId
|
|
|
|
);
|
|
|
|
if (!targetMessageCheck) {
|
|
|
|
log.info(
|
|
|
|
`${logId}: No message for reaction`,
|
|
|
|
'targeting',
|
|
|
|
reaction.targetAuthorAci
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (
|
|
|
|
fromConversation &&
|
|
|
|
isStory(targetMessageCheck) &&
|
|
|
|
isDirectConversation(fromConversation.attributes) &&
|
|
|
|
!isMe(fromConversation.attributes)
|
|
|
|
) {
|
|
|
|
targetConversation = fromConversation;
|
|
|
|
} else {
|
|
|
|
targetConversation =
|
|
|
|
await window.ConversationController.getConversationForTargetMessage(
|
|
|
|
targetConversationId,
|
|
|
|
reaction.targetTimestamp
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (!targetConversation) {
|
|
|
|
log.info(
|
|
|
|
`${logId}: No target conversation for reaction`,
|
|
|
|
reaction.targetAuthorAci,
|
|
|
|
reaction.targetTimestamp
|
2022-07-11 18:35:55 +00:00
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
remove(reaction);
|
|
|
|
return undefined;
|
|
|
|
}
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
// awaiting is safe since `onReaction` is never called from inside the queue
|
|
|
|
await targetConversation.queueJob('Reactions.onReaction', async () => {
|
|
|
|
log.info(`${logId}: handling`);
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
// Thanks TS.
|
|
|
|
if (!targetConversation) {
|
|
|
|
remove(reaction);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message is fetched inside the conversation queue so we have the
|
|
|
|
// most recent data
|
|
|
|
const targetMessage = await findMessage(
|
|
|
|
reaction.targetTimestamp,
|
2022-07-11 18:35:55 +00:00
|
|
|
targetConversationId
|
|
|
|
);
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (!targetMessage) {
|
|
|
|
remove(reaction);
|
2022-07-11 18:35:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const message = window.MessageController.register(
|
|
|
|
targetMessage.id,
|
|
|
|
targetMessage
|
|
|
|
);
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
// Use the generated message in ts/background.ts to create a message
|
|
|
|
// if the reaction is targeted at a story.
|
|
|
|
if (!isStory(targetMessage)) {
|
|
|
|
await message.handleReaction(reaction);
|
|
|
|
} else {
|
|
|
|
await generatedMessage.handleReaction(reaction, {
|
|
|
|
storyMessage: targetMessage,
|
|
|
|
});
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
remove(reaction);
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
remove(reaction);
|
|
|
|
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|