@@ -485,34 +485,49 @@ module.exports = React.createClass({
485485 } ,
486486
487487 _getFieldsForThirdPartyLocation : function ( user_input , network ) {
488- const getfields_funcs = {
489- irc : ( user_input , network ) => {
490- if ( ! this . protocols . irc ) return ;
491- const domain = this . props . config . networks [ network ] . domain ;
492- // search through to make sure this is a domain actually
493- // recognised by the HS
494- for ( const inst of this . protocols . irc . instances ) {
495- if ( inst . fields && inst . fields . domain == domain ) {
496- return {
497- domain : domain ,
498- channel : user_input ,
499- }
500- }
501- }
502- return null ;
503- } ,
504- gitter : ( user_input , network ) => {
505- if ( ! this . protocols . gitter ) return ;
506- return {
507- room : user_input ,
488+ if ( ! this . props . config . networks || ! this . props . config . networks [ network ] ) return null ;
489+
490+ const network_info = this . props . config . networks [ network ] ;
491+ if ( ! network_info . protocol ) return null ;
492+
493+ if ( ! this . protocols ) return null ;
494+
495+ let instance ;
496+ // Try to find which instance in the 'protocols' response
497+ // matches this network. We look for a matching protocol
498+ // and the existence of a 'domain' field and if present,
499+ // its value.
500+ if ( this . protocols [ network_info . protocol ] . instances . length == 1 ) {
501+ const the_instance = this . protocols [ network_info . protocol ] . instances [ 0 ] ;
502+ // If there's only one instance in this protocol, use it
503+ // as long as it has no domain (which we assume to mean it's
504+ // there is only one possible instance).
505+ if ( the_instance . fields . domain === undefined && network_info . domain === undefined ) {
506+ instance = this . protocols [ network_info . protocol ] . instances [ 0 ] ;
507+ }
508+ } else if ( network_info . domain ) {
509+ // otherwise, we look for one with a matching domain.
510+ for ( const this_instance of this . protocols [ network_info . protocol ] . instances ) {
511+ if ( this_instance . fields . domain == network_info . domain ) {
512+ instance = this_instance ;
508513 }
509- } ,
510- } ;
514+ }
515+ }
511516
512- const protocol = this . _protocolForThirdPartyNetwork ( network ) ;
513- if ( getfields_funcs [ protocol ] ) {
514- return getfields_funcs [ protocol ] ( user_input , network ) ;
517+ if ( instance === undefined ) return null ;
518+
519+ // now make an object with the fields specified by that protocol. We
520+ // require that the values of all but the last field come from the
521+ // instance. The last is the user input.
522+ const required_fields = this . protocols [ network_info . protocol ] . location_fields ;
523+ const fields = { } ;
524+ for ( let i = 0 ; i < required_fields . length - 1 ; ++ i ) {
525+ const this_field = required_fields [ i ] ;
526+ if ( instance . fields [ this_field ] === undefined ) return null ;
527+ fields [ this_field ] = instance . fields [ this_field ] ;
515528 }
529+ fields [ required_fields [ required_fields . length - 1 ] ] = user_input ;
530+ return fields ;
516531 } ,
517532
518533 render : function ( ) {
0 commit comments