Merge branch 'feed_refactor'

v1.18.x
Julian Lam 12 years ago
commit bcbcf40eae

@ -36,7 +36,8 @@
"reds": "~0.2.4", "reds": "~0.2.4",
"winston": "~0.7.2", "winston": "~0.7.2",
"nodebb-plugin-mentions": "~0.1.0", "nodebb-plugin-mentions": "~0.1.0",
"nodebb-plugin-markdown": "~0.1.0" "nodebb-plugin-markdown": "~0.1.0",
"rss": "~0.2.0"
}, },
"bugs": { "bugs": {
"url": "https://github.com/designcreateplay/NodeBB/issues" "url": "https://github.com/designcreateplay/NodeBB/issues"

@ -4,106 +4,98 @@
posts = require('./posts.js'), posts = require('./posts.js'),
topics = require('./topics.js'), topics = require('./topics.js'),
fs = require('fs'), fs = require('fs'),
rss = require('node-rss'), rss = require('rss'),
winston = require('winston'), winston = require('winston'),
path = require('path'); path = require('path');
function saveFeed(location, feed) { Feed.defaults = {
ttl: 60,
basePath: path.join(__dirname, '../', 'feeds'),
baseUrl: nconf.get('url') + 'feeds'
};
Feed.saveFeed = function(location, feed, callback) {
var savePath = path.join(__dirname, '../', location); var savePath = path.join(__dirname, '../', location);
fs.writeFile(savePath, rss.getFeedXML(feed), function (err) { fs.writeFile(savePath, feed.xml(), function (err) {
if(err) { if(err) return winston.err(err);
winston.err(err);
}
});
}
function createFeed(title, description, feed_url, xml_url, author, urn) { if (callback) callback(err);
return rss.createNewFeed( });
title,
feed_url,
description,
author,
xml_url,
{
'urn' : urn
}
);
} }
Feed.updateTopic = function(tid, callback) {
Feed.updateTopic = function(tid, cid) { if (process.env.NODE_ENV === 'development') winston.info('[rss] Updating RSS feeds for topic ' + tid);
winston.info('[RSS] Updating RSS feeds for topic ' + tid);
var cache_time_in_seconds = 60;
topics.getTopicWithPosts(tid, 0, 0, -1, function(err, topicData) { topics.getTopicWithPosts(tid, 0, 0, -1, function(err, topicData) {
if (err) winston.error('Problem saving topic RSS feed', err.stack); if (err) return winston.error('Problem saving topic RSS feed', err.stack);
var location = '/topic/' + topicData.slug, var feed = new rss({
xml_url = '/topic/' + tid + '.rss'; title: topicData.topic_name,
description: topicData.main_posts[0].content,
var post = topicData.main_posts[0]; feed_url: Feed.defaults.baseUrl + '/topics/' + tid + '.rss',
var urn = 'urn:' + cid + ':' + tid; site_url: nconf.get('url') + 'topic/' + topicData.slug,
image_url: topicData.main_posts[0].picture,
var feed = createFeed(topicData.topic_name, '', location, xml_url, post.username, urn); author: topicData.main_posts[0].username,
var title; pubDate: new Date(parseInt(topicData.main_posts[0].timestamp, 10)).toUTCString(),
ttl: Feed.defaults.ttl
var topic_posts = topicData.main_posts.concat(topicData.posts); }),
topic_posts = topicData.main_posts.concat(topicData.posts),
title, postData, dateStamp;
for (var i = 0, ii = topic_posts.length; i < ii; i++) { for (var i = 0, ii = topic_posts.length; i < ii; i++) {
urn = 'urn:' + cid + ':' + tid + ':' + topic_posts[i].pid; postData = topic_posts[i];
title = 'Reply to ' + topicData.topic_name + ' on ' + (new Date(parseInt(topic_posts[i].timestamp, 10)).toUTCString()); dateStamp = new Date(parseInt(postData.edited === 0 ? postData.timestamp : postData.edited, 10)).toUTCString();
title = 'Reply to ' + topicData.topic_name + ' on ' + dateStamp;
feed.addNewItem(
title, feed.item({
location, title: title,
topic_posts[i].timestamp, description: postData.content,
topic_posts[i].content, url: nconf.get('url') + 'topic/' + topicData.slug + '#' + postData.pid,
{ author: postData.username,
'urn' : urn, date: dateStamp
'username' : topic_posts[i].username });
}
);
} }
saveFeed('feeds/topics/' + tid + '.rss', feed); Feed.saveFeed('feeds/topics/' + tid + '.rss', feed, function(err) {
if (callback) callback();
});
}); });
}; };
Feed.updateCategory = function(cid) { 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) { if (err) return winston.error('Could not update RSS feed for category ' + cid, err.stack);
winston.error('Could not update RSS feed for category ' + cid, err.stack);
return; var feed = new rss({
} title: categoryData.category_name,
description: categoryData.category_description,
var location = '/category/' + categoryData.category_id + '/' + categoryData.category_name, feed_url: Feed.defaults.baseUrl + '/categories/' + cid + '.rss',
xml_url = '/category' + cid + '.rss'; site_url: nconf.get('url') + 'category/' + categoryData.category_id,
pubDate: new Date(parseInt(categoryData.topics[0].lastposttime, 10)).toUTCString(),
var urn = 'urn:' + cid; ttl: Feed.defaults.ttl
var feed = createFeed(categoryData.category_name, '', location, xml_url, 'NodeBB', urn); // not exactly sure if author for a category should be site_title? }),
topics = categoryData.topics,
var title; title, topicData, dateStamp;
var topics = categoryData.topics;
for (var i = 0, ii = topics.length; i < ii; i++) { for (var i = 0, ii = topics.length; i < ii; i++) {
urn = 'urn:' + cid + ':' + topics[i].tid; topicData = topics[i];
title = topics[i].title + '. Posted on ' + (new Date(parseInt(topics[i].timestamp, 10)).toUTCString()); dateStamp = new Date(parseInt(topicData.lastposttime, 10)).toUTCString();
title = topics[i].title;
feed.addNewItem(
title, feed.item({
location, title: title,
topics[i].timestamp, url: nconf.get('url') + 'topic/' + topicData.slug,
topics[i].teaser_text, author: topicData.username,
{ date: dateStamp
'urn' : urn, });
'username' : topics[i].username
}
);
} }
saveFeed('feeds/categories/' + cid + '.rss', feed); Feed.saveFeed('feeds/categories/' + cid + '.rss', feed, function(err) {
if (callback) callback();
});
}); });
}; };

