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
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
124 lines
4.6 KiB
TypeScript
124 lines
4.6 KiB
TypeScript
import type { IRoom } from '@rocket.chat/apps-engine/definition/rooms/IRoom';
|
|
import type { ISlashCommand } from '@rocket.chat/apps-engine/definition/slashcommands/ISlashCommand';
|
|
import { SlashCommandContext } from '@rocket.chat/apps-engine/definition/slashcommands/SlashCommandContext';
|
|
import type { Defined } from 'jsonrpc-lite';
|
|
import { JsonRpcError } from 'jsonrpc-lite';
|
|
|
|
import { AppObjectRegistry } from '../AppObjectRegistry';
|
|
import type { AppAccessors } from '../lib/accessors/mod';
|
|
import { AppAccessorsInstance } from '../lib/accessors/mod';
|
|
import type { RequestContext } from '../lib/requestContext';
|
|
import createRoom from '../lib/roomFactory';
|
|
import { wrapComposedApp } from '../lib/wrapAppForRequest';
|
|
|
|
export default async function slashCommandHandler(request: RequestContext): Promise<JsonRpcError | Defined> {
|
|
const { method: call, params } = request;
|
|
const { logger } = request.context;
|
|
|
|
const [, commandName, method] = call.split(':');
|
|
|
|
const command = AppObjectRegistry.get<ISlashCommand>(`slashcommand:${commandName}`);
|
|
|
|
if (!command) {
|
|
return new JsonRpcError(`Slashcommand ${commandName} not found`, -32000);
|
|
}
|
|
|
|
let result: Awaited<ReturnType<typeof handleExecutor>>;
|
|
|
|
logger.debug({ msg: `Command is being executed...`, commandName, method, params });
|
|
|
|
try {
|
|
if (method === 'executor' || method === 'previewer') {
|
|
result = await handleExecutor({ AppAccessorsInstance, request }, command, method, params);
|
|
} else if (method === 'executePreviewItem') {
|
|
result = await handlePreviewItem({ AppAccessorsInstance, request }, command, params);
|
|
} else {
|
|
return new JsonRpcError(`Method ${method} not found on slashcommand ${commandName}`, -32000);
|
|
}
|
|
|
|
logger.debug({ msg: `Command was successfully executed.`, commandName, method });
|
|
} catch (error) {
|
|
logger.debug({ msg: `Command was unsuccessful.`, commandName, method, err: error });
|
|
|
|
return new JsonRpcError(error.message, -32000);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
type Deps = {
|
|
AppAccessorsInstance: AppAccessors;
|
|
request: RequestContext;
|
|
};
|
|
|
|
/**
|
|
* @param deps Dependencies that need to be injected into the slashcommand
|
|
* @param command The slashcommand that is being executed
|
|
* @param method The method that is being executed
|
|
* @param params The parameters that are being passed to the method
|
|
*/
|
|
export function handleExecutor(deps: Deps, command: ISlashCommand, method: 'executor' | 'previewer', params: unknown) {
|
|
const executor = command[method];
|
|
|
|
if (typeof executor !== 'function') {
|
|
throw new Error(`Method ${method} not found on slashcommand ${command.command}`);
|
|
}
|
|
|
|
if (!Array.isArray(params) || typeof params[0] !== 'object' || !params[0]) {
|
|
throw new Error(`First parameter must be an object`);
|
|
}
|
|
|
|
const { sender, room, params: args, threadId, triggerId } = params[0] as Record<string, unknown>;
|
|
|
|
const context = new SlashCommandContext(
|
|
sender as SlashCommandContext['sender'],
|
|
createRoom(room as IRoom, deps.AppAccessorsInstance.getSenderFn()) as unknown as IRoom,
|
|
args as SlashCommandContext['params'],
|
|
threadId as SlashCommandContext['threadId'],
|
|
triggerId as SlashCommandContext['triggerId'],
|
|
);
|
|
|
|
return executor.apply(wrapComposedApp(command, deps.request), [
|
|
context,
|
|
deps.AppAccessorsInstance.getReader(),
|
|
deps.AppAccessorsInstance.getModifier(),
|
|
deps.AppAccessorsInstance.getHttp(),
|
|
deps.AppAccessorsInstance.getPersistence(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param deps Dependencies that need to be injected into the slashcommand
|
|
* @param command The slashcommand that is being executed
|
|
* @param params The parameters that are being passed to the method
|
|
*/
|
|
export function handlePreviewItem(deps: Deps, command: ISlashCommand, params: unknown) {
|
|
if (typeof command.executePreviewItem !== 'function') {
|
|
throw new Error(`Method not found on slashcommand ${command.command}`);
|
|
}
|
|
|
|
if (!Array.isArray(params) || typeof params[0] !== 'object' || !params[0]) {
|
|
throw new Error(`First parameter must be an object`);
|
|
}
|
|
|
|
const [previewItem, { sender, room, params: args, threadId, triggerId }] = params as [Record<string, unknown>, Record<string, unknown>];
|
|
|
|
const context = new SlashCommandContext(
|
|
sender as SlashCommandContext['sender'],
|
|
createRoom(room as IRoom, deps.AppAccessorsInstance.getSenderFn()) as unknown as IRoom,
|
|
args as SlashCommandContext['params'],
|
|
threadId as SlashCommandContext['threadId'],
|
|
triggerId as SlashCommandContext['triggerId'],
|
|
);
|
|
|
|
return command.executePreviewItem.call(
|
|
wrapComposedApp(command, deps.request),
|
|
previewItem,
|
|
context,
|
|
deps.AppAccessorsInstance.getReader(),
|
|
deps.AppAccessorsInstance.getModifier(),
|
|
deps.AppAccessorsInstance.getHttp(),
|
|
deps.AppAccessorsInstance.getPersistence(),
|
|
);
|
|
}
|