const { URL } = require('../../config.js');
const { generateEmbedUrl, generateStreamUrl } = require('../../ui/util/lbrytv');
const PAGES = require('../../ui/constants/pages');
const { getClaim } = require('./chainquery');
const { parseURI } = require('lbry-redux');
const fs = require('fs');
const path = require('path');
let html = fs.readFileSync(path.join(__dirname, '/../dist/index.html'), 'utf8');
function insertToHead(fullHtml, htmlToInsert) {
return fullHtml.replace(
/.*/s,
htmlToInsert || buildOgMetadata()
);
}
function truncateDescription(description) {
return description.length > 200 ? description.substr(0, 200) + '...' : description;
}
function escapeHtmlProperty(property) {
return property
? String(property)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
: '';
}
//
// Normal metadata with option to override certain values
//
function buildOgMetadata(overrideOptions = {}) {
const { title, description } = overrideOptions;
const head =
'
lbry.tv\n' +
`\n` +
`\n` +
'\n' +
`\n` +
`\n` +
'\n' +
`\n` +
'';
return head;
}
//
// Metadata used for urls that need claim information
// Also has option to override defaults
//
function buildClaimOgMetadata(uri, claim, overrideOptions = {}) {
// Initial setup for claim based og metadata
const { claimName } = parseURI(uri);
const claimTitle = escapeHtmlProperty(claim.title ? claim.title : claimName);
const claimDescription =
claim.description && claim.description.length > 0
? escapeHtmlProperty(truncateDescription(claim.description))
: `View ${claimTitle} on lbry.tv`;
const claimLanguage = escapeHtmlProperty(claim.language) || 'en_US';
const isImage = claim && claim.value && claim.value.stream_type === 'image';
const isFree = claim && claim.value && (!claim.value.fee || Number(claim.value.fee.amount) <= 0);
let imageThumbnail;
if (claim && isImage && isFree) {
imageThumbnail = generateStreamUrl(claimName, claim.claim_id);
}
const claimThumbnail = escapeHtmlProperty(claim.thumbnail_url) || imageThumbnail || `${URL}/v1-og.png`;
// Allow for ovverriding default claim based og metadata
const title = overrideOptions.title || claimTitle;
const description = overrideOptions.description || claimDescription;
let head = '';
head += '';
head += `${title}`;
head += ``;
if (claim.tags) {
head += ``;
}
head += ``;
head += ``;
head += ``;
head += ``;
head += ``;
head += ``;
head += ``;
// below should be canonical_url, but not provided by chainquery yet
head += ``;
if (
claim.source_media_type &&
(claim.source_media_type.startsWith('video/') || claim.source_media_type.startsWith('audio/'))
) {
const videoUrl = generateEmbedUrl(claim.name, claim.claim_id);
head += ``;
head += ``;
head += ``;
head += ``;
head += ``;
if (claim.frame_width && claim.frame_height) {
head += ``;
head += ``;
head += ``;
head += ``;
}
} else {
head += ``;
}
return head;
}
async function getClaimFromChainquery(url) {
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];
return claim;
}
return undefined;
}
module.exports.getHtml = async function getHtml(ctx) {
const path = decodeURIComponent(ctx.path);
if (path.length === 0) {
return insertToHead(html);
}
const invitePath = `/$/${PAGES.INVITE}/`;
const embedPath = `/$/${PAGES.EMBED}/`;
if (path.includes(invitePath)) {
const inviteChannel = path.slice(invitePath.length).replace(/:/g, '#');
const inviteChannelUrl = `lbry://${inviteChannel}`;
try {
parseURI(inviteChannelUrl);
const claim = await getClaimFromChainquery(inviteChannelUrl);
const invitePageMetadata = buildClaimOgMetadata(inviteChannelUrl, claim, {
title: `Join ${claim.name} on LBRY`,
description: `Join ${claim.name} on LBRY, 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({
title: `Join a friend on LBRY`,
description: `Join a friend on LBRY, a content wonderland owned by everyone (and no one).`,
});
return insertToHead(html, invitePageMetadata);
}
}
if (path.includes(embedPath)) {
const claimUri = path.replace(embedPath, '').replace(/:/g, '#');
const claim = await getClaimFromChainquery(claimUri);
if (claim) {
const ogMetadata = buildClaimOgMetadata(claimUri, claim);
return insertToHead(html, ogMetadata);
}
return insertToHead(html);
}
if (!path.includes('$')) {
const claimUri = path.slice(1).replace(/:/g, '#');
const claim = await getClaimFromChainquery(claimUri);
if (claim) {
const ogMetadata = buildClaimOgMetadata(claimUri, claim);
return insertToHead(html, ogMetadata);
}
}
return insertToHead(html);
};