2021-01-25 18:05:23 +01:00
|
|
|
const PAGES = require('../../ui/constants/pages');
|
2021-03-19 23:16:47 +01:00
|
|
|
// const config = require('../../config');
|
2021-01-25 18:05:23 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-11-07 20:39:22 +01:00
|
|
|
|
|
|
|
async function redirectMiddleware(ctx, next) {
|
|
|
|
const requestHost = ctx.host;
|
|
|
|
const path = ctx.path;
|
|
|
|
const url = ctx.url;
|
2020-12-23 03:36:42 +01:00
|
|
|
|
2020-01-10 04:18:23 +01:00
|
|
|
// Getting err: too many redirects on some urls because of this
|
|
|
|
// Need a better solution
|
|
|
|
// const decodedUrl = decodeURIComponent(url);
|
|
|
|
|
|
|
|
// if (decodedUrl !== url) {
|
|
|
|
// ctx.redirect(decodedUrl);
|
|
|
|
// return;
|
|
|
|
// }
|
2019-11-07 20:39:22 +01:00
|
|
|
|
|
|
|
if (!path.startsWith('/$/') && path.match(/^([^@/:]+)\/([^:/]+)$/)) {
|
|
|
|
ctx.redirect(url.replace(/^([^@/:]+)\/([^:/]+)(:(\/.*))/, '$1:$2')); // test against path, but use ctx.url to retain parameters
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-02 18:30:08 +01:00
|
|
|
if (requestHost === 'open.lbry.com' || requestHost === 'open.lbry.io') {
|
2019-11-26 20:04:28 +01:00
|
|
|
const openQuery = '?src=open';
|
2021-03-19 23:16:47 +01:00
|
|
|
// let redirectUrl = config.URL + formatInAppUrl(url);
|
|
|
|
// Blame tom for this, not me
|
|
|
|
let redirectUrl = 'https://odysee.com' + formatInAppUrl(url);
|
2019-11-26 20:04:28 +01:00
|
|
|
|
2019-12-02 18:30:08 +01:00
|
|
|
if (redirectUrl.includes('?')) {
|
|
|
|
redirectUrl = redirectUrl.replace('?', `${openQuery}&`);
|
2019-11-26 20:04:28 +01:00
|
|
|
} else {
|
2019-12-02 18:30:08 +01:00
|
|
|
redirectUrl += openQuery;
|
2019-11-26 20:04:28 +01:00
|
|
|
}
|
|
|
|
|
2019-11-07 20:39:22 +01:00
|
|
|
ctx.redirect(redirectUrl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No redirects needed
|
|
|
|
await next();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = redirectMiddleware;
|