Move URL abbreviation to its own util file
parent
c758b5d3f1
commit
2dc28a608f
|
@ -25,38 +25,7 @@ import dis from "../../../dispatcher";
|
|||
import { getThreepidBindStatus } from '../../../boundThreepids';
|
||||
import IdentityAuthClient from "../../../IdentityAuthClient";
|
||||
import {SERVICE_TYPES} from "matrix-js-sdk";
|
||||
|
||||
/**
|
||||
* If a url has no path component, etc. abbreviate it to just the hostname
|
||||
*
|
||||
* @param {string} u The url to be abbreviated
|
||||
* @returns {string} The abbreviated url
|
||||
*/
|
||||
export function abbreviateIdentityUrl(u) {
|
||||
if (!u) return '';
|
||||
|
||||
const parsedUrl = url.parse(u);
|
||||
// if it's something we can't parse as a url then just return it
|
||||
if (!parsedUrl) return u;
|
||||
|
||||
if (parsedUrl.path === '/') {
|
||||
// we ignore query / hash parts: these aren't relevant for IS server URLs
|
||||
return parsedUrl.host;
|
||||
}
|
||||
|
||||
return u;
|
||||
}
|
||||
|
||||
function unabbreviateUrl(u) {
|
||||
if (!u) return '';
|
||||
|
||||
let longUrl = u;
|
||||
if (!u.startsWith('https://')) longUrl = 'https://' + u;
|
||||
const parsed = url.parse(longUrl);
|
||||
if (parsed.hostname === null) return u;
|
||||
|
||||
return longUrl;
|
||||
}
|
||||
import {abbreviateUrl, unabbreviateUrl} from "../../../utils/UrlUtils";
|
||||
|
||||
/**
|
||||
* Check an IS URL is valid, including liveness check
|
||||
|
@ -93,7 +62,7 @@ export default class SetIdServer extends React.Component {
|
|||
if (!MatrixClientPeg.get().getIdentityServerUrl() && SdkConfig.get()['validated_server_config']['isUrl']) {
|
||||
// If no ID server is configured but there's one in the config, prepopulate
|
||||
// the field to help the user.
|
||||
defaultIdServer = abbreviateIdentityUrl(SdkConfig.get()['validated_server_config']['isUrl']);
|
||||
defaultIdServer = abbreviateUrl(SdkConfig.get()['validated_server_config']['isUrl']);
|
||||
}
|
||||
|
||||
this.state = {
|
||||
|
@ -124,7 +93,7 @@ export default class SetIdServer extends React.Component {
|
|||
|
||||
const fullUrl = MatrixClientPeg.get().getIdentityServerUrl();
|
||||
let abbr = '';
|
||||
if (fullUrl) abbr = abbreviateIdentityUrl(fullUrl);
|
||||
if (fullUrl) abbr = abbreviateUrl(fullUrl);
|
||||
|
||||
this.setState({
|
||||
currentClientIdServer: fullUrl,
|
||||
|
@ -234,15 +203,15 @@ export default class SetIdServer extends React.Component {
|
|||
"server <idserver />. You will need to reconnect to <idserver2 /> to stop " +
|
||||
"sharing them.", {},
|
||||
{
|
||||
idserver: sub => <b>{abbreviateIdentityUrl(this.state.currentClientIdServer)}</b>,
|
||||
idserver: sub => <b>{abbreviateUrl(this.state.currentClientIdServer)}</b>,
|
||||
// XXX: https://github.com/vector-im/riot-web/issues/9086
|
||||
idserver2: sub => <b>{abbreviateIdentityUrl(this.state.currentClientIdServer)}</b>,
|
||||
idserver2: sub => <b>{abbreviateUrl(this.state.currentClientIdServer)}</b>,
|
||||
},
|
||||
);
|
||||
} else {
|
||||
message = _t(
|
||||
"Disconnect from the identity server <idserver />?", {},
|
||||
{idserver: sub => <b>{abbreviateIdentityUrl(this.state.currentClientIdServer)}</b>},
|
||||
{idserver: sub => <b>{abbreviateUrl(this.state.currentClientIdServer)}</b>},
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -272,7 +241,7 @@ export default class SetIdServer extends React.Component {
|
|||
if (SdkConfig.get()['validated_server_config']['isUrl']) {
|
||||
// Prepopulate the client's default so the user at least has some idea of
|
||||
// a valid value they might enter
|
||||
newFieldVal = abbreviateIdentityUrl(SdkConfig.get()['validated_server_config']['isUrl']);
|
||||
newFieldVal = abbreviateUrl(SdkConfig.get()['validated_server_config']['isUrl']);
|
||||
}
|
||||
|
||||
this.setState({
|
||||
|
@ -290,12 +259,12 @@ export default class SetIdServer extends React.Component {
|
|||
let sectionTitle;
|
||||
let bodyText;
|
||||
if (idServerUrl) {
|
||||
sectionTitle = _t("Identity Server (%(server)s)", { server: abbreviateIdentityUrl(idServerUrl) });
|
||||
sectionTitle = _t("Identity Server (%(server)s)", { server: abbreviateUrl(idServerUrl) });
|
||||
bodyText = _t(
|
||||
"You are currently using <server></server> to discover and be discoverable by " +
|
||||
"existing contacts you know. You can change your identity server below.",
|
||||
{},
|
||||
{ server: sub => <b>{abbreviateIdentityUrl(idServerUrl)}</b> },
|
||||
{ server: sub => <b>{abbreviateUrl(idServerUrl)}</b> },
|
||||
);
|
||||
} else {
|
||||
sectionTitle = _t("Identity Server");
|
||||
|
|
|
@ -36,7 +36,7 @@ import dis from "../../../../../dispatcher";
|
|||
import {Service, startTermsFlow} from "../../../../../Terms";
|
||||
import {SERVICE_TYPES} from "matrix-js-sdk";
|
||||
import IdentityAuthClient from "../../../../../IdentityAuthClient";
|
||||
import {abbreviateIdentityUrl} from "../../SetIdServer";
|
||||
import {abbreviateUrl} from "../../../../../utils/UrlUtils";
|
||||
|
||||
export default class GeneralUserSettingsTab extends React.Component {
|
||||
static propTypes = {
|
||||
|
@ -101,7 +101,7 @@ export default class GeneralUserSettingsTab extends React.Component {
|
|||
)], (policiesAndServices, agreedUrls, extraClassNames) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.setState({
|
||||
idServerName: abbreviateIdentityUrl(MatrixClientPeg.get().getIdentityServerUrl()),
|
||||
idServerName: abbreviateUrl(MatrixClientPeg.get().getIdentityServerUrl()),
|
||||
requiredPolicyInfo: {
|
||||
hasTerms: true,
|
||||
policiesAndServices,
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
import url from "url";
|
||||
|
||||
/**
|
||||
* If a url has no path component, etc. abbreviate it to just the hostname
|
||||
*
|
||||
* @param {string} u The url to be abbreviated
|
||||
* @returns {string} The abbreviated url
|
||||
*/
|
||||
export function abbreviateUrl(u) {
|
||||
if (!u) return '';
|
||||
|
||||
const parsedUrl = url.parse(u);
|
||||
// if it's something we can't parse as a url then just return it
|
||||
if (!parsedUrl) return u;
|
||||
|
||||
if (parsedUrl.path === '/') {
|
||||
// we ignore query / hash parts: these aren't relevant for IS server URLs
|
||||
return parsedUrl.host;
|
||||
}
|
||||
|
||||
return u;
|
||||
}
|
||||
|
||||
export function unabbreviateUrl(u) {
|
||||
if (!u) return '';
|
||||
|
||||
let longUrl = u;
|
||||
if (!u.startsWith('https://')) longUrl = 'https://' + u;
|
||||
const parsed = url.parse(longUrl);
|
||||
if (parsed.hostname === null) return u;
|
||||
|
||||
return longUrl;
|
||||
}
|
Loading…
Reference in New Issue