lbry-desktop/web/src/html.js

320 lines
12 KiB
JavaScript
Raw Normal View History

2020-07-06 23:39:35 +02:00
const {
URL,
SITE_TITLE,
SITE_CANONICAL_URL,
OG_HOMEPAGE_TITLE,
OG_TITLE_SUFFIX,
OG_IMAGE_URL,
SITE_DESCRIPTION,
SITE_NAME,
} = require('../../config.js');
const { generateEmbedUrl, generateStreamUrl, generateDirectUrl } = require('../../ui/util/web');
const PAGES = require('../../ui/constants/pages');
const { CATEGORY_METADATA } = require('./category-metadata');
2019-11-07 20:39:22 +01:00
const { getClaim } = require('./chainquery');
const { parseURI } = require('lbry-redux');
const fs = require('fs');
const path = require('path');
const moment = require('moment');
const removeMd = require('remove-markdown');
2020-08-19 23:09:30 +02:00
const { getJsBundleId } = require('../bundle-id.js');
const jsBundleId = getJsBundleId();
2019-11-07 20:39:22 +01:00
function insertToHead(fullHtml, htmlToInsert) {
return fullHtml.replace(
/<!-- VARIABLE_HEAD_BEGIN -->.*<!-- VARIABLE_HEAD_END -->/s,
2020-08-19 23:09:30 +02:00
`
${htmlToInsert || buildOgMetadata()}
<script src="/public/ui-${jsBundleId}.js" async></script>
`
);
2019-11-07 20:39:22 +01:00
}
function truncateDescription(description) {
return description.length > 200 ? description.substr(0, 200) + '...' : description;
}
function escapeHtmlProperty(property) {
return property
? String(property)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;')
: '';
}
function getCategoryMeta(path) {
const page = Object.keys(CATEGORY_METADATA).find((x) => path.endsWith(x) || path.endsWith(`${x}/`));
return CATEGORY_METADATA[page];
}
//
// Normal metadata with option to override certain values
2020-01-28 19:00:45 +01:00
//
function buildOgMetadata(overrideOptions = {}) {
2021-05-27 23:00:19 +02:00
const { title, description, image } = overrideOptions;
const cleanDescription = removeMd(description || SITE_DESCRIPTION);
const head =
2020-07-06 23:39:35 +02:00
`<title>${SITE_TITLE}</title>\n` +
`<meta property="og:url" content="${URL}" />\n` +
2020-10-05 20:54:51 +02:00
`<meta property="og:title" content="${title || OG_HOMEPAGE_TITLE || SITE_TITLE}" />\n` +
2020-07-02 03:34:50 +02:00
`<meta property="og:site_name" content="${SITE_NAME || SITE_TITLE}"/>\n` +
`<meta property="og:description" content="${cleanDescription}" />\n` +
2021-05-27 23:00:19 +02:00
`<meta property="og:image" content="${image || OG_IMAGE_URL || `${URL}/public/v2-og.png`}" />\n` +
'<meta name="twitter:card" content="summary_large_image"/>\n' +
`<meta name="twitter:title" content="${
2021-05-27 23:00:19 +02:00
(title && title + ' ' + OG_TITLE_SUFFIX) || OG_HOMEPAGE_TITLE || SITE_TITLE
}" />\n` +
`<meta name="twitter:description" content="${cleanDescription}" />\n` +
2021-05-27 23:00:19 +02:00
`<meta name="twitter:image" content="${image || OG_IMAGE_URL || `${URL}/public/v2-og.png`}"/>\n` +
2020-07-01 00:44:46 +02:00
`<meta name="twitter:url" content="${URL}" />\n` +
2020-07-06 23:39:35 +02:00
'<meta property="fb:app_id" content="1673146449633983" />\n' +
2020-08-07 18:17:29 +02:00
`<link rel="canonical" content="${SITE_CANONICAL_URL || URL}"/>` +
`<link rel="search" type="application/opensearchdescription+xml" title="${
SITE_NAME || SITE_TITLE
}" href="${URL}/opensearch.xml">`;
2020-07-01 00:44:46 +02:00
return head;
}
function conditionallyAddPWA() {
let head = '';
if (SITE_TITLE === 'Odysee') {
head += '<link rel="manifest" href="./public/pwa/manifest.json"/>'
head += '<link rel="apple-touch-icon" sizes="180x180" href="./public/pwa/icon-180.png">'
head += '<script src="./serviceWorker.js"></script>'
}
return head;
}
function buildBasicOgMetadataAndPWA() {
const head = '<!-- VARIABLE_HEAD_BEGIN -->' + conditionallyAddPWA() + buildOgMetadata() + '<!-- VARIABLE_HEAD_END -->';
return head;
}
//
// Metadata used for urls that need claim information
// Also has option to override defaults
2020-01-28 19:00:45 +01:00
//
function buildClaimOgMetadata(uri, claim, overrideOptions = {}) {
// Initial setup for claim based og metadata
2020-01-28 19:00:45 +01:00
const { claimName } = parseURI(uri);
const claimTitle = escapeHtmlProperty(claim.title ? claim.title : claimName);
2019-11-07 20:39:22 +01:00
const claimDescription =
claim.description && claim.description.length > 0
? escapeHtmlProperty(truncateDescription(claim.description))
2020-07-06 23:39:35 +02:00
: `View ${claimTitle} on ${SITE_NAME}`;
2019-11-07 20:39:22 +01:00
const claimLanguage = escapeHtmlProperty(claim.language) || 'en_US';
2020-04-08 20:49:37 +02:00
let imageThumbnail;
if (Number(claim.fee) <= 0 && claim.source_media_type && claim.source_media_type.startsWith('image/')) {
imageThumbnail = generateStreamUrl(claim.name, claim.claim_id);
}
const claimThumbnail =
escapeHtmlProperty(claim.thumbnail_url) || imageThumbnail || OG_IMAGE_URL || `${URL}/public/v2-og.png`;
// Allow for ovverriding default claim based og metadata
const title = overrideOptions.title || claimTitle;
const description = overrideOptions.description || claimDescription;
const cleanDescription = removeMd(description);
2019-11-07 20:39:22 +01:00
let head = '';
head += '<meta charset="utf8"/>';
head += `<title>${title}</title>`;
head += `<meta name="description" content="${cleanDescription}"/>`;
2019-11-07 20:39:22 +01:00
if (claim.tags) {
head += `<meta name="keywords" content="${claim.tags.toString()}"/>`;
}
2020-01-28 20:43:02 +01:00
2019-11-07 20:39:22 +01:00
head += `<meta name="twitter:image" content="${claimThumbnail}"/>`;
head += `<meta property="og:description" content="${cleanDescription}"/>`;
2019-11-07 20:39:22 +01:00
head += `<meta property="og:image" content="${claimThumbnail}"/>`;
head += `<meta property="og:locale" content="${claimLanguage}"/>`;
2020-07-06 23:39:35 +02:00
head += `<meta property="og:site_name" content="${SITE_NAME}"/>`;
2019-11-07 20:39:22 +01:00
head += `<meta property="og:type" content="website"/>`;
head += `<meta property="og:title" content="${title}"/>`;
2020-07-01 00:44:46 +02:00
head += `<meta name="twitter:title" content="${title}"/>`;
2019-11-07 20:39:22 +01:00
// below should be canonical_url, but not provided by chainquery yet
head += `<meta property="og:url" content="${URL}/${claim.name}:${claim.claim_id}"/>`;
2020-07-01 00:44:46 +02:00
head += `<meta name="twitter:url" content="${URL}/${claim.name}:${claim.claim_id}"/>`;
2020-07-06 23:39:35 +02:00
head += `<link rel="canonical" content="${SITE_CANONICAL_URL || URL}/${claim.name}:${claim.claim_id}"/>`;
2019-11-07 20:39:22 +01:00
if (
claim.source_media_type &&
(claim.source_media_type.startsWith('video/') || claim.source_media_type.startsWith('audio/'))
) {
2020-01-28 20:45:47 +01:00
const videoUrl = generateEmbedUrl(claim.name, claim.claim_id);
2019-11-07 20:39:22 +01:00
head += `<meta property="og:video" content="${videoUrl}" />`;
head += `<meta property="og:video:secure_url" content="${videoUrl}" />`;
head += `<meta property="og:video:type" content="${claim.source_media_type}" />`;
if (claim.channel) {
head += `<meta name="og:video:series" content="${claim.channel}"/>`;
}
2020-01-28 20:37:03 +01:00
head += `<meta name="twitter:card" content="player"/>`;
2020-01-28 20:45:47 +01:00
head += `<meta name="twitter:player" content="${videoUrl}" />`;
if (claim.release_time) {
var release = new Date(claim.release_time * 1000).toISOString();
head += `<meta property="og:video:release_date" content="${release}"/>`;
}
if (claim.duration) {
head += `<meta property="og:video:duration" content="${claim.duration}"/>`;
} else if (claim.audio_duration) {
head += `<meta property="og:video:duration" content="${claim.audio_duration}"/>`;
}
2019-11-07 20:39:22 +01:00
if (claim.frame_width && claim.frame_height) {
head += `<meta property="og:video:width" content="${claim.frame_width}"/>`;
head += `<meta property="og:video:height" content="${claim.frame_height}"/>`;
2020-01-28 19:00:45 +01:00
head += `<meta name="twitter:player:width" content="${claim.frame_width}">`;
head += `<meta name="twitter:player:height" content="${claim.frame_height}">`;
2019-11-07 20:39:22 +01:00
}
2020-01-28 20:43:02 +01:00
} else {
head += `<meta name="twitter:card" content="summary_large_image"/>`;
2019-11-07 20:39:22 +01:00
}
return head;
}
function buildGoogleVideoMetadata(claimUri, claim) {
if (!claim.source_media_type || !claim.source_media_type.startsWith('video/')) {
return '';
}
const { claimName } = parseURI(claimUri);
const claimTitle = escapeHtmlProperty(claim.title ? claim.title : claimName);
const claimDescription =
claim.description && claim.description.length > 0
? escapeHtmlProperty(truncateDescription(claim.description))
: `View ${claimTitle} on ${SITE_NAME}`;
const claimThumbnail = escapeHtmlProperty(claim.thumbnail_url) || OG_IMAGE_URL || `${URL}/public/v2-og.png`;
const releaseTime = claim.release_time || 0;
// https://developers.google.com/search/docs/data-types/video
const googleVideoMetadata = {
// --- Must ---
'@context': 'https://schema.org',
'@type': 'VideoObject',
name: `${claimTitle}`,
description: `${removeMd(claimDescription)}`,
thumbnailUrl: `${claimThumbnail}`,
uploadDate: `${new Date(releaseTime * 1000).toISOString()}`,
// --- Recommended ---
duration: claim.duration ? moment.duration(claim.duration * 1000).toISOString() : undefined,
contentUrl: generateDirectUrl(claim.name, claim.claim_id),
embedUrl: generateEmbedUrl(claim.name, claim.claim_id),
};
if (
!googleVideoMetadata.description.replace(/\s/g, '').length ||
googleVideoMetadata.thumbnailUrl.startsWith('data:image') ||
!googleVideoMetadata.thumbnailUrl.startsWith('http')
) {
return '';
}
return (
'<script type="application/ld+json">\n' + JSON.stringify(googleVideoMetadata, null, ' ') + '\n' + '</script>\n'
);
}
2020-12-23 03:37:55 +01:00
async function getClaimFromChainqueryOrRedirect(ctx, url, ignoreRedirect = false) {
const { isChannel, streamName, channelName, channelClaimId, streamClaimId } = parseURI(url);
const claimName = isChannel ? '@' + channelName : streamName;
const claimId = isChannel ? channelClaimId : streamClaimId;
const rows = await getClaim(claimName, claimId, channelName, channelClaimId);
if (rows && rows.length) {
const claim = rows[0];
2020-12-23 03:37:55 +01:00
if (claim.reposted_name && claim.reposted_claim_id && !ignoreRedirect) {
ctx.redirect(`/${claim.reposted_name}:${claim.reposted_claim_id}`);
return;
}
return claim;
}
return undefined;
}
let html;
2020-07-01 00:44:46 +02:00
async function getHtml(ctx) {
if (!html) {
html = fs.readFileSync(path.join(__dirname, '/../dist/index.html'), 'utf8');
}
2020-07-08 19:02:18 +02:00
const requestPath = decodeURIComponent(ctx.path);
2020-07-08 19:02:18 +02:00
if (requestPath.length === 0) {
2020-07-01 00:44:46 +02:00
const ogMetadata = buildBasicOgMetadata();
return insertToHead(html, ogMetadata);
2019-11-07 20:39:22 +01:00
}
const invitePath = `/$/${PAGES.INVITE}/`;
2020-01-28 19:00:45 +01:00
const embedPath = `/$/${PAGES.EMBED}/`;
2019-11-07 20:39:22 +01:00
2020-07-08 19:02:18 +02:00
if (requestPath.includes(invitePath)) {
2020-07-08 22:15:55 +02:00
const inviteChannel = requestPath.slice(invitePath.length).replace(/:/g, '#');
const inviteChannelUrl = `lbry://${inviteChannel}`;
try {
parseURI(inviteChannelUrl);
const claim = await getClaimFromChainqueryOrRedirect(ctx, inviteChannelUrl);
const invitePageMetadata = buildClaimOgMetadata(inviteChannelUrl, claim, {
2020-10-01 22:59:11 +02:00
title: `Join ${claim.name} on ${SITE_NAME}`,
description: `Join ${claim.name} on ${SITE_NAME}, a content wonderland owned by everyone (and no one).`,
});
return insertToHead(html, invitePageMetadata);
} catch (e) {
// Something about the invite channel is messed up
// Enter generic invite metadata
const invitePageMetadata = buildOgMetadata({
2020-10-01 22:59:11 +02:00
title: `Join a friend on ${SITE_NAME}`,
description: `Join a friend on ${SITE_NAME}, a content wonderland owned by everyone (and no one).`,
});
return insertToHead(html, invitePageMetadata);
}
}
2020-07-08 19:02:18 +02:00
if (requestPath.includes(embedPath)) {
const claimUri = requestPath.replace(embedPath, '').replace(/:/g, '#');
2020-12-23 03:37:55 +01:00
const claim = await getClaimFromChainqueryOrRedirect(ctx, claimUri, true);
2020-01-28 19:00:45 +01:00
if (claim) {
2020-01-28 20:45:47 +01:00
const ogMetadata = buildClaimOgMetadata(claimUri, claim);
const googleVideoMetadata = buildGoogleVideoMetadata(claimUri, claim);
return insertToHead(html, ogMetadata.concat('\n', googleVideoMetadata));
2020-01-28 19:00:45 +01:00
}
return insertToHead(html);
}
const categoryMeta = getCategoryMeta(requestPath);
if (categoryMeta) {
const categoryPageMetadata = buildOgMetadata({
title: categoryMeta.title,
description: categoryMeta.description,
image: categoryMeta.image,
});
return insertToHead(html, categoryPageMetadata);
}
2020-07-08 19:02:18 +02:00
if (!requestPath.includes('$')) {
const claimUri = requestPath.slice(1).replace(/:/g, '#');
const claim = await getClaimFromChainqueryOrRedirect(ctx, claimUri);
if (claim) {
const ogMetadata = buildClaimOgMetadata(claimUri, claim);
const googleVideoMetadata = buildGoogleVideoMetadata(claimUri, claim);
return insertToHead(html, ogMetadata.concat('\n', googleVideoMetadata));
}
2019-11-07 20:39:22 +01:00
}
2020-07-01 00:44:46 +02:00
const ogMetadata = buildBasicOgMetadata();
return insertToHead(html, ogMetadata);
}
2020-07-02 03:34:50 +02:00
module.exports = { insertToHead, buildBasicOgMetadataAndPWA, getHtml };