From 2dc28a608f2a3a4b48547bae6fb6de387902893e Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 21 Aug 2019 08:46:10 -0600 Subject: [PATCH] Move URL abbreviation to its own util file --- src/components/views/settings/SetIdServer.js | 49 ++++--------------- .../tabs/user/GeneralUserSettingsTab.js | 4 +- src/utils/UrlUtils.js | 49 +++++++++++++++++++ 3 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 src/utils/UrlUtils.js diff --git a/src/components/views/settings/SetIdServer.js b/src/components/views/settings/SetIdServer.js index 135d3c32fd..9c2a59bc82 100644 --- a/src/components/views/settings/SetIdServer.js +++ b/src/components/views/settings/SetIdServer.js @@ -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 . You will need to reconnect to to stop " + "sharing them.", {}, { - idserver: sub => {abbreviateIdentityUrl(this.state.currentClientIdServer)}, + idserver: sub => {abbreviateUrl(this.state.currentClientIdServer)}, // XXX: https://github.com/vector-im/riot-web/issues/9086 - idserver2: sub => {abbreviateIdentityUrl(this.state.currentClientIdServer)}, + idserver2: sub => {abbreviateUrl(this.state.currentClientIdServer)}, }, ); } else { message = _t( "Disconnect from the identity server ?", {}, - {idserver: sub => {abbreviateIdentityUrl(this.state.currentClientIdServer)}}, + {idserver: sub => {abbreviateUrl(this.state.currentClientIdServer)}}, ); } @@ -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 to discover and be discoverable by " + "existing contacts you know. You can change your identity server below.", {}, - { server: sub => {abbreviateIdentityUrl(idServerUrl)} }, + { server: sub => {abbreviateUrl(idServerUrl)} }, ); } else { sectionTitle = _t("Identity Server"); diff --git a/src/components/views/settings/tabs/user/GeneralUserSettingsTab.js b/src/components/views/settings/tabs/user/GeneralUserSettingsTab.js index 18c18f61d8..e37fa003f7 100644 --- a/src/components/views/settings/tabs/user/GeneralUserSettingsTab.js +++ b/src/components/views/settings/tabs/user/GeneralUserSettingsTab.js @@ -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, diff --git a/src/utils/UrlUtils.js b/src/utils/UrlUtils.js new file mode 100644 index 0000000000..7b207c128e --- /dev/null +++ b/src/utils/UrlUtils.js @@ -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; +}