mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-08 15:05:46 +01:00
refactored feeds
This commit is contained in:
@@ -44,7 +44,6 @@ function hasCategoryPrivileges(req, res, next) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function generateForTopic(req, res, next) {
|
function generateForTopic(req, res, next) {
|
||||||
var tid = req.params.topic_id;
|
var tid = req.params.topic_id;
|
||||||
|
|
||||||
@@ -68,7 +67,6 @@ function generateForTopic(req, res, next) {
|
|||||||
}),
|
}),
|
||||||
dateStamp;
|
dateStamp;
|
||||||
|
|
||||||
// Add pubDate if topic contains posts
|
|
||||||
if (topicData.posts.length > 0) {
|
if (topicData.posts.length > 0) {
|
||||||
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
|
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
|
||||||
}
|
}
|
||||||
@@ -87,10 +85,8 @@ function generateForTopic(req, res, next) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var xml = feed.xml();
|
sendFeed(feed, res);
|
||||||
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForCategory(req, res, next) {
|
function generateForCategory(req, res, next) {
|
||||||
@@ -101,86 +97,61 @@ function generateForCategory(req, res, next) {
|
|||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
var feed = new rss({
|
var feed = generateTopicFeed({
|
||||||
title: categoryData.name,
|
title: categoryData.name,
|
||||||
description: categoryData.description,
|
description: categoryData.description,
|
||||||
feed_url: nconf.get('url') + '/category/' + cid + '.rss',
|
feed_url: nconf.get('url') + '/category/' + cid + '.rss',
|
||||||
site_url: nconf.get('url') + '/category/' + categoryData.cid,
|
site_url: nconf.get('url') + '/category/' + categoryData.cid,
|
||||||
ttl: 60
|
}, categoryData.topics);
|
||||||
});
|
|
||||||
|
|
||||||
// Add pubDate if category has topics
|
sendFeed(feed, res);
|
||||||
if (categoryData.topics.length > 0) {
|
|
||||||
feed.pubDate = new Date(parseInt(categoryData.topics[0].lastposttime, 10)).toUTCString();
|
|
||||||
}
|
|
||||||
|
|
||||||
categoryData.topics.forEach(function(topicData) {
|
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForRecent(req, res, next) {
|
function generateForRecent(req, res, next) {
|
||||||
topics.getLatestTopics(0, 0, 19, 'month', function (err, recentData) {
|
topics.getLatestTopics(0, 0, 19, 'month', function (err, recentData) {
|
||||||
if(err){
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
var feed = new rss({
|
var feed = generateTopicFeed({
|
||||||
title: 'Recently Active Topics',
|
title: 'Recently Active Topics',
|
||||||
description: 'A list of topics that have been active within the past 24 hours',
|
description: 'A list of topics that have been active within the past 24 hours',
|
||||||
feed_url: nconf.get('url') + '/recent.rss',
|
feed_url: nconf.get('url') + '/recent.rss',
|
||||||
site_url: nconf.get('url') + '/recent',
|
site_url: nconf.get('url') + '/recent'
|
||||||
ttl: 60
|
}, recentData.topics);
|
||||||
});
|
|
||||||
|
|
||||||
// Add pubDate if recent topics list contains topics
|
sendFeed(feed, res);
|
||||||
if (recentData.topics.length > 0) {
|
|
||||||
feed.pubDate = new Date(parseInt(recentData.topics[0].lastposttime, 10)).toUTCString();
|
|
||||||
}
|
|
||||||
|
|
||||||
recentData.topics.forEach(function(topicData) {
|
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForPopular(req, res, next) {
|
function generateForPopular(req, res, next) {
|
||||||
topics.getTopicsFromSet(0, 'topics:posts', 0, 19, function (err, popularData) {
|
topics.getTopicsFromSet(0, 'topics:posts', 0, 19, function (err, popularData) {
|
||||||
if(err){
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
var feed = new rss({
|
var feed = generateTopicFeed({
|
||||||
title: 'Popular Topics',
|
title: 'Popular Topics',
|
||||||
description: 'A list of topics that are sorted by post count',
|
description: 'A list of topics that are sorted by post count',
|
||||||
feed_url: nconf.get('url') + '/popular.rss',
|
feed_url: nconf.get('url') + '/popular.rss',
|
||||||
site_url: nconf.get('url') + '/popular',
|
site_url: nconf.get('url') + '/popular'
|
||||||
ttl: 60
|
}, popularData.topics);
|
||||||
});
|
|
||||||
|
|
||||||
// Add pubDate if recent topics list contains topics
|
sendFeed(feed, res);
|
||||||
if (popularData.topics.length > 0) {
|
});
|
||||||
feed.pubDate = new Date(parseInt(popularData.topics[0].lastposttime, 10)).toUTCString();
|
}
|
||||||
|
|
||||||
|
function generateTopicFeed(feedOptions, topics) {
|
||||||
|
|
||||||
|
feedOptions.ttl = 60;
|
||||||
|
var feed = new rss(feedOptions);
|
||||||
|
|
||||||
|
if (topics.length > 0) {
|
||||||
|
feed.pubDate = new Date(parseInt(topics[0].lastposttime, 10)).toUTCString();
|
||||||
}
|
}
|
||||||
|
|
||||||
popularData.topics.forEach(function(topicData) {
|
topics.forEach(function(topicData) {
|
||||||
feed.item({
|
feed.item({
|
||||||
title: topicData.title,
|
title: topicData.title,
|
||||||
url: nconf.get('url') + '/topic/' + topicData.slug,
|
url: nconf.get('url') + '/topic/' + topicData.slug,
|
||||||
@@ -189,9 +160,12 @@ function generateForPopular(req, res, next) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return feed;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendFeed(feed, res) {
|
||||||
var xml = feed.xml();
|
var xml = feed.xml();
|
||||||
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function(app, middleware, controllers){
|
module.exports = function(app, middleware, controllers){
|
||||||
|
|||||||
Reference in New Issue
Block a user