Add /me & /shrug commands

This commit is contained in:
Ajay Bura 2022-08-28 15:49:54 +05:30
parent 77f976392f
commit 685154889c
3 changed files with 44 additions and 33 deletions

View file

@ -21,6 +21,7 @@ import ScrollView from '../../atoms/scroll/ScrollView';
import { MessageReply } from '../../molecules/message/Message'; import { MessageReply } from '../../molecules/message/Message';
import StickerBoard from '../sticker-board/StickerBoard'; import StickerBoard from '../sticker-board/StickerBoard';
import { confirmDialog } from '../../molecules/confirm-dialog/ConfirmDialog';
import CirclePlusIC from '../../../../public/res/ic/outlined/circle-plus.svg'; import CirclePlusIC from '../../../../public/res/ic/outlined/circle-plus.svg';
import EmojiIC from '../../../../public/res/ic/outlined/emoji.svg'; import EmojiIC from '../../../../public/res/ic/outlined/emoji.svg';
@ -184,38 +185,17 @@ function RoomViewInput({
}; };
}, [roomId]); }, [roomId]);
const processCommand = (cmdBody) => { const sendBody = async (body, msgType = 'm.text') => {
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;
}
if (roomsInput.isSending(roomId)) return; if (roomsInput.isSending(roomId)) return;
if (msgBody.trim() === '' && attachment === null) return;
sendIsTyping(false); sendIsTyping(false);
roomsInput.setMessage(roomId, msgBody); roomsInput.setMessage(roomId, body);
if (attachment !== null) { if (attachment !== null) {
roomsInput.setAttachment(roomId, attachment); roomsInput.setAttachment(roomId, attachment);
} }
textAreaRef.current.disabled = true; textAreaRef.current.disabled = true;
textAreaRef.current.style.cursor = 'not-allowed'; textAreaRef.current.style.cursor = 'not-allowed';
await roomsInput.sendInput(roomId); await roomsInput.sendInput(roomId, msgType);
textAreaRef.current.disabled = false; textAreaRef.current.disabled = false;
textAreaRef.current.style.cursor = 'unset'; textAreaRef.current.style.cursor = 'unset';
focusInput(); focusInput();
@ -225,6 +205,34 @@ function RoomViewInput({
if (replyTo !== null) setReplyTo(null); 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) => { const handleSendSticker = async (data) => {
roomsInput.sendSticker(roomId, data); roomsInput.sendSticker(roomId, data);
}; };

View file

@ -13,17 +13,18 @@ const commands = {
me: { me: {
name: 'me', name: 'me',
description: 'Display action', description: 'Display action',
exe: (roomId, data) => { exe: (roomId, data, onSuccess) => {
const body = data.trim(); const body = data.trim();
if (body === '') return undefined; if (body === '') return;
return body; onSuccess(body, 'm.emote');
}, },
}, },
shrug: { shrug: {
name: 'shrug', name: 'shrug',
description: 'Send ¯\\_(ツ)_/¯ as message', description: 'Send ¯\\_(ツ)_/¯ as message',
exe: (roomId, data) => ( exe: (roomId, data, onSuccess) => onSuccess(
`¯\\_(ツ)_/¯${data.trim() !== '' ? ` ${data}` : ''}` `¯\\_(ツ)_/¯${data.trim() !== '' ? ` ${data}` : ''}`,
'm.text',
), ),
}, },
markdown: { markdown: {

View file

@ -274,7 +274,7 @@ class RoomsInput extends EventEmitter {
return this.roomIdToInput.get(roomId)?.isSending || false; return this.roomIdToInput.get(roomId)?.isSending || false;
} }
async sendInput(roomId) { async sendInput(roomId, msgType) {
const room = this.matrixClient.getRoom(roomId); const room = this.matrixClient.getRoom(roomId);
const input = this.getInput(roomId); const input = this.getInput(roomId);
input.isSending = true; input.isSending = true;
@ -288,7 +288,7 @@ class RoomsInput extends EventEmitter {
const rawMessage = input.message; const rawMessage = input.message;
let content = { let content = {
body: rawMessage, body: rawMessage,
msgtype: 'm.text', msgtype: msgType ?? 'm.text',
}; };
// Apply formatting if relevant // Apply formatting if relevant
@ -459,12 +459,14 @@ class RoomsInput extends EventEmitter {
const room = this.matrixClient.getRoom(roomId); const room = this.matrixClient.getRoom(roomId);
const isReply = typeof mEvent.getWireContent()['m.relates_to']?.['m.in_reply_to'] !== 'undefined'; const isReply = typeof mEvent.getWireContent()['m.relates_to']?.['m.in_reply_to'] !== 'undefined';
const msgtype = mEvent.getWireContent().msgtype ?? 'm.text';
const content = { const content = {
body: ` * ${editedBody}`, body: ` * ${editedBody}`,
msgtype: 'm.text', msgtype,
'm.new_content': { 'm.new_content': {
body: editedBody, body: editedBody,
msgtype: 'm.text', msgtype,
}, },
'm.relates_to': { 'm.relates_to': {
event_id: mEvent.getId(), event_id: mEvent.getId(),