From 6ea9aebda3e85afb2724a0455740a3f1c4c972d0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 11 May 2020 10:54:28 +0100 Subject: [PATCH 1/3] Add slash commands /query and /msg to match IRC Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SlashCommands.tsx | 46 +++++++++++++++++++++++++++++++++++++ src/i18n/strings/en_EN.json | 2 ++ 2 files changed, 48 insertions(+) diff --git a/src/SlashCommands.tsx b/src/SlashCommands.tsx index bd7e60e2f4..ea561d9f12 100644 --- a/src/SlashCommands.tsx +++ b/src/SlashCommands.tsx @@ -40,6 +40,7 @@ import { Jitsi } from "./widgets/Jitsi"; import { parseFragment as parseHtml } from "parse5"; import sendBugReport from "./rageshake/submit-rageshake"; import SdkConfig from "./SdkConfig"; +import { ensureDMExists } from "./createRoom"; // XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816 interface HTMLInputEvent extends Event { @@ -970,6 +971,51 @@ export const Commands = [ }, category: CommandCategories.advanced, }), + new Command({ + command: "query", + description: _td("Opens chat with the given user"), + args: "", + runFn: function(roomId, userId) { + if (!userId || !userId.startsWith("@") || !userId.includes(":")) { + return reject(this.getUsage()); + } + + return success((async () => { + dis.dispatch({ + action: 'view_room', + room_id: await ensureDMExists(MatrixClientPeg.get(), userId), + }); + })()); + }, + category: CommandCategories.actions, + }), + new Command({ + command: "msg", + description: _td("Sends a message to the given user"), + args: " ", + runFn: function(roomId, args) { + if (args) { + const matches = args.match(/^(\S+?)(?: +(.*))?$/s); + if (matches) { + const [userId, msg] = matches.slice(1); + if (msg && userId && userId.startsWith("@") && userId.includes(":")) { + return success((async () => { + const cli = MatrixClientPeg.get(); + const roomId = await ensureDMExists(cli, userId); + dis.dispatch({ + action: 'view_room', + room_id: roomId, + }); + cli.sendTextMessage(roomId, msg); + })()); + } + } + } + + return reject(this.getUsage()); + }, + category: CommandCategories.actions, + }), // Command definitions for autocompletion ONLY: // /me is special because its not handled by SlashCommands.js and is instead done inside the Composer classes diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 27b4252f77..ec5400179d 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -221,6 +221,8 @@ "Send a bug report with logs": "Send a bug report with logs", "Logs sent": "Logs sent", "Thank you!": "Thank you!", + "Opens chat with the given user": "Opens chat with the given user", + "Sends a message to the given user": "Sends a message to the given user", "Displays action": "Displays action", "Reason": "Reason", "%(targetName)s accepted the invitation for %(displayName)s.": "%(targetName)s accepted the invitation for %(displayName)s.", From 293bd15ee90a6cafd610aedd48f2bada3710b4af Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 11 May 2020 11:05:03 +0100 Subject: [PATCH 2/3] fix variable name shadowing Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SlashCommands.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SlashCommands.tsx b/src/SlashCommands.tsx index ea561d9f12..3fa6c0d7ee 100644 --- a/src/SlashCommands.tsx +++ b/src/SlashCommands.tsx @@ -993,7 +993,7 @@ export const Commands = [ command: "msg", description: _td("Sends a message to the given user"), args: " ", - runFn: function(roomId, args) { + runFn: function(_, args) { if (args) { const matches = args.match(/^(\S+?)(?: +(.*))?$/s); if (matches) { From 3fa13d7de3eb2b22fd665a87a10f78e91f40c73e Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 12 May 2020 10:23:53 +0100 Subject: [PATCH 3/3] Add comment for the regex Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SlashCommands.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SlashCommands.tsx b/src/SlashCommands.tsx index 3fa6c0d7ee..133a2708dc 100644 --- a/src/SlashCommands.tsx +++ b/src/SlashCommands.tsx @@ -995,6 +995,7 @@ export const Commands = [ args: " ", runFn: function(_, args) { if (args) { + // matches the first whitespace delimited group and then the rest of the string const matches = args.match(/^(\S+?)(?: +(.*))?$/s); if (matches) { const [userId, msg] = matches.slice(1);