@@ -48,6 +48,8 @@ import SettingsStore, {SettingLevel} from "../../settings/SettingsStore";
48
48
import { startAnyRegistrationFlow } from "../../Registration.js" ;
49
49
import { messageForSyncError } from '../../utils/ErrorUtils' ;
50
50
51
+ const AutoDiscovery = Matrix . AutoDiscovery ;
52
+
51
53
// Disable warnings for now: we use deprecated bluebird functions
52
54
// and need to migrate, but they spam the console with warnings.
53
55
Promise . config ( { warnings : false } ) ;
@@ -181,6 +183,12 @@ export default React.createClass({
181
183
register_is_url : null ,
182
184
register_id_sid : null ,
183
185
186
+ // Parameters used for setting up the login/registration views
187
+ defaultServerName : this . props . config . default_server_name ,
188
+ defaultHsUrl : this . props . config . default_hs_url ,
189
+ defaultIsUrl : this . props . config . default_is_url ,
190
+ defaultServerDiscoveryError : null ,
191
+
184
192
// When showing Modal dialogs we need to set aria-hidden on the root app element
185
193
// and disable it when there are no dialogs
186
194
hideToSRUsers : false ,
@@ -199,6 +207,10 @@ export default React.createClass({
199
207
} ;
200
208
} ,
201
209
210
+ getDefaultServerName : function ( ) {
211
+ return this . state . defaultServerName ;
212
+ } ,
213
+
202
214
getCurrentHsUrl : function ( ) {
203
215
if ( this . state . register_hs_url ) {
204
216
return this . state . register_hs_url ;
@@ -209,8 +221,10 @@ export default React.createClass({
209
221
}
210
222
} ,
211
223
212
- getDefaultHsUrl ( ) {
213
- return this . props . config . default_hs_url || "https://matrix.org" ;
224
+ getDefaultHsUrl ( defaultToMatrixDotOrg ) {
225
+ defaultToMatrixDotOrg = typeof ( defaultToMatrixDotOrg ) !== 'boolean' ? true : defaultToMatrixDotOrg ;
226
+ if ( ! this . state . defaultHsUrl && defaultToMatrixDotOrg ) return "https://matrix.org" ;
227
+ return this . state . defaultHsUrl ;
214
228
} ,
215
229
216
230
getFallbackHsUrl : function ( ) {
@@ -228,7 +242,7 @@ export default React.createClass({
228
242
} ,
229
243
230
244
getDefaultIsUrl ( ) {
231
- return this . props . config . default_is_url || "https://vector.im" ;
245
+ return this . state . defaultIsUrl || "https://vector.im" ;
232
246
} ,
233
247
234
248
componentWillMount : function ( ) {
@@ -278,6 +292,20 @@ export default React.createClass({
278
292
console . info ( `Team token set to ${ this . _teamToken } ` ) ;
279
293
}
280
294
295
+ // Set up the default URLs (async)
296
+ if ( this . getDefaultServerName ( ) && ! this . getDefaultHsUrl ( false ) ) {
297
+ this . setState ( { loadingDefaultHomeserver : true } ) ;
298
+ this . _tryDiscoverDefaultHomeserver ( this . getDefaultServerName ( ) ) ;
299
+ } else if ( this . getDefaultServerName ( ) && this . getDefaultHsUrl ( false ) ) {
300
+ // Ideally we would somehow only communicate this to the server admins, but
301
+ // given this is at login time we can't really do much besides hope that people
302
+ // will check their settings.
303
+ this . setState ( {
304
+ defaultServerName : null , // To un-hide any secrets people might be keeping
305
+ defaultServerDiscoveryError : _t ( "Invalid configuration: Cannot supply a default homeserver URL and a default server name" ) ,
306
+ } ) ;
307
+ }
308
+
281
309
// Set a default HS with query param `hs_url`
282
310
const paramHs = this . props . startingFragmentQueryParams . hs_url ;
283
311
if ( paramHs ) {
@@ -1728,6 +1756,36 @@ export default React.createClass({
1728
1756
this . setState ( newState ) ;
1729
1757
} ,
1730
1758
1759
+ _tryDiscoverDefaultHomeserver : async function ( serverName ) {
1760
+ try {
1761
+ const discovery = await AutoDiscovery . findClientConfig ( serverName ) ;
1762
+ const state = discovery [ "m.homeserver" ] . state ;
1763
+ if ( state !== AutoDiscovery . SUCCESS ) {
1764
+ console . error ( "Failed to discover homeserver on startup:" , discovery ) ;
1765
+ this . setState ( {
1766
+ defaultServerDiscoveryError : discovery [ "m.homeserver" ] . error ,
1767
+ loadingDefaultHomeserver : false ,
1768
+ } ) ;
1769
+ } else {
1770
+ const hsUrl = discovery [ "m.homeserver" ] . base_url ;
1771
+ const isUrl = discovery [ "m.identity_server" ] . state === AutoDiscovery . SUCCESS
1772
+ ? discovery [ "m.identity_server" ] . base_url
1773
+ : "https://vector.im" ;
1774
+ this . setState ( {
1775
+ defaultHsUrl : hsUrl ,
1776
+ defaultIsUrl : isUrl ,
1777
+ loadingDefaultHomeserver : false ,
1778
+ } ) ;
1779
+ }
1780
+ } catch ( e ) {
1781
+ console . error ( e ) ;
1782
+ this . setState ( {
1783
+ defaultServerDiscoveryError : _t ( "Unknown error discovering homeserver" ) ,
1784
+ loadingDefaultHomeserver : false ,
1785
+ } ) ;
1786
+ }
1787
+ } ,
1788
+
1731
1789
_makeRegistrationUrl : function ( params ) {
1732
1790
if ( this . props . startingFragmentQueryParams . referrer ) {
1733
1791
params . referrer = this . props . startingFragmentQueryParams . referrer ;
@@ -1742,7 +1800,7 @@ export default React.createClass({
1742
1800
render : function ( ) {
1743
1801
// console.log(`Rendering MatrixChat with view ${this.state.view}`);
1744
1802
1745
- if ( this . state . view === VIEWS . LOADING || this . state . view === VIEWS . LOGGING_IN ) {
1803
+ if ( this . state . view === VIEWS . LOADING || this . state . view === VIEWS . LOGGING_IN || this . state . loadingDefaultHomeserver ) {
1746
1804
const Spinner = sdk . getComponent ( 'elements.Spinner' ) ;
1747
1805
return (
1748
1806
< div className = "mx_MatrixChat_splash" >
@@ -1816,6 +1874,8 @@ export default React.createClass({
1816
1874
idSid = { this . state . register_id_sid }
1817
1875
email = { this . props . startingFragmentQueryParams . email }
1818
1876
referrer = { this . props . startingFragmentQueryParams . referrer }
1877
+ defaultServerName = { this . getDefaultServerName ( ) }
1878
+ defaultServerDiscoveryError = { this . state . defaultServerDiscoveryError }
1819
1879
defaultHsUrl = { this . getDefaultHsUrl ( ) }
1820
1880
defaultIsUrl = { this . getDefaultIsUrl ( ) }
1821
1881
brand = { this . props . config . brand }
@@ -1838,6 +1898,8 @@ export default React.createClass({
1838
1898
const ForgotPassword = sdk . getComponent ( 'structures.login.ForgotPassword' ) ;
1839
1899
return (
1840
1900
< ForgotPassword
1901
+ defaultServerName = { this . getDefaultServerName ( ) }
1902
+ defaultServerDiscoveryError = { this . state . defaultServerDiscoveryError }
1841
1903
defaultHsUrl = { this . getDefaultHsUrl ( ) }
1842
1904
defaultIsUrl = { this . getDefaultIsUrl ( ) }
1843
1905
customHsUrl = { this . getCurrentHsUrl ( ) }
@@ -1854,6 +1916,8 @@ export default React.createClass({
1854
1916
< Login
1855
1917
onLoggedIn = { Lifecycle . setLoggedIn }
1856
1918
onRegisterClick = { this . onRegisterClick }
1919
+ defaultServerName = { this . getDefaultServerName ( ) }
1920
+ defaultServerDiscoveryError = { this . state . defaultServerDiscoveryError }
1857
1921
defaultHsUrl = { this . getDefaultHsUrl ( ) }
1858
1922
defaultIsUrl = { this . getDefaultIsUrl ( ) }
1859
1923
customHsUrl = { this . getCurrentHsUrl ( ) }
0 commit comments