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')) {
require('./src/cli/manage').listPlugins();
} 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')) {
require('./src/cli/manage').listEvents();
} else {

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

@ -1,12 +1,12 @@
'use strict';
var async = require('async');
var winston = require('winston');
var nconf = require('nconf');
var _ = require('lodash');
const async = require('async');
const winston = require('winston');
const nconf = require('nconf');
const _ = require('lodash');
var cacheBuster = require('./cacheBuster');
var meta;
const cacheBuster = require('./cacheBuster');
let meta;
function step(target, callback) {
var startTime = Date.now();
@ -134,7 +134,7 @@ function buildTargets(targets, parallel, callback) {
}, callback);
}
function build(targets, options, callback) {
exports.build = function (targets, options, callback) {
if (!callback && typeof options === 'function') {
callback = options;
options = {};
@ -178,17 +178,6 @@ function build(targets, options, callback) {
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) {
winston.info('[build] No valid targets supplied. Aborting.');
callback();
@ -226,10 +215,10 @@ function build(targets, options, callback) {
winston.info('[build] Asset compilation successful. Completed in ' + totalTime + 'sec.');
callback();
});
}
exports.build = build;
};
exports.buildAll = function (callback) {
build(allTargets, callback);
exports.build(allTargets, callback);
};
require('../promisify')(exports);

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

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

Loading…
Cancel
Save