refactor: async/await

v1.18.x
Barış Soner Uşaklı 5 years ago
parent 63a741cfb3
commit 75d7ae9272

@ -83,7 +83,7 @@ if (nconf.get('setup') || nconf.get('install')) {
} else if (nconf.get('plugins')) { } else if (nconf.get('plugins')) {
require('./src/cli/manage').listPlugins(); require('./src/cli/manage').listPlugins();
} else if (nconf.get('build')) { } else if (nconf.get('build')) {
require('./src/meta/build').build(nconf.get('build')); require('./src/cli/manage').build(nconf.get('build'));
} else if (nconf.get('events')) { } else if (nconf.get('events')) {
require('./src/cli/manage').listEvents(); require('./src/cli/manage').listEvents();
} else { } else {

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const path = require('path');
var async = require('async'); const async = require('async');
var sanitizeHTML = require('sanitize-html'); const sanitizeHTML = require('sanitize-html');
var nconf = require('nconf'); const nconf = require('nconf');
var file = require('../file'); const file = require('../file');
var Translator = require('../translator').Translator; const Translator = require('../translator').Translator;
function filterDirectories(directories) { function filterDirectories(directories) {
return directories.map(function (dir) { return directories.map(function (dir) {
@ -204,3 +204,5 @@ module.exports.getDictionary = getDictionary;
module.exports.filterDirectories = filterDirectories; module.exports.filterDirectories = filterDirectories;
module.exports.simplify = simplify; module.exports.simplify = simplify;
module.exports.sanitize = sanitize; module.exports.sanitize = sanitize;
require('../promisify')(module.exports);

@ -192,7 +192,17 @@ function info() {
}); });
} }
exports.build = build.build; function buildWrapper(targets, options) {
build.build(targets, options, function (err) {
if (err) {
winston.error(err);
process.exit(1);
}
process.exit(0);
});
}
exports.build = buildWrapper;
exports.buildTargets = buildTargets; exports.buildTargets = buildTargets;
exports.activate = activate; exports.activate = activate;
exports.listPlugins = listPlugins; exports.listPlugins = listPlugins;

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var async = require('async'); const async = require('async');
var winston = require('winston'); const winston = require('winston');
var nconf = require('nconf'); const nconf = require('nconf');
var _ = require('lodash'); const _ = require('lodash');
var cacheBuster = require('./cacheBuster'); const cacheBuster = require('./cacheBuster');
var meta; let meta;
function step(target, callback) { function step(target, callback) {
var startTime = Date.now(); var startTime = Date.now();
@ -134,7 +134,7 @@ function buildTargets(targets, parallel, callback) {
}, callback); }, callback);
} }
function build(targets, options, callback) { exports.build = function (targets, options, callback) {
if (!callback && typeof options === 'function') { if (!callback && typeof options === 'function') {
callback = options; callback = options;
options = {}; options = {};
@ -178,17 +178,6 @@ function build(targets, options, callback) {
winston.verbose('[build] building the following targets: ' + targets.join(', ')); winston.verbose('[build] building the following targets: ' + targets.join(', '));
if (typeof callback !== 'function') {
callback = function (err) {
if (err) {
winston.error(err);
process.exit(1);
} else {
process.exit(0);
}
};
}
if (!targets) { if (!targets) {
winston.info('[build] No valid targets supplied. Aborting.'); winston.info('[build] No valid targets supplied. Aborting.');
callback(); callback();
@ -226,10 +215,10 @@ function build(targets, options, callback) {
winston.info('[build] Asset compilation successful. Completed in ' + totalTime + 'sec.'); winston.info('[build] Asset compilation successful. Completed in ' + totalTime + 'sec.');
callback(); callback();
}); });
} };
exports.build = build;
exports.buildAll = function (callback) { exports.buildAll = function (callback) {
build(allTargets, callback); exports.build(allTargets, callback);
}; };
require('../promisify')(exports);

@ -1,48 +1,47 @@
'use strict'; 'use strict';
var async = require('async'); const async = require('async');
var winston = require('winston'); const winston = require('winston');
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const path = require('path');
var nconf = require('nconf'); const nconf = require('nconf');
var meta = require('../meta'); const meta = require('../meta');
var plugins = require('../plugins'); const plugins = require('../plugins');
var widgets = require('../widgets'); const widgets = require('../widgets');
var user = require('../user'); const user = require('../user');
var userDigest = require('../user/digest'); const userDigest = require('../user/digest');
var userEmail = require('../user/email'); const userEmail = require('../user/email');
var logger = require('../logger'); const logger = require('../logger');
var events = require('../events'); const events = require('../events');
var notifications = require('../notifications'); const notifications = require('../notifications');
var emailer = require('../emailer'); const emailer = require('../emailer');
var db = require('../database'); const db = require('../database');
var analytics = require('../analytics'); const analytics = require('../analytics');
var websockets = require('../socket.io/index'); const websockets = require('../socket.io/index');
var index = require('./index'); const index = require('./index');
var getAdminSearchDict = require('../admin/search').getDictionary; const getAdminSearchDict = require('../admin/search').getDictionary;
var utils = require('../../public/src/utils'); const utils = require('../../public/src/utils');
var SocketAdmin = { const SocketAdmin = module.exports;
user: require('./admin/user'), SocketAdmin.user = require('./admin/user');
categories: require('./admin/categories'), SocketAdmin.categories = require('./admin/categories');
groups: require('./admin/groups'), SocketAdmin.groups = require('./admin/groups');
tags: require('./admin/tags'), SocketAdmin.tags = require('./admin/tags');
rewards: require('./admin/rewards'), SocketAdmin.rewards = require('./admin/rewards');
navigation: require('./admin/navigation'), SocketAdmin.navigation = require('./admin/navigation');
rooms: require('./admin/rooms'), SocketAdmin.rooms = require('./admin/rooms');
social: require('./admin/social'), SocketAdmin.social = require('./admin/social');
themes: {}, SocketAdmin.themes = {};
plugins: {}, SocketAdmin.plugins = {};
widgets: {}, SocketAdmin.widgets = {};
config: {}, SocketAdmin.config = {};
settings: {}, SocketAdmin.settings = {};
email: {}, SocketAdmin.email = {};
analytics: {}, SocketAdmin.analytics = {};
logs: {}, SocketAdmin.logs = {};
errors: {}, SocketAdmin.errors = {};
uploads: {}, SocketAdmin.uploads = {};
};
SocketAdmin.before = function (socket, method, data, next) { SocketAdmin.before = function (socket, method, data, next) {
async.waterfall([ async.waterfall([
@ -78,13 +77,9 @@ function logRestart(socket) {
}); });
} }
SocketAdmin.reload = function (socket, data, callback) { SocketAdmin.reload = async function (socket) {
async.waterfall([ await require('../meta/build').buildAll();
function (next) { await events.log({
require('../meta/build').buildAll(next);
},
function (next) {
events.log({
type: 'build', type: 'build',
uid: socket.uid, uid: socket.uid,
ip: socket.ip, ip: socket.ip,
@ -92,9 +87,6 @@ SocketAdmin.reload = function (socket, data, callback) {
logRestart(socket); logRestart(socket);
meta.restart(); meta.restart();
next();
},
], callback);
}; };
SocketAdmin.fireEvent = function (socket, data, callback) { SocketAdmin.fireEvent = function (socket, data, callback) {
@ -147,14 +139,9 @@ SocketAdmin.plugins.getActive = function (socket, data, callback) {
plugins.getActive(callback); plugins.getActive(callback);
}; };
SocketAdmin.plugins.orderActivePlugins = function (socket, data, callback) { SocketAdmin.plugins.orderActivePlugins = async function (socket, data) {
async.each(data, function (plugin, next) { data = data.filter(plugin => plugin && plugin.name);
if (plugin && plugin.name) { await Promise.all(data.map(plugin => db.sortedSetAdd('plugins:active', plugin.order || 0, plugin.name)));
db.sortedSetAdd('plugins:active', plugin.order || 0, plugin.name, next);
} else {
setImmediate(next);
}
}, callback);
}; };
SocketAdmin.plugins.upgrade = function (socket, data, callback) { SocketAdmin.plugins.upgrade = function (socket, data, callback) {
@ -169,18 +156,18 @@ SocketAdmin.widgets.set = function (socket, data, callback) {
async.eachSeries(data, widgets.setArea, callback); async.eachSeries(data, widgets.setArea, callback);
}; };
SocketAdmin.config.set = function (socket, data, callback) { SocketAdmin.config.set = async function (socket, data) {
if (!data) { if (!data) {
return callback(new Error('[[error:invalid-data]]')); throw new Error('[[error:invalid-data]]');
} }
var _data = {}; const _data = {};
_data[data.key] = data.value; _data[data.key] = data.value;
SocketAdmin.config.setMultiple(socket, _data, callback); await SocketAdmin.config.setMultiple(socket, _data);
}; };
SocketAdmin.config.setMultiple = function (socket, data, callback) { SocketAdmin.config.setMultiple = async function (socket, data) {
if (!data) { if (!data) {
return callback(new Error('[[error:invalid-data]]')); throw new Error('[[error:invalid-data]]');
} }
var changes = {}; var changes = {};
@ -191,12 +178,7 @@ SocketAdmin.config.setMultiple = function (socket, data, callback) {
changes[key + '_old'] = meta.config[key]; changes[key + '_old'] = meta.config[key];
} }
}); });
await meta.configs.setMultiple(data);
async.waterfall([
function (next) {
meta.configs.setMultiple(data, next);
},
function (next) {
var setting; var setting;
for (var field in data) { for (var field in data) {
if (data.hasOwnProperty(field)) { if (data.hasOwnProperty(field)) {
@ -208,17 +190,12 @@ SocketAdmin.config.setMultiple = function (socket, data, callback) {
logger.monitorConfig({ io: index.server }, setting); logger.monitorConfig({ io: index.server }, setting);
} }
} }
if (Object.keys(changes).length) { if (Object.keys(changes).length) {
changes.type = 'config-change'; changes.type = 'config-change';
changes.uid = socket.uid; changes.uid = socket.uid;
changes.ip = socket.ip; changes.ip = socket.ip;
events.log(changes, next); await events.log(changes);
} else {
next();
} }
},
], callback);
}; };
SocketAdmin.config.remove = function (socket, key, callback) { SocketAdmin.config.remove = function (socket, key, callback) {
@ -229,20 +206,14 @@ SocketAdmin.settings.get = function (socket, data, callback) {
meta.settings.get(data.hash, callback); meta.settings.get(data.hash, callback);
}; };
SocketAdmin.settings.set = function (socket, data, callback) { SocketAdmin.settings.set = async function (socket, data) {
async.waterfall([ await meta.settings.set(data.hash, data.values);
function (next) { const eventData = data.values;
meta.settings.set(data.hash, data.values, next);
},
function (next) {
var eventData = data.values;
eventData.type = 'settings-change'; eventData.type = 'settings-change';
eventData.uid = socket.uid; eventData.uid = socket.uid;
eventData.ip = socket.ip; eventData.ip = socket.ip;
eventData.hash = data.hash; eventData.hash = data.hash;
events.log(eventData, next); await events.log(eventData);
},
], callback);
}; };
SocketAdmin.settings.clearSitemapCache = function (socket, data, callback) { SocketAdmin.settings.clearSitemapCache = function (socket, data, callback) {
@ -371,16 +342,10 @@ SocketAdmin.deleteAllEvents = function (socket, data, callback) {
events.deleteAll(callback); events.deleteAll(callback);
}; };
SocketAdmin.getSearchDict = function (socket, data, callback) { SocketAdmin.getSearchDict = async function (socket) {
async.waterfall([ const settings = await user.getSettings(socket.uid);
function (next) {
user.getSettings(socket.uid, next);
},
function (settings, next) {
var lang = settings.userLang || meta.config.defaultLang || 'en-GB'; var lang = settings.userLang || meta.config.defaultLang || 'en-GB';
getAdminSearchDict(lang, next); return await getAdminSearchDict(lang);
},
], callback);
}; };
SocketAdmin.deleteAllSessions = function (socket, data, callback) { SocketAdmin.deleteAllSessions = function (socket, data, callback) {
@ -401,6 +366,4 @@ SocketAdmin.uploads.delete = function (socket, pathToFile, callback) {
fs.unlink(pathToFile, callback); fs.unlink(pathToFile, callback);
}; };
module.exports = SocketAdmin;
require('../promisify')(SocketAdmin); require('../promisify')(SocketAdmin);

@ -1,46 +1,36 @@
'use strict'; 'use strict';
var async = require('async'); const user = require('../user');
const meta = require('../meta');
const events = require('../events');
var user = require('../user'); const SocketBlacklist = module.exports;
var meta = require('../meta');
var events = require('../events');
var SocketBlacklist = module.exports; SocketBlacklist.validate = async function (socket, data) {
return meta.blacklist.validate(data.rules);
SocketBlacklist.validate = function (socket, data, callback) {
const rules = meta.blacklist.validate(data.rules);
callback(null, rules);
}; };
SocketBlacklist.save = function (socket, rules, callback) { SocketBlacklist.save = async function (socket, rules) {
blacklist(socket, 'save', rules, callback); await blacklist(socket, 'save', rules);
}; };
SocketBlacklist.addRule = function (socket, rule, callback) { SocketBlacklist.addRule = async function (socket, rule) {
blacklist(socket, 'addRule', rule, callback); await blacklist(socket, 'addRule', rule);
}; };
function blacklist(socket, method, rule, callback) { async function blacklist(socket, method, rule) {
async.waterfall([ const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(socket.uid);
function (next) {
user.isAdminOrGlobalMod(socket.uid, next);
},
function (isAdminOrGlobalMod, next) {
if (!isAdminOrGlobalMod) { if (!isAdminOrGlobalMod) {
return callback(new Error('[[error:no-privileges]]')); throw new Error('[[error:no-privileges]]');
} }
await meta.blacklist[method](rule);
meta.blacklist[method](rule, next); await events.log({
},
function (next) {
events.log({
type: 'ip-blacklist-' + method, type: 'ip-blacklist-' + method,
uid: socket.uid, uid: socket.uid,
ip: socket.ip, ip: socket.ip,
rule: rule, rule: rule,
}, next); });
},
], callback);
} }
require('../promisify')(SocketBlacklist);

Loading…
Cancel
Save