Skip to content

Commit

Permalink
Chore: Convert slashCommands to typescript (#25592)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo Cabrera <eduardo.cabrera@rocket.chat>
Co-authored-by: Leonardo Ostjen Couto <leonardoostjen@gmail.com>
Co-authored-by: eduardofcabrera <64327259+eduardofcabrera@users.noreply.github.com>
Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>
  • Loading branch information
5 people committed May 23, 2022
1 parent a859082 commit 2ec6982
Show file tree
Hide file tree
Showing 71 changed files with 648 additions and 643 deletions.
18 changes: 12 additions & 6 deletions apps/meteor/app/apps/server/bridges/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Utilities } from '../../lib/misc/Utilities';
import { AppServerOrchestrator } from '../orchestrator';

export class AppCommandsBridge extends CommandBridge {
disabledCommands: Map<string, object>;
disabledCommands: Map<string, typeof slashCommands.commands[string]>;

// eslint-disable-next-line no-empty-function
constructor(private readonly orch: AppServerOrchestrator) {
Expand Down Expand Up @@ -40,7 +40,7 @@ export class AppCommandsBridge extends CommandBridge {
throw new Error(`The command is not currently disabled: "${cmd}"`);
}

slashCommands.commands[cmd] = this.disabledCommands.get(cmd);
slashCommands.commands[cmd] = this.disabledCommands.get(cmd) as typeof slashCommands.commands[string];
this.disabledCommands.delete(cmd);

this.orch.getNotifier().commandUpdated(cmd);
Expand All @@ -59,11 +59,13 @@ export class AppCommandsBridge extends CommandBridge {
return;
}

if (typeof slashCommands.commands[cmd] === 'undefined') {
const commandObj = slashCommands.commands[cmd];

if (typeof commandObj === 'undefined') {
throw new Error(`Command does not exist in the system currently: "${cmd}"`);
}

this.disabledCommands.set(cmd, slashCommands.commands[cmd]);
this.disabledCommands.set(cmd, commandObj);
delete slashCommands.commands[cmd];

this.orch.getNotifier().commandDisabled(cmd);
Expand All @@ -87,7 +89,9 @@ export class AppCommandsBridge extends CommandBridge {
item.callback = this._appCommandExecutor.bind(this);
item.providesPreview = command.providesPreview;
item.previewer = command.previewer ? this._appCommandPreviewer.bind(this) : item.previewer;
item.previewCallback = command.executePreviewItem ? this._appCommandPreviewExecutor.bind(this) : item.previewCallback;
item.previewCallback = (
command.executePreviewItem ? this._appCommandPreviewExecutor.bind(this) : item.previewCallback
) as typeof slashCommands.commands[string]['previewCallback'];

slashCommands.commands[cmd] = item;
this.orch.getNotifier().commandUpdated(cmd);
Expand All @@ -107,7 +111,9 @@ export class AppCommandsBridge extends CommandBridge {
callback: this._appCommandExecutor.bind(this),
providesPreview: command.providesPreview,
previewer: !command.previewer ? undefined : this._appCommandPreviewer.bind(this),
previewCallback: !command.executePreviewItem ? undefined : this._appCommandPreviewExecutor.bind(this),
previewCallback: (!command.executePreviewItem ? undefined : this._appCommandPreviewExecutor.bind(this)) as
| typeof slashCommands.commands[string]['previewCallback']
| undefined,
};

slashCommands.commands[command.command.toLowerCase()] = item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ function SlackBridgeImport(command, params, item) {
});
throw error;
}
return SlackBridgeImport;
}

slashCommands.add('slackbridge-import', SlackBridgeImport);
20 changes: 0 additions & 20 deletions apps/meteor/app/slashcommand-asciiarts/lib/gimme.js

This file was deleted.

19 changes: 19 additions & 0 deletions apps/meteor/app/slashcommand-asciiarts/lib/gimme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Meteor } from 'meteor/meteor';
import type { IMessage } from '@rocket.chat/core-typings';

import { slashCommands } from '../../utils/lib/slashCommand';
/*
* Gimme is a named function that will replace /gimme commands
* @param {Object} message - The message object
*/

function Gimme(_command: 'gimme', params: string, item: IMessage): void {
const msg = item;
msg.msg = `༼ つ ◕_◕ ༽つ ${params}`;
Meteor.call('sendMessage', msg);
}

slashCommands.add('gimme', Gimme, {
description: 'Slash_Gimme_Description',
params: 'your_message_optional',
});
20 changes: 0 additions & 20 deletions apps/meteor/app/slashcommand-asciiarts/lib/lenny.js

This file was deleted.

19 changes: 19 additions & 0 deletions apps/meteor/app/slashcommand-asciiarts/lib/lenny.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { IMessage } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';

import { slashCommands } from '../../utils/lib/slashCommand';
/*
* Lenny is a named function that will replace /lenny commands
* @param {Object} message - The message object
*/

function LennyFace(_command: 'lennyface', params: string, item: IMessage): void {
const msg = item;
msg.msg = `${params} ( ͡° ͜ʖ ͡°)`;
Meteor.call('sendMessage', msg);
}

slashCommands.add('lennyface', LennyFace, {
description: 'Slash_LennyFace_Description',
params: 'your_message_optional',
});
20 changes: 0 additions & 20 deletions apps/meteor/app/slashcommand-asciiarts/lib/shrug.js

This file was deleted.

19 changes: 19 additions & 0 deletions apps/meteor/app/slashcommand-asciiarts/lib/shrug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { IMessage } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';

import { slashCommands } from '../../utils/lib/slashCommand';
/*
* Shrug is a named function that will replace /shrug commands
* @param {Object} message - The message object
*/

