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
129 lines
4.4 KiB
TypeScript
129 lines
4.4 KiB
TypeScript
/*
|
|
* Mentions is a named function that will process Mentions
|
|
* @param {Object} message - The message object
|
|
*/
|
|
import { isE2EEMessage, type IMessage, type IRoom, type IUser } from '@rocket.chat/core-typings';
|
|
|
|
import { type MentionsParserArgs, MentionsParser } from '../../../../app/mentions/lib/MentionsParser';
|
|
import { extractMentionsFromMessageAST } from '../../messages/extractMentionsFromMessageAST';
|
|
|
|
type MentionsServerArgs = MentionsParserArgs & {
|
|
messageMaxAll: () => number;
|
|
getChannels: (c: string[]) => Promise<Pick<IRoom, '_id' | 'name' | 'fname' | 'federated'>[]>;
|
|
getUsers: (u: string[]) => Promise<{ type: 'team' | 'user'; _id: string; username?: string; name?: string }[]>;
|
|
getUser: (u: string) => Promise<IUser | null>;
|
|
getTotalChannelMembers: (rid: string) => Promise<number>;
|
|
onMaxRoomMembersExceeded: ({ sender, rid }: { sender: IMessage['u']; rid: string }) => Promise<void>;
|
|
};
|
|
|
|
export class MentionsServer extends MentionsParser {
|
|
messageMaxAll: MentionsServerArgs['messageMaxAll'];
|
|
|
|
getChannels: MentionsServerArgs['getChannels'];
|
|
|
|
getUsers: MentionsServerArgs['getUsers'];
|
|
|
|
getUser: MentionsServerArgs['getUser'];
|
|
|
|
getTotalChannelMembers: MentionsServerArgs['getTotalChannelMembers'];
|
|
|
|
onMaxRoomMembersExceeded: MentionsServerArgs['onMaxRoomMembersExceeded'];
|
|
|
|
constructor(args: MentionsServerArgs) {
|
|
super(args);
|
|
|
|
this.messageMaxAll = args.messageMaxAll;
|
|
this.getChannels = args.getChannels;
|
|
this.getUsers = args.getUsers;
|
|
this.getUser = args.getUser;
|
|
this.getTotalChannelMembers = args.getTotalChannelMembers;
|
|
this.onMaxRoomMembersExceeded =
|
|
args.onMaxRoomMembersExceeded ||
|
|
(() => {
|
|
/* do nothing */
|
|
});
|
|
}
|
|
|
|
async getUsersByMentions(message: IMessage): Promise<IMessage['mentions']> {
|
|
const { msg, rid, u: sender, e2eMentions }: Pick<IMessage, 'msg' | 'rid' | 'u' | 't' | 'e2eMentions'> = message;
|
|
|
|
const mentions =
|
|
isE2EEMessage(message) && e2eMentions?.e2eUserMentions && e2eMentions?.e2eUserMentions.length > 0
|
|
? e2eMentions?.e2eUserMentions
|
|
: this.getUserMentions(msg);
|
|
|
|
return this.convertMentionsToUsers(mentions, rid, sender);
|
|
}
|
|
|
|
async convertMentionsToUsers(mentions: string[], rid: string, sender: IMessage['u']): Promise<IMessage['mentions']> {
|
|
const mentionsAll: { _id: string; username: string }[] = [];
|
|
const userMentions = new Set<string>();
|
|
|
|
// A message may mention both @all and @here; fetch the member count at
|
|
// most once per message instead of once per mention.
|
|
let totalChannelMembers: number | undefined;
|
|
|
|
for (const m of mentions) {
|
|
let mention: string;
|
|
if (m.includes(':')) {
|
|
mention = m.trim();
|
|
} else if (m.startsWith('@')) {
|
|
mention = m.substring(1);
|
|
} else {
|
|
mention = m;
|
|
}
|
|
if (mention !== 'all' && mention !== 'here') {
|
|
userMentions.add(mention);
|
|
continue;
|
|
}
|
|
if (this.messageMaxAll() > 0) {
|
|
totalChannelMembers ??= await this.getTotalChannelMembers(rid);
|
|
if (totalChannelMembers > this.messageMaxAll()) {
|
|
await this.onMaxRoomMembersExceeded({ sender, rid });
|
|
continue;
|
|
}
|
|
}
|
|
mentionsAll.push({
|
|
_id: mention,
|
|
username: mention,
|
|
});
|
|
}
|
|
|
|
return [...mentionsAll, ...(userMentions.size ? await this.getUsers(Array.from(userMentions)) : [])];
|
|
}
|
|
|
|
async getChannelbyMentions(message: IMessage) {
|
|
const { msg, e2eMentions }: Pick<IMessage, 'msg' | 't' | 'e2eMentions'> = message;
|
|
|
|
const channels =
|
|
isE2EEMessage(message) && e2eMentions?.e2eChannelMentions && e2eMentions?.e2eChannelMentions.length > 0
|
|
? e2eMentions?.e2eChannelMentions
|
|
: this.getChannelMentions(msg);
|
|
return this.convertMentionsToChannels(channels);
|
|
}
|
|
|
|
async convertMentionsToChannels(channels: string[]): Promise<Pick<IRoom, '_id' | 'name' | 'fname' | 'federated'>[]> {
|
|
// Most messages don't mention any channel; skip the (empty) database
|
|
// query entirely in that case. An empty $in matches nothing anyway.
|
|
if (channels.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
return this.getChannels(channels.map((c) => (c.startsWith('#') ? c.substring(1) : c)));
|
|
}
|
|
|
|
async execute(message: IMessage) {
|
|
if (message.md) {
|
|
const { mentions, channels } = extractMentionsFromMessageAST(message.md);
|
|
message.mentions = await this.convertMentionsToUsers(mentions, message.rid, message.u);
|
|
message.channels = await this.convertMentionsToChannels(channels);
|
|
return message;
|
|
}
|
|
|
|
message.mentions = await this.getUsersByMentions(message);
|
|
message.channels = await this.getChannelbyMentions(message);
|
|
|
|
return message;
|
|
}
|
|
}
|