diff --git a/app/utils/deep_link/index.ts b/app/utils/deep_link/index.ts index 6ae6080824..e3f5b242d2 100644 --- a/app/utils/deep_link/index.ts +++ b/app/utils/deep_link/index.ts @@ -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. 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; } diff --git a/app/utils/url/test.ts b/app/utils/url/test.ts index ba8f951b66..f7057e1f46 100644 --- a/app/utils/url/test.ts +++ b/app/utils/url/test.ts @@ -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) {