From e35dd60f8f4b2d2cec052e4a984a106f0f319320 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 6 Oct 2014 13:43:41 -0400 Subject: [PATCH] closes #1940 --- src/routes/feeds.js | 90 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 8 deletions(-) diff --git a/src/routes/feeds.js b/src/routes/feeds.js index 3f6cc0cca1..1c681c6354 100644 --- a/src/routes/feeds.js +++ b/src/routes/feeds.js @@ -1,13 +1,13 @@ "use strict"; -var posts = require('./../posts'), - topics = require('./../topics'), - categories = require('./../categories'), - meta = require('./../meta'), - +var async = require('async'), rss = require('rss'), nconf = require('nconf'), + posts = require('../posts'), + topics = require('../topics'), + categories = require('../categories'), + meta = require('../meta'), privileges = require('../privileges'); function hasTopicPrivileges(req, res, next) { @@ -31,7 +31,7 @@ function hasPrivileges(method, id, req, res, next) { } if (!canRead) { - return res.redirect(nconf.get('relative_path') + '/403') + return res.redirect(nconf.get('relative_path') + '/403'); } return next(); @@ -53,7 +53,7 @@ function generateForTopic(req, res, next) { } if (topicData.deleted && !userPrivileges.view_deleted) { - return res.redirect(nconf.get('relative_path') + '/404') + return res.redirect(nconf.get('relative_path') + '/404'); } var description = topicData.posts.length ? topicData.posts[0].content : ''; @@ -133,7 +133,7 @@ function generateForPopular(req, res, next) { function disabledRSS(req, res, next) { if (meta.config['feeds:disableRSS'] === '1') { - return res.redirect(nconf.get('relative_path') + '/404') + return res.redirect(nconf.get('relative_path') + '/404'); } next(); @@ -176,6 +176,78 @@ function generateTopicsFeed(feedOptions, topics) { return feed; } +function generateForRecentPosts(req, res, next) { + var uid = req.user ? req.user.uid : 0; + posts.getRecentPosts(uid, 0, 19, 'month', function(err, posts) { + if (err) { + return next(err); + } + + var feed = generateForPostsFeed({ + title: 'Recent Posts', + description: 'A list of recent posts', + feed_url: '/recentposts.rss', + site_url: '/recentposts' + }, posts); + + sendFeed(feed, res); + }); +} + +function generateForCategoryRecentPosts(req, res, next) { + var uid = req.user ? req.user.uid : 0; + var cid = req.params.category_id; + + async.parallel({ + category: function(next) { + categories.getCategoryData(cid, next); + }, + posts: function(next) { + categories.getRecentReplies(cid, uid, 20, next); + } + }, function(err, results) { + if (err) { + return next(err); + } + + var category = results.category; + var posts = results.posts; + + var feed = generateForPostsFeed({ + title: category.name + ' Recent Posts', + description: 'A list of recent posts from ' + category.name, + feed_url: '/category/' + cid + '/recentposts.rss', + site_url: '/category/' + cid + '/recentposts' + }, posts); + + sendFeed(feed, res); + }); +} + +function generateForPostsFeed(feedOptions, posts) { + feedOptions.ttl = 60; + feedOptions.feed_url = nconf.get('url') + feedOptions.feed_url; + feedOptions.site_url = nconf.get('url') + feedOptions.site_url; + + var feed = new rss(feedOptions); + + if (posts.length > 0) { + feed.pubDate = new Date(parseInt(posts[0].timestamp, 10)).toUTCString(); + } + + posts.forEach(function(postData) { + feed.item({ + title: postData.topic ? postData.topic.title : '', + description: postData.content, + url: nconf.get('url') + '/topic/' + (postData.topic ? postData.topic.slug : '#') + '/'+postData.index, + author: postData.user ? postData.user.username : '', + date: new Date(parseInt(postData.timestamp, 10)).toUTCString() + }); + }); + + return feed; +} + function sendFeed(feed, res) { var xml = feed.xml(); res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml); @@ -186,4 +258,6 @@ module.exports = function(app, middleware, controllers){ app.get('/category/:category_id.rss', hasCategoryPrivileges, disabledRSS, generateForCategory); app.get('/recent.rss', disabledRSS, generateForRecent); app.get('/popular.rss', disabledRSS, generateForPopular); + app.get('/recentposts.rss', disabledRSS, generateForRecentPosts); + app.get('/category/:category_id/recentposts.rss', hasCategoryPrivileges, disabledRSS, generateForCategoryRecentPosts); };