Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit e21c12c

Browse files
committed
Merge remote-tracking branch 'origin/dbkr/change_is' into travis/integs/account_set
2 parents ef33adc + 4d33438 commit e21c12c

File tree

7 files changed

+241
-13
lines changed

7 files changed

+241
-13
lines changed

res/css/_components.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
@import "./views/settings/_Notifications.scss";
169169
@import "./views/settings/_PhoneNumbers.scss";
170170
@import "./views/settings/_ProfileSettings.scss";
171+
@import "./views/settings/_SetIdServer.scss";
171172
@import "./views/settings/tabs/_SettingsTab.scss";
172173
@import "./views/settings/tabs/room/_GeneralRoomSettingsTab.scss";
173174
@import "./views/settings/tabs/room/_RolesRoomSettingsTab.scss";

res/css/views/elements/_Tooltip.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ limitations under the License.
5555
border-radius: 4px;
5656
box-shadow: 4px 4px 12px 0 $menu-box-shadow-color;
5757
background-color: $menu-bg-color;
58-
z-index: 2000;
58+
z-index: 4000; // Higher than dialogs so tooltips can be used in dialogs
5959
padding: 10px;
6060
pointer-events: none;
6161
line-height: 14px;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2019 New Vector Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
.mx_SetIdServer .mx_Field_input {
18+
width: 300px;
19+
}

src/components/views/elements/Field.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export default class Field extends React.PureComponent {
4646
// and a `feedback` react component field to provide feedback
4747
// to the user.
4848
onValidate: PropTypes.func,
49+
// If specified, contents will appear as a tooltip on the element and
50+
// validation feedback tooltips will be suppressed.
51+
tooltip: PropTypes.node,
4952
// All other props pass through to the <input>.
5053
};
5154

