Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 5636204

Browse files
committed
Support replying with a message command
Signed-off-by: Robin Townsend <[email protected]>
1 parent 667c94b commit 5636204

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

src/SlashCommands.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020

2121
import * as React from 'react';
2222

23+
import { ContentHelpers } from 'matrix-js-sdk';
2324
import {MatrixClientPeg} from './MatrixClientPeg';
2425
import dis from './dispatcher/dispatcher';
2526
import * as sdk from './index';
@@ -126,10 +127,10 @@ export class Command {
126127
return this.getCommand() + " " + this.args;
127128
}
128129

129-
run(roomId: string, args: string, cmd: string) {
130+
run(roomId: string, args: string) {
130131
// if it has no runFn then its an ignored/nop command (autocomplete only) e.g `/me`
131132
if (!this.runFn) return reject(_t("Command error"));
132-
return this.runFn.bind(this)(roomId, args, cmd);
133+
return this.runFn.bind(this)(roomId, args);
133134
}
134135

135136
getUsage() {
@@ -163,7 +164,7 @@ export const Commands = [
163164
if (args) {
164165
message = message + ' ' + args;
165166
}
166-
return success(MatrixClientPeg.get().sendTextMessage(roomId, message));
167+
return success(ContentHelpers.makeTextMessage(message));
167168
},
168169
category: CommandCategories.messages,
169170
}),
@@ -176,7 +177,7 @@ export const Commands = [
176177
if (args) {
177178
message = message + ' ' + args;
178179
}
179-
return success(MatrixClientPeg.get().sendTextMessage(roomId, message));
180+
return success(ContentHelpers.makeTextMessage(message));
180181
},
181182
category: CommandCategories.messages,
182183
}),
@@ -189,7 +190,7 @@ export const Commands = [
189190
if (args) {
190191
message = message + ' ' + args;
191192
}
192-
return success(MatrixClientPeg.get().sendTextMessage(roomId, message));
193+
return success(ContentHelpers.makeTextMessage(message));
193194
},
194195
category: CommandCategories.messages,
195196
}),
@@ -202,7 +203,7 @@ export const Commands = [
202203
if (args) {
203204
message = message + ' ' + args;
204205
}
205-
return success(MatrixClientPeg.get().sendTextMessage(roomId, message));
206+
return success(ContentHelpers.makeTextMessage(message));
206207
},
207208
category: CommandCategories.messages,
208209
}),
@@ -211,7 +212,7 @@ export const Commands = [
211212
args: '<message>',
212213
description: _td('Sends a message as plain text, without interpreting it as markdown'),
213214
runFn: function(roomId, messages) {
214-
return success(MatrixClientPeg.get().sendTextMessage(roomId, messages));
215+
return success(ContentHelpers.makeTextMessage(messages));
215216
},
216217
category: CommandCategories.messages,
217218
}),
@@ -220,7 +221,7 @@ export const Commands = [
220221
args: '<message>',
221222
description: _td('Sends a message as html, without interpreting it as markdown'),
222223
runFn: function(roomId, messages) {
223-
return success(MatrixClientPeg.get().sendHtmlMessage(roomId, messages, messages));
224+
return success(ContentHelpers.makeHtmlMessage(messages, messages));
224225
},
225226
category: CommandCategories.messages,
226227
}),
@@ -966,7 +967,7 @@ export const Commands = [
966967
args: '<message>',
967968
runFn: function(roomId, args) {
968969
if (!args) return reject(this.getUserId());
969-
return success(MatrixClientPeg.get().sendHtmlMessage(roomId, args, textToHtmlRainbow(args)));
970+
return success(ContentHelpers.makeHtmlMessage(args, textToHtmlRainbow(args)));
970971
},
971972
category: CommandCategories.messages,
972973
}),
@@ -976,7 +977,7 @@ export const Commands = [
976977
args: '<message>',
977978
runFn: function(roomId, args) {
978979
if (!args) return reject(this.getUserId());
979-
return success(MatrixClientPeg.get().sendHtmlEmote(roomId, args, textToHtmlRainbow(args)));
980+
return success(ContentHelpers.makeHtmlEmote(args, textToHtmlRainbow(args)));
980981
},
981982
category: CommandCategories.messages,
982983
}),
@@ -1201,10 +1202,13 @@ export function parseCommandString(input: string) {
12011202
* processing the command, or 'promise' if a request was sent out.
12021203
* Returns null if the input didn't match a command.
12031204
*/
1204-
export function getCommand(roomId: string, input: string) {
1205+
export function getCommand(input: string) {
12051206
const {cmd, args} = parseCommandString(input);
12061207

12071208
if (CommandMap.has(cmd) && CommandMap.get(cmd).isEnabled()) {
1208-
return () => CommandMap.get(cmd).run(roomId, args, cmd);
1209+
return {
1210+
cmd: CommandMap.get(cmd),
1211+
args,
1212+
};
12091213
}
12101214
}

src/components/views/rooms/SendMessageComposer.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import ReplyThread from "../elements/ReplyThread";
3333
import {parseEvent} from '../../../editor/deserialize';
3434
import {findEditableEvent} from '../../../utils/EventUtils';
3535
import SendHistoryManager from "../../../SendHistoryManager";
36-
import {getCommand} from '../../../SlashCommands';
36+
import {CommandCategories, getCommand} from '../../../SlashCommands';
3737
import * as sdk from '../../../index';
3838
import Modal from '../../../Modal';
3939
import {_t, _td} from '../../../languageHandler';
@@ -287,15 +287,22 @@ export default class SendMessageComposer extends React.Component {
287287
}
288288
return text + part.text;
289289
}, "");
290-
return [getCommand(this.props.room.roomId, commandText), commandText];
290+
const {cmd, args} = getCommand(commandText);
291+
return [cmd, args, commandText];
291292
}
292293

