From 6a94a7728130cc2fe4a820de705d64e43d6038e5 Mon Sep 17 00:00:00 2001 From: eletroswing Date: Tue, 12 Dec 2023 13:12:11 -0300 Subject: [PATCH] feat(rss): adding rss to relevant route adding rss to relevant route and changing default path to starts with rss --- models/content.js | 21 +++++++++++++++++++-- models/rss.js | 6 ++---- next.config.js | 10 +++++++++- pages/api/v1/contents/rss/index.public.js | 10 +++++----- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/models/content.js b/models/content.js index 72904060a..aa2011947 100644 --- a/models/content.js +++ b/models/content.js @@ -193,18 +193,19 @@ async function findWithStrategy(options = {}) { new: getNew, old: getOld, relevant: getRelevant, + relevant_rss: getRelevantRss, }; return await strategies[options.strategy](options); async function getNew(options = {}) { const results = {}; - + options.order = 'published_at DESC'; results.rows = await findAll(options); options.totalRows = results.rows[0]?.total_rows; results.pagination = await getPagination(options); - + return results; } @@ -241,6 +242,22 @@ async function findWithStrategy(options = {}) { return results; } + + async function getRelevantRss(values = {}) { + const results = {}; + const options = {}; + + values.order = 'published_at DESC'; + + const contentList = await findAll(values, options); + + results.rows = rankContentListByRelevance(contentList); + + values.totalRows = results.rows[0]?.total_rows; + results.pagination = await getPagination(values, options); + + return results; + } } async function getPagination(values, options = {}) { diff --git a/models/rss.js b/models/rss.js index 4af78e2e3..0878c3f8b 100644 --- a/models/rss.js +++ b/models/rss.js @@ -5,12 +5,10 @@ import { Viewer } from '@/TabNewsUI'; import webserver from 'infra/webserver.js'; import removeMarkdown from 'models/remove-markdown'; -function generateRss2(contentList) { +function generateRss2(contentList, path) { const webserverHost = webserver.host; - // TODO: make this property flexible in the future to - // support things like: `/[username]/rss` - const feedURL = `${webserverHost}/recentes/rss`; + const feedURL = `${webserverHost}/${path}`; const feed = new Feed({ title: 'TabNews', diff --git a/next.config.js b/next.config.js index 53e5c0319..58c65d096 100644 --- a/next.config.js +++ b/next.config.js @@ -27,7 +27,15 @@ module.exports = { return [ { source: '/recentes/rss', - destination: '/api/v1/contents/rss', + destination: '/api/v1/contents/rss?strategy=new', + }, + { + source: '/rss/recentes', + destination: '/api/v1/contents/rss?strategy=new', + }, + { + source: '/rss/relevantes', + destination: '/api/v1/contents/rss?strategy=relevant_rss', }, ]; }, diff --git a/pages/api/v1/contents/rss/index.public.js b/pages/api/v1/contents/rss/index.public.js index 4c30a72b8..b94117ce7 100644 --- a/pages/api/v1/contents/rss/index.public.js +++ b/pages/api/v1/contents/rss/index.public.js @@ -19,21 +19,21 @@ export default nextConnect({ async function handleRequest(request, response) { const userTryingToList = user.createAnonymous(); - + const results = await content.findWithStrategy({ - strategy: 'new', + strategy: request.query.strategy, where: { parent_id: null, status: 'published', }, - page: 1, - per_page: 30, + page: request.query.page, + per_page: request.query.per_page, }); const contentListFound = results.rows; const secureContentListFound = authorization.filterOutput(userTryingToList, 'read:content:list', contentListFound); - const rss2 = rss.generateRss2(secureContentListFound); + const rss2 = rss.generateRss2(secureContentListFound, request.params.wild); response.setHeader('Content-Type', 'text/xml; charset=utf-8'); response.status(200).send(rss2);