fix redirection and normalization of claim urls

This commit is contained in:
btzr-io 2021-08-07 04:00:33 -05:00 committed by infinite-persistence
parent 68e4684ccd
commit c85e448499
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
2 changed files with 19 additions and 17 deletions

View file

@ -12,10 +12,13 @@ import Card from 'component/common/card';
import { formatLbryUrlForWeb } from 'util/url';
import { parseURI, COLLECTIONS_CONSTS } from 'lbry-redux';
const AbandonedChannelPreview = lazyImport(() => import('component/abandonedChannelPreview' /* webpackChunkName: "abandonedChannelPreview" */));
const AbandonedChannelPreview = lazyImport(() =>
import('component/abandonedChannelPreview' /* webpackChunkName: "abandonedChannelPreview" */)
);
const FilePage = lazyImport(() => import('page/file' /* webpackChunkName: "filePage" */));
const LivestreamPage = lazyImport(() => import('page/livestream' /* webpackChunkName: "livestream" */));
const Yrbl = lazyImport(() => import('component/yrbl' /* webpackChunkName: "yrbl" */));
const isDev = process.env.NODE_ENV !== 'production';
type Props = {
isResolvingUri: boolean,
@ -85,7 +88,8 @@ function ShowPage(props: Props) {
const canonicalUrlPath = '/' + canonicalUrl.replace(/^lbry:\/\//, '').replace(/#/g, ':');
// Only redirect if we are in lbry.tv land
// replaceState will fail if on a different domain (like webcache.googleusercontent.com)
if (canonicalUrlPath !== window.location.pathname && DOMAIN === window.location.hostname) {
const hostname = isDev ? 'localhost' : DOMAIN;
if (canonicalUrlPath !== window.location.pathname && hostname === window.location.hostname) {
const urlParams = new URLSearchParams(search);
if (urlParams.get(COLLECTIONS_CONSTS.COLLECTION_ID)) {
const listId = urlParams.get(COLLECTIONS_CONSTS.COLLECTION_ID) || '';

View file

@ -16,7 +16,7 @@ const { Lbry } = require('lbry-redux');
const { generateEmbedUrl, generateStreamUrl, generateDirectUrl } = require('../../ui/util/web');
const PAGES = require('../../ui/constants/pages');
const { CATEGORY_METADATA } = require('./category-metadata');
const { parseURI, buildURI } = require('lbry-redux');
const { parseURI, normalizeURI } = require('lbry-redux');
const fs = require('fs');
const path = require('path');
const moment = require('moment');
@ -45,6 +45,10 @@ function truncateDescription(description) {
return description.length > 200 ? description.substr(0, 200) + '...' : description;
}
function normalizeClaimUrl(url) {
return normalizeURI(url.replace(/:/g, '#'));
}
function escapeHtmlProperty(property) {
return property
? String(property)
@ -266,20 +270,16 @@ function buildGoogleVideoMetadata(uri, claim) {
async function resolveClaimOrRedirect(ctx, url, ignoreRedirect = false) {
let claim;
const parsedURI = parseURI(url);
try {
const url = buildURI(parsedURI);
const response = await Lbry.resolve({ urls: [url] });
if (response && response[url] && !response[url].error) {
claim = response && response[url];
if (claim.reposted_claim) {
if (claim.reposted_claim.name && claim.reposted_claim.claim_id && !ignoreRedirect) {
const isRepost = claim.reposted_claim && claim.reposted_claim.name && claim.reposted_claim.claim_id;
if (isRepost && !ignoreRedirect) {
ctx.redirect(`/${claim.reposted_claim.name}:${claim.reposted_claim.claim_id}`);
return;
}
}
}
} catch {}
return claim;
}
@ -300,11 +300,9 @@ async function getHtml(ctx) {
const embedPath = `/$/${PAGES.EMBED}/`;
if (requestPath.includes(invitePath)) {
const inviteChannel = requestPath.slice(invitePath.length).replace(/:/g, '#');
const inviteChannelUrl = `lbry://${inviteChannel}`;
try {
parseURI(inviteChannelUrl);
const inviteChannel = requestPath.slice(invitePath.length);
const inviteChannelUrl = normalizeClaimUrl(inviteChannel);
const claim = await resolveClaimOrRedirect(ctx, inviteChannelUrl);
const invitePageMetadata = buildClaimOgMetadata(inviteChannelUrl, claim, {
title: `Join ${claim.name} on ${SITE_NAME}`,
@ -324,7 +322,7 @@ async function getHtml(ctx) {
}
if (requestPath.includes(embedPath)) {
const claimUri = requestPath.replace(embedPath, '').replace(/:/g, '#').replace('/', '#');
const claimUri = normalizeClaimUrl(requestPath.replace(embedPath, ''));
const claim = await resolveClaimOrRedirect(ctx, claimUri, true);
if (claim) {
@ -347,7 +345,7 @@ async function getHtml(ctx) {
}
if (!requestPath.includes('$')) {
const claimUri = requestPath.slice(1).replace(/:/g, '#');
const claimUri = normalizeClaimUrl(requestPath.slice(1));
const claim = await resolveClaimOrRedirect(ctx, claimUri);
if (claim) {