293-
async _runSlashCommand(fn) {
294-
const cmd = fn();
295-
let error = cmd.error;
296-
if (cmd.promise) {
294+
async _runSlashCommand(cmd, args) {
295+
const result = cmd.run(this.props.room.roomId, args);
296+
let messageContent;
297+
let error = result.error;
298+
if (result.promise) {
297299
try {
298-
await cmd.promise;
300+
if (cmd.category === CommandCategories.messages) {
301+
// The command returns a modified message that we need to pass on
302+
messageContent = await result.promise;
303+
} else {
304+
await result.promise;
305+
}
299306
} catch (err) {
300307
error = err;
301308
}
@@ -304,7 +311,7 @@ export default class SendMessageComposer extends React.Component {
304311
console.error("Command failure: %s", error);
305312
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
306313
// assume the error is a server error when the command is async
307-
const isServerError = !!cmd.promise;
314+
const isServerError = !!result.promise;
308315
const title = isServerError ? _td("Server error") : _td("Command error");
309316

310317
let errText;
@@ -322,6 +329,7 @@ export default class SendMessageComposer extends React.Component {
322329
});
323330
} else {
324331
console.log("Command success.");
332+
if (messageContent) return messageContent;
325333
}
326334
}
327335

@@ -330,13 +338,22 @@ export default class SendMessageComposer extends React.Component {
330338
return;
331339
}
332340

341+
const replyToEvent = this.props.replyToEvent;
333342
let shouldSend = true;
343+
let content;
334344

335345
if (!containsEmote(this.model) && this._isSlashCommand()) {
336-
const [cmd, commandText] = this._getSlashCommand();
346+
const [cmd, args, commandText] = this._getSlashCommand();
337347
if (cmd) {
338-
shouldSend = false;
339-
this._runSlashCommand(cmd);
348+
if (cmd.category === CommandCategories.messages) {
349+
content = await this._runSlashCommand(cmd, args);
350+
if (replyToEvent) {
351+
addReplyToMessageContent(content, replyToEvent, this.props.permalinkCreator);
352+
}
353+
} else {
354+
this._runSlashCommand(cmd, args);
355+
shouldSend = false;
356+
}
340357
} else {
341358
// ask the user if their unknown command should be sent as a message
342359
const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
@@ -371,11 +388,12 @@ export default class SendMessageComposer extends React.Component {
371388
this._sendQuickReaction();
372389
}
373390

374-
const replyToEvent = this.props.replyToEvent;
375391
if (shouldSend) {
376392
const startTime = CountlyAnalytics.getTimestamp();
377393
const {roomId} = this.props.room;
378-
const content = createMessageContent(this.model, this.props.permalinkCreator, replyToEvent);
394+
if (!content) {
395+
content = createMessageContent(this.model, this.props.permalinkCreator, replyToEvent);
396+
}
379397
// don't bother sending an empty message
380398
if (!content.body.trim()) return;
381399

0 commit comments

Comments
 (0)