@@ -24,6 +24,8 @@ import sdk from './index';
24
24
import { _t , _td } from './languageHandler' ;
25
25
import Modal from './Modal' ;
26
26
import SettingsStore , { SettingLevel } from './settings/SettingsStore' ;
27
+ import { MATRIXTO_URL_PATTERN } from "./linkify-matrix" ;
28
+ import * as querystring from "querystring" ;
27
29
28
30
29
31
class Command {
@@ -152,12 +154,26 @@ export const CommandMap = {
152
154
args : '<room-alias>' ,
153
155
description : _td ( 'Joins room with given alias' ) ,
154
156
runFn : function ( roomId , args ) {
157
+ console . log ( args ) ;
155
158
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 ] ;
161
177
if ( ! roomAlias . includes ( ':' ) ) {
162
178
roomAlias += ':' + MatrixClientPeg . get ( ) . getDomain ( ) ;
163
179
}
@@ -167,7 +183,65 @@ export const CommandMap = {
167
183
room_alias : roomAlias ,
168
184
auto_join : true ,
169
185
} ) ;
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
+ }
170
243
244
+ dis . dispatch ( dispatch ) ;
171
245
return success ( ) ;
172
246
}
173
247
}
@@ -492,6 +566,7 @@ export const CommandMap = {
492
566
const aliases = {
493
567
j : "join" ,
494
568
newballsplease : "discardsession" ,
569
+ goto : "join" , // because it handles event permalinks magically
495
570
} ;
496
571
497
572
0 commit comments