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
118 lines
3.1 KiB
TypeScript
118 lines
3.1 KiB
TypeScript
import type { IMessage, SlashCommand } from '@rocket.chat/core-typings';
|
|
import { Random } from '@rocket.chat/random';
|
|
import { escapeHTML } from '@rocket.chat/tools';
|
|
|
|
import { t } from '../../../../app/utils/lib/i18n';
|
|
import { sdk } from '../../SDKClient';
|
|
import { hasAtLeastOnePermission } from '../../authorization';
|
|
import { settings } from '../../settings';
|
|
import { slashCommands } from '../../slashCommand';
|
|
import type { ChatAPI } from '../ChatAPI';
|
|
|
|
const parse = (msg: string): { command: string; params: string } | { command: SlashCommand; params: string } | undefined => {
|
|
const match = msg.match(/^\/([^\s]+)(.*)/);
|
|
|
|
if (!match) {
|
|
return undefined;
|
|
}
|
|
|
|
const [, cmd, params] = match;
|
|
const command = slashCommands.commands[cmd];
|
|
|
|
if (!command) {
|
|
return { command: cmd, params };
|
|
}
|
|
|
|
return { command, params };
|
|
};
|
|
|
|
const warnUnrecognizedSlashCommand = async (chat: ChatAPI, message: string): Promise<void> => {
|
|
console.error(message);
|
|
|
|
await chat.data.pushEphemeralMessage({
|
|
_id: Random.id(),
|
|
ts: new Date(),
|
|
msg: message,
|
|
u: {
|
|
_id: 'rocket.cat',
|
|
username: 'rocket.cat',
|
|
name: 'Rocket.Cat',
|
|
},
|
|
private: true,
|
|
_updatedAt: new Date(),
|
|
});
|
|
};
|
|
|
|
export const processSlashCommand = async (chat: ChatAPI, message: IMessage): Promise<boolean> => {
|
|
const match = parse(message.msg);
|
|
|
|
if (!match) {
|
|
return false;
|
|
}
|
|
|
|
const { command, params } = match;
|
|
|
|
if (typeof command === 'string') {
|
|
if (!settings.peek('Message_AllowUnrecognizedSlashCommand')) {
|
|
chat.composer?.clear();
|
|
await warnUnrecognizedSlashCommand(chat, t('No_such_command', { command: escapeHTML(command) }));
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
const { permission, clientOnly, callback: handleOnClient, result: handleResult, appId, command: commandName } = command;
|
|
|
|
if (permission && !hasAtLeastOnePermission(permission, message.rid)) {
|
|
chat.composer?.clear();
|
|
await warnUnrecognizedSlashCommand(chat, t('You_do_not_have_permission_to_execute_this_command', { command: escapeHTML(commandName) }));
|
|
return true;
|
|
}
|
|
|
|
if (clientOnly && chat.uid) {
|
|
chat.composer?.clear();
|
|
handleOnClient?.({ command: commandName, message, params, userId: chat.uid });
|
|
return true;
|
|
}
|
|
|
|
void sdk.rest.post('/v1/statistics.telemetry', {
|
|
params: [{ eventName: 'slashCommandsStats', timestamp: Date.now(), command: commandName }],
|
|
});
|
|
|
|
const triggerId = chat.ActionManager.generateTriggerId(appId);
|
|
|
|
const data = {
|
|
cmd: commandName,
|
|
params,
|
|
msg: message,
|
|
userId: chat.uid,
|
|
} as const;
|
|
|
|
try {
|
|
if (appId) {
|
|
chat.ActionManager.notifyBusy();
|
|
}
|
|
|
|
chat.composer?.clear();
|
|
const { result } = await sdk.rest.post('/v1/commands.run', {
|
|
command: commandName,
|
|
params,
|
|
roomId: message.rid,
|
|
...(message.tmid && { tmid: message.tmid }),
|
|
...(triggerId && { triggerId }),
|
|
});
|
|
|
|
handleResult?.(undefined, result, data);
|
|
} catch (error: unknown) {
|
|
await warnUnrecognizedSlashCommand(chat, t('Something_went_wrong_while_executing_command', { command: commandName }));
|
|
handleResult?.(error, undefined, data);
|
|
}
|
|
|
|
if (appId) {
|
|
chat.ActionManager.notifyIdle();
|
|
}
|
|
|
|
return true;
|
|
};
|