wrapped up rss for topics. added a new route to access these feeds from the url topic/tid.rss

v1.18.x
psychobunny 12 years ago
parent 9a15177d50
commit 4b17f1d4e6

@ -2,11 +2,15 @@
var RDB = require('./redis.js'), var RDB = require('./redis.js'),
schema = require('./schema.js'), schema = require('./schema.js'),
posts = require('./posts.js'), posts = require('./posts.js'),
topics = require('./topics.js'); topics = require('./topics.js'),
fs = require('fs');
function saveFeed(feed, xml) { function saveFeed(feed, xml, tid) {
feed.endEntry(); feed.endEntry();
console.log(xml.toString())
fs.writeFile('feeds/topics/' + tid + '.rss', xml.toString(), function (err) {
if (err) throw err;
});
} }
function createFeed(xml, urn, title, feed_url, author) { function createFeed(xml, urn, title, feed_url, author) {
@ -25,7 +29,7 @@
function createEntry(feed, urn, title, content, url, author) { function createEntry(feed, urn, title, content, url, author) {
return feed return feed
.startEntry(urn) .startEntry(urn)
.writeTitle('Reply #' + title) .writeTitle(title)
.writeLink(url, 'text/html') .writeLink(url, 'text/html')
.writeContent(content, 'text', 'en') .writeContent(content, 'text', 'en')
.writeAuthorRAW(author) .writeAuthorRAW(author)
@ -54,18 +58,20 @@
var topicData = results[0], var topicData = results[0],
postsData = results[1].postData, postsData = results[1].postData,
userData = results[1].userData, userData = results[1].userData,
url = topicData.category_slug + '/' + topicData.slug; url = '/topic/' + topicData.slug;
var post = topicData.main_posts[0]; var post = topicData.main_posts[0];
var urn = 'urn:' + cid + ':' + tid; var urn = 'urn:' + cid + ':' + tid;
var feed = createFeed(xml, urn, topicData.topic_name, url, post.username); var feed = createFeed(xml, urn, topicData.topic_name, url, post.username);
var title;
for (var i = 0, ii = postsData.pid.length; i < ii; i++) { for (var i = 0, ii = postsData.pid.length; i < ii; i++) {
urn = 'urn:' + cid + ':' + tid + ':' + postsData.pid[i]; urn = 'urn:' + cid + ':' + tid + ':' + postsData.pid[i];
feed = createEntry(feed, urn, postsData.pid[i], postsData.content[i], url, userData[postsData.uid[i]].username); title = 'Reply to ' + topicData.topic_name + ' on ' + (new Date(parseInt(postsData.timestamp[i], 10)).toUTCString());
feed = createEntry(feed, urn, title, postsData.content[i], url, userData[postsData.uid[i]].username);
} }
saveFeed(feed, xml); saveFeed(feed, xml, tid);
}); });
}; };

@ -94,8 +94,21 @@ var express = require('express'),
// Complex Routes // Complex Routes
app.get('/topic/:topic_id/:slug?', function(req, res) { app.get('/topic/:topic_id/:slug?', function(req, res) {
var topic_url = req.params.topic_id + (req.params.slug ? '/' + req.params.slug : ''); var tid = req.params.topic_id;
topics.get_cid_by_tid(req.params.topic_id, function(cid) { if (tid.match('.rss')) {
fs.readFile('feeds/topics/' + tid, function (err, data) {
if (err) {
res.send("Unable to locate an rss feed at this location.");
return;
}
res.send(data);
});
return;
}
var topic_url = tid + (req.params.slug ? '/' + req.params.slug : '');
topics.get_cid_by_tid(tid, function(cid) {
res.send( res.send(
build_header() + build_header() +
'<script>templates.ready(function(){ajaxify.go("topic/' + topic_url + '");});</script>' + '<script>templates.ready(function(){ajaxify.go("topic/' + topic_url + '");});</script>' +

Loading…
Cancel
Save