@@ -135,6 +138,7 @@ export default class Field extends React.PureComponent {
135138

136139
render() {
137140
const { element, prefix, onValidate, children, ...inputProps } = this.props;
141+
delete inputProps.tooltip; // needs to be removed from props but we don't need it here
138142

139143
const inputElement = element || "input";
140144

@@ -165,20 +169,20 @@ export default class Field extends React.PureComponent {
165169

166170
// Handle displaying feedback on validity
167171
const Tooltip = sdk.getComponent("elements.Tooltip");
168-
let tooltip;
169-
if (this.state.feedback) {
170-
tooltip = <Tooltip
172+
let fieldTooltip;
173+
if (this.props.tooltip || this.state.feedback) {
174+
fieldTooltip = <Tooltip
171175
tooltipClassName="mx_Field_tooltip"
172176
visible={this.state.feedbackVisible}
173-
label={this.state.feedback}
177+
label={this.props.tooltip || this.state.feedback}
174178
/>;
175179
}
176180

177181
return <div className={fieldClasses}>
178182
{prefixContainer}
179183
{fieldInput}
180184
<label htmlFor={this.props.id}>{this.props.label}</label>
181-
{tooltip}
185+
{fieldTooltip}
182186
</div>;
183187
}
184188
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
Copyright 2019 New Vector Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import request from 'browser-request';
18+
import url from 'url';
19+
import React from 'react';
20+
import {_t} from "../../../languageHandler";
21+
import sdk from '../../../index';
22+
import MatrixClientPeg from "../../../MatrixClientPeg";
23+
import SdkConfig from "../../../SdkConfig";
24+
import Field from "../elements/Field";
25+
26+
/**
27+
* If a url has no path component, etc. abbreviate it to just the hostname
28+
*
29+
* @param {string} u The url to be abbreviated
30+
* @returns {string} The abbreviated url
31+
*/
32+
function abbreviateUrl(u) {
33+
if (!u) return '';
34+
35+
const parsedUrl = url.parse(u);
36+
// if it's something we can't parse as a url then just return it
37+
if (!parsedUrl) return u;
38+
39+
if (parsedUrl.path == '/') {
40+
// we ignore query / hash parts: these aren't relevant for IS server URLs
41+
return parsedUrl.host;
42+
}
43+
44+
return u;
45+
}
46+
47+
function unabbreviateUrl(u) {
48+
if (!u) return '';
49+
50+
let longUrl = u;
51+
if (!u.startsWith('https://')) longUrl = 'https://' + u;
52+
const parsed = url.parse(longUrl);
53+
if (parsed.hostname === null) return u;
54+
55+
return longUrl;
56+
}
57+
58+
/**
59+
* Check an IS URL is valid, including liveness check
60+
*
61+
* @param {string} isUrl The url to check
62+
* @returns {string} null if url passes all checks, otherwise i18ned error string
63+
*/
64+
async function checkIsUrl(isUrl) {
65+
const parsedUrl = url.parse(isUrl);
66+
67+
if (parsedUrl.protocol !== 'https:') return _t("Identity Server URL must be HTTPS");
68+
69+
// XXX: duplicated logic from js-sdk but it's quite tied up in the validation logic in the
70+
// js-sdk so probably as easy to duplicate it than to separate it out so we can reuse it
71+
return new Promise((resolve) => {
72+
request(
73+
// also XXX: we don't really know whether to hit /v1 or /v2 for this: we
74+
// probably want a /versions endpoint like the C/S API.
75+
{ method: "GET", url: isUrl + '/_matrix/identity/api/v1' },
76+
(err, response, body) => {
77+
if (err) {
78+
resolve(_t("Could not connect to ID Server"));
79+
} else if (response.status < 200 || response.status >= 300) {
80+
resolve(_t("Not a valid ID Server (status code %(code)s)", {code: response.status}));
81+
} else {
82+
resolve(null);
83+
}
84+
},
85+
);
86+
});
87+
}
88+
89+
export default class SetIdServer extends React.Component {
90+
constructor() {
91+
super();
92+
93+
let defaultIdServer = abbreviateUrl(MatrixClientPeg.get().getIdentityServerUrl());
94+
if (!defaultIdServer) {
95+
defaultIdServer = abbreviateUrl(SdkConfig.get()['validated_server_config']['idServer']) || '';
96+
}
97+
98+
this.state = {
99+
currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(),
100+
idServer: defaultIdServer,
101+
error: null,
102+
busy: false,
103+
};
104+
}
105+
106+
_onIdentityServerChanged = (ev) => {
107+
const u = ev.target.value;
108+
109+
this.setState({idServer: u});
110+
};
111+
112+
_getTooltip = () => {
113+
if (this.state.busy) {
114+
const InlineSpinner = sdk.getComponent('views.elements.InlineSpinner');
115+
return <div>
116+
<InlineSpinner />
117+
{ _t("Checking Server") }
118+
</div>;
119+
} else if (this.state.error) {
120+
return this.state.error;
121+
} else {
122+
return null;
123+
}
124+
};
125+
126+
_idServerChangeEnabled = () => {
127+
return !!this.state.idServer && !this.state.busy;
128+
};
129+
130+
_saveIdServer = async () => {
131+
this.setState({busy: true});
132+
133+
const fullUrl = unabbreviateUrl(this.state.idServer);
134+
135+
const errStr = await checkIsUrl(fullUrl);
136+
137+
let newFormValue = this.state.idServer;
138+
if (!errStr) {
139+
MatrixClientPeg.get().setIdentityServerUrl(fullUrl);
140+
localStorage.removeItem("mx_is_access_token");
141+
localStorage.setItem("mx_is_url", fullUrl);
142+
newFormValue = '';
143+
}
144+
this.setState({
145+
busy: false,
146+
error: errStr,
147+
currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(),
148+
idServer: newFormValue,
149+
});
150+
};
151+
152+
render() {
153+
const idServerUrl = this.state.currentClientIdServer;
154+
let sectionTitle;
155+
let bodyText;
156+
if (idServerUrl) {
157+
sectionTitle = _t("Identity Server (%(server)s)", { server: abbreviateUrl(idServerUrl) });
158+
bodyText = _t(
159+
"You are currently using <server></server> to discover and be discoverable by " +
160+
"existing contacts you know. You can change your identity server below.",
161+
{},
162+
{ server: sub => <b>{abbreviateUrl(idServerUrl)}</b> },
163+
);
164+
} else {
165+
sectionTitle = _t("Identity Server");
166+
bodyText = _t(
167+
"You are not currently using an Identity Server. " +
168+
"To discover and be discoverable by existing contacts you know, " +
169+
"add one below",
170+
);
171+
}
172+
173+
return (
174+
<form className="mx_SettingsTab_section mx_SetIdServer" onSubmit={this._saveIdServer}>
175+
<span className="mx_SettingsTab_subheading">
176+
{sectionTitle}
177+
</span>
178+
<span className="mx_SettingsTab_subsectionText">
179+
{bodyText}
180+
</span>
181+
<Field label={_t("Identity Server")}
182+
id="mx_SetIdServer_idServer"
183+
type="text" value={this.state.idServer} autoComplete="off"
184+
onChange={this._onIdentityServerChanged}
185+
tooltip={this._getTooltip()}
186+
/>
187+
<input className="mx_Dialog_primary"
188+
type="submit" value={_t("Change")}
189+
disabled={!this._idServerChangeEnabled()}
190+
/>
191+
</form>
192+
);
193+
}
194+
}

src/components/views/settings/tabs/user/GeneralUserSettingsTab.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import AccessibleButton from "../../../elements/AccessibleButton";
2828
import DeactivateAccountDialog from "../../../dialogs/DeactivateAccountDialog";
2929
import PropTypes from "prop-types";
3030
import {THEMES} from "../../../../../themes";
31-
const PlatformPeg = require("../../../../../PlatformPeg");
32-
const MatrixClientPeg = require("../../../../../MatrixClientPeg");
33-
const sdk = require('../../../../..');
34-
const Modal = require("../../../../../Modal");
35-
const dis = require("../../../../../dispatcher");
31+
import PlatformPeg from "../../../../../PlatformPeg";
32+
import MatrixClientPeg from "../../../../../MatrixClientPeg";
33+
import sdk from "../../../../..";
34+
import Modal from "../../../../../Modal";
35+
import dis from "../../../../../dispatcher";
3636

3737
export default class GeneralUserSettingsTab extends React.Component {
3838
static propTypes = {
@@ -174,6 +174,7 @@ export default class GeneralUserSettingsTab extends React.Component {
174174
_renderDiscoverySection() {
175175
const EmailAddresses = sdk.getComponent("views.settings.discovery.EmailAddresses");
176176
const PhoneNumbers = sdk.getComponent("views.settings.discovery.PhoneNumbers");
177+
const SetIdServer = sdk.getComponent("views.settings.SetIdServer");
177178

178179
return (
179180
<div className="mx_SettingsTab_section">
@@ -182,6 +183,8 @@ export default class GeneralUserSettingsTab extends React.Component {
182183

183184
<span className="mx_SettingsTab_subheading">{_t("Phone numbers")}</span>
184185
<PhoneNumbers />
186+
{ /* has its own heading as it includes the current ID server */ }
187+
<SetIdServer />
185188
</div>
186189
);
187190
}

src/i18n/strings/en_EN.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,15 @@
539539
"<a>Upgrade</a> to your own domain": "<a>Upgrade</a> to your own domain",
540540
"Display Name": "Display Name",
541541
"Save": "Save",
542+
"Identity Server URL must be HTTPS": "Identity Server URL must be HTTPS",
543+
"Could not connect to ID Server": "Could not connect to ID Server",
544+
"Not a valid ID Server (status code %(code)s)": "Not a valid ID Server (status code %(code)s)",
545+
"Checking Server": "Checking Server",
546+
"Identity Server (%(server)s)": "Identity Server (%(server)s)",
547+
"You are currently using <server></server> to discover and be discoverable by existing contacts you know. You can change your identity server below.": "You are currently using <server></server> to discover and be discoverable by existing contacts you know. You can change your identity server below.",
548+
"Identity Server": "Identity Server",
549+
"You are not currently using an Identity Server. To discover and be discoverable by existing contacts you know, add one below": "You are not currently using an Identity Server. To discover and be discoverable by existing contacts you know, add one below",
550+
"Change": "Change",
542551
"Flair": "Flair",
543552
"Failed to change password. Is your password correct?": "Failed to change password. Is your password correct?",
544553
"Success": "Success",
@@ -1276,7 +1285,6 @@
12761285
"Missing session data": "Missing session data",
12771286
"Some session data, including encrypted message keys, is missing. Sign out and sign in to fix this, restoring keys from backup.": "Some session data, including encrypted message keys, is missing. Sign out and sign in to fix this, restoring keys from backup.",
12781287
"Your browser likely removed this data when running low on disk space.": "Your browser likely removed this data when running low on disk space.",
1279-
"Identity Server": "Identity Server",
12801288
"Integrations Manager": "Integrations Manager",
12811289
"Find others by phone or email": "Find others by phone or email",
12821290
"Be found by phone or email": "Be found by phone or email",
@@ -1396,7 +1404,6 @@
13961404
"Not sure of your password? <a>Set a new one</a>": "Not sure of your password? <a>Set a new one</a>",
13971405
"Sign in to your Matrix account on %(serverName)s": "Sign in to your Matrix account on %(serverName)s",
13981406
"Sign in to your Matrix account on <underlinedServerName />": "Sign in to your Matrix account on <underlinedServerName />",
1399-
"Change": "Change",
14001407
"Sign in with": "Sign in with",
14011408
"If you don't specify an email address, you won't be able to reset your password. Are you sure?": "If you don't specify an email address, you won't be able to reset your password. Are you sure?",
14021409
"No Identity Server is configured so you cannot add add an email address in order to reset your password in the future.": "No Identity Server is configured so you cannot add add an email address in order to reset your password in the future.",

0 commit comments

Comments
 (0)