Skip to content

Commit

Permalink
refactor: Reactions set/unset (#32994)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Sep 19, 2024
1 parent 015c862 commit 7faba77
Show file tree
Hide file tree
Showing 5 changed files with 499 additions and 49 deletions.
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/v1/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ API.v1.addRoute(
throw new Meteor.Error('error-emoji-param-not-provided', 'The required "emoji" param is missing.');
}

await executeSetReaction(this.userId, emoji, msg._id, this.bodyParams.shouldReact);
await executeSetReaction(this.userId, emoji, msg, this.bodyParams.shouldReact);

return API.v1.success();
},
Expand Down
109 changes: 61 additions & 48 deletions apps/meteor/app/reactions/server/setReaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,46 @@ import type { IMessage, IRoom, IUser } from '@rocket.chat/core-typings';
import type { ServerMethods } from '@rocket.chat/ddp-client';
import { Messages, EmojiCustom, Rooms, Users } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';

import { callbacks } from '../../../lib/callbacks';
import { i18n } from '../../../server/lib/i18n';
import { canAccessRoomAsync } from '../../authorization/server';
import { hasPermissionAsync } from '../../authorization/server/functions/hasPermission';
import { emoji } from '../../emoji/server';
import { isTheLastMessage } from '../../lib/server/functions/isTheLastMessage';
import { notifyOnRoomChangedById, notifyOnMessageChange } from '../../lib/server/lib/notifyListener';
import { notifyOnMessageChange } from '../../lib/server/lib/notifyListener';

