Extend slash command '/topic' to display the room topic

If no <topic> is provided, the command will display a modal dialog containing
the sanitized and linkified room topic. This is just adding some juice to make
reading long room topics more convenient.
This commit is contained in:
Bastian 2019-01-30 11:22:05 +01:00
parent c1860575c5
commit bc6d13e768

View file

@ -27,7 +27,12 @@ import SettingsStore, {SettingLevel} from './settings/SettingsStore';
import {MATRIXTO_URL_PATTERN} from "./linkify-matrix"; import {MATRIXTO_URL_PATTERN} from "./linkify-matrix";
import * as querystring from "querystring"; import * as querystring from "querystring";
import MultiInviter from './utils/MultiInviter'; import MultiInviter from './utils/MultiInviter';
import * as linkify from 'linkifyjs';
import linkifyString from 'linkifyjs/string';
import linkifyMatrix from './linkify-matrix';
import sanitizeHtml from 'sanitize-html';
linkifyMatrix(linkify);
class Command { class Command {
constructor({name, args='', description, runFn, hideCompletionAfterSpace=false}) { constructor({name, args='', description, runFn, hideCompletionAfterSpace=false}) {
@ -125,13 +130,27 @@ export const CommandMap = {
topic: new Command({ topic: new Command({
name: 'topic', name: 'topic',
args: '<topic>', args: '[<topic>]',
description: _td('Sets the room topic'), description: _td('Gets or sets the room topic'),
runFn: function(roomId, args) { runFn: function(roomId, args) {
const cli = MatrixClientPeg.get();
if (args) { if (args) {
return success(MatrixClientPeg.get().setRoomTopic(roomId, args)); return success(cli.setRoomTopic(roomId, args));
} }
return reject(this.getUsage()); const room = cli.getRoom(roomId);
if (!room) return reject('Bad room ID: ' + roomId);
const topicEvents = room.currentState.getStateEvents('m.room.topic', '');
const topic = topicEvents.getContent().topic;
const topicHtml = linkifyString(sanitizeHtml(topic));
const QuestionDialog = sdk.getComponent('dialogs.QuestionDialog');
Modal.createTrackedDialog('Slash Commands', 'Topic', QuestionDialog, {
title: room.name,
description: <div dangerouslySetInnerHTML={{ __html: topicHtml }} />,
hasCancelButton: false,
});
return success();
}, },
}), }),