Homepage API v2: includes meme

Ticket: 1318

Had to create v2 as there is no backward-compatible location in the v1 json to place it.

v2 allows any future expansion beyond 'categories' and 'meme'.

One-meme-per-homepage is also supported, but not enabled for now.
This commit is contained in:
infinite-persistence 2022-04-15 20:35:10 +08:00 committed by Thomas Zarebczan
parent 6e34282cdc
commit 45bb9ad0fa
4 changed files with 62 additions and 31 deletions

View file

@ -1,13 +1,39 @@
const memo = {};
// this didn't seem to help.
if (!memo.homepageData) {
try {
memo.homepageData = require('../../custom/homepages/v2');
memo.meme = require('../../custom/homepages/meme');
} catch (err) {
console.log('homepage data failed');
console.log('getHomepageJSON:', err);
}
}
const getHomepageJSON = () => {
const getHomepageJsonV1 = () => {
return memo.homepageData || {};
};
module.exports = { getHomepageJSON };
const getHomepageJsonV2 = () => {
if (!memo.homepageData) {
return {};
}
const v2 = {};
const homepageKeys = Object.keys(memo.homepageData);
homepageKeys.forEach((hp) => {
v2[hp] = {
categories: memo.homepageData[hp],
};
});
if (memo.meme && v2['en']) {
// Only supporting English memes for now, but one-per-homepage is possible.
v2['en'].meme = memo.meme;
}
return v2;
};
module.exports = { getHomepageJsonV1, getHomepageJsonV2 };

27
web/src/homepageApi.js Normal file
View file

@ -0,0 +1,27 @@
const { CUSTOM_HOMEPAGE } = require('../../config');
const { getHomepageJsonV1, getHomepageJsonV2 } = require('./getHomepageJSON');
async function getHomepage(ctx, version) {
if (!CUSTOM_HOMEPAGE) {
ctx.status = 404;
ctx.body = { message: 'Not Found' };
return;
}
try {
const content = version === 1 ? getHomepageJsonV1() : getHomepageJsonV2();
ctx.set('Content-Type', 'application/json');
ctx.set('Access-Control-Allow-Origin', '*');
ctx.body = {
status: 'success',
data: content,
};
} catch (err) {
ctx.status = err.statusCode || err.status || 500;
ctx.body = {
message: err.message,
};
}
}
module.exports = { getHomepage };

View file

@ -22,7 +22,7 @@ const {
} = require('../../ui/util/web');
const { getJsBundleId } = require('../bundle-id.js');
const { lbryProxy: Lbry } = require('../lbry');
const { getHomepageJSON } = require('./getHomepageJSON');
const { getHomepageJsonV1 } = require('./getHomepageJSON');
const { buildURI, parseURI, normalizeClaimUrl } = require('./lbryURI');
const fs = require('fs');
const moment = require('moment');
@ -61,7 +61,7 @@ function truncateDescription(description, maxChars = 200) {
}
function getCategoryMeta(path) {
const homepage = getHomepageJSON();
const homepage = getHomepageJsonV1();
if (path === `/$/${PAGES.WILD_WEST}` || path === `/$/${PAGES.WILD_WEST}/`) {
return {

View file

@ -1,6 +1,4 @@
const { CUSTOM_HOMEPAGE } = require('../../config.js');
const { generateStreamUrl } = require('../../ui/util/web');
const { getHomepageJSON } = require('./getHomepageJSON');
const { getHtml } = require('./html');
const { getOEmbed } = require('./oEmbed');
const { getRss } = require('./rss');
@ -8,6 +6,7 @@ const { getTempFile } = require('./tempfile');
const fetch = require('node-fetch');
const Router = require('@koa/router');
const { getHomepage } = require('./homepageApi');
// So any code from 'lbry-redux'/'lbryinc' that uses `fetch` can be run on the server
global.fetch = fetch;
@ -39,30 +38,9 @@ const tempfileMiddleware = async (ctx) => {
ctx.body = temp;
};
router.get(`/$/api/content/v1/get`, async (ctx) => {
if (!CUSTOM_HOMEPAGE) {
ctx.status = 404;
ctx.body = {
message: 'Not Found',
};
} else {
let content;
try {
content = getHomepageJSON();
ctx.set('Content-Type', 'application/json');
ctx.set('Access-Control-Allow-Origin', '*');
ctx.body = {
status: 'success',
data: content,
};
} catch (err) {
ctx.status = err.statusCode || err.status || 500;
ctx.body = {
message: err.message,
};
}
}
});
router.get(`/$/api/content/v1/get`, async (ctx) => getHomepage(ctx, 1));
router.get(`/$/api/content/v2/get`, async (ctx) => getHomepage(ctx, 2));
router.get(`/$/download/:claimName/:claimId`, async (ctx) => {
const streamUrl = getStreamUrl(ctx);