const removeUserReaction = (message: IMessage, reaction: string, username: string) => {
export const removeUserReaction = (message: IMessage, reaction: string, username: string) => {
if (!message.reactions) {
return message;
}

message.reactions[reaction].usernames.splice(message.reactions[reaction].usernames.indexOf(username), 1);
if (message.reactions[reaction].usernames.length === 0) {
const idx = message.reactions[reaction].usernames.indexOf(username);

// user not found in reaction array
if (idx === -1) {
return message;
}

message.reactions[reaction].usernames.splice(idx, 1);
if (!message.reactions[reaction].usernames.length) {
delete message.reactions[reaction];
}
return message;
};

async function setReaction(room: IRoom, user: IUser, message: IMessage, reaction: string, shouldReact?: boolean) {
reaction = `:${reaction.replace(/:/g, '')}:`;
export async function setReaction(
room: Pick<IRoom, '_id' | 'muted' | 'unmuted' | 'reactWhenReadOnly' | 'ro' | 'lastMessage' | 'federated'>,
user: IUser,
message: IMessage,
reaction: string,
userAlreadyReacted?: boolean,
) {
await Message.beforeReacted(message, room);

if (!emoji.list[reaction] && (await EmojiCustom.findByNameOrAlias(reaction, {}).count()) === 0) {
throw new Meteor.Error('error-not-allowed', 'Invalid emoji provided.', {
method: 'setReaction',
if (Array.isArray(room.muted) && room.muted.includes(user.username as string)) {
throw new Meteor.Error('error-not-allowed', i18n.t('You_have_been_muted', { lng: user.language }), {
rid: room._id,
});
}

Expand All @@ -42,50 +54,23 @@ async function setReaction(room: IRoom, user: IUser, message: IMessage, reaction
}
}

if (Array.isArray(room.muted) && room.muted.indexOf(user.username as string) !== -1) {
throw new Meteor.Error('error-not-allowed', i18n.t('You_have_been_muted', { lng: user.language }), {
rid: room._id,
});
}

// if (!('reactions' in message)) {
// return;
// }

await Message.beforeReacted(message, room);

const userAlreadyReacted =
message.reactions &&
Boolean(message.reactions[reaction]) &&
message.reactions[reaction].usernames.indexOf(user.username as string) !== -1;
// When shouldReact was not informed, toggle the reaction.
if (shouldReact === undefined) {
shouldReact = !userAlreadyReacted;
}

if (userAlreadyReacted === shouldReact) {
return;
}

let isReacted;

if (userAlreadyReacted) {
const oldMessage = JSON.parse(JSON.stringify(message));
removeUserReaction(message, reaction, user.username as string);
if (_.isEmpty(message.reactions)) {
if (Object.keys(message.reactions || {}).length === 0) {
delete message.reactions;
await Messages.unsetReactions(message._id);
if (isTheLastMessage(room, message)) {
await Rooms.unsetReactionsInLastMessage(room._id);
void notifyOnRoomChangedById(room._id);
}
await Messages.unsetReactions(message._id);
} else {
await Messages.setReactions(message._id, message.reactions);
if (isTheLastMessage(room, message)) {
await Rooms.setReactionsInLastMessage(room._id, message.reactions);
}
}
await callbacks.run('afterUnsetReaction', message, { user, reaction, shouldReact, oldMessage });
void callbacks.run('afterUnsetReaction', message, { user, reaction, shouldReact: false, oldMessage });

isReacted = false;
} else {
Expand All @@ -101,33 +86,61 @@ async function setReaction(room: IRoom, user: IUser, message: IMessage, reaction
await Messages.setReactions(message._id, message.reactions);
if (isTheLastMessage(room, message)) {
await Rooms.setReactionsInLastMessage(room._id, message.reactions);
void notifyOnRoomChangedById(room._id);
}
await callbacks.run('afterSetReaction', message, { user, reaction, shouldReact });

void callbacks.run('afterSetReaction', message, { user, reaction, shouldReact: true });

isReacted = true;
}

await Apps.self?.triggerEvent(AppEvents.IPostMessageReacted, message, user, reaction, isReacted);
void Apps.self?.triggerEvent(AppEvents.IPostMessageReacted, message, user, reaction, isReacted);

void notifyOnMessageChange({
id: message._id,
});
}

export async function executeSetReaction(userId: string, reaction: string, messageId: IMessage['_id'], shouldReact?: boolean) {
const user = await Users.findOneById(userId);
export async function executeSetReaction(
userId: string,
reaction: string,
messageParam: IMessage['_id'] | IMessage,
shouldReact?: boolean,
) {
// Check if the emoji is valid before proceeding
const reactionWithoutColons = reaction.replace(/:/g, '');
reaction = `:${reactionWithoutColons}:`;

if (!emoji.list[reaction] && (await EmojiCustom.countByNameOrAlias(reactionWithoutColons)) === 0) {
throw new Meteor.Error('error-not-allowed', 'Invalid emoji provided.', {
method: 'setReaction',
});
}

const user = await Users.findOneById(userId);
if (!user) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setReaction' });
}

const message = await Messages.findOneById(messageId);
const message = typeof messageParam === 'string' ? await Messages.findOneById(messageParam) : messageParam;
if (!message) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setReaction' });
}

const room = await Rooms.findOneById(message.rid);
const userAlreadyReacted =
message.reactions && Boolean(message.reactions[reaction]) && message.reactions[reaction].usernames.includes(user.username as string);

// When shouldReact was not informed, toggle the reaction.
if (shouldReact === undefined) {
shouldReact = !userAlreadyReacted;
}

if (userAlreadyReacted === shouldReact) {
return;
}

const room = await Rooms.findOneById<
Pick<IRoom, '_id' | 'ro' | 'muted' | 'reactWhenReadOnly' | 'lastMessage' | 't' | 'prid' | 'federated'>
>(message.rid, { projection: { _id: 1, ro: 1, muted: 1, reactWhenReadOnly: 1, lastMessage: 1, t: 1, prid: 1, federated: 1 } });
if (!room) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setReaction' });
}
Expand All @@ -136,7 +149,7 @@ export async function executeSetReaction(userId: string, reaction: string, messa
throw new Meteor.Error('not-authorized', 'Not Authorized', { method: 'setReaction' });
}

return setReaction(room, user, message, reaction, shouldReact);
return setReaction(room, user, message, reaction, userAlreadyReacted);
}

declare module '@rocket.chat/ddp-client' {
Expand Down
8 changes: 8 additions & 0 deletions apps/meteor/server/models/raw/EmojiCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ export class EmojiCustomRaw extends BaseRaw<IEmojiCustom> implements IEmojiCusto
create(data: InsertionModel<IEmojiCustom>): Promise<InsertOneResult<WithId<IEmojiCustom>>> {
return this.insertOne(data);
}

countByNameOrAlias(name: string): Promise<number> {
const query = {
$or: [{ name }, { aliases: name }],
};

return this.countDocuments(query);
}
}
Loading

0 comments on commit 7faba77

Please sign in to comment.