mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
closed #536
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -13,3 +13,5 @@ Vagrantfile
|
|||||||
.vagrant
|
.vagrant
|
||||||
provision.sh
|
provision.sh
|
||||||
*.komodoproject
|
*.komodoproject
|
||||||
|
|
||||||
|
feeds/recent.rss
|
||||||
|
|||||||
113
src/feed.js
113
src/feed.js
@@ -2,12 +2,14 @@
|
|||||||
var RDB = require('./redis.js'),
|
var RDB = require('./redis.js'),
|
||||||
posts = require('./posts.js'),
|
posts = require('./posts.js'),
|
||||||
topics = require('./topics.js'),
|
topics = require('./topics.js'),
|
||||||
|
categories = require('./categories'),
|
||||||
|
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
rss = require('rss'),
|
rss = require('rss'),
|
||||||
winston = require('winston'),
|
winston = require('winston'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
nconf = require('nconf'),
|
nconf = require('nconf'),
|
||||||
categories = require('./categories');
|
async = require('async');
|
||||||
|
|
||||||
Feed.defaults = {
|
Feed.defaults = {
|
||||||
ttl: 60,
|
ttl: 60,
|
||||||
@@ -26,43 +28,43 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
Feed.updateTopic = function (tid, callback) {
|
Feed.updateTopic = function (tid, callback) {
|
||||||
if (process.env.NODE_ENV === 'development') winston.info('[rss] Updating RSS feeds for topic ' + tid);
|
|
||||||
|
|
||||||
topics.getTopicWithPosts(tid, 0, 0, -1, function (err, topicData) {
|
topics.getTopicWithPosts(tid, 0, 0, -1, function (err, topicData) {
|
||||||
if (err) return callback(new Error('topic-invalid'));
|
if (err) return callback(new Error('topic-invalid'));
|
||||||
|
|
||||||
var feed = new rss({
|
var feed = new rss({
|
||||||
title: topicData.topic_name,
|
title: topicData.topic_name,
|
||||||
description: topicData.main_posts[0].content,
|
description: topicData.main_posts[0].content,
|
||||||
feed_url: Feed.defaults.baseUrl + '/topics/' + tid + '.rss',
|
feed_url: Feed.defaults.baseUrl + '/topics/' + tid + '.rss',
|
||||||
site_url: nconf.get('url') + 'topic/' + topicData.slug,
|
site_url: nconf.get('url') + 'topic/' + topicData.slug,
|
||||||
image_url: topicData.main_posts[0].picture,
|
image_url: topicData.main_posts[0].picture,
|
||||||
author: topicData.main_posts[0].username,
|
author: topicData.main_posts[0].username,
|
||||||
ttl: Feed.defaults.ttl
|
ttl: Feed.defaults.ttl
|
||||||
}),
|
}),
|
||||||
topic_posts = topicData.main_posts.concat(topicData.posts),
|
topic_posts = topicData.main_posts.concat(topicData.posts),
|
||||||
title, postData, dateStamp;
|
dateStamp;
|
||||||
|
|
||||||
// Add pubDate if topic contains posts
|
// Add pubDate if topic contains posts
|
||||||
if (topicData.main_posts.length > 0) feed.pubDate = new Date(parseInt(topicData.main_posts[0].timestamp, 10)).toUTCString();
|
if (topicData.main_posts.length > 0) feed.pubDate = new Date(parseInt(topicData.main_posts[0].timestamp, 10)).toUTCString();
|
||||||
|
|
||||||
for (var i = 0, ii = topic_posts.length; i < ii; i++) {
|
async.each(topic_posts, function(postData, next) {
|
||||||
if (topic_posts[i].deleted === '0') {
|
if (postData.deleted === '0') {
|
||||||
postData = topic_posts[i];
|
|
||||||
dateStamp = new Date(parseInt(postData.edited === '0' ? postData.timestamp : postData.edited, 10)).toUTCString();
|
dateStamp = new Date(parseInt(postData.edited === '0' ? postData.timestamp : postData.edited, 10)).toUTCString();
|
||||||
title = 'Reply to ' + topicData.topic_name + ' on ' + dateStamp;
|
|
||||||
|
|
||||||
feed.item({
|
feed.item({
|
||||||
title: title,
|
title: 'Reply to ' + topicData.topic_name + ' on ' + dateStamp,
|
||||||
description: postData.content,
|
description: postData.content,
|
||||||
url: nconf.get('url') + 'topic/' + topicData.slug + '#' + postData.pid,
|
url: nconf.get('url') + 'topic/' + topicData.slug + '#' + postData.pid,
|
||||||
author: postData.username,
|
author: postData.username,
|
||||||
date: dateStamp
|
date: dateStamp
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Feed.saveFeed('feeds/topics/' + tid + '.rss', feed, function (err) {
|
next();
|
||||||
|
}, function() {
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
winston.info('[rss] Re-generated RSS Feed for tid ' + tid + '.');
|
||||||
|
}
|
||||||
|
|
||||||
if (callback) callback();
|
if (callback) callback();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -70,40 +72,75 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
Feed.updateCategory = function (cid, callback) {
|
Feed.updateCategory = function (cid, callback) {
|
||||||
if (process.env.NODE_ENV === 'development') winston.info('[rss] Updating RSS feeds for category ' + cid);
|
|
||||||
categories.getCategoryById(cid, 0, function (err, categoryData) {
|
categories.getCategoryById(cid, 0, function (err, categoryData) {
|
||||||
if (err) return callback(new Error('category-invalid'));
|
if (err) return callback(new Error('category-invalid'));
|
||||||
|
|
||||||
var feed = new rss({
|
var feed = new rss({
|
||||||
title: categoryData.category_name,
|
title: categoryData.category_name,
|
||||||
description: categoryData.category_description,
|
description: categoryData.category_description,
|
||||||
feed_url: Feed.defaults.baseUrl + '/categories/' + cid + '.rss',
|
feed_url: Feed.defaults.baseUrl + '/categories/' + cid + '.rss',
|
||||||
site_url: nconf.get('url') + 'category/' + categoryData.category_id,
|
site_url: nconf.get('url') + 'category/' + categoryData.category_id,
|
||||||
ttl: Feed.defaults.ttl
|
ttl: Feed.defaults.ttl
|
||||||
}),
|
});
|
||||||
topics = categoryData.topics,
|
|
||||||
title, topicData, dateStamp;
|
|
||||||
|
|
||||||
// Add pubDate if category has topics
|
// Add pubDate if category has topics
|
||||||
if (categoryData.topics.length > 0) feed.pubDate = new Date(parseInt(categoryData.topics[0].lastposttime, 10)).toUTCString();
|
if (categoryData.topics.length > 0) feed.pubDate = new Date(parseInt(categoryData.topics[0].lastposttime, 10)).toUTCString();
|
||||||
|
|
||||||
for (var i = 0, ii = topics.length; i < ii; i++) {
|
async.eachSeries(categoryData.topics, function(topicData, next) {
|
||||||
topicData = topics[i];
|
|
||||||
dateStamp = new Date(parseInt(topicData.lastposttime, 10)).toUTCString();
|
|
||||||
title = topics[i].title;
|
|
||||||
|
|
||||||
feed.item({
|
feed.item({
|
||||||
title: title,
|
title: topicData.title,
|
||||||
url: nconf.get('url') + 'topic/' + topicData.slug,
|
url: nconf.get('url') + 'topic/' + topicData.slug,
|
||||||
author: topicData.username,
|
author: topicData.username,
|
||||||
date: dateStamp
|
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
Feed.saveFeed('feeds/categories/' + cid + '.rss', feed, function (err) {
|
next();
|
||||||
if (callback) callback();
|
}, function() {
|
||||||
|
Feed.saveFeed('feeds/categories/' + cid + '.rss', feed, function (err) {
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
winston.info('[rss] Re-generated RSS Feed for cid ' + cid + '.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (callback) callback();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Feed.updateRecent = function(callback) {
|
||||||
|
console.log('entered');
|
||||||
|
if (process.env.NODE_ENV === 'development') winston.info('[rss] Updating Recent Posts RSS feed');
|
||||||
|
topics.getLatestTopics(0, 0, 19, undefined, function (err, recentData) {
|
||||||
|
var feed = new rss({
|
||||||
|
title: 'Recently Active Topics',
|
||||||
|
description: 'A list of topics that have been active within the past 24 hours',
|
||||||
|
feed_url: Feed.defaults.baseUrl + '/recent.rss',
|
||||||
|
site_url: nconf.get('url') + 'recent',
|
||||||
|
ttl: Feed.defaults.ttl
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add pubDate if recent topics list contains topics
|
||||||
|
if (recentData.topics.length > 0) {
|
||||||
|
feed.pubDate = new Date(parseInt(recentData.topics[0].lastposttime, 10)).toUTCString();
|
||||||
|
}
|
||||||
|
|
||||||
|
async.eachSeries(recentData.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/recent.rss', feed, function (err) {
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
winston.info('[rss] Re-generated "recent posts" RSS Feed.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (callback) callback();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
}(exports));
|
}(exports));
|
||||||
@@ -157,6 +157,7 @@ var RDB = require('./redis.js'),
|
|||||||
});
|
});
|
||||||
|
|
||||||
Feed.updateTopic(postData.tid);
|
Feed.updateTopic(postData.tid);
|
||||||
|
Feed.updateRecent();
|
||||||
|
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
@@ -197,6 +198,7 @@ var RDB = require('./redis.js'),
|
|||||||
});
|
});
|
||||||
|
|
||||||
Feed.updateTopic(postData.tid);
|
Feed.updateTopic(postData.tid);
|
||||||
|
Feed.updateRecent();
|
||||||
|
|
||||||
postSearch.index(postData.content, pid);
|
postSearch.index(postData.content, pid);
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ var RDB = require('./redis.js'),
|
|||||||
var cid = topicData.cid;
|
var cid = topicData.cid;
|
||||||
|
|
||||||
feed.updateTopic(tid);
|
feed.updateTopic(tid);
|
||||||
|
feed.updateRecent();
|
||||||
|
|
||||||
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
|
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
|
||||||
|
|
||||||
|
|||||||
@@ -138,8 +138,12 @@ var user = require('./../user.js'),
|
|||||||
|
|
||||||
app.get('/recent/:term?', function (req, res) {
|
app.get('/recent/:term?', function (req, res) {
|
||||||
var uid = (req.user) ? req.user.uid : 0;
|
var uid = (req.user) ? req.user.uid : 0;
|
||||||
topics.getLatestTopics(uid, 0, 19, req.params.term, function (data) {
|
topics.getLatestTopics(uid, 0, 19, req.params.term, function (err, data) {
|
||||||
res.json(data);
|
if (!err) {
|
||||||
|
res.json(data);
|
||||||
|
} else {
|
||||||
|
res.send(500);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ var user = require('./../user'),
|
|||||||
topics = require('./../topics'),
|
topics = require('./../topics'),
|
||||||
posts = require('./../posts');
|
posts = require('./../posts');
|
||||||
|
|
||||||
|
|
||||||
var DebugRoute = function(app) {
|
var DebugRoute = function(app) {
|
||||||
|
|
||||||
app.namespace('/debug', function() {
|
app.namespace('/debug', function() {
|
||||||
|
|||||||
@@ -221,6 +221,9 @@ var RDB = require('./redis.js'),
|
|||||||
var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1];
|
var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1];
|
||||||
|
|
||||||
RDB.zrevrangebyscore(args, function(err, tids) {
|
RDB.zrevrangebyscore(args, function(err, tids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
var latestTopics = {
|
var latestTopics = {
|
||||||
'category_name': 'Recent',
|
'category_name': 'Recent',
|
||||||
@@ -234,13 +237,13 @@ var RDB = require('./redis.js'),
|
|||||||
|
|
||||||
if (!tids || !tids.length) {
|
if (!tids || !tids.length) {
|
||||||
latestTopics.no_topics_message = 'show';
|
latestTopics.no_topics_message = 'show';
|
||||||
callback(latestTopics);
|
callback(err, latestTopics);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Topics.getTopicsByTids(tids, current_user, function(topicData) {
|
Topics.getTopicsByTids(tids, current_user, function(topicData) {
|
||||||
latestTopics.topics = topicData;
|
latestTopics.topics = topicData;
|
||||||
callback(latestTopics);
|
callback(err, latestTopics);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -318,7 +318,7 @@ var path = require('path'),
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
translator.translate(templates.logout.toString(), function(parsedTemplate) {
|
translator.translate(templates.logout.toString(), function(parsedTemplate) {
|
||||||
templates.logout = parsedTemplate;
|
templates.logout = parsedTemplate;
|
||||||
});
|
});
|
||||||
@@ -649,6 +649,32 @@ var path = require('path'),
|
|||||||
"Sitemap: " + nconf.get('url') + "sitemap.xml");
|
"Sitemap: " + nconf.get('url') + "sitemap.xml");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!fs.existsSync(rssPath)) {
|
||||||
|
feed.updaterecent(function (err) {
|
||||||
|
if (err) {
|
||||||
|
res.redirect('/404');
|
||||||
|
} else {
|
||||||
|
loadFeed();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
loadFeed();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
app.get('/recent/:term?', function (req, 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.
|
// TODO consolidate with /recent route as well -> that can be combined into this area. See "Basic Routes" near top.
|
||||||
app.build_header({
|
app.build_header({
|
||||||
|
|||||||
@@ -807,8 +807,12 @@ module.exports.init = function(io) {
|
|||||||
var start = data.after,
|
var start = data.after,
|
||||||
end = start + 9;
|
end = start + 9;
|
||||||
|
|
||||||
topics.getLatestTopics(uid, start, end, data.term, function(latestTopics) {
|
topics.getLatestTopics(uid, start, end, data.term, function(err, latestTopics) {
|
||||||
callback(latestTopics);
|
if (!err) {
|
||||||
|
callback(latestTopics);
|
||||||
|
} else {
|
||||||
|
winston.error('[socket api:topics.loadMoreRecentTopics] ' + err.message);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user