Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

151 lines
5.0 KiB
TypeScript

import { Apps, AppEvents } from '@rocket.chat/apps';
import { Message } from '@rocket.chat/core-services';
import type { IMessage, IRoom, IUser } from '@rocket.chat/core-typings';
import { Messages, EmojiCustom, Rooms, Users } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';
import { canAccessRoomAsync } from '../../authorization';
import { hasPermissionAsync } from '../../authorization/hasPermission';
import { callbacks } from '../../callbacks';
import { i18n } from '../../i18n';
import { isTheLastMessage } from '../../messages/isTheLastMessage';
import { notifyOnMessageChange } from '../../notifyListener';
import { emoji } from '../emoji';
export const removeUserReaction = (message: IMessage, reaction: string, username: string) => {
if (!message.reactions) {
return message;
}
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;
};
export async function setReaction(room: IRoom, user: IUser, message: IMessage, reaction: string, userAlreadyReacted?: boolean) {
await Message.beforeReacted(message, room);
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,
});
}
if (room.ro === true && !room.reactWhenReadOnly && !(await hasPermissionAsync(user, 'post-readonly', room._id))) {
// Unless the user was manually unmuted
if (!(room.unmuted || []).includes(user.username as string)) {
throw new Error("You can't send messages because the room is readonly.");
}
}
let isReacted;
if (userAlreadyReacted) {
const oldMessage = JSON.parse(JSON.stringify(message));
removeUserReaction(message, reaction, user.username as string);
if (Object.keys(message.reactions || {}).length === 0) {
delete message.reactions;
await Messages.unsetReactions(message._id);
if (isTheLastMessage(room, message)) {
await Rooms.unsetReactionsInLastMessage(room._id);
}
} else {
await Messages.setReactions(message._id, message.reactions);
if (isTheLastMessage(room, message)) {
await Rooms.setReactionsInLastMessage(room._id, message.reactions);
}
}
void callbacks.run('afterUnsetReaction', message, { user, reaction, shouldReact: false, oldMessage, room });
isReacted = false;
} else {
if (!message.reactions) {
message.reactions = {};
}
if (!message.reactions[reaction]) {
message.reactions[reaction] = {
usernames: [],
};
}
message.reactions[reaction].usernames.push(user.username as string);
await Messages.setReactions(message._id, message.reactions);
if (isTheLastMessage(room, message)) {
await Rooms.setReactionsInLastMessage(room._id, message.reactions);
}
void callbacks.run('afterSetReaction', message, { user, reaction, shouldReact: true, room });
isReacted = true;
}
void Apps.self?.triggerEvent(AppEvents.IPostMessageReacted, message, user, reaction, isReacted);
void notifyOnMessageChange({
id: message._id,
});
}
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 = typeof messageParam === 'string' ? await Messages.findOneById(messageParam) : messageParam;
if (!message) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setReaction' });
}
const userAlreadyReacted = Boolean(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(message.rid);
if (!room) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setReaction' });
}
if (!(await canAccessRoomAsync(room, user))) {
throw new Meteor.Error('not-authorized', 'Not Authorized', { method: 'setReaction' });
}
return setReaction(room, user, message, reaction, userAlreadyReacted);
}
declare module '@rocket.chat/ddp-client' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
setReaction(reaction: string, messageId: IMessage['_id'], shouldReact?: boolean): boolean | undefined;
}
}