2013-09-18 10:50:02 -04:00
|
|
|
(function (Feed) {
|
2013-12-02 21:20:55 -05:00
|
|
|
var db = require('./database'),
|
|
|
|
|
posts = require('./posts'),
|
|
|
|
|
topics = require('./topics'),
|
2013-11-22 11:42:42 -05:00
|
|
|
categories = require('./categories'),
|
|
|
|
|
|
2013-06-04 13:21:08 -04:00
|
|
|
fs = require('fs'),
|
2013-09-03 23:18:25 -04:00
|
|
|
rss = require('rss'),
|
2013-09-03 13:11:21 -04:00
|
|
|
winston = require('winston'),
|
2013-09-17 15:38:50 -04:00
|
|
|
path = require('path'),
|
2013-10-15 14:44:42 -04:00
|
|
|
nconf = require('nconf'),
|
2013-11-22 11:42:42 -05:00
|
|
|
async = require('async');
|
2013-05-27 14:55:07 -04:00
|
|
|
|
2013-09-03 23:18:25 -04:00
|
|
|
Feed.defaults = {
|
2014-02-09 21:29:21 +00:00
|
|
|
ttl: 60
|
2013-09-03 23:18:25 -04:00
|
|
|
};
|
|
|
|
|
|
2014-02-09 21:29:21 +00:00
|
|
|
Feed.forTopic = function (tid, callback) {
|
2014-02-09 21:33:20 +00:00
|
|
|
topics.getTopicWithPosts(tid, 0, 0, 25, true, function (err, topicData) {
|
2013-11-28 15:18:19 -05:00
|
|
|
if (err) {
|
2014-02-09 21:29:21 +00:00
|
|
|
return callback(new Error('topic-invalid'));
|
2013-11-28 15:18:19 -05:00
|
|
|
}
|
2013-09-03 23:18:25 -04:00
|
|
|
|
2014-01-26 23:20:37 -05: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({
|
2013-11-22 11:42:42 -05:00
|
|
|
title: topicData.topic_name,
|
2014-01-26 23:20:37 -05:00
|
|
|
description: description,
|
2014-02-09 21:29:21 +00:00
|
|
|
feed_url: nconf.get('url') + '/topic/' + tid + '.rss',
|
2014-01-22 14:04:04 -05:00
|
|
|
site_url: nconf.get('url') + '/topic/' + topicData.slug,
|
2014-01-26 23:20:37 -05:00
|
|
|
image_url: image_url,
|
|
|
|
|
author: author,
|
2013-11-22 11:42:42 -05:00
|
|
|
ttl: Feed.defaults.ttl
|
|
|
|
|
}),
|
|
|
|
|
dateStamp;
|
2013-07-02 19:02:23 -04:00
|
|
|
|
2013-09-10 11:25:19 -04:00
|
|
|
// Add pubDate if topic contains posts
|
2013-11-28 15:18:19 -05:00
|
|
|
if (topicData.posts.length > 0) {
|
|
|
|
|
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
|
|
|
|
|
}
|
2013-09-10 11:25:19 -04:00
|
|
|
|
2014-02-09 21:29:21 +00: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
|
|
|
|
2013-09-10 12:34:48 -04:00
|
|
|
feed.item({
|
2013-11-22 11:42:42 -05:00
|
|
|
title: 'Reply to ' + topicData.topic_name + ' on ' + dateStamp,
|
2013-09-10 12:34:48 -04:00
|
|
|
description: postData.content,
|
2014-01-22 14:04:04 -05:00
|
|
|
url: nconf.get('url') + '/topic/' + topicData.slug + '#' + postData.pid,
|
2013-09-10 12:34:48 -04:00
|
|
|
author: postData.username,
|
|
|
|
|
date: dateStamp
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-09-04 13:52:28 -04:00
|
|
|
});
|
2014-02-09 21:29:21 +00:00
|
|
|
|
|
|
|
|
callback(null, feed.xml());
|
2013-05-27 14:55:07 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-09 21:29:21 +00:00
|
|
|
Feed.forCategory = function (cid, callback) {
|
2014-02-09 12:35:26 -05:00
|
|
|
categories.getCategoryById(cid, 0, 25, 0, function (err, categoryData) {
|
2014-02-09 21:29:21 +00:00
|
|
|
if (err) {
|
|
|
|
|
return callback(new Error('category-invalid'));
|
|
|
|
|
}
|
2013-09-03 23:18:25 -04:00
|
|
|
|
2013-09-17 13:09:37 -04:00
|
|
|
var feed = new rss({
|
2013-11-22 11:42:42 -05:00
|
|
|
title: categoryData.category_name,
|
|
|
|
|
description: categoryData.category_description,
|
2014-02-09 21:29:21 +00:00
|
|
|
feed_url: nconf.get('url') + '/category/' + cid + '.rss',
|
2014-01-22 14:04:04 -05:00
|
|
|
site_url: nconf.get('url') + '/category/' + categoryData.category_id,
|
2013-11-22 11:42:42 -05:00
|
|
|
ttl: Feed.defaults.ttl
|
|
|
|
|
});
|
2013-06-04 12:58:04 -04: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();
|
|
|
|
|
|
2014-02-09 21:29:21 +00:00
|
|
|
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
|
|
|
});
|
2013-09-04 13:52:28 -04:00
|
|
|
});
|
2014-02-09 21:29:21 +00:00
|
|
|
|
|
|
|
|
callback(null, feed.xml());
|
2013-06-04 12:58:04 -04:00
|
|
|
});
|
2013-11-22 11:42:42 -05:00
|
|
|
};
|
2013-05-27 14:55:07 -04:00
|
|
|
|
2014-02-09 21:29:21 +00:00
|
|
|
Feed.forRecent = function(callback) {
|
2014-02-09 17:14:31 -05:00
|
|
|
|
2013-11-22 11:42:42 -05:00
|
|
|
topics.getLatestTopics(0, 0, 19, undefined, function (err, recentData) {
|
2014-02-09 21:29:21 +00:00
|
|
|
if(err){
|
|
|
|
|
return callback(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',
|
2014-02-09 21:29:21 +00:00
|
|
|
feed_url: nconf.get('url') + '/recent.rss',
|
2014-01-22 14:04:04 -05:00
|
|
|
site_url: nconf.get('url') + '/recent',
|
2013-11-22 11:42:42 -05:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 21:29:21 +00:00
|
|
|
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()
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-02-09 21:29:21 +00:00
|
|
|
|
|
|
|
|
callback(null, feed.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
|
|
|
|
2014-02-09 21:29:21 +00:00
|
|
|
Feed.forPopular = function(callback) {
|
2014-01-31 11:16:23 -05:00
|
|
|
topics.getTopicsFromSet(0, 'topics:posts', 0, 19, function (err, popularData) {
|
2014-02-09 21:29:21 +00:00
|
|
|
if(err){
|
|
|
|
|
return callback(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',
|
2014-02-09 21:29:21 +00:00
|
|
|
feed_url: nconf.get('url') + '/popular.rss',
|
2014-01-31 11:16:23 -05:00
|
|
|
site_url: nconf.get('url') + '/popular',
|
|
|
|
|
ttl: Feed.defaults.ttl
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2014-02-09 21:29:21 +00:00
|
|
|
callback(null, feed.xml());
|
2014-01-31 11:16:23 -05:00
|
|
|
});
|
|
|
|
|
};
|
2014-02-09 21:29:21 +00:00
|
|
|
}(exports));
|