RSS: Switch from ChainQuery to SDK

## Issue
3779 RSS feed for channels
This commit is contained in:
infinite-persistence 2021-07-01 09:24:02 +08:00
parent 49046c9d25
commit c84d820b09
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
2 changed files with 54 additions and 40 deletions

View file

@ -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);
};

View file

@ -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, '<br>');
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 };