Skip to content

Node.js based forum software built for the modern web

categories refactor

barisusaklicc0ac29b3667
diff --git a/src/categories.js b/src/categories.js
index c2cddcf744..134fea78f2 100644
--- a/src/categories.js
+++ b/src/categories.js
@@ -17,6 +17,9 @@ var db = require('./database'),
(function(Categories) {
+ require('./categories/activeusers')(Categories);
+ require('./categories/recentreplies')(Categories);
+
Categories.create = function(data, callback) {
db.incrObjectField('global', 'nextCid', function(err, cid) {
if (err) {
@@ -56,7 +59,6 @@ var db = require('./database'),
};
Categories.getCategoryById = function(cid, start, end, uid, callback) {
-
CategoryTools.exists(cid, function(err, exists) {
if(err || !exists) {
return callback(err || new Error('category-not-found [' + cid + ']'));
@@ -226,56 +228,6 @@ var db = require('./database'),
db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback);
};
- Categories.getRecentReplies = function(cid, uid, count, callback) {
- if(count === 0 || !count) {
- return callback(null, []);
- }
-
- CategoryTools.privileges(cid, uid, function(err, privileges) {
- if(err) {
- return callback(err);
- }
-
- if (!privileges.read) {
- return callback(null, []);
- }
-
- db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
- if (err) {
- return callback(err, []);
- }
-
- if (!pids || !pids.length) {
- return callback(null, []);
- }
-
- posts.getPostSummaryByPids(pids, true, callback);
- });
- });
- };
-
- Categories.moveRecentReplies = function(tid, oldCid, cid, callback) {
- function movePost(pid, callback) {
- posts.getPostField(pid, 'timestamp', function(err, timestamp) {
- if(err) {
- return callback(err);
- }
-
- db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, pid);
- db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
- callback();
- });
- }
-
- topics.getPids(tid, function(err, pids) {
- if(err) {
- return callback(err);
- }
-
- async.each(pids, movePost, callback);
- });
- };
-
Categories.getCategoryData = function(cid, callback) {
Categories.getCategoriesData([cid], function(err, categories) {
callback(err, categories ? categories[0] : null);
@@ -351,79 +303,6 @@ var db = require('./database'),
});
};
- Categories.isUserActiveIn = function(cid, uid, callback) {
-
- db.getSortedSetRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
- if (err) {
- return callback(err);
- }
-
- var index = 0,
- active = false;
-
- async.whilst(
- function() {
- return active === false && index < pids.length;
- },
- function(callback) {
- posts.getCidByPid(pids[index], function(err, postCid) {
- if (err) {
- return callback(err);
- }
-
- if (postCid === cid) {
- active = true;
- }
-
- ++index;
- callback();
- });
- },
- function(err) {
- callback(err, active);
- }
- );
- });
- };
-
- Categories.addActiveUser = function(cid, uid, timestamp) {
- if(parseInt(uid, 10)) {
- db.sortedSetAdd('cid:' + cid + ':active_users', timestamp, uid);
- }
- };
-
- Categories.removeActiveUser = function(cid, uid) {
- db.sortedSetRemove('cid:' + cid + ':active_users', uid);
- };
-
- Categories.getActiveUsers = function(cid, callback) {
- db.getSortedSetRevRange('cid:' + cid + ':active_users', 0, 23, callback);
- };
-
- Categories.moveActiveUsers = function(tid, oldCid, cid, callback) {
- function updateUser(uid, timestamp) {
- Categories.addActiveUser(cid, uid, timestamp);
- Categories.isUserActiveIn(oldCid, uid, function(err, active) {
-
- if (!err && !active) {
- Categories.removeActiveUser(oldCid, uid);
- }
- });
- }
-
- topics.getTopicField(tid, 'timestamp', function(err, timestamp) {
- if(!err) {
- topics.getUids(tid, function(err, uids) {
- if (!err && uids) {
- for (var i = 0; i < uids.length; ++i) {
- updateUser(uids[i], timestamp);
- }
- }
- });
- }
- });
- };
-
Categories.onNewPostMade = function(uid, tid, pid, timestamp) {
topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) {
diff --git a/src/categories/activeusers.js b/src/categories/activeusers.js
new file mode 100644
index 0000000000..059432f3e5
--- /dev/null
+++ b/src/categories/activeusers.js
@@ -0,0 +1,83 @@
+'use strict';
+
+var async = require('async'),
+
+ db = require('./../database'),
+ posts = require('./../posts'),
+ topics = require('./../topics');
+
+module.exports = function(Categories) {
+
+ Categories.isUserActiveIn = function(cid, uid, callback) {
+
+ db.getSortedSetRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
+ if (err) {
+ return callback(err);
+ }
+
+ var index = 0,
+ active = false;
+
+ async.whilst(
+ function() {
+ return active === false && index < pids.length;
+ },
+ function(callback) {
+ posts.getCidByPid(pids[index], function(err, postCid) {
+ if (err) {
+ return callback(err);
+ }
+
+ if (postCid === cid) {
+ active = true;
+ }
+
+ ++index;
+ callback();
+ });
+ },
+ function(err) {
+ callback(err, active);
+ }
+ );
+ });
+ };
+
+ Categories.addActiveUser = function(cid, uid, timestamp) {
+ if(parseInt(uid, 10)) {
+ db.sortedSetAdd('cid:' + cid + ':active_users', timestamp, uid);
+ }
+ };
+
+ Categories.removeActiveUser = function(cid, uid) {
+ db.sortedSetRemove('cid:' + cid + ':active_users', uid);
+ };
+
+ Categories.getActiveUsers = function(cid, callback) {
+ db.getSortedSetRevRange('cid:' + cid + ':active_users', 0, 23, callback);
+ };
+
+ Categories.moveActiveUsers = function(tid, oldCid, cid, callback) {
+ function updateUser(uid, timestamp) {
+ Categories.addActiveUser(cid, uid, timestamp);
+ Categories.isUserActiveIn(oldCid, uid, function(err, active) {
+
+ if (!err && !active) {
+ Categories.removeActiveUser(oldCid, uid);
+ }
+ });
+ }
+
+ topics.getTopicField(tid, 'timestamp', function(err, timestamp) {
+ if(!err) {
+ topics.getUids(tid, function(err, uids) {
+ if (!err && uids) {
+ for (var i = 0; i < uids.length; ++i) {
+ updateUser(uids[i], timestamp);
+ }
+ }
+ });
+ }
+ });
+ };
+};
\ No newline at end of file
diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js
new file mode 100644
index 0000000000..a5dd10bc4e
--- /dev/null
+++ b/src/categories/recentreplies.js
@@ -0,0 +1,62 @@
+
+'use strict';
+
+var async = require('async'),
+ db = require('./../database'),
+ posts = require('./../posts'),
+ topics = require('./../topics'),
+ CategoryTools = require('./../categoryTools');
+
+module.exports = function(Categories) {
+ Categories.getRecentReplies = function(cid, uid, count, callback) {
+ if(count === 0 || !count) {
+ return callback(null, []);
+ }
+
+ CategoryTools.privileges(cid, uid, function(err, privileges) {
+ if(err) {
+ return callback(err);
+ }
+
+ if (!privileges.read) {
+ return callback(null, []);
+ }
+
+ db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
+ if (err) {
+ return callback(err, []);
+ }
+
+ if (!pids || !pids.length) {
+ return callback(null, []);
+ }
+
+ posts.getPostSummaryByPids(pids, true, callback);
+ });
+ });
+ };
+
+ Categories.moveRecentReplies = function(tid, oldCid, cid, callback) {
+ function movePost(pid, callback) {
+ posts.getPostField(pid, 'timestamp', function(err, timestamp) {
+ if(err) {
+ return callback(err);
+ }
+
+ db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, pid);
+ db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
+ callback();
+ });
+ }
+
+ topics.getPids(tid, function(err, pids) {
+ if(err) {
+ return callback(err);
+ }
+
+ async.each(pids, movePost, callback);
+ });
+ };
+};
+
+