From 685154889c8af74c65d804868f646c0ed8df9423 Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Sun, 28 Aug 2022 15:49:54 +0530 Subject: [PATCH] Add /me & /shrug commands --- src/app/organisms/room/RoomViewInput.jsx | 56 ++++++++++++++---------- src/app/organisms/room/commands.js | 11 ++--- src/client/state/RoomsInput.js | 10 +++-- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/app/organisms/room/RoomViewInput.jsx b/src/app/organisms/room/RoomViewInput.jsx index 38e20ad6..0a0a171c 100644 --- a/src/app/organisms/room/RoomViewInput.jsx +++ b/src/app/organisms/room/RoomViewInput.jsx @@ -21,6 +21,7 @@ import ScrollView from '../../atoms/scroll/ScrollView'; import { MessageReply } from '../../molecules/message/Message'; import StickerBoard from '../sticker-board/StickerBoard'; +import { confirmDialog } from '../../molecules/confirm-dialog/ConfirmDialog'; import CirclePlusIC from '../../../../public/res/ic/outlined/circle-plus.svg'; import EmojiIC from '../../../../public/res/ic/outlined/emoji.svg'; @@ -184,38 +185,17 @@ function RoomViewInput({ }; }, [roomId]); - const processCommand = (cmdBody) => { - const spaceIndex = cmdBody.indexOf(' '); - const cmdName = cmdBody.slice(1, spaceIndex > -1 ? spaceIndex : undefined); - const cmdData = spaceIndex > -1 ? cmdBody.slice(spaceIndex + 1) : ''; - if (!commands[cmdName]) { - console.log('Invalid command'); - return; - } - commands[cmdName].exe(roomId, cmdData); - }; - - const sendMessage = async () => { - requestAnimationFrame(() => deactivateCmdAndEmit()); - const msgBody = textAreaRef.current.value; - if (msgBody.startsWith('/')) { - processCommand(msgBody.trim()); - - textAreaRef.current.value = ''; - textAreaRef.current.style.height = 'unset'; - return; - } + const sendBody = async (body, msgType = 'm.text') => { if (roomsInput.isSending(roomId)) return; - if (msgBody.trim() === '' && attachment === null) return; sendIsTyping(false); - roomsInput.setMessage(roomId, msgBody); + roomsInput.setMessage(roomId, body); if (attachment !== null) { roomsInput.setAttachment(roomId, attachment); } textAreaRef.current.disabled = true; textAreaRef.current.style.cursor = 'not-allowed'; - await roomsInput.sendInput(roomId); + await roomsInput.sendInput(roomId, msgType); textAreaRef.current.disabled = false; textAreaRef.current.style.cursor = 'unset'; focusInput(); @@ -225,6 +205,34 @@ function RoomViewInput({ if (replyTo !== null) setReplyTo(null); }; + const processCommand = (cmdBody) => { + const spaceIndex = cmdBody.indexOf(' '); + const cmdName = cmdBody.slice(1, spaceIndex > -1 ? spaceIndex : undefined); + const cmdData = spaceIndex > -1 ? cmdBody.slice(spaceIndex + 1) : ''; + if (!commands[cmdName]) { + confirmDialog('Invalid Command', `"${cmdName}" is not a valid command.`, 'Alright'); + return; + } + if (['me', 'shrug'].includes(cmdName)) { + commands[cmdName].exe(roomId, cmdData, (message, msgType) => sendBody(message, msgType)); + return; + } + commands[cmdName].exe(roomId, cmdData); + }; + + const sendMessage = async () => { + requestAnimationFrame(() => deactivateCmdAndEmit()); + const msgBody = textAreaRef.current.value.trim(); + if (msgBody.startsWith('/')) { + processCommand(msgBody.trim()); + textAreaRef.current.value = ''; + textAreaRef.current.style.height = 'unset'; + return; + } + if (msgBody === '' && attachment === null) return; + sendBody(msgBody, 'm.text'); + }; + const handleSendSticker = async (data) => { roomsInput.sendSticker(roomId, data); }; diff --git a/src/app/organisms/room/commands.js b/src/app/organisms/room/commands.js index 1c2e614b..57796a01 100644 --- a/src/app/organisms/room/commands.js +++ b/src/app/organisms/room/commands.js @@ -13,17 +13,18 @@ const commands = { me: { name: 'me', description: 'Display action', - exe: (roomId, data) => { + exe: (roomId, data, onSuccess) => { const body = data.trim(); - if (body === '') return undefined; - return body; + if (body === '') return; + onSuccess(body, 'm.emote'); }, }, shrug: { name: 'shrug', description: 'Send ¯\\_(ツ)_/¯ as message', - exe: (roomId, data) => ( - `¯\\_(ツ)_/¯${data.trim() !== '' ? ` ${data}` : ''}` + exe: (roomId, data, onSuccess) => onSuccess( + `¯\\_(ツ)_/¯${data.trim() !== '' ? ` ${data}` : ''}`, + 'm.text', ), }, markdown: { diff --git a/src/client/state/RoomsInput.js b/src/client/state/RoomsInput.js index 03dd0745..4277b2f0 100644 --- a/src/client/state/RoomsInput.js +++ b/src/client/state/RoomsInput.js @@ -274,7 +274,7 @@ class RoomsInput extends EventEmitter { return this.roomIdToInput.get(roomId)?.isSending || false; } - async sendInput(roomId) { + async sendInput(roomId, msgType) { const room = this.matrixClient.getRoom(roomId); const input = this.getInput(roomId); input.isSending = true; @@ -288,7 +288,7 @@ class RoomsInput extends EventEmitter { const rawMessage = input.message; let content = { body: rawMessage, - msgtype: 'm.text', + msgtype: msgType ?? 'm.text', }; // Apply formatting if relevant @@ -459,12 +459,14 @@ class RoomsInput extends EventEmitter { const room = this.matrixClient.getRoom(roomId); const isReply = typeof mEvent.getWireContent()['m.relates_to']?.['m.in_reply_to'] !== 'undefined'; + const msgtype = mEvent.getWireContent().msgtype ?? 'm.text'; + const content = { body: ` * ${editedBody}`, - msgtype: 'm.text', + msgtype, 'm.new_content': { body: editedBody, - msgtype: 'm.text', + msgtype, }, 'm.relates_to': { event_id: mEvent.getId(),