Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adicionando rss para as rotas da api #1566

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions models/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 = {}) {
Expand Down
6 changes: 2 additions & 4 deletions models/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 9 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];
},
Expand Down
10 changes: 5 additions & 5 deletions pages/api/v1/contents/rss/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading