chore(client): Remove Meteor.Error from `slashCommand` (#32915)

pull/32940/head
Douglas Fabris 1 year ago committed by GitHub
parent 77989f51dd
commit c676b357c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      apps/meteor/app/slackbridge/client/slackbridge_import.client.js
  2. 2
      apps/meteor/app/slashcommand-asciiarts/client/gimme.ts
  3. 2
      apps/meteor/app/slashcommand-asciiarts/client/lenny.ts
  4. 2
      apps/meteor/app/slashcommand-asciiarts/client/shrug.ts
  5. 2
      apps/meteor/app/slashcommand-asciiarts/client/tableflip.ts
  6. 2
      apps/meteor/app/slashcommand-asciiarts/client/unflip.ts
  7. 2
      apps/meteor/app/slashcommand-asciiarts/server/gimme.ts
  8. 2
      apps/meteor/app/slashcommand-asciiarts/server/lenny.ts
  9. 2
      apps/meteor/app/slashcommand-asciiarts/server/shrug.ts
  10. 2
      apps/meteor/app/slashcommand-asciiarts/server/tableflip.ts
  11. 2
      apps/meteor/app/slashcommand-asciiarts/server/unflip.ts
  12. 2
      apps/meteor/app/slashcommands-archiveroom/client/client.ts
  13. 2
      apps/meteor/app/slashcommands-archiveroom/server/server.ts
  14. 2
      apps/meteor/app/slashcommands-create/client/client.ts
  15. 2
      apps/meteor/app/slashcommands-create/server/server.ts
  16. 2
      apps/meteor/app/slashcommands-help/server/server.ts
  17. 2
      apps/meteor/app/slashcommands-hide/client/hide.ts
  18. 2
      apps/meteor/app/slashcommands-invite/client/client.ts
  19. 2
      apps/meteor/app/slashcommands-invite/server/server.ts
  20. 2
      apps/meteor/app/slashcommands-inviteall/client/client.ts
  21. 2
      apps/meteor/app/slashcommands-inviteall/server/server.ts
  22. 2
      apps/meteor/app/slashcommands-join/client/client.ts
  23. 2
      apps/meteor/app/slashcommands-join/server/server.ts
  24. 2
      apps/meteor/app/slashcommands-kick/client/client.ts
  25. 2
      apps/meteor/app/slashcommands-kick/server/server.ts
  26. 2
      apps/meteor/app/slashcommands-leave/server/leave.ts
  27. 2
      apps/meteor/app/slashcommands-me/server/me.ts
  28. 2
      apps/meteor/app/slashcommands-msg/server/server.ts
  29. 2
      apps/meteor/app/slashcommands-mute/server/mute.ts
  30. 2
      apps/meteor/app/slashcommands-mute/server/unmute.ts
  31. 2
      apps/meteor/app/slashcommands-open/client/client.ts
  32. 2
      apps/meteor/app/slashcommands-status/client/status.ts
  33. 2
      apps/meteor/app/slashcommands-status/server/status.ts
  34. 2
      apps/meteor/app/slashcommands-topic/client/topic.ts
  35. 2
      apps/meteor/app/slashcommands-topic/server/topic.ts
  36. 2
      apps/meteor/app/slashcommands-unarchiveroom/client/client.ts
  37. 2
      apps/meteor/app/slashcommands-unarchiveroom/server/server.ts
  38. 2
      apps/meteor/app/utils/client/index.ts
  39. 11
      apps/meteor/app/utils/client/slashCommand.ts
  40. 136
      apps/meteor/app/utils/server/slashCommand.ts
  41. 2
      apps/meteor/client/hooks/useAppSlashCommands.ts
  42. 7
      apps/meteor/client/lib/errors/InvalidCommandUsage.ts
  43. 7
      apps/meteor/client/lib/errors/InvalidPreview.ts
  44. 2
      apps/meteor/client/lib/errors/index.ts
  45. 2
      apps/meteor/client/startup/slashCommands/federation.ts
  46. 2
      apps/meteor/client/views/room/composer/hooks/useComposerBoxPopupQueries.ts

@ -1,5 +1,5 @@
import { settings } from '../../settings/client';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
settings.onload('SlackBridge_Enabled', (key, value) => {
if (value) {

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { sdk } from '../../utils/client/lib/SDKClient';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
/*
* Gimme is a named function that will replace /gimme commands
* @param {Object} message - The message object

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { sdk } from '../../utils/client/lib/SDKClient';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
/*
* Lenny is a named function that will replace /lenny commands
* @param {Object} message - The message object

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { sdk } from '../../utils/client/lib/SDKClient';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
/*
* Shrug is a named function that will replace /shrug commands
* @param {Object} message - The message object

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { sdk } from '../../utils/client/lib/SDKClient';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
/*
* Tableflip is a named function that will replace /Tableflip commands
* @param {Object} message - The message object

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { sdk } from '../../utils/client/lib/SDKClient';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
/*
* Unflip is a named function that will replace /unflip commands
* @param {Object} message - The message object

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { executeSendMessage } from '../../lib/server/methods/sendMessage';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Gimme is a named function that will replace /gimme commands
* @param {Object} message - The message object

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { executeSendMessage } from '../../lib/server/methods/sendMessage';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Lenny is a named function that will replace /lenny commands
* @param {Object} message - The message object

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { executeSendMessage } from '../../lib/server/methods/sendMessage';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Shrug is a named function that will replace /shrug commands
* @param {Object} message - The message object

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { executeSendMessage } from '../../lib/server/methods/sendMessage';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Tableflip is a named function that will replace /Tableflip commands
* @param {Object} message - The message object

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { executeSendMessage } from '../../lib/server/methods/sendMessage';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Unflip is a named function that will replace /unflip commands
* @param {Object} message - The message object

@ -1,4 +1,4 @@
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'archive',

@ -10,7 +10,7 @@ import { roomCoordinator } from '../../../server/lib/rooms/roomCoordinator';
import { hasPermissionAsync } from '../../authorization/server/functions/hasPermission';
import { archiveRoom } from '../../lib/server/functions/archiveRoom';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
slashCommands.add({
command: 'archive',

@ -1,4 +1,4 @@
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'create',

@ -6,7 +6,7 @@ import { i18n } from '../../../server/lib/i18n';
import { createChannelMethod } from '../../lib/server/methods/createChannel';
import { createPrivateGroupMethod } from '../../lib/server/methods/createPrivateGroup';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
slashCommands.add({
command: 'create',

@ -4,7 +4,7 @@ import { Users } from '@rocket.chat/models';
import { i18n } from '../../../server/lib/i18n';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Help is a named function that will replace /help commands

@ -1,4 +1,4 @@
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'hide',

@ -1,4 +1,4 @@
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'invite',

@ -6,7 +6,7 @@ import { Meteor } from 'meteor/meteor';
import { i18n } from '../../../server/lib/i18n';
import { addUsersToRoomMethod } from '../../lib/server/methods/addUsersToRoom';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Invite is a named function that will replace /invite commands

@ -1,4 +1,4 @@
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'invite-all-to',

@ -15,7 +15,7 @@ import { addUsersToRoomMethod } from '../../lib/server/methods/addUsersToRoom';
import { createChannelMethod } from '../../lib/server/methods/createChannel';
import { createPrivateGroupMethod } from '../../lib/server/methods/createPrivateGroup';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
function inviteAll<T extends string>(type: T): SlashCommand<T>['callback'] {
return async function inviteAll({ command, params, message, userId }: SlashCommandCallbackParams<T>): Promise<void> {

@ -1,6 +1,6 @@
import type { Meteor } from 'meteor/meteor';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'join',

@ -5,7 +5,7 @@ import { Meteor } from 'meteor/meteor';
import { i18n } from '../../../server/lib/i18n';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
slashCommands.add({
command: 'join',

@ -1,6 +1,6 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'kick',

@ -6,7 +6,7 @@ import { Users } from '@rocket.chat/models';
import { i18n } from '../../../server/lib/i18n';
import { removeUserFromRoomMethod } from '../../../server/methods/removeUserFromRoom';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
slashCommands.add({
command: 'kick',

@ -5,7 +5,7 @@ import { Users } from '@rocket.chat/models';
import { i18n } from '../../../server/lib/i18n';
import { leaveRoomMethod } from '../../lib/server/methods/leaveRoom';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Leave is a named function that will replace /leave commands

@ -1,7 +1,7 @@
import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { executeSendMessage } from '../../lib/server/methods/sendMessage';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Me is a named function that will replace /me commands

@ -7,7 +7,7 @@ import { i18n } from '../../../server/lib/i18n';
import { createDirectMessage } from '../../../server/methods/createDirectMessage';
import { executeSendMessage } from '../../lib/server/methods/sendMessage';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Msg is a named function that will replace /msg commands

@ -5,7 +5,7 @@ import { Users } from '@rocket.chat/models';
import { i18n } from '../../../server/lib/i18n';
import { muteUserInRoom } from '../../../server/methods/muteUserInRoom';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Mute is a named function that will replace /mute commands

@ -5,7 +5,7 @@ import { Users } from '@rocket.chat/models';
import { i18n } from '../../../server/lib/i18n';
import { unmuteUserInRoom } from '../../../server/methods/unmuteUserInRoom';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
/*
* Unmute is a named function that will replace /unmute commands

@ -5,7 +5,7 @@ import { roomCoordinator } from '../../../client/lib/rooms/roomCoordinator';
import { router } from '../../../client/providers/RouterProvider';
import { Subscriptions, ChatSubscription } from '../../models/client';
import { sdk } from '../../utils/client/lib/SDKClient';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'open',

@ -2,7 +2,7 @@ import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { dispatchToastMessage } from '../../../client/lib/toast';
import { sdk } from '../../utils/client/lib/SDKClient';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'status',

@ -5,7 +5,7 @@ import { Users } from '@rocket.chat/models';
import { i18n } from '../../../server/lib/i18n';
import { settings } from '../../settings/server';
import { setUserStatusMethod } from '../../user-status/server/methods/setUserStatus';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
slashCommands.add({
command: 'status',

@ -5,7 +5,7 @@ import { callbacks } from '../../../lib/callbacks';
import { hasPermission } from '../../authorization/client';
import { ChatRoom } from '../../models/client/models/ChatRoom';
import { sdk } from '../../utils/client/lib/SDKClient';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'topic',

@ -2,7 +2,7 @@ import type { SlashCommandCallbackParams } from '@rocket.chat/core-typings';
import { hasPermissionAsync } from '../../authorization/server/functions/hasPermission';
import { saveRoomSettings } from '../../channel-settings/server/methods/saveRoomSettings';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
slashCommands.add({
command: 'topic',

@ -1,4 +1,4 @@
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/client/slashCommand';
slashCommands.add({
command: 'unarchive',

@ -10,7 +10,7 @@ import { roomCoordinator } from '../../../server/lib/rooms/roomCoordinator';
import { hasPermissionAsync } from '../../authorization/server/functions/hasPermission';
import { unarchiveRoom } from '../../lib/server/functions/unarchiveRoom';
import { settings } from '../../settings/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { slashCommands } from '../../utils/server/slashCommand';
slashCommands.add({
command: 'unarchive',

@ -2,6 +2,6 @@ export { Info } from '../rocketchat.info';
export { getUserPreference } from './lib/getUserPreference';
export { fileUploadIsValidContentType } from './restrictions';
export { getUserAvatarURL } from './getUserAvatarURL';
export { slashCommands } from '../lib/slashCommand';
export { slashCommands } from './slashCommand';
export { getURL } from './getURL';
export { APIClient } from './lib/RestApiClient';

@ -6,7 +6,8 @@ import type {
SlashCommandPreviewItem,
SlashCommandPreviews,
} from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';
import { InvalidCommandUsage, InvalidPreview } from '../../../client/lib/errors';
interface ISlashCommandAddParams<T extends string> {
command: string;
@ -69,7 +70,7 @@ export const slashCommands = {
}
if (!message?.rid) {
throw new Meteor.Error('invalid-command-usage', 'Executing a command requires at least a message with a room id.');
throw new InvalidCommandUsage();
}
return cmd.callback({ command, params, message, triggerId, userId });
@ -85,7 +86,7 @@ export const slashCommands = {
}
if (!message?.rid) {
throw new Meteor.Error('invalid-command-usage', 'Executing a command requires at least a message with a room id.');
throw new InvalidCommandUsage();
}
const previewInfo = await cmd.previewer(command, params, message);
@ -114,12 +115,12 @@ export const slashCommands = {
}
if (!message?.rid) {
throw new Meteor.Error('invalid-command-usage', 'Executing a command requires at least a message with a room id.');
throw new InvalidCommandUsage();
}
// { id, type, value }
if (!preview.id || !preview.type || !preview.value) {
throw new Meteor.Error('error-invalid-preview', 'Preview Item must have an id, type, and value.');
throw new InvalidPreview();
}
return cmd.previewCallback(command, params, message, preview, triggerId);

@ -1,7 +1,139 @@
import { MeteorError } from '@rocket.chat/core-services';
import type {
IMessage,
SlashCommand,
SlashCommandOptions,
RequiredField,
SlashCommandPreviewItem,
SlashCommandPreviews,
} from '@rocket.chat/core-typings';
import type { ServerMethods } from '@rocket.chat/ddp-client';
import { Meteor } from 'meteor/meteor';
import { slashCommands } from '../lib/slashCommand';
interface ISlashCommandAddParams<T extends string> {
command: string;
callback?: SlashCommand<T>['callback'];
options?: SlashCommandOptions;
result?: SlashCommand['result'];
providesPreview?: boolean;
previewer?: SlashCommand['previewer'];
previewCallback?: SlashCommand['previewCallback'];
appId?: string;
description?: string;
}
export const slashCommands = {
commands: {} as Record<string, SlashCommand>,
add<T extends string>({
command,
callback,
options = {},
result,
providesPreview = false,
previewer,
previewCallback,
appId,
description = '',
}: ISlashCommandAddParams<T>): void {
if (this.commands[command]) {
return;
}
this.commands[command] = {
command,
callback,
params: options.params,
description: options.description || description,
permission: options.permission,
clientOnly: options.clientOnly || false,
result,
providesPreview: Boolean(providesPreview),
previewer,
previewCallback,
appId,
} as SlashCommand;
},
async run({
command,
message,
params,
triggerId,
userId,
}: {
command: string;
params: string;
message: RequiredField<Partial<IMessage>, 'rid' | '_id'>;
userId: string;
triggerId?: string | undefined;
}): Promise<unknown> {
const cmd = this.commands[command];
if (typeof cmd?.callback !== 'function') {
return;
}
if (!message?.rid) {
throw new MeteorError('invalid-command-usage', 'Executing a command requires at least a message with a room id.');
}
return cmd.callback({ command, params, message, triggerId, userId });
},
async getPreviews(
command: string,
params: string,
message: RequiredField<Partial<IMessage>, 'rid'>,
): Promise<SlashCommandPreviews | undefined> {
const cmd = this.commands[command];
if (typeof cmd?.previewer !== 'function') {
return;
}
if (!message?.rid) {
throw new MeteorError('invalid-command-usage', 'Executing a command requires at least a message with a room id.');
}
const previewInfo = await cmd.previewer(command, params, message);
if (!previewInfo?.items?.length) {
return;
}
// A limit of ten results, to save time and bandwidth
if (previewInfo.items.length >= 10) {
previewInfo.items = previewInfo.items.slice(0, 10);
}
return previewInfo;
},
async executePreview(
command: string,
params: string,
message: Pick<IMessage, 'rid'> & Partial<Omit<IMessage, 'rid'>>,
preview: SlashCommandPreviewItem,
triggerId?: string,
) {
const cmd = this.commands[command];
if (typeof cmd?.previewCallback !== 'function') {
return;
}
if (!message?.rid) {
throw new MeteorError('invalid-command-usage', 'Executing a command requires at least a message with a room id.');
}
// { id, type, value }
if (!preview.id || !preview.type || !preview.value) {
throw new MeteorError('error-invalid-preview', 'Preview Item must have an id, type, and value.');
}
return cmd.previewCallback(command, params, message, preview, triggerId);
},
};
declare module '@rocket.chat/ddp-client' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
slashCommand(params: { cmd: string; params: string; msg: IMessage; triggerId: string }): unknown;
}
}
Meteor.methods<ServerMethods>({
async slashCommand(command) {
@ -27,5 +159,3 @@ Meteor.methods<ServerMethods>({
});
},
});
export { slashCommands };

@ -3,7 +3,7 @@ import { useEndpoint, useStream, useUserId } from '@rocket.chat/ui-contexts';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useEffect } from 'react';
import { slashCommands } from '../../app/utils/lib/slashCommand';
import { slashCommands } from '../../app/utils/client/slashCommand';
export const useAppSlashCommands = () => {
const queryClient = useQueryClient();

@ -0,0 +1,7 @@
import { RocketChatError } from './RocketChatError';
export class InvalidCommandUsage extends RocketChatError<'invalid-command-usage'> {
constructor(message = 'Executing a command requires at least a message with a room id.', details?: string) {
super('invalid-command-usage', message, details);
}
}

@ -0,0 +1,7 @@
import { RocketChatError } from './RocketChatError';
export class InvalidPreview extends RocketChatError<'error-invalid-preview'> {
constructor(message = 'Preview Item must have an id, type, and value.', details?: string) {
super('error-invalid-preview', message, details);
}
}

@ -0,0 +1,2 @@
export * from './InvalidCommandUsage';
export * from './InvalidPreview';

@ -1,4 +1,4 @@
import { slashCommands } from '../../../app/utils/lib/slashCommand';
import { slashCommands } from '../../../app/utils/client/slashCommand';
const callback = undefined;
const result = undefined;

@ -2,7 +2,7 @@ import type { QueriesResults } from '@tanstack/react-query';
import { useQueries } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { slashCommands } from '../../../../../app/utils/lib/slashCommand';
import { slashCommands } from '../../../../../app/utils/client/slashCommand';
import type { ComposerPopupOption } from '../../contexts/ComposerPopupContext';
import { useEnablePopupPreview } from './useEnablePopupPreview';

Loading…
Cancel
Save