Files
NodeBB/src/routes/feeds.js

202 lines
5.6 KiB
JavaScript
Raw Normal View History

(function (Feeds) {
var posts = require('../posts'),
topics = require('../topics'),
categories = require('../categories'),
2013-11-22 11:42:42 -05:00
2013-09-03 23:18:25 -04:00
rss = require('rss'),
2013-10-15 14:44:42 -04:00
nconf = require('nconf'),
2013-05-27 14:55:07 -04:00
ThreadTools = require('../threadTools'),
CategoryTools = require('../categoryTools');
Feeds.createRoutes = function(app){
app.get('/topic/:topic_id.rss', hasTopicPrivileges, generateForTopic);
app.get('/category/:category_id.rss', hasCategoryPrivileges, generateForCategory);
app.get('/recent.rss', generateForRecent);
app.get('/popular.rss', generateForPopular);
2013-09-03 23:18:25 -04:00
};
function hasTopicPrivileges(req, res, next) {
var tid = req.params.topic_id;
var uid = req.user ? req.user.uid || 0 : 0;
ThreadTools.privileges(tid, uid, function(err, privileges) {
if(err) {
return next(err);
}
if(!privileges.read) {
return res.redirect('403');
}
return next();
});
}
function hasCategoryPrivileges(req, res, next) {
var cid = req.params.category_id;
var uid = req.user ? req.user.uid || 0 : 0;
CategoryTools.privileges(cid, uid, function(err, privileges) {
if(err) {
return next(err);
}
if(!privileges.read) {
return res.redirect('403');
}
return next();
});
}
function generateForTopic(req, res, next) {
2014-02-09 18:49:34 -05:00
var tid = req.params.topic_id;
2014-02-26 16:43:21 -05:00
topics.getTopicWithPosts(tid, 0, 0, 25, function (err, topicData) {
if (err) {
return next(err);
}
2013-09-03 23:18:25 -04:00
var description = topicData.posts.length ? topicData.posts[0].content : '';
var image_url = topicData.posts.length ? topicData.posts[0].picture : '';
var author = topicData.posts.length ? topicData.posts[0].username : '';
2013-09-17 13:09:37 -04:00
var feed = new rss({
2014-02-26 16:43:21 -05:00
title: topicData.title,
description: description,
feed_url: nconf.get('url') + '/topic/' + tid + '.rss',
2014-01-22 14:04:04 -05:00
site_url: nconf.get('url') + '/topic/' + topicData.slug,
image_url: image_url,
author: author,
ttl: 60
2013-11-22 11:42:42 -05:00
}),
dateStamp;
2013-07-02 19:02:23 -04:00
2013-09-10 11:25:19 -04:00
// Add pubDate if topic contains posts
if (topicData.posts.length > 0) {
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
}
2013-09-10 11:25:19 -04:00
topicData.posts.forEach(function(postData) {
2013-12-05 13:11:27 -05:00
if (parseInt(postData.deleted, 10) === 0) {
dateStamp = new Date(parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)).toUTCString();
2013-09-03 23:18:25 -04:00
feed.item({
2014-02-26 16:43:21 -05:00
title: 'Reply to ' + topicData.title + ' on ' + dateStamp,
description: postData.content,
2014-01-22 14:04:04 -05:00
url: nconf.get('url') + '/topic/' + topicData.slug + '#' + postData.pid,
author: postData.username,
date: dateStamp
});
}
});
var xml = feed.xml();
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
2013-05-27 14:55:07 -04:00
});
};
function generateForCategory(req, res, next) {
var cid = req.params.category_id;
categories.getCategoryById(cid, 0, 25, 0, function (err, categoryData) {
if (err) {
return next(err);
}
2013-09-03 23:18:25 -04:00
2013-09-17 13:09:37 -04:00
var feed = new rss({
2014-02-26 16:43:21 -05:00
title: categoryData.name,
description: categoryData.description,
feed_url: nconf.get('url') + '/category/' + cid + '.rss',
2014-02-26 16:43:21 -05:00
site_url: nconf.get('url') + '/category/' + categoryData.cid,
ttl: 60
2013-11-22 11:42:42 -05:00
});
2013-09-10 11:25:19 -04:00
// Add pubDate if category has topics
if (categoryData.topics.length > 0) feed.pubDate = new Date(parseInt(categoryData.topics[0].lastposttime, 10)).toUTCString();
categoryData.topics.forEach(function(topicData) {
2013-09-03 23:18:25 -04:00
feed.item({
2013-11-22 11:42:42 -05:00
title: topicData.title,
2014-01-22 14:04:04 -05:00
url: nconf.get('url') + '/topic/' + topicData.slug,
2013-09-03 23:18:25 -04:00
author: topicData.username,
2013-11-22 11:42:42 -05:00
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
2013-09-03 23:18:25 -04:00
});
});
var xml = feed.xml();
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
});
2013-11-22 11:42:42 -05:00
};
2013-05-27 14:55:07 -04:00
function generateForRecent(req, res, next) {
topics.getLatestTopics(0, 0, 19, 'month', function (err, recentData) {
if(err){
return next(err);
}
2014-02-09 17:14:56 -05:00
2013-11-22 11:42:42 -05:00
var feed = new rss({
title: 'Recently Active Topics',
description: 'A list of topics that have been active within the past 24 hours',
feed_url: nconf.get('url') + '/recent.rss',
2014-01-22 14:04:04 -05:00
site_url: nconf.get('url') + '/recent',
ttl: 60
2013-11-22 11:42:42 -05:00
});
// Add pubDate if recent topics list contains topics
if (recentData.topics.length > 0) {
feed.pubDate = new Date(parseInt(recentData.topics[0].lastposttime, 10)).toUTCString();
}
recentData.topics.forEach(function(topicData) {
2013-11-22 11:42:42 -05:00
feed.item({
title: topicData.title,
2014-01-22 14:04:04 -05:00
url: nconf.get('url') + '/topic/' + topicData.slug,
2013-11-22 11:42:42 -05:00
author: topicData.username,
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
});
});
var xml = feed.xml();
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
2013-11-22 11:42:42 -05:00
});
2013-05-27 14:55:07 -04:00
};
2014-01-31 11:16:23 -05:00
function generateForPopular(req, res, next) {
2014-01-31 11:16:23 -05:00
topics.getTopicsFromSet(0, 'topics:posts', 0, 19, function (err, popularData) {
if(err){
return next(err);
}
2014-01-31 11:16:23 -05:00
var feed = new rss({
title: 'Popular Topics',
description: 'A list of topics that are sorted by post count',
feed_url: nconf.get('url') + '/popular.rss',
2014-01-31 11:16:23 -05:00
site_url: nconf.get('url') + '/popular',
ttl: 60
2014-01-31 11:16:23 -05:00
});
// Add pubDate if recent topics list contains topics
if (popularData.topics.length > 0) {
feed.pubDate = new Date(parseInt(popularData.topics[0].lastposttime, 10)).toUTCString();
}
2014-02-09 17:14:31 -05:00
popularData.topics.forEach(function(topicData) {
2014-01-31 11:16:23 -05:00
feed.item({
title: topicData.title,
url: nconf.get('url') + '/topic/' + topicData.slug,
author: topicData.username,
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
});
});
var xml = feed.xml();
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
2014-01-31 11:16:23 -05:00
});
};
}(exports));