From d7f95ddd4366efa59f8a7ac0af68becd877100ec Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Mon, 25 Jan 2021 12:05:23 -0500 Subject: [PATCH] fix desktop build --- ui/util/url.js | 18 +++++++++--------- web/middleware/redirect.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/ui/util/url.js b/ui/util/url.js index 9a8d00efa..fa3307674 100644 --- a/ui/util/url.js +++ b/ui/util/url.js @@ -8,7 +8,7 @@ function encodeWithApostropheEncode(string) { return encodeURIComponent(string).replace(/'/g, '%27'); } -exports.formatLbryUrlForWeb = uri => { +export const formatLbryUrlForWeb = uri => { let newUrl = uri.replace('lbry://', '/').replace(/#/g, ':'); if (newUrl.startsWith('/?')) { // This is a lbry link to an internal page ex: lbry://?rewards @@ -18,7 +18,7 @@ exports.formatLbryUrlForWeb = uri => { return newUrl; }; -exports.formatFileSystemPath = path => { +export const formatFileSystemPath = path => { if (!path) { return; } @@ -37,7 +37,7 @@ exports.formatFileSystemPath = path => { ex: lbry://?rewards ex: open.lbry.com/?rewards */ -exports.formatInAppUrl = path => { +export const formatInAppUrl = path => { // Determine if we need to add a leading "/$/" for app pages const APP_PAGE_REGEX = /(\?)([a-z]*)(.*)/; const appPageMatches = APP_PAGE_REGEX.exec(path); @@ -61,7 +61,7 @@ exports.formatInAppUrl = path => { return path; }; -exports.formatWebUrlIntoLbryUrl = (pathname, search) => { +export const formatWebUrlIntoLbryUrl = (pathname, search) => { // If there is no uri, the user is on an internal page // pathname will either be "/" or "/$/{page}" const path = pathname.startsWith('/$/') ? pathname.slice(3) : pathname.slice(1); @@ -75,7 +75,7 @@ exports.formatWebUrlIntoLbryUrl = (pathname, search) => { return appLink; }; -exports.generateInitialUrl = hash => { +export const generateInitialUrl = hash => { let url = '/'; if (hash) { hash = hash.replace('#', ''); @@ -84,21 +84,21 @@ exports.generateInitialUrl = hash => { return url; }; -exports.generateLbryContentUrl = (canonicalUrl, permanentUrl) => { +export const generateLbryContentUrl = (canonicalUrl, permanentUrl) => { return canonicalUrl ? canonicalUrl.split('lbry://')[1] : permanentUrl.split('lbry://')[1]; }; -exports.generateLbryWebUrl = lbryUrl => { +export const generateLbryWebUrl = lbryUrl => { return lbryUrl.replace(/#/g, ':'); }; -exports.generateEncodedLbryURL = (domain, lbryWebUrl, includeStartTime, startTime) => { +export const generateEncodedLbryURL = (domain, lbryWebUrl, includeStartTime, startTime) => { const queryParam = includeStartTime ? `?t=${startTime}` : ''; const encodedPart = encodeWithApostropheEncode(`${lbryWebUrl}${queryParam}`); return `${domain}/${encodedPart}`; }; -exports.generateShareUrl = (domain, lbryUrl, referralCode, rewardsApproved, includeStartTime, startTime) => { +export const generateShareUrl = (domain, lbryUrl, referralCode, rewardsApproved, includeStartTime, startTime) => { let urlParams = new URLSearchParams(); if (referralCode && rewardsApproved) { urlParams.append('r', referralCode); diff --git a/web/middleware/redirect.js b/web/middleware/redirect.js index c7e130c8f..55598b8d6 100644 --- a/web/middleware/redirect.js +++ b/web/middleware/redirect.js @@ -1,5 +1,29 @@ +const PAGES = require('../../ui/constants/pages'); const config = require('../../config'); -const { formatInAppUrl } = require('../../ui/util/url'); + +function formatInAppUrl(path) { + // Determine if we need to add a leading "/$/" for app pages + const APP_PAGE_REGEX = /(\?)([a-z]*)(.*)/; + const appPageMatches = APP_PAGE_REGEX.exec(path); + + if (appPageMatches && appPageMatches.length) { + // Definitely an app page (or it's formatted like one) + const [, , page, queryString] = appPageMatches; + + if (Object.values(PAGES).includes(page)) { + let actualUrl = '/$/' + page; + + if (queryString) { + actualUrl += `?${queryString.slice(1)}`; + } + + return actualUrl; + } + } + + // Regular claim url + return path; +} async function redirectMiddleware(ctx, next) { const requestHost = ctx.host; @@ -22,7 +46,7 @@ async function redirectMiddleware(ctx, next) { if (requestHost === 'open.lbry.com' || requestHost === 'open.lbry.io') { const openQuery = '?src=open'; - let redirectUrl = config.URL + formatInAppUrl(url, openQuery); + let redirectUrl = config.URL + formatInAppUrl(url); if (redirectUrl.includes('?')) { redirectUrl = redirectUrl.replace('?', `${openQuery}&`);