@ -314,7 +314,7 @@ var RDB = require('./redis.js'),
topics.getTopicField(tid, 'cid', function(err, cid) { topics.getTopicField(tid, 'cid', function(err, cid) {
RDB.handle(err); RDB.handle(err);
feed.updateTopic(tid, cid); feed.updateTopic(tid);
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid); RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
RDB.zadd('categories:' + cid + ':tid', timestamp, tid); RDB.zadd('categories:' + cid + ':tid', timestamp, tid);

@ -21,7 +21,8 @@ var express = require('express'),
installRoute = require('./routes/install.js'), installRoute = require('./routes/install.js'),
testBed = require('./routes/testbed.js'), testBed = require('./routes/testbed.js'),
auth = require('./routes/authentication.js'), auth = require('./routes/authentication.js'),
meta = require('./meta.js'); meta = require('./meta.js'),
feed = require('./feed');
(function(app) { (function(app) {
var templates = null; var templates = null;
@ -209,17 +210,25 @@ var express = require('express'),
app.get('/topic/:topic_id/:slug?', function(req, res) { app.get('/topic/:topic_id/:slug?', function(req, res) {
var tid = req.params.topic_id; var tid = req.params.topic_id;
if (tid.match(/^\d+\.rss$/)) { if (tid.match(/^\d+\.rss$/)) {
fs.readFile(path.join(__dirname, '../', 'feeds/topics', tid), function (err, data) { tid = tid.slice(0, -4);
if (err) { var rssPath = path.join(__dirname, '../', 'feeds/topics', tid + '.rss'),
res.type('text').send(404, "Unable to locate an rss feed at this location."); loadFeed = function() {
return; 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.updateTopic(tid, function() {
loadFeed();
});
} else loadFeed();
res.type('xml').set('Content-Length', data.length).send(data);
});
return; return;
} }
@ -280,14 +289,22 @@ var express = require('express'),
var cid = req.params.category_id; var cid = req.params.category_id;
if (cid.match(/^\d+\.rss$/)) { if (cid.match(/^\d+\.rss$/)) {
fs.readFile(path.join(__dirname, '../', 'feeds/categories', cid), function (err, data) { cid = cid.slice(0, -4);
if (err) { var rssPath = path.join(__dirname, '../', 'feeds/categories', cid + '.rss'),
res.type('text').send(404, "Unable to locate an rss feed at this location."); loadFeed = function() {
return; 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.updateCategory(cid, function() {
loadFeed();
});
} else loadFeed();
res.type('xml').set('Content-Length', data.length).send(data);
});
return; return;
} }

Loading…
Cancel
Save