function Shrug(_command: 'shrug', params: string, item: IMessage): void {
const msg = item;
msg.msg = `${params} ¯\\_(ツ)_/¯`;
Meteor.call('sendMessage', msg);
}

slashCommands.add('shrug', Shrug, {
description: 'Slash_Shrug_Description',
params: 'your_message_optional',
});
20 changes: 0 additions & 20 deletions apps/meteor/app/slashcommand-asciiarts/lib/tableflip.js

This file was deleted.

19 changes: 19 additions & 0 deletions apps/meteor/app/slashcommand-asciiarts/lib/tableflip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { IMessage } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';

import { slashCommands } from '../../utils/lib/slashCommand';
/*
* Tableflip is a named function that will replace /Tableflip commands
* @param {Object} message - The message object
*/

function Tableflip(_command: 'tableflip', params: string, item: IMessage): void {
const msg = item;
msg.msg = `${params} (╯°□°)╯︵ ┻━┻`;
Meteor.call('sendMessage', msg);
}

slashCommands.add('tableflip', Tableflip, {
description: 'Slash_Tableflip_Description',
params: 'your_message_optional',
});
20 changes: 0 additions & 20 deletions apps/meteor/app/slashcommand-asciiarts/lib/unflip.js

This file was deleted.

19 changes: 19 additions & 0 deletions apps/meteor/app/slashcommand-asciiarts/lib/unflip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { IMessage } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';

import { slashCommands } from '../../utils/lib/slashCommand';
/*
* Unflip is a named function that will replace /unflip commands
* @param {Object} message - The message object
*/

function Unflip(_command: 'unflip', params: string, item: IMessage): void {
const msg = item;
msg.msg = `${params} ┬─┬ ノ( ゜-゜ノ)`;
Meteor.call('sendMessage', msg);
}

slashCommands.add('unflip', Unflip, {
description: 'Slash_TableUnflip_Description',
params: 'your_message_optional',
});
23 changes: 7 additions & 16 deletions apps/meteor/app/slashcommands-archiveroom/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { IMessage } from '@rocket.chat/core-typings';

import { Rooms, Messages } from '../../models/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { api } from '../../../server/sdk/api';
import { settings } from '../../settings/server';

function Archive(_command: 'archive', params: string, item: Record<string, string>): void | Function {
function Archive(_command: 'archive', params: string, item: IMessage): void {
let channel = params.trim();

let room;
Expand Down Expand Up @@ -61,20 +62,10 @@ function Archive(_command: 'archive', params: string, item: Record<string, strin
lng: settings.get('Language') || 'en',
}),
});

return Archive;
}

slashCommands.add(
'archive',
Archive,
{
description: 'Archive',
params: '#channel',
permission: 'archive-room',
},
undefined,
false,
undefined,
undefined,
);
slashCommands.add('archive', Archive, {
description: 'Archive',
params: '#channel',
permission: 'archive-room',
});
19 changes: 6 additions & 13 deletions apps/meteor/app/slashcommands-bridge/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Meteor } from 'meteor/meteor';
import { Match } from 'meteor/check';
import { IMessage } from '@rocket.chat/core-typings';

import { slashCommands } from '../../utils/lib/slashCommand';
import { matrixClient } from '../../federation-v2/server/matrix-client';

function Bridge(_command: 'bridge', stringParams: string, item: Record<string, string>): void {
function Bridge(_command: 'bridge', stringParams: string, item: IMessage): void {
if (_command !== 'bridge' || !Match.test(stringParams, String)) {
return;
}
Expand All @@ -29,15 +30,7 @@ function Bridge(_command: 'bridge', stringParams: string, item: Record<string, s
}
}

slashCommands.add(
'bridge',
Bridge,
{
description: 'Invites_an_user_to_a_bridged_room',
params: '#command #user',
},
undefined,
false,
undefined,
undefined,
);
slashCommands.add('bridge', Bridge, {
description: 'Invites_an_user_to_a_bridged_room',
params: '#command #user',
});
21 changes: 7 additions & 14 deletions apps/meteor/app/slashcommands-create/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { IMessage } from '@rocket.chat/core-typings';

import { settings } from '../../settings/server';
import { Rooms } from '../../models/server';
import { slashCommands } from '../../utils/lib/slashCommand';
import { api } from '../../../server/sdk/api';

function Create(_command: 'create', params: string, item: Record<string, string>): void {
function Create(_command: 'create', params: string, item: IMessage): void {
function getParams(str: string): string[] {
const regex = /(--(\w+))+/g;
const result = [];
Expand Down Expand Up @@ -53,16 +54,8 @@ function Create(_command: 'create', params: string, item: Record<string, string>
Meteor.call('createChannel', channelStr, []);
}

slashCommands.add(
'create',
Create,
{
description: 'Create_A_New_Channel',
params: '#channel',
permission: ['create-c', 'create-p'],
},
undefined,
false,
undefined,
undefined,
);
slashCommands.add('create', Create, {
description: 'Create_A_New_Channel',
params: '#channel',
permission: ['create-c', 'create-p'],
});
1 change: 0 additions & 1 deletion apps/meteor/app/slashcommands-help/index.ts

This file was deleted.

14 changes: 3 additions & 11 deletions apps/meteor/app/slashcommands-help/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ function Help(_command: 'help', _params: string, item: IMessage): void {
});
}

slashCommands.add(
'help',
Help,
{
description: 'Show_the_keyboard_shortcut_list',
},
undefined,
false,
undefined,
undefined,
);
slashCommands.add('help', Help, {
description: 'Show_the_keyboard_shortcut_list',
});
Loading

0 comments on commit 2ec6982

Please sign in to comment.