snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
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 1s
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 1s
This commit is contained in:
140
apps/meteor/server/hooks/messages/mentionUserNotInChannel.ts
Normal file
140
apps/meteor/server/hooks/messages/mentionUserNotInChannel.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { api } from '@rocket.chat/core-services';
|
||||
import type { IMessage } from '@rocket.chat/core-typings';
|
||||
import { isDirectMessageRoom, isEditedMessage, isOmnichannelRoom, isRoomFederated, getUserDisplayName } from '@rocket.chat/core-typings';
|
||||
import { Subscriptions, Users } from '@rocket.chat/models';
|
||||
import { isTruthy } from '@rocket.chat/tools';
|
||||
import type { ActionsBlock } from '@rocket.chat/ui-kit';
|
||||
import moment from 'moment';
|
||||
|
||||
import { hasPermissionAsync } from '../../lib/authorization/hasPermission';
|
||||
import { callbacks } from '../../lib/callbacks';
|
||||
import { i18n } from '../../lib/i18n';
|
||||
import { settings } from '../../settings';
|
||||
|
||||
const APP_ID = 'mention-core';
|
||||
const getBlocks = (mentions: IMessage['mentions'], messageId: string, lng: string | undefined) => {
|
||||
const stringifiedMentions = JSON.stringify(mentions);
|
||||
return {
|
||||
addUsersBlock: {
|
||||
type: 'button',
|
||||
appId: APP_ID,
|
||||
blockId: messageId,
|
||||
value: stringifiedMentions,
|
||||
actionId: 'add-users',
|
||||
text: {
|
||||
type: 'plain_text',
|
||||
text: i18n.t('Add_them', { lng }),
|
||||
},
|
||||
},
|
||||
dismissBlock: {
|
||||
type: 'button',
|
||||
appId: APP_ID,
|
||||
blockId: messageId,
|
||||
value: stringifiedMentions,
|
||||
actionId: 'dismiss',
|
||||
text: {
|
||||
type: 'plain_text',
|
||||
text: i18n.t('Do_nothing', { lng }),
|
||||
},
|
||||
},
|
||||
dmBlock: {
|
||||
type: 'button',
|
||||
appId: APP_ID,
|
||||
value: stringifiedMentions,
|
||||
blockId: messageId,
|
||||
actionId: 'share-message',
|
||||
text: {
|
||||
type: 'plain_text',
|
||||
text: i18n.t('Let_them_know', { lng }),
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
};
|
||||
|
||||
callbacks.add(
|
||||
'afterSaveMessage',
|
||||
async (message, { room }) => {
|
||||
// TODO: check if I need to test this 60 second rule.
|
||||
// If the message was edited, or is older than 60 seconds (imported)
|
||||
// the notifications will be skipped, so we can also skip this validation
|
||||
if (isEditedMessage(message) || (message.ts && Math.abs(moment(message.ts).diff(moment())) > 60000) || !message.mentions) {
|
||||
return message;
|
||||
}
|
||||
|
||||
const mentions = message.mentions.filter(({ _id, type }) => _id !== 'all' && _id !== 'here' && type !== 'team');
|
||||
if (!mentions.length) {
|
||||
return message;
|
||||
}
|
||||
|
||||
if (isDirectMessageRoom(room) || isRoomFederated(room) || isOmnichannelRoom(room)) {
|
||||
return message;
|
||||
}
|
||||
|
||||
const subs = await Subscriptions.findByRoomIdAndUserIds(
|
||||
message.rid,
|
||||
mentions.map(({ _id }) => _id),
|
||||
{ projection: { u: 1 } },
|
||||
).toArray();
|
||||
|
||||
// get all users that are mentioned but not in the channel
|
||||
const mentionsUsersNotInChannel = mentions.filter(({ _id }) => !subs.some((sub) => sub.u._id === _id));
|
||||
|
||||
if (!mentionsUsersNotInChannel.length) {
|
||||
return message;
|
||||
}
|
||||
|
||||
const canAddUsersToThisRoom = await hasPermissionAsync(message.u._id, 'add-user-to-joined-room', message.rid);
|
||||
const canAddToAnyRoom = await (room.t === 'c'
|
||||
? hasPermissionAsync(message.u._id, 'add-user-to-any-c-room')
|
||||
: hasPermissionAsync(message.u._id, 'add-user-to-any-p-room'));
|
||||
const canDMUsers = await hasPermissionAsync(message.u._id, 'create-d'); // TODO: Perhaps check if user has DM with mentioned user (might be too expensive)
|
||||
const canAddUsers = canAddUsersToThisRoom || canAddToAnyRoom;
|
||||
|
||||
const { language } = (await Users.findOneById(message.u._id)) || {};
|
||||
|
||||
const actionBlocks = getBlocks(mentionsUsersNotInChannel, message._id, language);
|
||||
const elements: ActionsBlock['elements'] = [
|
||||
canAddUsers && actionBlocks.addUsersBlock,
|
||||
(canAddUsers || canDMUsers) && actionBlocks.dismissBlock,
|
||||
canDMUsers && actionBlocks.dmBlock,
|
||||
].filter(isTruthy);
|
||||
|
||||
const messageLabel = canAddUsers
|
||||
? 'You_mentioned___mentions__but_theyre_not_in_this_room'
|
||||
: 'You_mentioned___mentions__but_theyre_not_in_this_room_You_can_ask_a_room_admin_to_add_them';
|
||||
|
||||
const useRealName = settings.get<boolean>('UI_Use_Real_Name');
|
||||
|
||||
const usernamesOrNames = mentionsUsersNotInChannel.map(({ username, name }) => `*${getUserDisplayName(name, username, useRealName)}*`);
|
||||
|
||||
const mentionsText = usernamesOrNames.join(', ');
|
||||
|
||||
// TODO: Mentions style
|
||||
void api.broadcast('notify.ephemeralMessage', message.u._id, message.rid, {
|
||||
msg: '',
|
||||
mentions: mentionsUsersNotInChannel,
|
||||
tmid: message.tmid,
|
||||
blocks: [
|
||||
{
|
||||
appId: APP_ID,
|
||||
type: 'section',
|
||||
text: {
|
||||
type: 'mrkdwn',
|
||||
text: i18n.t(messageLabel, { mentions: mentionsText, lng: language }),
|
||||
},
|
||||
} as const,
|
||||
Boolean(elements.length) &&
|
||||
({
|
||||
type: 'actions',
|
||||
appId: APP_ID,
|
||||
elements,
|
||||
} as const),
|
||||
].filter(isTruthy),
|
||||
private: true,
|
||||
});
|
||||
|
||||
return message;
|
||||
},
|
||||
callbacks.priority.LOW,
|
||||
'mention-user-not-in-channel',
|
||||
);
|
||||
Reference in New Issue
Block a user