diff --git a/src/feed.js b/src/feed.js index 0b8fe46f49..551d6ee5aa 100644 --- a/src/feed.js +++ b/src/feed.js @@ -159,4 +159,49 @@ }); }); }; + + Feed.updatePopular = function(callback) { + topics.getTopicsFromSet(0, 'topics:posts', 0, 19, function (err, popularData) { + var feed = new rss({ + title: 'Popular Topics', + description: 'A list of topics that are sorted by post count', + feed_url: Feed.defaults.baseUrl + '/popular.rss', + site_url: nconf.get('url') + '/popular', + ttl: Feed.defaults.ttl + }); + + // Add pubDate if recent topics list contains topics + if (popularData.topics.length > 0) { + feed.pubDate = new Date(parseInt(popularData.topics[0].lastposttime, 10)).toUTCString(); + } + + async.eachSeries(popularData.topics, function(topicData, next) { + feed.item({ + title: topicData.title, + url: nconf.get('url') + '/topic/' + topicData.slug, + author: topicData.username, + date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString() + }); + next(); + }, function() { + Feed.saveFeed('feeds/popular.rss', feed, function (err) { + if (process.env.NODE_ENV === 'development') { + winston.info('[rss] Re-generated "popular posts" RSS Feed.'); + } + + if (callback) callback(); + }); + }); + }); + }; + + Feed.loadFeed = function(rssPath, res) { + fs.readFile(rssPath, function (err, data) { + if (err) { + res.type('text').send(404, "Unable to locate an rss feed at this location."); + } else { + res.type('xml').set('Content-Length', data.length).send(data); + } + }); + }; }(exports)); \ No newline at end of file diff --git a/src/webserver.js b/src/webserver.js index 5c3a5874dc..b47340a085 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -832,31 +832,34 @@ module.exports.server = server; }); app.get('/recent.rss', function(req, res) { - var rssPath = path.join(__dirname, '../', 'feeds/recent.rss'), - loadFeed = function () { - fs.readFile(rssPath, function (err, data) { - if (err) { - res.type('text').send(404, "Unable to locate an rss feed at this location."); - } else { - res.type('xml').set('Content-Length', data.length).send(data); - } - }); - - }; + var rssPath = path.join(__dirname, '../', 'feeds/recent.rss'); if (!fs.existsSync(rssPath)) { feed.updateRecent(function (err) { if (err) { res.redirect('/404'); } else { - loadFeed(); + feed.loadFeed(rssPath, res); } }); } else { - loadFeed(); + feed.loadFeed(rssPath, res); } }); + app.get('/popular.rss', function(req, res) { + var rssPath = path.join(__dirname, '../', 'feeds/popular.rss'); + + feed.updatePopular(function (err) { + if (err) { + res.redirect('/404'); + } else { + feed.loadFeed(rssPath, res); + } + }); + + }); + app.get('/recent/:term?', function (req, res) { // TODO consolidate with /recent route as well -> that can be combined into this area. See "Basic Routes" near top. app.build_header({