2015-11-30 21:11:04 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-01-25 00:07:49 +03:00
|
|
|
Copyright 2019 New Vector Ltd
|
2015-11-30 21:11:04 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-01-29 06:44:41 +03:00
|
|
|
import React from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2019-01-29 06:44:41 +03:00
|
|
|
import Modal from '../../../Modal';
|
|
|
|
import sdk from '../../../index';
|
2017-05-30 17:09:57 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2019-05-03 07:57:49 +03:00
|
|
|
import {ValidatedServerConfig} from "../../../utils/AutoDiscoveryUtils";
|
|
|
|
import AutoDiscoveryUtils from "../../../utils/AutoDiscoveryUtils";
|
|
|
|
import SdkConfig from "../../../SdkConfig";
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2019-01-29 06:44:41 +03:00
|
|
|
/*
|
2015-11-30 21:11:04 +03:00
|
|
|
* A pure UI component which displays the HS and IS to use.
|
|
|
|
*/
|
|
|
|
|
2019-01-29 06:44:41 +03:00
|
|
|
export default class ServerConfig extends React.PureComponent {
|
|
|
|
static propTypes = {
|
2019-05-14 01:30:34 +03:00
|
|
|
onServerConfigChange: PropTypes.func.isRequired,
|
2016-03-15 16:48:46 +03:00
|
|
|
|
2019-05-03 07:57:49 +03:00
|
|
|
// The current configuration that the user is expecting to change.
|
|
|
|
serverConfig: PropTypes.instanceOf(ValidatedServerConfig).isRequired,
|
2016-03-15 16:48:46 +03:00
|
|
|
|
2017-12-26 04:03:18 +03:00
|
|
|
delayTimeMs: PropTypes.number, // time to wait before invoking onChanged
|
2019-05-14 01:30:34 +03:00
|
|
|
|
|
|
|
// Called after the component calls onServerConfigChange
|
|
|
|
onAfterSubmit: PropTypes.func,
|
|
|
|
|
|
|
|
// Optional text for the submit button. If falsey, no button will be shown.
|
|
|
|
submitText: PropTypes.string,
|
|
|
|
|
|
|
|
// Optional class for the submit button. Only applies if the submit button
|
|
|
|
// is to be rendered.
|
|
|
|
submitClass: PropTypes.string,
|
2019-05-03 07:57:49 +03:00
|
|
|
};
|
2019-01-29 06:44:41 +03:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
onServerConfigChange: function() {},
|
|
|
|
delayTimeMs: 0,
|
2019-05-03 07:57:49 +03:00
|
|
|
};
|
2019-01-29 06:44:41 +03:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2019-01-29 06:44:41 +03:00
|
|
|
this.state = {
|
2019-05-03 07:57:49 +03:00
|
|
|
busy: false,
|
|
|
|
errorText: "",
|
|
|
|
hsUrl: props.serverConfig.hsUrl,
|
|
|
|
isUrl: props.serverConfig.isUrl,
|
2017-01-20 17:22:27 +03:00
|
|
|
};
|
2019-01-29 06:44:41 +03:00
|
|
|
}
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2019-01-29 06:44:41 +03:00
|
|
|
componentWillReceiveProps(newProps) {
|
2019-05-03 07:57:49 +03:00
|
|
|
if (newProps.serverConfig.hsUrl === this.state.hsUrl &&
|
|
|
|
newProps.serverConfig.isUrl === this.state.isUrl) return;
|
|
|
|
|
|
|
|
this.validateAndApplyServer(newProps.serverConfig.hsUrl, newProps.serverConfig.isUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
async validateServer() {
|
|
|
|
// TODO: Do we want to support .well-known lookups here?
|
|
|
|
// If for some reason someone enters "matrix.org" for a URL, we could do a lookup to
|
|
|
|
// find their homeserver without demanding they use "https://matrix.org"
|
|
|
|
return this.validateAndApplyServer(this.state.hsUrl, this.state.isUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
async validateAndApplyServer(hsUrl, isUrl) {
|
|
|
|
// Always try and use the defaults first
|
|
|
|
const defaultConfig: ValidatedServerConfig = SdkConfig.get()["validated_server_config"];
|
|
|
|
if (defaultConfig.hsUrl === hsUrl && defaultConfig.isUrl === isUrl) {
|
|
|
|
this.setState({busy: false, errorText: ""});
|
|
|
|
this.props.onServerConfigChange(defaultConfig);
|
|
|
|
return defaultConfig;
|
|
|
|
}
|
2018-10-19 01:42:54 +03:00
|
|
|
|
|
|
|
this.setState({
|
2019-05-03 07:57:49 +03:00
|
|
|
hsUrl,
|
|
|
|
isUrl,
|
|
|
|
busy: true,
|
|
|
|
errorText: "",
|
2018-10-19 01:42:54 +03:00
|
|
|
});
|
2019-05-03 07:57:49 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await AutoDiscoveryUtils.validateServerConfigWithStaticUrls(hsUrl, isUrl);
|
|
|
|
this.setState({busy: false, errorText: ""});
|
|
|
|
this.props.onServerConfigChange(result);
|
|
|
|
return result;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2019-06-05 08:41:59 +03:00
|
|
|
|
2019-06-13 20:23:33 +03:00
|
|
|
const stateForError = AutoDiscoveryUtils.authComponentStateForError(e);
|
|
|
|
if (!stateForError.isFatalError) {
|
2019-06-17 20:47:20 +03:00
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
});
|
2019-06-13 20:23:33 +03:00
|
|
|
// carry on anyway
|
|
|
|
const result = await AutoDiscoveryUtils.validateServerConfigWithStaticUrls(hsUrl, isUrl, true);
|
|
|
|
this.props.onServerConfigChange(result);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
let message = _t("Unable to validate homeserver/identity server");
|
|
|
|
if (e.translatedMessage) {
|
|
|
|
message = e.translatedMessage;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
errorText: message,
|
|
|
|
});
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2019-05-03 07:57:49 +03:00
|
|
|
}
|
2019-01-29 06:44:41 +03:00
|
|
|
}
|
2018-10-19 01:42:54 +03:00
|
|
|
|
2019-01-30 08:47:35 +03:00
|
|
|
onHomeserverBlur = (ev) => {
|
|
|
|
this._hsTimeoutId = this._waitThenInvoke(this._hsTimeoutId, () => {
|
2019-05-03 07:57:49 +03:00
|
|
|
this.validateServer();
|
2015-11-30 21:11:04 +03:00
|
|
|
});
|
2019-05-03 07:57:49 +03:00
|
|
|
};
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2019-01-30 08:47:35 +03:00
|
|
|
onHomeserverChange = (ev) => {
|
|
|
|
const hsUrl = ev.target.value;
|
|
|
|
this.setState({ hsUrl });
|
2019-05-03 07:57:49 +03:00
|
|
|
};
|
2019-01-30 08:47:35 +03:00
|
|
|
|
|
|
|
onIdentityServerBlur = (ev) => {
|
|
|
|
this._isTimeoutId = this._waitThenInvoke(this._isTimeoutId, () => {
|
2019-05-03 07:57:49 +03:00
|
|
|
this.validateServer();
|
2015-11-30 21:11:04 +03:00
|
|
|
});
|
2019-05-03 07:57:49 +03:00
|
|
|
};
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2019-01-30 08:47:35 +03:00
|
|
|
onIdentityServerChange = (ev) => {
|
|
|
|
const isUrl = ev.target.value;
|
|
|
|
this.setState({ isUrl });
|
2019-05-03 07:57:49 +03:00
|
|
|
};
|
2019-01-30 08:47:35 +03:00
|
|
|
|
2019-05-14 01:30:34 +03:00
|
|
|
onSubmit = async (ev) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2019-06-05 08:41:59 +03:00
|
|
|
const result = await this.validateServer();
|
|
|
|
if (!result) return; // Do not continue.
|
2019-05-14 01:30:34 +03:00
|
|
|
|
|
|
|
if (this.props.onAfterSubmit) {
|
|
|
|
this.props.onAfterSubmit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-29 06:44:41 +03:00
|
|
|
_waitThenInvoke(existingTimeoutId, fn) {
|
2015-11-30 21:11:04 +03:00
|
|
|
if (existingTimeoutId) {
|
|
|
|
clearTimeout(existingTimeoutId);
|
|
|
|
}
|
|
|
|
return setTimeout(fn.bind(this), this.props.delayTimeMs);
|
2019-01-29 06:44:41 +03:00
|
|
|
}
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2019-01-29 06:44:41 +03:00
|
|
|
showHelpPopup = () => {
|
2019-01-22 01:11:10 +03:00
|
|
|
const CustomServerDialog = sdk.getComponent('auth.CustomServerDialog');
|
2017-07-27 19:19:18 +03:00
|
|
|
Modal.createTrackedDialog('Custom Server Dialog', '', CustomServerDialog);
|
2019-05-03 07:57:49 +03:00
|
|
|
};
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2019-01-29 06:44:41 +03:00
|
|
|
render() {
|
2019-01-26 09:48:33 +03:00
|
|
|
const Field = sdk.getComponent('elements.Field');
|
2019-05-14 01:30:34 +03:00
|
|
|
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2019-05-03 07:57:49 +03:00
|
|
|
const errorText = this.state.errorText
|
|
|
|
? <span className='mx_ServerConfig_error'>{this.state.errorText}</span>
|
|
|
|
: null;
|
|
|
|
|
2019-05-14 01:30:34 +03:00
|
|
|
const submitButton = this.props.submitText
|
|
|
|
? <AccessibleButton
|
|
|
|
element="button"
|
|
|
|
type="submit"
|
|
|
|
className={this.props.submitClass}
|
|
|
|
onClick={this.onSubmit}
|
|
|
|
disabled={this.state.busy}>{this.props.submitText}</AccessibleButton>
|
|
|
|
: null;
|
|
|
|
|
2015-11-30 21:11:04 +03:00
|
|
|
return (
|
2019-01-26 09:48:33 +03:00
|
|
|
<div className="mx_ServerConfig">
|
|
|
|
<h3>{_t("Other servers")}</h3>
|
|
|
|
{_t("Enter custom server URLs <a>What does this mean?</a>", {}, {
|
|
|
|
a: sub => <a className="mx_ServerConfig_help" href="#" onClick={this.showHelpPopup}>
|
|
|
|
{ sub }
|
|
|
|
</a>,
|
|
|
|
})}
|
2019-05-03 07:57:49 +03:00
|
|
|
{errorText}
|
2019-05-14 01:30:34 +03:00
|
|
|
<form onSubmit={this.onSubmit} autoComplete={false} action={null}>
|
|
|
|
<div className="mx_ServerConfig_fields">
|
|
|
|
<Field id="mx_ServerConfig_hsUrl"
|
|
|
|
label={_t("Homeserver URL")}
|
|
|
|
placeholder={this.props.serverConfig.hsUrl}
|
|
|
|
value={this.state.hsUrl}
|
|
|
|
onBlur={this.onHomeserverBlur}
|
|
|
|
onChange={this.onHomeserverChange}
|
|
|
|
disabled={this.state.busy}
|
|
|
|
/>
|
|
|
|
<Field id="mx_ServerConfig_isUrl"
|
|
|
|
label={_t("Identity Server URL")}
|
|
|
|
placeholder={this.props.serverConfig.isUrl}
|
2019-08-07 13:15:56 +03:00
|
|
|
value={this.state.isUrl || ''}
|
2019-05-14 01:30:34 +03:00
|
|
|
onBlur={this.onIdentityServerBlur}
|
|
|
|
onChange={this.onIdentityServerChange}
|
|
|
|
disabled={this.state.busy}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{submitButton}
|
|
|
|
</form>
|
2015-11-30 21:11:04 +03:00
|
|
|
</div>
|
|
|
|
);
|
2019-01-29 06:44:41 +03:00
|
|
|
}
|
|
|
|
}
|