Skip to content

Commit

Permalink
RSS for users and tags added.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpox committed May 10, 2019
1 parent 05e91a1 commit aca4b81
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
95 changes: 94 additions & 1 deletion app/controller/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import RssGen from '../rss';

import Post from '../model/post';

import Tag from '../model/tag';

import User from '../model/user';

class Rss {
async index(req, res) {
try {
Expand All @@ -11,7 +15,10 @@ class Rss {
* Constructing the feed.
*/

const feed = new RssGen(`${process.env.SITE_DOMAIN}/rss`);
const feed = new RssGen(
`RSS feed for ${process.env.SITE_NAME}`,
`${process.env.SITE_DOMAIN}/rss`,
);
let lastUpdate = null;

latest.forEach((post, index) => {
Expand All @@ -34,6 +41,92 @@ class Rss {
req.log.error(err);
}
}

async user(req, res) {
try {
// const latest = await Post.find({ created_by: req.params.user }).sort('createdAt').limit(10);
const fields = ['username', 'email', 'about', 'created_at', 'avatar', '_id'];
const user = await User
.findOne({ username: req.params.user })
.select(fields.join(' '))
.populate('posts');
if (user) {
const latest = await Post.find({ hidden: false, created_by: user._id }).sort('-createdAt').limit(10);

/*
* Constructing the feed.
*/
const feed = new RssGen(
`User RSS feed for ${user.username}`,
`${process.env.SITE_DOMAIN}/rss/user/${user.username}`,
);
let lastUpdate = null;

latest.forEach((post, index) => {
lastUpdate = (lastUpdate == null) ? post.createdAt : lastUpdate;
feed.item({
title: post.title,
description: post.no_tags_short,
link: `${process.env.SITE_DOMAIN}/post/${post.url}`,
guid: `${process.env.SITE_DOMAIN}/post/${post.url}`,
});
});
// res.json(latest);

/*
* Setting header and replying the request.
*/
res.setHeader('Content-Type', 'application/rss+xml');
res.send(feed.output());
} else {
res.redirect('/');
}
} catch (err) {
req.log.error(err);
}
}

async tag(req, res) {
try {
// const latest = await Post.find({ created_by: req.params.user }).sort('createdAt').limit(10);
const tag = await Tag
.findOne({ url: req.params.tag });
if (tag) {
const latest = await Post.find({ hidden: false, tag: tag._id }).sort('-createdAt').limit(10);

/*
* Constructing the feed.
*/
const feed = new RssGen(
`Tag RSS feed for ${tag.title}`,
`${process.env.SITE_DOMAIN}/rss/tag/${tag.title}`,
);

let lastUpdate = null;

latest.forEach((post, index) => {
lastUpdate = (lastUpdate == null) ? post.createdAt : lastUpdate;
feed.item({
title: post.title,
description: post.no_tags_short,
link: `${process.env.SITE_DOMAIN}/post/${post.url}`,
guid: `${process.env.SITE_DOMAIN}/post/${post.url}`,
});
});
// res.json(latest);

/*
* Setting header and replying the request.
*/
res.setHeader('Content-Type', 'application/rss+xml');
res.send(feed.output());
} else {
res.redirect('/');
}
} catch (err) {
req.log.error(err);
}
}
}

export default Rss;
2 changes: 2 additions & 0 deletions app/route/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ module.exports = (app) => {
const RssController = new Rss();

app.get('/rss', RssController.index);
app.get('/rss/user/:user', RssController.user);
app.get('/rss/tag/:tag', RssController.tag);
};
5 changes: 3 additions & 2 deletions app/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class Rss {
/*
* url = URL of the RSS feed.
*/
constructor(url) {
constructor(description, url) {
this.description = description;
this.url = url;
this.items = '';
}
Expand All @@ -27,7 +28,7 @@ class Rss {
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${process.env.SITE_NAME}</title>
<description>${process.env.SITE_NAME}</description>
<description>${this.description}</description>
<link>${process.env.SITE_DOMAIN}</link>
<atom:link href="${this.url}" rel="self" type="application/rss+xml" />
${this.items}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ribbon",
"version": "2019.5.10",
"version": "2019.5.11",
"description": "A platform to share your stories.",
"main": "dist/ribbon.js",
"scripts": {
Expand Down

0 comments on commit aca4b81

Please sign in to comment.