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';
|
|
|
|
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';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader } from '../sql/Client';
|
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';
|
2024-04-29 21:20:20 +00:00
|
|
|
import { getAuthor } from '../messages/helpers';
|
2023-08-21 20:08:27 +00:00
|
|
|
import { getMessageSentTimestampSet } from '../util/getMessageSentTimestampSet';
|
2024-04-29 21:20:20 +00:00
|
|
|
import { isMe } from '../util/whatTypeOfConversation';
|
|
|
|
import { isStory } from '../state/selectors/message';
|
|
|
|
import { getPropForTimestamp } from '../util/editHelpers';
|
|
|
|
import { isSent } from '../messages/MessageSendState';
|
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;
|
2024-04-29 21:20:20 +00:00
|
|
|
// If this is a reaction to a 1:1 story, we can use this message, generated from the
|
|
|
|
// reaction message itself. Necessary to put 1:1 story replies into the right
|
|
|
|
// conversation - not the same conversation as the target message!
|
|
|
|
generatedMessageForStoryReaction?: MessageModel;
|
2023-08-21 20:08:27 +00:00
|
|
|
targetAuthorAci: AciString;
|
|
|
|
targetTimestamp: number;
|
|
|
|
timestamp: number;
|
2023-12-08 01:30:20 +00:00
|
|
|
receivedAtDate: number;
|
2023-08-21 20:08:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
export function findReactionsForMessage(
|
2023-08-21 20:08:27 +00:00
|
|
|
message: MessageModel
|
|
|
|
): Array<ReactionAttributesType> {
|
2024-04-29 21:20:20 +00:00
|
|
|
const matchingReactions = Array.from(reactions.values()).filter(reaction => {
|
|
|
|
return isMessageAMatchForReaction({
|
|
|
|
message: message.attributes,
|
|
|
|
targetTimestamp: reaction.targetTimestamp,
|
|
|
|
targetAuthorAci: reaction.targetAuthorAci,
|
|
|
|
reactionSenderConversationId: reaction.fromId,
|
|
|
|
});
|
|
|
|
});
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
matchingReactions.forEach(reaction => remove(reaction));
|
|
|
|
return matchingReactions;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findMessageForReaction({
|
|
|
|
targetTimestamp,
|
|
|
|
targetAuthorAci,
|
|
|
|
reactionSenderConversationId,
|
|
|
|
logId,
|
|
|
|
}: {
|
|
|
|
targetTimestamp: number;
|
|
|
|
targetAuthorAci: string;
|
|
|
|
reactionSenderConversationId: string;
|
|
|
|
logId: string;
|
|
|
|
}): Promise<MessageAttributesType | undefined> {
|
2024-07-22 18:16:33 +00:00
|
|
|
const messages = await DataReader.getMessagesBySentAt(targetTimestamp);
|
2024-04-29 21:20:20 +00:00
|
|
|
|
|
|
|
const matchingMessages = messages.filter(message =>
|
|
|
|
isMessageAMatchForReaction({
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
targetAuthorAci,
|
|
|
|
reactionSenderConversationId,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!matchingMessages.length) {
|
|
|
|
return undefined;
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
if (matchingMessages.length > 1) {
|
|
|
|
// This could theoretically happen given limitations in the reaction proto but
|
|
|
|
// is very unlikely
|
|
|
|
log.warn(
|
|
|
|
`${logId}/findMessageForReaction: found ${matchingMessages.length} matching messages for the reaction!`
|
2023-08-21 20:08:27 +00:00
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
return matchingMessages[0];
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
function isMessageAMatchForReaction({
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
targetAuthorAci,
|
|
|
|
reactionSenderConversationId,
|
|
|
|
}: {
|
|
|
|
message: MessageAttributesType;
|
|
|
|
targetTimestamp: number;
|
|
|
|
targetAuthorAci: string;
|
|
|
|
reactionSenderConversationId: string;
|
|
|
|
}): boolean {
|
|
|
|
if (!getMessageSentTimestampSet(message).has(targetTimestamp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const targetAuthorConversation =
|
|
|
|
window.ConversationController.get(targetAuthorAci);
|
|
|
|
const reactionSenderConversation = window.ConversationController.get(
|
|
|
|
reactionSenderConversationId
|
2023-08-21 20:08:27 +00:00
|
|
|
);
|
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
if (!targetAuthorConversation || !reactionSenderConversation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const author = getAuthor(message);
|
|
|
|
if (!author) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-21 20:08:27 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
if (author.id !== targetAuthorConversation.id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isMe(reactionSenderConversation.attributes)) {
|
|
|
|
// I am either the recipient or sender of all the messages I know about!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.type === 'outgoing') {
|
|
|
|
const sendStateByConversationId = getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'sendStateByConversationId',
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
|
|
|
|
|
|
|
const sendState =
|
|
|
|
sendStateByConversationId?.[reactionSenderConversation.id];
|
|
|
|
if (!sendState) {
|
2023-08-21 20:08:27 +00:00
|
|
|
return false;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
return isSent(sendState.status);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.type === 'incoming') {
|
|
|
|
const messageConversation = window.ConversationController.get(
|
|
|
|
message.conversationId
|
|
|
|
);
|
|
|
|
if (!messageConversation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const reactionSenderServiceId = reactionSenderConversation.getServiceId();
|
|
|
|
return (
|
|
|
|
reactionSenderServiceId != null &&
|
|
|
|
messageConversation.hasMember(reactionSenderServiceId)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2023-08-21 20:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function onReaction(
|
|
|
|
reaction: ReactionAttributesType
|
|
|
|
): Promise<void> {
|
|
|
|
reactions.set(reaction.envelopeId, reaction);
|
|
|
|
|
|
|
|
const logId = `Reactions.onReaction(timestamp=${reaction.timestamp};target=${reaction.targetTimestamp})`;
|
|
|
|
|
|
|
|
try {
|
2024-04-29 21:20:20 +00:00
|
|
|
const matchingMessage = await findMessageForReaction({
|
|
|
|
targetTimestamp: reaction.targetTimestamp,
|
|
|
|
targetAuthorAci: reaction.targetAuthorAci,
|
|
|
|
reactionSenderConversationId: reaction.fromId,
|
|
|
|
logId,
|
|
|
|
});
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
if (!matchingMessage) {
|
2023-08-21 20:08:27 +00:00
|
|
|
log.info(
|
|
|
|
`${logId}: No message for reaction`,
|
|
|
|
'targeting',
|
|
|
|
reaction.targetAuthorAci
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
const matchingMessageConversation = window.ConversationController.get(
|
|
|
|
matchingMessage.conversationId
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
if (!matchingMessageConversation) {
|
2023-08-21 20:08:27 +00:00
|
|
|
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
|
2024-04-29 21:20:20 +00:00
|
|
|
await matchingMessageConversation.queueJob(
|
|
|
|
'Reactions.onReaction',
|
|
|
|
async () => {
|
|
|
|
log.info(`${logId}: handling`);
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
// Message is fetched inside the conversation queue so we have the
|
|
|
|
// most recent data
|
|
|
|
const targetMessage = await findMessageForReaction({
|
|
|
|
targetTimestamp: reaction.targetTimestamp,
|
|
|
|
targetAuthorAci: reaction.targetAuthorAci,
|
|
|
|
reactionSenderConversationId: reaction.fromId,
|
|
|
|
logId: `${logId}/conversationQueue`,
|
|
|
|
});
|
2023-08-21 20:08:27 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
if (!targetMessage || targetMessage.id !== matchingMessage.id) {
|
|
|
|
log.warn(
|
|
|
|
`${logId}: message no longer a match for reaction! Maybe it's been deleted?`
|
|
|
|
);
|
|
|
|
remove(reaction);
|
|
|
|
return;
|
|
|
|
}
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
const targetMessageModel = window.MessageCache.__DEPRECATED$register(
|
|
|
|
targetMessage.id,
|
|
|
|
targetMessage,
|
|
|
|
'Reactions.onReaction'
|
|
|
|
);
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2024-04-29 21:20:20 +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 targetMessageModel.handleReaction(reaction);
|
|
|
|
} else {
|
|
|
|
const generatedMessage = reaction.generatedMessageForStoryReaction;
|
|
|
|
strictAssert(
|
|
|
|
generatedMessage,
|
|
|
|
'Generated message must exist for story reaction'
|
|
|
|
);
|
|
|
|
await generatedMessage.handleReaction(reaction, {
|
|
|
|
storyMessage: targetMessage,
|
|
|
|
});
|
|
|
|
}
|
2022-07-11 18:35:55 +00:00
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
remove(reaction);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
2024-04-29 21:20:20 +00:00
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
} catch (error) {
|
|
|
|
remove(reaction);
|
|
|
|
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|