Fix matchDeepLink when server is on a subpath (#7010)

* Fixes matchDeepLink when server is in a subpath

* Fix matchDeepLink when serverUrl is in a subpath
This commit is contained in:
Elias Nahum
2023-01-25 15:04:05 +02:00
committed by GitHub
parent 5160cf9212
commit 983d0aab66
2 changed files with 51 additions and 9 deletions

View File

@@ -152,8 +152,10 @@ export function matchDeepLink(url?: string, serverURL?: string, siteURL?: string
let urlToMatch = url;
const urlBase = serverURL || siteURL || '';
const parsedUrl = urlParse(url);
const parsedBase = urlParse(urlBase);
if (!url.startsWith('mattermost://')) {
if (!parsedUrl.protocol) {
// If url doesn't contain site or server URL, tack it on.
// e.g. <jump to convo> URLs from autolink plugin.
const match = new RegExp(escapeRegex(urlBase)).exec(url);
@@ -162,7 +164,18 @@ export function matchDeepLink(url?: string, serverURL?: string, siteURL?: string
}
}
if (urlParse(urlToMatch).hostname === urlParse(urlBase).hostname) {
const finalUrl = urlParse(urlToMatch);
const baseSubpath = parsedBase.pathname.replace('/', '');
const baseHostname = parsedBase.hostname;
const urlSubpath = finalUrl.pathname.split('/')[1];
const urlHostname = finalUrl.hostname;
if (baseSubpath) {
// if the server is in a subpath
if (urlHostname === baseHostname && urlSubpath === baseSubpath) {
return urlToMatch;
}
} else if (urlHostname === baseHostname) {
return urlToMatch;
}

View File

@@ -128,9 +128,11 @@ describe('UrlUtils', () => {
});
describe('matchDeepLink', () => {
const URL_NO_PROTOCOL = 'localhost:8065/subdir';
const URL_NO_PROTOCOL = 'localhost:8065';
const URL_PATH_NO_PROTOCOL = 'localhost:8065/subpath';
const SITE_URL = `http://${URL_NO_PROTOCOL}`;
const SERVER_URL = `http://${URL_NO_PROTOCOL}`;
const SERVER_WITH_SUBPATH = `http://${URL_PATH_NO_PROTOCOL}`;
const DEEPLINK_URL_ROOT = `mattermost://${URL_NO_PROTOCOL}`;
const tests = [
@@ -233,21 +235,48 @@ describe('UrlUtils', () => {
},
},
{
name: 'should match permalink with depplink prefix',
name: 'should match permalink with deeplink prefix on a Server hosted in a Subpath',
input: {
url: DEEPLINK_URL_ROOT + '/ad-1/pl/qe93kkfd7783iqwuwfcwcxbsgy',
serverURL: SERVER_URL,
siteURL: SITE_URL,
url: DEEPLINK_URL_ROOT + '/subpath/ad-1/pl/qe93kkfd7783iqwuwfcwcxbsrr',
serverURL: SERVER_WITH_SUBPATH,
siteURL: SERVER_WITH_SUBPATH,
},
expected: {
data: {
postId: 'qe93kkfd7783iqwuwfcwcxbsgy',
serverUrl: URL_NO_PROTOCOL,
postId: 'qe93kkfd7783iqwuwfcwcxbsrr',
serverUrl: URL_PATH_NO_PROTOCOL,
teamName: 'ad-1',
},
type: 'permalink',
},
},
{
name: 'should match permalink on a Server hosted in a Subpath',
input: {
url: SERVER_WITH_SUBPATH + '/ad-1/pl/qe93kkfd7783iqwuwfcwcxbsrr',
serverURL: SERVER_WITH_SUBPATH,
siteURL: SERVER_WITH_SUBPATH,
},
expected: {
data: {
postId: 'qe93kkfd7783iqwuwfcwcxbsrr',
serverUrl: URL_PATH_NO_PROTOCOL,
teamName: 'ad-1',
},
type: 'permalink',
},
},
{
name: 'should not match url',
input: {
url: 'https://github.com/mattermost/mattermost-mobile/issues/new',
serverURL: SERVER_WITH_SUBPATH,
siteURL: SERVER_WITH_SUBPATH,
},
expected: {
type: 'invalid',
},
},
];
for (const test of tests) {