Open all Signal links in app

This commit is contained in:
Evan Hahn 2022-02-02 12:29:01 -06:00 committed by GitHub
parent 07968ea42b
commit 60d348e7cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 7 deletions

View file

@ -1,10 +1,11 @@
// Copyright 2020-2021 Signal Messenger, LLC
// Copyright 2020-2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { LoggerType } from '../types/Logging';
import { maybeParseUrl } from './url';
import { isValidE164 } from './isValidE164';
const SIGNAL_HOSTS = new Set(['signal.group', 'signal.art', 'signal.me']);
const SIGNAL_DOT_ME_HASH_PREFIX = 'p/';
function parseUrl(value: string | URL, logger: LoggerType): undefined | URL {
@ -44,9 +45,7 @@ export function isSignalHttpsLink(
!url.password &&
!url.port &&
url.protocol === 'https:' &&
(url.host === 'signal.group' ||
url.host === 'signal.art' ||
url.host === 'signal.me')
SIGNAL_HOSTS.has(url.host)
);
}
@ -143,3 +142,25 @@ export function parseE164FromSignalDotMeHash(hash: string): undefined | string {
const maybeE164 = hash.slice(SIGNAL_DOT_ME_HASH_PREFIX.length);
return isValidE164(maybeE164, true) ? maybeE164 : undefined;
}
/**
* Converts `http://signal.group/#abc` to `https://signal.group/#abc`. Does the same for
* other Signal hosts, like signal.me. Does nothing to other URLs. Expects a valid href.
*/
export function rewriteSignalHrefsIfNecessary(href: string): string {
const resultUrl = new URL(href);
const isHttp = resultUrl.protocol === 'http:';
const isHttpOrHttps = isHttp || resultUrl.protocol === 'https:';
if (SIGNAL_HOSTS.has(resultUrl.host) && isHttpOrHttps) {
if (isHttp) {
resultUrl.protocol = 'https:';
}
resultUrl.username = '';
resultUrl.password = '';
return resultUrl.href;
}
return href;
}