Skip to content

Node.js based forum software built for the modern web

refactor

Barış Soner Uşaklıf57b5f4e4cec
diff --git a/src/categories/data.js b/src/categories/data.js
index 25b7021610..e3d9296c72 100644
--- a/src/categories/data.js
+++ b/src/categories/data.js
@@ -8,32 +8,19 @@ var db = require('../database');
module.exports = function (Categories) {
Categories.getCategoryData = function (cid, callback) {
- db.getObject('category:' + cid, function (err, category) {
- if (err) {
- return callback(err);
- }
-
- modifyCategory(category);
- callback(null, category);
- });
+ async.waterfall([
+ function (next) {
+ db.getObject('category:' + cid, next);
+ },
+ function (category, next) {
+ modifyCategory(category);
+ next(null, category);
+ },
+ ], callback);
};
Categories.getCategoriesData = function (cids, callback) {
- if (!Array.isArray(cids) || !cids.length) {
- return callback(null, []);
- }
- var keys = cids.map(function (cid) {
- return 'category:' + cid;
- });
-
- db.getObjects(keys, function (err, categories) {
- if (err || !Array.isArray(categories) || !categories.length) {
- return callback(err, []);
- }
-
- categories.forEach(modifyCategory);
- callback(null, categories);
- });
+ Categories.getCategoriesFields(cids, [], callback);
};
function modifyCategory(category) {
@@ -76,15 +63,19 @@ module.exports = function (Categories) {
var keys = cids.map(function (cid) {
return 'category:' + cid;
});
-
- db.getObjectsFields(keys, fields, function (err, categories) {
- if (err) {
- return callback(err);
- }
-
- categories.forEach(modifyCategory);
- callback(null, categories);
- });
+ async.waterfall([
+ function (next) {
+ if (fields.length) {
+ db.getObjectsFields(keys, fields, next);
+ } else {
+ db.getObjects(keys, next);
+ }
+ },
+ function (categories, next) {
+ categories.forEach(modifyCategory);
+ next(null, categories);
+ },
+ ], callback);
};
Categories.getMultipleCategoryFields = function (cids, fields, callback) {
diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js
index 595cba3f9e..5936863102 100644
--- a/src/categories/recentreplies.js
+++ b/src/categories/recentreplies.js
@@ -2,7 +2,6 @@
'use strict';
var async = require('async');
-var winston = require('winston');
var _ = require('underscore');
var db = require('../database');
@@ -31,36 +30,33 @@ module.exports = function (Categories) {
};
Categories.updateRecentTid = function (cid, tid, callback) {
- async.parallel({
- count: function (next) {
- db.sortedSetCard('cid:' + cid + ':recent_tids', next);
+ async.waterfall([
+ function (next) {
+ async.parallel({
+ count: function (next) {
+ db.sortedSetCard('cid:' + cid + ':recent_tids', next);
+ },
+ numRecentReplies: function (next) {
+ db.getObjectField('category:' + cid, 'numRecentReplies', next);
+ },
+ }, next);
},
- numRecentReplies: function (next) {
- db.getObjectField('category:' + cid, 'numRecentReplies', next);
+ function (results, next) {
+ if (results.count < results.numRecentReplies) {
+ return db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, callback);
+ }
+ db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, results.count - results.numRecentReplies, next);
},
- }, function (err, results) {
- if (err) {
- return callback(err);
- }
-
- if (results.count < results.numRecentReplies) {
- return db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, callback);
- }
- async.waterfall([
- function (next) {
- db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, results.count - results.numRecentReplies, next);
- },
- function (data, next) {
- if (!data.length) {
- return next();
- }
- db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score, next);
- },
- function (next) {
- db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, next);
- },
- ], callback);
- });
+ function (data, next) {
+ if (!data.length) {
+ return next();
+ }
+ db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score, next);
+ },
+ function (next) {
+ db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, next);
+ },
+ ], callback);
};
Categories.getRecentTopicReplies = function (categoryData, uid, callback) {
@@ -185,65 +181,60 @@ module.exports = function (Categories) {
Categories.moveRecentReplies = function (tid, oldCid, cid, callback) {
callback = callback || function () {};
updatePostCount(tid, oldCid, cid);
- topics.getPids(tid, function (err, pids) {
- if (err) {
- return winston.error(err.message);
- }
+ async.waterfall([
+ function (next) {
+ topics.getPids(tid, next);
+ },
+ function (pids, next) {
+ if (!Array.isArray(pids) || !pids.length) {
+ return callback();
+ }
- if (!Array.isArray(pids) || !pids.length) {
- return;
- }
+ batch.processArray(pids, function (pids, next) {
+ async.waterfall([
+ function (next) {
+ posts.getPostsFields(pids, ['timestamp'], next);
+ },
+ function (postData, next) {
+ var timestamps = postData.map(function (post) {
+ return post && post.timestamp;
+ });
+
+ async.parallel([
+ function (next) {
+ db.sortedSetRemove('cid:' + oldCid + ':pids', pids, next);
+ },
+ function (next) {
+ db.sortedSetAdd('cid:' + cid + ':pids', timestamps, pids, next);
+ },
+ ], next);
+ },
+ ], next);
+ }, next);
+ },
+ ], callback);
+ };
- batch.processArray(pids, function (pids, next) {
- async.waterfall([
+ function updatePostCount(tid, oldCid, newCid, callback) {
+ callback = callback || function () {};
+ async.waterfall([
+ function (next) {
+ topics.getTopicField(tid, 'postcount', next);
+ },
+ function (postCount, next) {
+ if (!parseInt(postCount, 10)) {
+ return callback();
+ }
+ async.parallel([
function (next) {
- posts.getPostsFields(pids, ['timestamp'], next);
+ db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount, next);
},
- function (postData, next) {
- var timestamps = postData.map(function (post) {
- return post && post.timestamp;
- });
-
- async.parallel([
- function (next) {
- db.sortedSetRemove('cid:' + oldCid + ':pids', pids, next);
- },
- function (next) {
- db.sortedSetAdd('cid:' + cid + ':pids', timestamps, pids, next);
- },
- ], next);
+ function (next) {
+ db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount, next);
},
], next);
- }, function (err) {
- if (err) {
- winston.error(err.stack);
- }
- callback(err);
- });
- });
- };
-
- function updatePostCount(tid, oldCid, newCid) {
- topics.getTopicField(tid, 'postcount', function (err, postCount) {
- if (err) {
- return winston.error(err.message);
- }
- if (!parseInt(postCount, 10)) {
- return;
- }
- async.parallel([
- function (next) {
- db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount, next);
- },
- function (next) {
- db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount, next);
- },
- ], function (err) {
- if (err) {
- winston.error(err.message);
- }
- });
- });
+ },
+ ], callback);
}
};
diff --git a/src/categories/unread.js b/src/categories/unread.js
index 1ef6ee40b5..498f5e65dc 100644
--- a/src/categories/unread.js
+++ b/src/categories/unread.js
@@ -1,5 +1,7 @@
'use strict';
+var async = require('async');
+
var db = require('../database');
module.exports = function (Categories) {
@@ -12,21 +14,22 @@ module.exports = function (Categories) {
return 'cid:' + cid + ':read_by_uid';
});
- db.isMemberOfSets(keys, uid, function (err, hasRead) {
- if (err) {
- return callback(err);
- }
-
- keys = keys.filter(function (key, index) {
- return !hasRead[index];
- });
-
- if (!keys.length) {
- return callback();
- }
-
- db.setsAdd(keys, uid, callback);
- });
+ async.waterfall([
+ function (next) {
+ db.isMemberOfSets(keys, uid, next);
+ },
+ function (hasRead, next) {
+ keys = keys.filter(function (key, index) {
+ return !hasRead[index];
+ });
+
+ if (!keys.length) {
+ return callback();
+ }
+
+ db.setsAdd(keys, uid, next);
+ },
+ ], callback);
};
Categories.markAsUnreadForAll = function (cid, callback) {
@@ -38,11 +41,9 @@ module.exports = function (Categories) {
};
Categories.hasReadCategories = function (cids, uid, callback) {
- var sets = [];
-
- for (var i = 0, ii = cids.length; i < ii; i += 1) {
- sets.push('cid:' + cids[i] + ':read_by_uid');
- }
+ var sets = cids.map(function (cid) {
+ return 'cid:' + cid + ':read_by_uid';
+ });
db.isMemberOfSets(sets, uid, callback);
};
diff --git a/src/middleware/admin.js b/src/middleware/admin.js
index f40e273317..d320e9c850 100644
--- a/src/middleware/admin.js
+++ b/src/middleware/admin.js
@@ -15,74 +15,54 @@ module.exports = function (middleware) {
middleware.admin = {};
middleware.admin.isAdmin = function (req, res, next) {
winston.warn('[middleware.admin.isAdmin] deprecation warning, no need to use this from plugins!');
+ middleware.isAdmin(req, res, next);
+ };
+
+ middleware.admin.buildHeader = function (req, res, next) {
+ res.locals.renderAdminHeader = true;
async.waterfall([
function (next) {
- user.isAdministrator(req.uid, next);
+ controllers.api.getConfig(req, res, next);
},
- function (isAdmin, next) {
- if (!isAdmin) {
- return controllers.helpers.notAllowed(req, res);
- }
+ function (config, next) {
+ res.locals.config = config;
next();
},
], next);
};
- middleware.admin.buildHeader = function (req, res, next) {
- res.locals.renderAdminHeader = true;
-
- controllers.api.getConfig(req, res, function (err, config) {
- if (err) {
- return next(err);
- }
-
- res.locals.config = config;
- next();
- });
- };
-
middleware.admin.renderHeader = function (req, res, data, next) {
var custom_header = {
plugins: [],
authentication: [],
};
- user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed'], function (err, userData) {
- if (err) {
- return next(err);
- }
-
- userData.uid = req.uid;
- userData['email:confirmed'] = parseInt(userData['email:confirmed'], 10) === 1;
-
- async.parallel({
- scripts: function (next) {
- plugins.fireHook('filter:admin.scripts.get', [], function (err, scripts) {
- if (err) {
- return next(err);
- }
- var arr = [];
- scripts.forEach(function (script) {
- arr.push({ src: script });
- });
+ async.waterfall([
+ function (next) {
+ async.parallel({
+ userData: function (next) {
+ user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed'], next);
+ },
+ scripts: function (next) {
+ getAdminScripts(next);
+ },
+ custom_header: function (next) {
+ plugins.fireHook('filter:admin.header.build', custom_header, next);
+ },
+ config: function (next) {
+ controllers.api.getConfig(req, res, next);
+ },
+ configs: function (next) {
+ meta.configs.list(next);
+ },
+ }, next);
+ },
+ function (results, next) {
+ var userData = results.userData;
+ userData.uid = req.uid;
+ userData['email:confirmed'] = parseInt(userData['email:confirmed'], 10) === 1;
- next(null, arr);
- });
- },
- custom_header: function (next) {
- plugins.fireHook('filter:admin.header.build', custom_header, next);
- },
- config: function (next) {
- controllers.api.getConfig(req, res, next);
- },
- configs: function (next) {
- meta.configs.list(next);
- },
- }, function (err, results) {
- if (err) {
- return next(err);
- }
res.locals.config = results.config;
var acpPath = req.path.slice(1).split('/');
@@ -111,10 +91,22 @@ module.exports = function (middleware) {
templateValues.template[res.locals.template] = true;
req.app.render('admin/header', templateValues, next);
- });
- });
+ },
+ ], next);
};
+ function getAdminScripts(callback) {
+ async.waterfall([
+ function (next) {
+ plugins.fireHook('filter:admin.scripts.get', [], next);
+ },
+ function (scripts, next) {
+ next(null, scripts.map(function (script) {
+ return { src: script };
+ }));
+ },
+ ], callback);
+ }
middleware.admin.renderFooter = function (req, res, data, next) {
req.app.render('admin/footer', data, next);