Node.js based forum software built for the modern web
refactor
Barış Soner Uşaklı2faf175739c1
diff --git a/src/groups/delete.js b/src/groups/delete.jsindex 346b2e1ef5..260c4ad381 100644--- a/src/groups/delete.js+++ b/src/groups/delete.js@@ -8,45 +8,48 @@ var batch = require('../batch');module.exports = function (Groups) {Groups.destroy = function (groupName, callback) {- Groups.getGroupsData([groupName], function (err, groupsData) {- if (err) {- return callback(err);- }- if (!Array.isArray(groupsData) || !groupsData[0]) {- return callback();- }- var groupObj = groupsData[0];-- async.parallel([- async.apply(db.delete, 'group:' + groupName),- async.apply(db.sortedSetRemove, 'groups:createtime', groupName),- async.apply(db.sortedSetRemove, 'groups:visible:createtime', groupName),- async.apply(db.sortedSetRemove, 'groups:visible:memberCount', groupName),- async.apply(db.sortedSetRemove, 'groups:visible:name', groupName.toLowerCase() + ':' + groupName),- async.apply(db.delete, 'group:' + groupName + ':members'),- async.apply(db.delete, 'group:' + groupName + ':pending'),- async.apply(db.delete, 'group:' + groupName + ':invited'),- async.apply(db.delete, 'group:' + groupName + ':owners'),- async.apply(db.delete, 'group:' + groupName + ':member:pids'),- async.apply(db.deleteObjectField, 'groupslug:groupname', utils.slugify(groupName)),- function (next) {- batch.processSortedSet('groups:createtime', function (groupNames, next) {- var keys = groupNames.map(function (group) {- return 'group:' + group + ':members';- });- db.sortedSetsRemove(keys, groupName, next);- }, {- batch: 500,- }, next);- },- ], function (err) {- if (err) {- return callback(err);+ var groupObj;+ async.waterfall([+ function (next) {+ Groups.getGroupsData([groupName], next);+ },+ function (groupsData, next) {+ if (!Array.isArray(groupsData) || !groupsData[0]) {+ return callback();}+ groupObj = groupsData[0];++ async.parallel([+ async.apply(db.delete, 'group:' + groupName),+ async.apply(db.sortedSetRemove, 'groups:createtime', groupName),+ async.apply(db.sortedSetRemove, 'groups:visible:createtime', groupName),+ async.apply(db.sortedSetRemove, 'groups:visible:memberCount', groupName),+ async.apply(db.sortedSetRemove, 'groups:visible:name', groupName.toLowerCase() + ':' + groupName),+ async.apply(db.delete, 'group:' + groupName + ':members'),+ async.apply(db.delete, 'group:' + groupName + ':pending'),+ async.apply(db.delete, 'group:' + groupName + ':invited'),+ async.apply(db.delete, 'group:' + groupName + ':owners'),+ async.apply(db.delete, 'group:' + groupName + ':member:pids'),+ async.apply(db.deleteObjectField, 'groupslug:groupname', utils.slugify(groupName)),+ function (next) {+ batch.processSortedSet('groups:createtime', function (groupNames, next) {+ var keys = groupNames.map(function (group) {+ return 'group:' + group + ':members';+ });+ db.sortedSetsRemove(keys, groupName, next);+ }, {+ batch: 500,+ }, next);+ },+ ], function (err) {+ next(err);+ });+ },+ function (next) {Groups.resetCache();plugins.fireHook('action:group.destroy', { group: groupObj });- callback();- });- });+ next();+ },+ ], callback);};};diff --git a/src/groups/membership.js b/src/groups/membership.jsindex 2660d716e1..1b74badb02 100644--- a/src/groups/membership.js+++ b/src/groups/membership.js@@ -287,13 +287,14 @@ module.exports = function (Groups) {Groups.getMemberUsers = function (groupNames, start, stop, callback) {async.map(groupNames, function (groupName, next) {- Groups.getMembers(groupName, start, stop, function (err, uids) {- if (err) {- return next(err);- }-- user.getUsersFields(uids, ['uid', 'username', 'picture', 'userslug'], next);- });+ async.waterfall([+ function (next) {+ Groups.getMembers(groupName, start, stop, next);+ },+ function (uids, next) {+ user.getUsersFields(uids, ['uid', 'username', 'picture', 'userslug'], next);+ },+ ], next);}, callback);};diff --git a/src/meta/blacklist.js b/src/meta/blacklist.jsindex 31ce06af8c..1ef6cfe0a9 100644--- a/src/meta/blacklist.js+++ b/src/meta/blacklist.js@@ -13,27 +13,24 @@ var Blacklist = {Blacklist.load = function (callback) {callback = callback || function () {};+async.waterfall([Blacklist.get,Blacklist.validate,- ], function (err, rules) {- if (err) {- return callback(err);- }-- winston.verbose('[meta/blacklist] Loading ' + rules.valid.length + ' blacklist rules');- if (rules.invalid.length) {- winston.warn('[meta/blacklist] ' + rules.invalid.length + ' invalid blacklist rule(s) were ignored.');- }-- Blacklist._rules = {- ipv4: rules.ipv4,- ipv6: rules.ipv6,- cidr: rules.cidr,- };-- callback();- });+ function (rules, next) {+ winston.verbose('[meta/blacklist] Loading ' + rules.valid.length + ' blacklist rules');+ if (rules.invalid.length) {+ winston.warn('[meta/blacklist] ' + rules.invalid.length + ' invalid blacklist rule(s) were ignored.');+ }++ Blacklist._rules = {+ ipv4: rules.ipv4,+ ipv6: rules.ipv6,+ cidr: rules.cidr,+ };+ next();+ },+ ], callback);};pubsub.on('blacklist:reload', Blacklist.load);diff --git a/src/meta/cacheBuster.js b/src/meta/cacheBuster.jsindex 461bcaf801..d402fe16f7 100644--- a/src/meta/cacheBuster.js+++ b/src/meta/cacheBuster.js@@ -1,5 +1,6 @@'use strict';+var async = require('async');var fs = require('fs');var path = require('path');var mkdirp = require('mkdirp');@@ -15,13 +16,14 @@ function generate() {}exports.write = function write(callback) {- mkdirp(path.dirname(filePath), function (err) {- if (err) {- return callback(err);- }-- fs.writeFile(filePath, generate(), callback);- });+ async.waterfall([+ function (next) {+ mkdirp(path.dirname(filePath), next);+ },+ function (data, next) {+ fs.writeFile(filePath, generate(), next);+ },+ ], callback);};exports.read = function read(callback) {diff --git a/src/meta/logs.js b/src/meta/logs.jsindex 4df96261cc..30dd983a36 100644--- a/src/meta/logs.js+++ b/src/meta/logs.js@@ -2,7 +2,6 @@var path = require('path');var fs = require('fs');-var winston = require('winston');module.exports = function (Meta) {Meta.logs = {@@ -12,13 +11,7 @@ module.exports = function (Meta) {Meta.logs.get = function (callback) {fs.readFile(Meta.logs.path, {encoding: 'utf-8',- }, function (err, logs) {- if (err) {- winston.error('[meta/logs] Could not retrieve logs: ' + err.message);- }-- callback(undefined, logs || '');- });+ }, callback);};Meta.logs.clear = function (callback) {diff --git a/src/meta/templates.js b/src/meta/templates.jsindex 9420959694..f6776f64e1 100644--- a/src/meta/templates.js+++ b/src/meta/templates.js@@ -11,14 +11,75 @@ var nconf = require('nconf');var plugins = require('../plugins');var file = require('../file');-var Templates = {};+var Templates = module.exports;Templates.compile = function (callback) {callback = callback || function () {};- compile(callback);-};+ var themeConfig = require(nconf.get('theme_config'));+ var baseTemplatesPaths = themeConfig.baseTheme ? getBaseTemplates(themeConfig.baseTheme) : [nconf.get('base_templates_path')];+ var viewsPath = nconf.get('views_dir');++ function processImports(paths, relativePath, source, callback) {+ var regex = /<!-- IMPORT (.+?) -->/;++ var matches = source.match(regex);++ if (!matches) {+ return callback(null, source);+ }++ var partial = '/' + matches[1];+ if (paths[partial] && relativePath !== partial) {+ fs.readFile(paths[partial], function (err, file) {+ if (err) {+ return callback(err);+ }++ var partialSource = file.toString();+ source = source.replace(regex, partialSource);++ processImports(paths, relativePath, source, callback);+ });+ } else {+ winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);+ source = source.replace(regex, '');++ processImports(paths, relativePath, source, callback);+ }+ }+ async.waterfall([+ function (next) {+ preparePaths(baseTemplatesPaths, next);+ },+ function (paths, next) {+ async.each(Object.keys(paths), function (relativePath, next) {+ async.waterfall([+ function (next) {+ fs.readFile(paths[relativePath], next);+ },+ function (file, next) {+ var source = file.toString();+ processImports(paths, relativePath, source, next);+ },+ function (compiled, next) {+ mkdirp(path.join(viewsPath, path.dirname(relativePath)), function (err) {+ next(err, compiled);+ });+ },+ function (compiled, next) {+ fs.writeFile(path.join(viewsPath, relativePath), compiled, next);+ },+ ], next);+ }, next);+ },+ function (next) {+ winston.verbose('[meta/templates] Successfully compiled templates.');+ next();+ },+ ], callback);+};function getBaseTemplates(theme) {var baseTemplatesPaths = [];@@ -39,7 +100,7 @@ function getBaseTemplates(theme) {function preparePaths(baseTemplatesPaths, callback) {var coreTemplatesPath = nconf.get('core_templates_path');var viewsPath = nconf.get('views_dir');-+ var pluginTemplates;async.waterfall([function (next) {rimraf(viewsPath, next);@@ -53,32 +114,31 @@ function preparePaths(baseTemplatesPaths, callback) {function (next) {plugins.getTemplates(next);},- ], function (err, pluginTemplates) {- if (err) {- return callback(err);- }+ function (_pluginTemplates, next) {+ pluginTemplates = _pluginTemplates;+ winston.verbose('[meta/templates] Compiling templates');- winston.verbose('[meta/templates] Compiling templates');-- async.parallel({- coreTpls: function (next) {- file.walk(coreTemplatesPath, next);- },- baseThemes: function (next) {- async.map(baseTemplatesPaths, function (baseTemplatePath, next) {- file.walk(baseTemplatePath, function (err, paths) {- paths = paths.map(function (tpl) {- return {- base: baseTemplatePath,- path: tpl.replace(baseTemplatePath, ''),- };+ async.parallel({+ coreTpls: function (next) {+ file.walk(coreTemplatesPath, next);+ },+ baseThemes: function (next) {+ async.map(baseTemplatesPaths, function (baseTemplatePath, next) {+ file.walk(baseTemplatePath, function (err, paths) {+ paths = paths.map(function (tpl) {+ return {+ base: baseTemplatePath,+ path: tpl.replace(baseTemplatePath, ''),+ };+ });++ next(err, paths);});-- next(err, paths);- });- }, next);- },- }, function (err, data) {+ }, next);+ },+ }, next);+ },+ function (data, next) {var baseThemes = data.baseThemes;var coreTpls = data.coreTpls;var paths = {};@@ -99,79 +159,7 @@ function preparePaths(baseTemplatesPaths, callback) {}}- callback(err, paths);- });- });-}--function compile(callback) {- var themeConfig = require(nconf.get('theme_config'));- var baseTemplatesPaths = themeConfig.baseTheme ? getBaseTemplates(themeConfig.baseTheme) : [nconf.get('base_templates_path')];- var viewsPath = nconf.get('views_dir');-- function processImports(paths, relativePath, source, callback) {- var regex = /<!-- IMPORT (.+?) -->/;-- var matches = source.match(regex);-- if (!matches) {- return callback(null, source);- }-- var partial = '/' + matches[1];- if (paths[partial] && relativePath !== partial) {- fs.readFile(paths[partial], function (err, file) {- if (err) {- return callback(err);- }-- var partialSource = file.toString();- source = source.replace(regex, partialSource);-- processImports(paths, relativePath, source, callback);- });- } else {- winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);- source = source.replace(regex, '');-- processImports(paths, relativePath, source, callback);- }- }-- preparePaths(baseTemplatesPaths, function (err, paths) {- if (err) {- return callback(err);- }-- async.each(Object.keys(paths), function (relativePath, next) {- async.waterfall([- function (next) {- fs.readFile(paths[relativePath], next);- },- function (file, next) {- var source = file.toString();- processImports(paths, relativePath, source, next);- },- function (compiled, next) {- mkdirp(path.join(viewsPath, path.dirname(relativePath)), function (err) {- next(err, compiled);- });- },- function (compiled, next) {- fs.writeFile(path.join(viewsPath, relativePath), compiled, next);- },- ], next);- }, function (err) {- if (err) {- winston.error('[meta/templates] ' + err.stack);- return callback(err);- }-- winston.verbose('[meta/templates] Successfully compiled templates.');-- callback();- });- });+ next(null, paths);+ },+ ], callback);}--module.exports = Templates;diff --git a/src/meta/themes.js b/src/meta/themes.jsindex 0d3be261e0..a41bf52e59 100644--- a/src/meta/themes.js+++ b/src/meta/themes.js@@ -19,27 +19,25 @@ module.exports = function (Meta) {return callback(null, []);}- fs.readdir(themePath, function (err, files) {- if (err) {- return callback(err);- }-- async.filter(files, function (file, next) {- fs.stat(path.join(themePath, file), function (err, fileStat) {- if (err) {- if (err.code === 'ENOENT') {- return next(null, false);+ async.waterfall([+ function (next) {+ fs.readdir(themePath, next);+ },+ function (files, next) {+ async.filter(files, function (file, next) {+ fs.stat(path.join(themePath, file), function (err, fileStat) {+ if (err) {+ if (err.code === 'ENOENT') {+ return next(null, false);+ }+ return next(err);}- return next(err);- }-- next(null, (fileStat.isDirectory() && file.slice(0, 13) === 'nodebb-theme-'));- });- }, function (err, themes) {- if (err) {- return callback(err);- }+ next(null, (fileStat.isDirectory() && file.slice(0, 13) === 'nodebb-theme-'));+ });+ }, next);+ },+ function (themes, next) {async.map(themes, function (theme, next) {var config = path.join(themePath, theme, 'theme.json');@@ -66,16 +64,13 @@ module.exports = function (Meta) {next(null, null);}});- }, function (err, themes) {- if (err) {- return callback(err);- }-- themes = themes.filter(Boolean);- callback(null, themes);- });- });- });+ }, next);+ },+ function (themes, next) {+ themes = themes.filter(Boolean);+ next(null, themes);+ },+ ], callback);};Meta.themes.set = function (data, callback) {@@ -134,33 +129,34 @@ module.exports = function (Meta) {};Meta.themes.setupPaths = function (callback) {- async.parallel({- themesData: Meta.themes.get,- currentThemeId: function (next) {- db.getObjectField('config', 'theme:id', next);+ async.waterfall([+ function (next) {+ async.parallel({+ themesData: Meta.themes.get,+ currentThemeId: function (next) {+ db.getObjectField('config', 'theme:id', next);+ },+ }, next);},- }, function (err, data) {- if (err) {- return callback(err);- }-- var themeId = data.currentThemeId || 'nodebb-theme-persona';+ function (data, next) {+ var themeId = data.currentThemeId || 'nodebb-theme-persona';- var themeObj = data.themesData.filter(function (themeObj) {- return themeObj.id === themeId;- })[0];+ var themeObj = data.themesData.filter(function (themeObj) {+ return themeObj.id === themeId;+ })[0];- if (process.env.NODE_ENV === 'development') {- winston.info('[themes] Using theme ' + themeId);- }+ if (process.env.NODE_ENV === 'development') {+ winston.info('[themes] Using theme ' + themeId);+ }- if (!themeObj) {- return callback(new Error('[[error:theme-not-found]]'));- }+ if (!themeObj) {+ return callback(new Error('[[error:theme-not-found]]'));+ }- Meta.themes.setPath(themeObj);- callback();- });+ Meta.themes.setPath(themeObj);+ next();+ },+ ], callback);};Meta.themes.setPath = function (themeObj) {