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

Commit 9017635

Browse files
committed
Support room IDs and event permalinks in the join command
Mostly useful for power users who want to jump to places more easily. Examples: * `/join !somewhere:example.org` * `/join !somewhere:example.org altserver.com` - to join through `altserver.com` * `/goto https://matrix.to/#/!somewhere:example.org/$something:example.org?via=altserver.com` Fixes element-hq/element-web#3248 Fixes element-hq/element-web#7543
1 parent aeef80f commit 9017635

File tree

1 file changed

+80
-5
lines changed

1 file changed

+80
-5
lines changed

src/SlashCommands.js

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import sdk from './index';
2424
import {_t, _td} from './languageHandler';
2525
import Modal from './Modal';
2626
import SettingsStore, {SettingLevel} from './settings/SettingsStore';
27+
import {MATRIXTO_URL_PATTERN} from "./linkify-matrix";
28+
import * as querystring from "querystring";
2729

2830

2931
class Command {
@@ -152,12 +154,26 @@ export const CommandMap = {
152154
args: '<room-alias>',
153155
description: _td('Joins room with given alias'),
154156
runFn: function(roomId, args) {
157+
console.log(args);
155158
if (args) {
156-
const matches = args.match(/^(\S+)$/);
157-
if (matches) {
158-
let roomAlias = matches[1];
159-
if (roomAlias[0] !== '#') return reject(this.getUsage());
160-
159+
// Note: we support 2 versions of this command. The first is
160+
// the public-facing one for most users and the other is a
161+
// power-user edition where someone may join via permalink or
162+
// room ID with optional servers. Practically, this results
163+
// in the following variations:
164+
// /join #example:example.org
165+
// /join !example:example.org
166+
// /join !example:example.org altserver.com elsewhere.ca
167+
// /join https://matrix.to/#/!example:example.org?via=altserver.com
168+
// The command also supports event permalinks transparently:
169+
// /join https://matrix.to/#/!example:example.org/$something:example.org
170+
// /join https://matrix.to/#/!example:example.org/$something:example.org?via=altserver.com
171+
const params = args.split(' ');
172+
if (params.length < 1) return reject(this.getUsage());
173+
174+
const matrixToMatches = params[0].match(MATRIXTO_URL_PATTERN);
175+
if (params[0][0] === '#') {
176+
let roomAlias = params[0];
161177
if (!roomAlias.includes(':')) {
162178
roomAlias += ':' + MatrixClientPeg.get().getDomain();
163179
}
@@ -167,7 +183,65 @@ export const CommandMap = {
167183
room_alias: roomAlias,
168184
auto_join: true,
169185
});
186+
return success();
187+
} else if(params[0][0] === '!') {
188+
let roomId = params[0];
189+
let viaServers = params.splice(0);
190+
191+
dis.dispatch({
192+
action: 'view_room',
193+
room_id: roomId,
194+
opts: {
195+
// These are passed down to the js-sdk's /join call
196+
server_name: viaServers,
197+
},
198+
auto_join: true,
199+
});
200+
return success();
201+
} else if(matrixToMatches) {
202+
let entity = matrixToMatches[1];
203+
let eventId = null;
204+
let viaServers = [];
205+
206+
if (entity[0] !== '!' && entity[0] !== '#') return reject(this.getUsage());
207+
208+
if (entity.indexOf('?') !== -1) {
209+
const parts = entity.split('?');
210+
entity = parts[0];
211+
212+
const parsed = querystring.parse(parts[1]);
213+
viaServers = parsed["via"];
214+
if (typeof viaServers === 'string') viaServers = [viaServers];
215+
}
216+
217+
// We quietly support event ID permalinks too
218+
if (entity.indexOf('/$') !== -1) {
219+
const parts = entity.split("/$");
220+
entity = parts[0];
221+
eventId = `$${parts[1]}`;
222+
}
223+
224+
const dispatch = {
225+
action: 'view_room',
226+
auto_join: true,
227+
};
228+
229+
if (entity[0] === '!') dispatch["room_id"] = entity;
230+
else dispatch["room_alias"] = entity;
231+
232+
if (eventId) {
233+
dispatch["event_id"] = eventId;
234+
dispatch["highlighted"] = true;
235+
}
236+
237+
if (viaServers) {
238+
dispatch["opts"] = {
239+
// These are passed down to the js-sdk's /join call
240+
server_name: viaServers,
241+
}
242+
}
170243

244+
dis.dispatch(dispatch);
171245
return success();
172246
}
173247
}
@@ -492,6 +566,7 @@ export const CommandMap = {
492566
const aliases = {
493567
j: "join",
494568
newballsplease: "discardsession",
569+
goto: "join", // because it handles event permalinks magically
495570
};
496571

497572

0 commit comments

Comments
 (0)