From c84d820b0914384ed85dd0b0603c66988a14c0c4 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Thu, 1 Jul 2021 09:24:02 +0800 Subject: [PATCH] RSS: Switch from ChainQuery to SDK ## Issue 3779 RSS feed for channels --- web/src/chainquery.js | 18 ---------- web/src/rss.js | 76 ++++++++++++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/web/src/chainquery.js b/web/src/chainquery.js index 686005dfc..bc2fd4a87 100644 --- a/web/src/chainquery.js +++ b/web/src/chainquery.js @@ -58,21 +58,3 @@ module.exports.getClaim = async function getClaim(claimName, claimId, channelNam return queryPool(sql, params); }; - -module.exports.getChannelClaim = async function getChannelClaim(channelClaimId) { - const params = []; - const select = ['claim_id', 'name', 'title', 'thumbnail_url', 'description'].join(', '); - - const sql = `SELECT ${select} FROM claim WHERE claim_id = "${channelClaimId}"`; - return queryPool(sql, params); -}; - -module.exports.getClaimsFromChannel = async function getClaimsFromChannel(channelClaimId, count = 10) { - const params = []; - const select = ['claim_id', 'name', 'title', 'thumbnail_url', 'description', 'created_at'].join(', '); - const sort = 'ORDER BY created_at DESC'; - const limit = `LIMIT ${count}`; - - const sql = `SELECT ${select} FROM claim WHERE publisher_id = "${channelClaimId}" ${sort} ${limit}`; - return queryPool(sql, params); -}; diff --git a/web/src/rss.js b/web/src/rss.js index 5c60893fa..0c25e0c13 100644 --- a/web/src/rss.js +++ b/web/src/rss.js @@ -1,27 +1,56 @@ -const { URL, SITE_NAME } = require('../../config.js'); -const { getChannelClaim, getClaimsFromChannel } = require('./chainquery'); +const { URL, SITE_NAME, LBRY_WEB_API } = require('../../config.js'); +const { Lbry } = require('lbry-redux'); const Feed = require('feed').Feed; -async function getChannelClaimFromChainquery(claimId) { - const rows = await getChannelClaim(claimId); - if (rows && rows.length) { - const claim = rows[0]; - return claim; - } +const SDK_API_PATH = `${LBRY_WEB_API}/api/v1`; +const proxyURL = `${SDK_API_PATH}/proxy`; +Lbry.setDaemonConnectionString(proxyURL); - return undefined; +async function doClaimSearch(options) { + let results; + try { + results = await Lbry.claim_search(options); + } catch {} + return results ? results.items : undefined; +} + +async function getChannelClaim(claimId) { + const options = { + claim_ids: [claimId], + page_size: 1, + no_totals: true, + }; + + const claims = await doClaimSearch(options); + return claims ? claims[0] : undefined; +} + +async function getClaimsFromChannel(claimId, count) { + const options = { + channel_ids: [claimId], + page_size: count, + has_source: true, + claim_type: 'stream', + order_by: ['creation_timestamp'], + no_totals: true, + }; + + return await doClaimSearch(options); } async function getFeed(channelClaim) { const replaceLineFeeds = (str) => str.replace(/(?:\r\n|\r|\n)/g, '
'); + const value = channelClaim.value; + const title = value ? value.title : channelClaim.name; + const options = { - title: channelClaim.title + ' on ' + SITE_NAME, - description: channelClaim.description ? replaceLineFeeds(channelClaim.description) : '', + title: title + ' on ' + SITE_NAME, + description: value ? replaceLineFeeds(value.description) : '', link: `${URL}/${channelClaim.name}:${channelClaim.claim_id}`, favicon: URL + '/public/favicon.png', generator: SITE_NAME + ' RSS Feed', - image: channelClaim.thumbnail_url, + image: value ? value.thumbnail.url : '', author: { name: channelClaim.name, link: URL + '/' + channelClaim.name + ':' + channelClaim.claim_id, @@ -30,17 +59,20 @@ async function getFeed(channelClaim) { const feed = new Feed(options); - const latestClaims = await getClaimsFromChannel(channelClaim.claim_id, 10); + const latestClaims = await getClaimsFromChannel(channelClaim.claim_id, 50); latestClaims.forEach((c) => { + const meta = c.meta; + const value = c.value; + feed.addItem({ guid: c.claim_id, id: c.claim_id, - title: c.title, - description: c.description ? replaceLineFeeds(c.description) : '', - image: c.thumbnail_url, + title: value ? value.title : c.name, + description: value ? replaceLineFeeds(value.description) : '', + image: value ? value.thumbnail.url : '', link: URL + '/' + c.name + ':' + c.claim_id, - date: new Date(c.created_at), + date: new Date(meta ? meta.creation_timestamp * 1000 : null), }); }); @@ -52,13 +84,13 @@ async function getRss(ctx) { return 'Invalid URL'; } - const channelClaim = await getChannelClaimFromChainquery(ctx.params.claimId); - if (channelClaim) { - const feed = await getFeed(channelClaim); - return feed.rss2(); + const channelClaim = await getChannelClaim(ctx.params.claimId); + if (typeof channelClaim === 'string' || !channelClaim) { + return channelClaim; } - return 'Invalid channel'; + const feed = await getFeed(channelClaim); + return feed.rss2(); } module.exports = { getRss };