@@ -52,23 +52,41 @@ module.exports = React.createClass({
5252 return {
5353 publicRooms : [ ] ,
5454 loading : true ,
55- filterByNetwork : null ,
55+ network : null ,
5656 roomServer : null ,
57+ filterString : null ,
5758 }
5859 } ,
5960
6061 componentWillMount : function ( ) {
6162 // precompile Regexps
62- this . networkPatterns = { } ;
63- if ( this . props . config . networkPatterns ) {
64- for ( const network of Object . keys ( this . props . config . networkPatterns ) ) {
65- this . networkPatterns [ network ] = new RegExp ( this . props . config . networkPatterns [ network ] ) ;
63+ this . portalRoomPatterns = { } ;
64+ this . nativePatterns = { } ;
65+ if ( this . props . config . networks ) {
66+ for ( const network of Object . keys ( this . props . config . networks ) ) {
67+ if ( this . props . config . networks [ network ] . portalRoomPattern ) {
68+ this . portalRoomPatterns [ network ] = new RegExp ( this . props . config . networks [ network ] . portalRoomPattern ) ;
69+ }
70+ if ( this . props . config . networks [ network ] . nativePattern ) {
71+ this . nativePatterns [ network ] = new RegExp ( this . props . config . networks [ network ] . nativePattern ) ;
72+ }
6673 }
6774 }
75+
6876 this . nextBatch = null ;
69- this . filterString = null ;
7077 this . filterTimeout = null ;
7178 this . scrollPanel = null ;
79+ this . protocols = null ;
80+
81+ MatrixClientPeg . get ( ) . getThirdpartyProtocols ( ) . done ( ( response ) => {
82+ this . protocols = response ;
83+ } , ( err ) => {
84+ const ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
85+ Modal . createDialog ( ErrorDialog , {
86+ title : "Failed to get protocol list from Home Server" ,
87+ description : "The Home Server may be too old to support third party networks" ,
88+ } ) ;
89+ } ) ;
7290
7391 // dis.dispatch({
7492 // action: 'ui_opacity',
@@ -97,7 +115,7 @@ module.exports = React.createClass({
97115 getMoreRooms : function ( ) {
98116 if ( ! MatrixClientPeg . get ( ) ) return q ( ) ;
99117
100- const my_filter_string = this . filterString ;
118+ const my_filter_string = this . state . filterString ;
101119 const my_server = this . state . roomServer ;
102120 // remember the next batch token when we sent the request
103121 // too. If it's changed, appending to the list will corrupt it.
@@ -107,10 +125,10 @@ module.exports = React.createClass({
107125 opts . server = my_server ;
108126 }
109127 if ( this . nextBatch ) opts . since = this . nextBatch ;
110- if ( this . filterString ) opts . filter = { generic_search_term : my_filter_string } ;
128+ if ( this . state . filterString ) opts . filter = { generic_search_term : my_filter_string } ;
111129 return MatrixClientPeg . get ( ) . publicRooms ( opts ) . then ( ( data ) => {
112130 if (
113- my_filter_string != this . filterString ||
131+ my_filter_string != this . state . filterString ||
114132 my_server != this . state . roomServer ||
115133 my_next_batch != this . nextBatch )
116134 {
@@ -129,7 +147,7 @@ module.exports = React.createClass({
129147 return Boolean ( data . next_batch ) ;
130148 } , ( err ) => {
131149 if (
132- my_filter_string != this . filterString ||
150+ my_filter_string != this . state . filterString ||
133151 my_server != this . state . roomServer ||
134152 my_next_batch != this . nextBatch )
135153 {
@@ -215,7 +233,7 @@ module.exports = React.createClass({
215233 // to clear the list anyway.
216234 publicRooms : [ ] ,
217235 roomServer : server ,
218- filterByNetwork : network ,
236+ network : network ,
219237 } , this . refreshRoomList ) ;
220238 // We also refresh the room list each time even though this
221239 // filtering is client-side. It hopefully won't be client side
@@ -232,7 +250,9 @@ module.exports = React.createClass({
232250 } ,
233251
234252 onFilterChange : function ( alias ) {
235- this . filterString = alias || null ;
253+ this . setState ( {
254+ filterString : alias || null ,
255+ } ) ;
236256
237257 // don't send the request for a little bit,
238258 // no point hammering the server with a
@@ -248,17 +268,52 @@ module.exports = React.createClass({
248268 } ,
249269
250270 onFilterClear : function ( ) {
251- this . filterString = null ;
271+ // update immediately
272+ this . setState ( {
273+ filterString : null ,
274+ } , this . refreshRoomList ) ;
252275
253276 if ( this . filterTimeout ) {
254277 clearTimeout ( this . filterTimeout ) ;
255278 }
256- // update immediately
257- this . refreshRoomList ( ) ;
258279 } ,
259280
260281 onJoinClick : function ( alias ) {
261- this . showRoomAlias ( alias ) ;
282+ // If we're on the 'Matrix' network (or all networks),
283+ // just show that rooms alias
284+ if ( this . state . network == null || this . state . network == '_matrix' ) {
285+ this . showRoomAlias ( alias ) ;
286+ } else {
287+ // This is a 3rd party protocol. Let's see if we
288+ // can join it
289+ const fields = this . _getFieldsForThirdPartyLocation ( alias , this . state . network ) ;
290+ if ( ! fields ) {
291+ const ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
292+ Modal . createDialog ( ErrorDialog , {
293+ title : "Unable to join network" ,
294+ description : "Riot does not know how to join a room on this network" ,
295+ } ) ;
296+ return ;
297+ }
298+ const protocol = this . _protocolForThirdPartyNetwork ( this . state . network ) ;
299+ MatrixClientPeg . get ( ) . getThirdpartyLocation ( protocol , fields ) . done ( ( resp ) => {
300+ if ( resp . length > 0 && resp [ 0 ] . alias ) {
301+ this . showRoomAlias ( resp [ 0 ] . alias ) ;
302+ } else {
303+ const ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
304+ Modal . createDialog ( ErrorDialog , {
305+ title : "Room not found" ,
306+ description : "Couldn't find a matching Matrix room" ,
307+ } ) ;
308+ }
309+ } , ( e ) => {
310+ const ErrorDialog = sdk . getComponent ( "dialogs.ErrorDialog" ) ;
311+ Modal . createDialog ( ErrorDialog , {
312+ title : "Fetching third party location failed" ,
313+ description : "Unable to look up room ID from server" ,
314+ } ) ;
315+ } ) ;
316+ }
262317 } ,
263318
264319 showRoomAlias : function ( alias ) {
@@ -311,8 +366,8 @@ module.exports = React.createClass({
311366 if ( ! this . state . publicRooms ) return [ ] ;
312367
313368 var rooms = this . state . publicRooms . filter ( ( a ) => {
314- if ( this . state . filterByNetwork ) {
315- if ( ! this . _isRoomInNetwork ( a , this . state . roomServer , this . state . filterByNetwork ) ) return false ;
369+ if ( this . state . network ) {
370+ if ( ! this . _isRoomInNetwork ( a , this . state . roomServer , this . state . network ) ) return false ;
316371 }
317372
318373 return true ;
@@ -382,7 +437,7 @@ module.exports = React.createClass({
382437 * Terrible temporary function that guess what network a public room
383438 * entry is in, until synapse is able to tell us
384439 */
385- _isRoomInNetwork ( room , server , network ) {
440+ _isRoomInNetwork : function ( room , server , network ) {
386441 // We carve rooms into two categories here. 'portal' rooms are
387442 // rooms created by a user joining a bridge 'portal' alias to
388443 // participate in that room or a foreign network. A room is a
@@ -396,7 +451,7 @@ module.exports = React.createClass({
396451 if ( room . aliases && room . aliases . length == 1 ) {
397452 if ( this . props . config . serverConfig && this . props . config . serverConfig [ server ] && this . props . config . serverConfig [ server ] . networks ) {
398453 for ( const n of this . props . config . serverConfig [ server ] . networks ) {
399- const pat = this . networkPatterns [ n ] ;
454+ const pat = this . portalRoomPatterns [ n ] ;
400455 if ( pat && pat . test ( room . aliases [ 0 ] ) ) {
401456 roomNetwork = n ;
402457 }
@@ -406,6 +461,59 @@ module.exports = React.createClass({
406461 return roomNetwork == network ;
407462 } ,
408463
464+ _stringLooksLikeId : function ( s , network ) {
465+ let pat = / ^ # [ ^ \s ] + : [ ^ \s ] / ;
466+ if (
467+ network && network != '_matrix' &&
468+ this . nativePatterns [ network ]
469+ ) {
470+ pat = this . nativePatterns [ network ] ;
471+ }
472+
473+ return pat . test ( s ) ;
474+ } ,
475+
476+ _protocolForThirdPartyNetwork : function ( network ) {
477+ if (
478+ this . props . config . networks &&
479+ this . props . config . networks [ network ] &&
480+ this . props . config . networks [ network ] . protocol
481+ ) {
482+ return this . props . config . networks [ network ] . protocol ;
483+ }
484+ } ,
485+
486+ _getFieldsForThirdPartyLocation : function ( user_input , network ) {
487+ const getfields_funcs = {
488+ irc : ( user_input , network ) => {
489+ if ( ! this . protocols . irc ) return ;
490+ const domain = this . props . config . networks [ network ] . domain ;
491+ // search through to make sure this is a domain actually
492+ // recognised by the HS
493+ for ( const inst of this . protocols . irc . instances ) {
494+ if ( inst . fields && inst . fields . domain == domain ) {
495+ return {
496+ domain : domain ,
497+ channel : user_input ,
498+ }
499+ }
500+ }
501+ return null ;
502+ } ,
503+ gitter : ( user_input , network ) => {
504+ if ( ! this . protocols . gitter ) return ;
505+ return {
506+ room : user_input ,
507+ }
508+ } ,
509+ } ;
510+
511+ const protocol = this . _protocolForThirdPartyNetwork ( network ) ;
512+ if ( getfields_funcs [ protocol ] ) {
513+ return getfields_funcs [ protocol ] ( user_input , network ) ;
514+ }
515+ } ,
516+
409517 render : function ( ) {
410518 let content ;
411519 if ( this . state . loading ) {
@@ -430,6 +538,23 @@ module.exports = React.createClass({
430538 </ ScrollPanel > ;
431539 }
432540
541+ let placeholder = 'Search for a room' ;
542+ if ( this . state . network === null || this . state . network === '_matrix' ) {
543+ placeholder = '#example:' + this . state . roomServer ;
544+ } else if (
545+ this . props . config . networks &&
546+ this . props . config . networks [ this . state . network ] &&
547+ this . props . config . networks [ this . state . network ] . example &&
548+ this . _getFieldsForThirdPartyLocation ( this . state . filterString , this . state . network )
549+ ) {
550+ placeholder = this . props . config . networks [ this . state . network ] . example ;
551+ }
552+
553+ const showJoinButton = (
554+ this . _stringLooksLikeId ( this . state . filterString , this . state . network ) &&
555+ this . _getFieldsForThirdPartyLocation ( this . state . filterString , this . state . network )
556+ ) ;
557+
433558 const SimpleRoomHeader = sdk . getComponent ( 'rooms.SimpleRoomHeader' ) ;
434559 const NetworkDropdown = sdk . getComponent ( 'directory.NetworkDropdown' ) ;
435560 const DirectorySearchBox = sdk . getComponent ( 'elements.DirectorySearchBox' ) ;
@@ -441,6 +566,7 @@ module.exports = React.createClass({
441566 < DirectorySearchBox
442567 className = "mx_RoomDirectory_searchbox"
443568 onChange = { this . onFilterChange } onClear = { this . onFilterClear } onJoinClick = { this . onJoinClick }
569+ placeholder = { placeholder } showJoinButton = { showJoinButton }
444570 />
445571 < NetworkDropdown config = { this . props . config } onOptionChange = { this . onOptionChange } />
446572 </ div >
0 commit comments