Do not confirm messages until we have handled them
This commit is contained in:
parent
29aa188c0f
commit
04f716986c
16 changed files with 990 additions and 960 deletions
|
@ -1,197 +1,220 @@
|
|||
// Copyright 2020 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
/* eslint-disable max-classes-per-file */
|
||||
|
||||
import { Collection, Model } from 'backbone';
|
||||
import type { AciString } from '../types/ServiceId';
|
||||
import type { ConversationModel } from '../models/conversations';
|
||||
import type { MessageAttributesType } from '../model-types.d';
|
||||
import type { MessageModel } from '../models/messages';
|
||||
import type {
|
||||
MessageAttributesType,
|
||||
ReactionAttributesType,
|
||||
} from '../model-types.d';
|
||||
import type { ReactionSource } from '../reactions/ReactionSource';
|
||||
import * as Errors from '../types/errors';
|
||||
import * as log from '../logging/log';
|
||||
import { getContactId, getContact } from '../messages/helpers';
|
||||
import { getMessageIdForLogging } from '../util/idForLogging';
|
||||
import { getMessageSentTimestampSet } from '../util/getMessageSentTimestampSet';
|
||||
import { isDirectConversation, isMe } from '../util/whatTypeOfConversation';
|
||||
import { isOutgoing, isStory } from '../state/selectors/message';
|
||||
import { strictAssert } from '../util/assert';
|
||||
import { getMessageSentTimestampSet } from '../util/getMessageSentTimestampSet';
|
||||
|
||||
export class ReactionModel extends Model<ReactionAttributesType> {}
|
||||
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;
|
||||
};
|
||||
|
||||
let singleton: Reactions | undefined;
|
||||
const reactions = new Map<string, ReactionAttributesType>();
|
||||
|
||||
export class Reactions extends Collection<ReactionModel> {
|
||||
static getSingleton(): Reactions {
|
||||
if (!singleton) {
|
||||
singleton = new Reactions();
|
||||
}
|
||||
function remove(reaction: ReactionAttributesType): void {
|
||||
reactions.delete(reaction.envelopeId);
|
||||
reaction.removeFromMessageReceiverCache();
|
||||
}
|
||||
|
||||
return singleton;
|
||||
}
|
||||
export function forMessage(
|
||||
message: MessageModel
|
||||
): Array<ReactionAttributesType> {
|
||||
const logId = `Reactions.forMessage(${getMessageIdForLogging(
|
||||
message.attributes
|
||||
)})`;
|
||||
|
||||
forMessage(message: MessageModel): Array<ReactionModel> {
|
||||
const sentTimestamps = getMessageSentTimestampSet(message.attributes);
|
||||
if (isOutgoing(message.attributes)) {
|
||||
const outgoingReactions = this.filter(item =>
|
||||
sentTimestamps.has(item.get('targetTimestamp'))
|
||||
);
|
||||
|
||||
if (outgoingReactions.length > 0) {
|
||||
log.info('Found early reaction for outgoing message');
|
||||
this.remove(outgoingReactions);
|
||||
return outgoingReactions;
|
||||
}
|
||||
}
|
||||
|
||||
const senderId = getContactId(message.attributes);
|
||||
const reactionsBySource = this.filter(re => {
|
||||
const targetSender = window.ConversationController.lookupOrCreate({
|
||||
serviceId: re.get('targetAuthorAci'),
|
||||
reason: 'Reactions.forMessage',
|
||||
});
|
||||
const targetTimestamp = re.get('targetTimestamp');
|
||||
return (
|
||||
targetSender?.id === senderId && sentTimestamps.has(targetTimestamp)
|
||||
);
|
||||
});
|
||||
|
||||
if (reactionsBySource.length > 0) {
|
||||
log.info('Found early reaction for message');
|
||||
this.remove(reactionsBySource);
|
||||
return reactionsBySource;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
private async findMessage(
|
||||
targetTimestamp: number,
|
||||
targetConversationId: string
|
||||
): Promise<MessageAttributesType | undefined> {
|
||||
const messages = await window.Signal.Data.getMessagesBySentAt(
|
||||
targetTimestamp
|
||||
const reactionValues = Array.from(reactions.values());
|
||||
const sentTimestamps = getMessageSentTimestampSet(message.attributes);
|
||||
if (isOutgoing(message.attributes)) {
|
||||
const outgoingReactions = reactionValues.filter(item =>
|
||||
sentTimestamps.has(item.targetTimestamp)
|
||||
);
|
||||
|
||||
return messages.find(m => {
|
||||
const contact = getContact(m);
|
||||
|
||||
if (!contact) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const mcid = contact.get('id');
|
||||
return mcid === targetConversationId;
|
||||
});
|
||||
if (outgoingReactions.length > 0) {
|
||||
log.info(`${logId}: Found early reaction for outgoing message`);
|
||||
outgoingReactions.forEach(item => {
|
||||
remove(item);
|
||||
});
|
||||
return outgoingReactions;
|
||||
}
|
||||
}
|
||||
|
||||
async onReaction(reaction: ReactionModel): Promise<void> {
|
||||
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.get('targetAuthorAci'),
|
||||
reason: 'Reactions.onReaction',
|
||||
});
|
||||
const targetConversationId = targetAuthorConversation?.id;
|
||||
if (!targetConversationId) {
|
||||
throw new Error(
|
||||
'onReaction: No conversationId returned from lookupOrCreate!'
|
||||
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;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
const targetConversationId = targetAuthorConversation?.id;
|
||||
if (!targetConversationId) {
|
||||
throw new Error(
|
||||
`${logId} Error: No conversationId returned from lookupOrCreate!`
|
||||
);
|
||||
}
|
||||
|
||||
const generatedMessage = reaction.storyReactionMessage;
|
||||
strictAssert(
|
||||
generatedMessage,
|
||||
`${logId} strictAssert: Story reactions must provide storyReactionMessage`
|
||||
);
|
||||
const fromConversation = window.ConversationController.get(
|
||||
generatedMessage.get('conversationId')
|
||||
);
|
||||
|
||||
let targetConversation: ConversationModel | undefined | null;
|
||||
|
||||
const targetMessageCheck = await findMessage(
|
||||
reaction.targetTimestamp,
|
||||
targetConversationId
|
||||
);
|
||||
if (!targetMessageCheck) {
|
||||
log.info(
|
||||
`${logId}: No message for reaction`,
|
||||
'targeting',
|
||||
reaction.targetAuthorAci
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
fromConversation &&
|
||||
isStory(targetMessageCheck) &&
|
||||
isDirectConversation(fromConversation.attributes) &&
|
||||
!isMe(fromConversation.attributes)
|
||||
) {
|
||||
targetConversation = fromConversation;
|
||||
} else {
|
||||
targetConversation =
|
||||
await window.ConversationController.getConversationForTargetMessage(
|
||||
targetConversationId,
|
||||
reaction.targetTimestamp
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const generatedMessage = reaction.get('storyReactionMessage');
|
||||
strictAssert(
|
||||
generatedMessage,
|
||||
'Story reactions must provide storyReactionMessage'
|
||||
);
|
||||
const fromConversation = window.ConversationController.get(
|
||||
generatedMessage.get('conversationId')
|
||||
if (!targetConversation) {
|
||||
log.info(
|
||||
`${logId}: No target conversation for reaction`,
|
||||
reaction.targetAuthorAci,
|
||||
reaction.targetTimestamp
|
||||
);
|
||||
remove(reaction);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let targetConversation: ConversationModel | undefined | null;
|
||||
|
||||
const targetMessageCheck = await this.findMessage(
|
||||
reaction.get('targetTimestamp'),
|
||||
targetConversationId
|
||||
);
|
||||
if (!targetMessageCheck) {
|
||||
log.info(
|
||||
'No message for reaction',
|
||||
reaction.get('timestamp'),
|
||||
'targeting',
|
||||
reaction.get('targetAuthorAci'),
|
||||
reaction.get('targetTimestamp')
|
||||
);
|
||||
// awaiting is safe since `onReaction` is never called from inside the queue
|
||||
await targetConversation.queueJob('Reactions.onReaction', async () => {
|
||||
log.info(`${logId}: handling`);
|
||||
|
||||
// Thanks TS.
|
||||
if (!targetConversation) {
|
||||
remove(reaction);
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
fromConversation &&
|
||||
isStory(targetMessageCheck) &&
|
||||
isDirectConversation(fromConversation.attributes) &&
|
||||
!isMe(fromConversation.attributes)
|
||||
) {
|
||||
targetConversation = fromConversation;
|
||||
// Message is fetched inside the conversation queue so we have the
|
||||
// most recent data
|
||||
const targetMessage = await findMessage(
|
||||
reaction.targetTimestamp,
|
||||
targetConversationId
|
||||
);
|
||||
|
||||
if (!targetMessage) {
|
||||
remove(reaction);
|
||||
return;
|
||||
}
|
||||
|
||||
const message = window.MessageController.register(
|
||||
targetMessage.id,
|
||||
targetMessage
|
||||
);
|
||||
|
||||
// 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 {
|
||||
targetConversation =
|
||||
await window.ConversationController.getConversationForTargetMessage(
|
||||
targetConversationId,
|
||||
reaction.get('targetTimestamp')
|
||||
);
|
||||
await generatedMessage.handleReaction(reaction, {
|
||||
storyMessage: targetMessage,
|
||||
});
|
||||
}
|
||||
|
||||
if (!targetConversation) {
|
||||
log.info(
|
||||
'No target conversation for reaction',
|
||||
reaction.get('targetAuthorAci'),
|
||||
reaction.get('targetTimestamp')
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// awaiting is safe since `onReaction` is never called from inside the queue
|
||||
await targetConversation.queueJob('Reactions.onReaction', async () => {
|
||||
log.info('Handling reaction for', reaction.get('targetTimestamp'));
|
||||
|
||||
// Thanks TS.
|
||||
if (!targetConversation) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Message is fetched inside the conversation queue so we have the
|
||||
// most recent data
|
||||
const targetMessage = await this.findMessage(
|
||||
reaction.get('targetTimestamp'),
|
||||
targetConversationId
|
||||
);
|
||||
|
||||
if (!targetMessage) {
|
||||
return;
|
||||
}
|
||||
|
||||
const message = window.MessageController.register(
|
||||
targetMessage.id,
|
||||
targetMessage
|
||||
);
|
||||
|
||||
// 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,
|
||||
});
|
||||
}
|
||||
|
||||
this.remove(reaction);
|
||||
});
|
||||
} catch (error) {
|
||||
log.error('Reactions.onReaction error:', Errors.toLogFormat(error));
|
||||
}
|
||||
remove(reaction);
|
||||
});
|
||||
} catch (error) {
|
||||
remove(reaction);
|
||||
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue