Skip to content

Node.js based forum software built for the modern web

moved posts and recent to new files

barisusakli7a1037dc1145
diff --git a/src/sitemap.js b/src/sitemap.js
index 89b3a4b2ba..56db3b1b2c 100644
--- a/src/sitemap.js
+++ b/src/sitemap.js
@@ -1,3 +1,5 @@
+'use strict';
+
var path = require('path'),
async = require('async'),
sm = require('sitemap'),
@@ -28,6 +30,10 @@ var path = require('path'),
function(next) {
var categoryUrls = [];
categories.getAllCategories(0, function(err, data) {
+ if (err) {
+ return next(err);
+ }
+
data.categories.forEach(function(category) {
if (!category.disabled) {
categoryUrls.push({
@@ -43,16 +49,17 @@ var path = require('path'),
},
function(next) {
var topicUrls = [];
- topics.getAllTopics(0, -1, function(err, topics) {
- topics.forEach(function(topic) {
+ topics.getTopicsFromSet(0, 'topics:recent', 0, -1, function(err, data) {
+ if (err) {
+ return next(err);
+ }
- if (parseInt(topic.deleted, 10) !== 1) {
- topicUrls.push({
- url: path.join('/topic', topic.slug),
- changefreq: 'daily',
- priority: '0.6'
- });
- }
+ data.topics.forEach(function(topic) {
+ topicUrls.push({
+ url: path.join('/topic', topic.slug),
+ changefreq: 'daily',
+ priority: '0.6'
+ });
});
next(null, topicUrls);
@@ -63,13 +70,13 @@ var path = require('path'),
returnUrls = returnUrls.concat(data[0]).concat(data[1]);
}
- callback(null, returnUrls);
+ callback(err, returnUrls);
});
},
render: function(callback) {
async.parallel([sitemap.getStaticUrls, sitemap.getDynamicUrls], function(err, urls) {
- var urls = urls[0].concat(urls[1]),
- map = sm.createSitemap({
+ urls = urls[0].concat(urls[1]);
+ var map = sm.createSitemap({
hostname: nconf.get('url'),
cacheTime: 600000,
urls: urls
@@ -79,6 +86,6 @@ var path = require('path'),
});
});
}
- }
+ };
module.exports = sitemap;
\ No newline at end of file
diff --git a/src/topics.js b/src/topics.js
index 19bd811b2f..3ec8604685 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -2,11 +2,7 @@
var async = require('async'),
gravatar = require('gravatar'),
- path = require('path'),
- nconf = require('nconf'),
validator = require('validator'),
- S = require('string'),
- winston = require('winston'),
db = require('./database'),
posts = require('./posts'),
@@ -15,20 +11,15 @@ var async = require('async'),
user = require('./user'),
categories = require('./categories'),
categoryTools = require('./categoryTools'),
- posts = require('./posts'),
- threadTools = require('./threadTools'),
- postTools = require('./postTools'),
- notifications = require('./notifications'),
- favourites = require('./favourites'),
- meta = require('./meta'),
- Plugins = require('./plugins'),
- emitter = require('./emitter');
+ threadTools = require('./threadTools');
(function(Topics) {
require('./topics/create')(Topics);
require('./topics/unread')(Topics);
+ require('./topics/recent')(Topics);
require('./topics/fork')(Topics);
+ require('./topics/posts')(Topics);
Topics.getTopicData = function(tid, callback) {
@@ -90,62 +81,6 @@ var async = require('async'),
});
};
- Topics.getTopicPosts = function(tid, start, end, uid, reverse, callback) {
- posts.getPostsByTid(tid, start, end, reverse, function(err, postData) {
- if(err) {
- return callback(err);
- }
-
- if (Array.isArray(postData) && !postData.length) {
- return callback(null, []);
- }
-
- for(var i=0; i<postData.length; ++i) {
- postData[i].index = start + i;
- }
-
- var pids = postData.map(function(post) {
- return post.pid;
- });
-
- async.parallel({
- favourites : function(next) {
- favourites.getFavouritesByPostIDs(pids, uid, next);
- },
- voteData : function(next) {
- favourites.getVoteStatusByPostIDs(pids, uid, next);
- },
- userData : function(next) {
- async.each(postData, posts.addUserInfoToPost, next);
- },
- privileges : function(next) {
- async.map(pids, function (pid, next) {
- postTools.privileges(pid, uid, next);
- }, next);
- }
- }, function(err, results) {
- if(err) {
- return callback(err);
- }
-
- for (var i = 0; i < postData.length; ++i) {
- postData[i].favourited = results.favourites[i];
- postData[i].upvoted = results.voteData[i].upvoted;
- postData[i].downvoted = results.voteData[i].downvoted;
- postData[i].votes = postData[i].votes || 0;
- postData[i].display_moderator_tools = parseInt(uid, 10) !== 0 && results.privileges[i].editable;
- postData[i].display_move_tools = results.privileges[i].move;
-
- if(parseInt(postData[i].deleted, 10) === 1 && !results.privileges[i].view_deleted) {
- postData[i].content = 'This post is deleted!';
- }
- }
-
- callback(null, postData);
- });
- });
- };
-
Topics.getPageCount = function(tid, uid, callback) {
db.sortedSetCard('tid:' + tid + ':posts', function(err, postCount) {
if(err) {
@@ -194,7 +129,7 @@ var async = require('async'),
});
};
- function getTopics(set, uid, tids, callback) {
+ Topics.getTopics = function(set, uid, tids, callback) {
var returnTopics = {
topics: [],
nextStart: 0
@@ -229,32 +164,6 @@ var async = require('async'),
});
});
});
- }
-
- Topics.getLatestTids = function(start, end, term, callback) {
- var terms = {
- day: 86400000,
- week: 604800000,
- month: 2592000000
- };
-
- var since = terms.day;
- if(terms[term]) {
- since = terms[term];
- }
-
- var count = parseInt(end, 10) === -1 ? end : end - start + 1;
-
- db.getSortedSetRevRangeByScore(['topics:recent', '+inf', Date.now() - since, 'LIMIT', start, count], callback);
- };
-
- Topics.getLatestTopics = function(uid, start, end, term, callback) {
- Topics.getLatestTids(start, end, term, function(err, tids) {
- if(err) {
- return callback(err);
- }
- getTopics('topics:recent', uid, tids, callback);
- });
};
Topics.getTopicsFromSet = function(uid, set, start, end, callback) {
@@ -263,7 +172,7 @@ var async = require('async'),
return callback(err);
}
- getTopics(set, uid, tids, callback);
+ Topics.getTopics(set, uid, tids, callback);
});
};
@@ -385,11 +294,10 @@ var async = require('async'),
Topics.getPageCount(tid, uid, next);
},
threadTools : function(next) {
- Plugins.fireHook('filter:topic.thread_tools', [], next);
+ plugins.fireHook('filter:topic.thread_tools', [], next);
}
}, function(err, results) {
if (err) {
- winston.error('[Topics.getTopicWithPosts] Could not retrieve topic data: ', err.message);
return callback(err);
}
@@ -406,34 +314,6 @@ var async = require('async'),
});
};
- Topics.getAllTopics = function(start, end, callback) {
- db.getSortedSetRevRange('topics:tid', start, end, function(err, tids) {
- if(err) {
- return callback(err);
- }
-
- async.map(tids, function(tid, next) {
- Topics.getTopicDataWithUser(tid, next);
- }, callback);
- });
- };
-
- Topics.getTitleByPid = function(pid, callback) {
- Topics.getTopicFieldByPid('title', pid, callback);
- };
-
- Topics.getTopicFieldByPid = function(field, pid, callback) {
- posts.getPostField(pid, 'tid', function(err, tid) {
- Topics.getTopicField(tid, field, callback);
- });
- };
-
- Topics.getTopicDataByPid = function(pid, callback) {
- posts.getPostField(pid, 'tid', function(err, tid) {
- Topics.getTopicData(tid, callback);
- });
- };
-
Topics.getTeasers = function(tids, callback) {
if(!Array.isArray(tids)) {
@@ -489,27 +369,6 @@ var async = require('async'),
db.setObjectField('topic:' + tid, field, value, callback);
};
- Topics.increasePostCount = function(tid, callback) {
- incrementFieldAndUpdateSortedSet(tid, 'postcount', 1, 'topics:posts', callback);
- };
-
- Topics.decreasePostCount = function(tid, callback) {
- incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts', callback);
- };
-
- Topics.increaseViewCount = function(tid, callback) {
- incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, 'topics:views', callback);
- };
-
- function incrementFieldAndUpdateSortedSet(tid, field, by, set, callback) {
- db.incrObjectFieldBy('topic:' + tid, field, by, function(err, value) {
- if(err) {
- return callback(err);
- }
- db.sortedSetAdd(set, value, tid, callback);
- });
- }
-
Topics.isLocked = function(tid, callback) {
Topics.getTopicField(tid, 'locked', function(err, locked) {
if(err) {
@@ -524,26 +383,6 @@ var async = require('async'),
Topics.setTopicField(tid, 'lastposttime', timestamp);
};
- Topics.onNewPostMade = function(postData) {
- Topics.increasePostCount(postData.tid);
- Topics.updateTimestamp(postData.tid, postData.timestamp);
- Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp);
- };
-
- emitter.on('event:newpost', Topics.onNewPostMade);
-
- Topics.addPostToTopic = function(tid, pid, timestamp, callback) {
- db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, callback);
- };
-
- Topics.removePostFromTopic = function(tid, pid, callback) {
- db.sortedSetRemove('tid:' + tid + ':posts', pid, callback);
- };
-
- Topics.getPids = function(tid, callback) {
- db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, callback);
- };
-
Topics.getUids = function(tid, callback) {
var uids = {};
Topics.getPids(tid, function(err, pids) {
diff --git a/src/topics/posts.js b/src/topics/posts.js
new file mode 100644
index 0000000000..a90be8fd0d
--- /dev/null
+++ b/src/topics/posts.js
@@ -0,0 +1,131 @@
+
+
+'use strict';
+
+var async = require('async'),
+
+ db = require('./../database'),
+ emitter = require('./../emitter'),
+ favourites = require('./../favourites'),
+ posts = require('./../posts'),
+ postTools = require('./../postTools');
+
+
+
+module.exports = function(Topics) {
+
+ Topics.onNewPostMade = function(postData) {
+ Topics.increasePostCount(postData.tid);
+ Topics.updateTimestamp(postData.tid, postData.timestamp);
+ Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp);
+ };
+
+ emitter.on('event:newpost', Topics.onNewPostMade);
+
+ Topics.getTopicPosts = function(tid, start, end, uid, reverse, callback) {
+ posts.getPostsByTid(tid, start, end, reverse, function(err, postData) {
+ if(err) {
+ return callback(err);
+ }
+
+ if (Array.isArray(postData) && !postData.length) {
+ return callback(null, []);
+ }
+
+ for(var i=0; i<postData.length; ++i) {
+ postData[i].index = start + i;
+ }
+
+ var pids = postData.map(function(post) {
+ return post.pid;
+ });
+
+ async.parallel({
+ favourites : function(next) {
+ favourites.getFavouritesByPostIDs(pids, uid, next);
+ },
+ voteData : function(next) {
+ favourites.getVoteStatusByPostIDs(pids, uid, next);
+ },
+ userData : function(next) {
+ async.each(postData, posts.addUserInfoToPost, next);
+ },
+ privileges : function(next) {
+ async.map(pids, function (pid, next) {
+ postTools.privileges(pid, uid, next);
+ }, next);
+ }
+ }, function(err, results) {
+ if(err) {
+ return callback(err);
+ }
+
+ for (var i = 0; i < postData.length; ++i) {
+ postData[i].favourited = results.favourites[i];
+ postData[i].upvoted = results.voteData[i].upvoted;
+ postData[i].downvoted = results.voteData[i].downvoted;
+ postData[i].votes = postData[i].votes || 0;
+ postData[i].display_moderator_tools = parseInt(uid, 10) !== 0 && results.privileges[i].editable;
+ postData[i].display_move_tools = results.privileges[i].move;
+
+ if(parseInt(postData[i].deleted, 10) === 1 && !results.privileges[i].view_deleted) {
+ postData[i].content = 'This post is deleted!';
+ }
+ }
+
+ callback(null, postData);
+ });
+ });
+ };
+
+ Topics.addPostToTopic = function(tid, pid, timestamp, callback) {
+ db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, callback);
+ };
+
+ Topics.removePostFromTopic = function(tid, pid, callback) {
+ db.sortedSetRemove('tid:' + tid + ':posts', pid, callback);
+ };
+
+ Topics.getPids = function(tid, callback) {
+ db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, callback);
+ };
+
+ Topics.increasePostCount = function(tid, callback) {
+ incrementFieldAndUpdateSortedSet(tid, 'postcount', 1, 'topics:posts', callback);
+ };
+
+ Topics.decreasePostCount = function(tid, callback) {
+ incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts', callback);
+ };
+
+ Topics.increaseViewCount = function(tid, callback) {
+ incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, 'topics:views', callback);
+ };
+
+ function incrementFieldAndUpdateSortedSet(tid, field, by, set, callback) {
+ db.incrObjectFieldBy('topic:' + tid, field, by, function(err, value) {
+ if(err) {
+ return callback(err);
+ }
+ db.sortedSetAdd(set, value, tid, callback);
+ });
+ }
+
+ Topics.getTitleByPid = function(pid, callback) {
+ Topics.getTopicFieldByPid('title', pid, callback);
+ };
+
+ Topics.getTopicFieldByPid = function(field, pid, callback) {
+ posts.getPostField(pid, 'tid', function(err, tid) {
+ Topics.getTopicField(tid, field, callback);
+ });
+ };
+
+ Topics.getTopicDataByPid = function(pid, callback) {
+ posts.getPostField(pid, 'tid', function(err, tid) {
+ Topics.getTopicData(tid, callback);
+ });
+ };
+
+
+};
diff --git a/src/topics/recent.js b/src/topics/recent.js
new file mode 100644
index 0000000000..bb51800785
--- /dev/null
+++ b/src/topics/recent.js
@@ -0,0 +1,36 @@
+
+
+'use strict';
+
+var db = require('./../database');
+
+module.exports = function(Topics) {
+
+ Topics.getLatestTopics = function(uid, start, end, term, callback) {
+ Topics.getLatestTids(start, end, term, function(err, tids) {
+ if(err) {
+ return callback(err);
+ }
+
+ Topics.getTopics('topics:recent', uid, tids, callback);
+ });
+ };
+
+ Topics.getLatestTids = function(start, end, term, callback) {
+ var terms = {
+ day: 86400000,
+ week: 604800000,
+ month: 2592000000
+ };
+
+ var since = terms.day;
+ if(terms[term]) {
+ since = terms[term];
+ }
+
+ var count = parseInt(end, 10) === -1 ? end : end - start + 1;
+
+ db.getSortedSetRevRangeByScore(['topics:recent', '+inf', Date.now() - since, 'LIMIT', start, count], callback);
+ };
+
+};