From bc6d13e768c3a6d69c91ac31d8b4b45444aed942 Mon Sep 17 00:00:00 2001 From: Bastian Date: Wed, 30 Jan 2019 11:22:05 +0100 Subject: [PATCH] Extend slash command '/topic' to display the room topic If no 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. --- src/SlashCommands.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/SlashCommands.js b/src/SlashCommands.js index 24328d6372..5b5a53b83f 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -27,7 +27,12 @@ import SettingsStore, {SettingLevel} from './settings/SettingsStore'; import {MATRIXTO_URL_PATTERN} from "./linkify-matrix"; import * as querystring from "querystring"; 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 { constructor({name, args='', description, runFn, hideCompletionAfterSpace=false}) { @@ -125,13 +130,27 @@ export const CommandMap = { topic: new Command({ name: 'topic', - args: '', - description: _td('Sets the room topic'), + args: '[]', + description: _td('Gets or sets the room topic'), runFn: function(roomId, args) { + const cli = MatrixClientPeg.get(); 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:
, + hasCancelButton: false, + }); + return success(); }, }),