refactor: async/await, remove dupe code for homepage routes
parent
1a2a381ae3
commit
c9250a01a2
@ -1,33 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var json2csv = require('json-2-csv').json2csv;
|
||||
const json2csv = require('json-2-csv').json2csv;
|
||||
const util = require('util');
|
||||
|
||||
var meta = require('../../meta');
|
||||
var analytics = require('../../analytics');
|
||||
const meta = require('../../meta');
|
||||
const analytics = require('../../analytics');
|
||||
const utils = require('../../utils');
|
||||
|
||||
var errorsController = module.exports;
|
||||
const errorsController = module.exports;
|
||||
|
||||
errorsController.get = function (req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
'not-found': async.apply(meta.errors.get, true),
|
||||
analytics: async.apply(analytics.getErrorAnalytics),
|
||||
}, next);
|
||||
},
|
||||
function (data) {
|
||||
errorsController.get = async function (req, res) {
|
||||
const data = await utils.promiseParallel({
|
||||
'not-found': meta.errors.get(true),
|
||||
analytics: analytics.getErrorAnalytics(),
|
||||
});
|
||||
res.render('admin/advanced/errors', data);
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
||||
errorsController.export = function (req, res, next) {
|
||||
async.waterfall([
|
||||
async.apply(meta.errors.get, false),
|
||||
async.apply(json2csv),
|
||||
function (csv) {
|
||||
const json2csvAsync = util.promisify(function (data, callback) {
|
||||
json2csv(data, (err, output) => callback(err, output));
|
||||
});
|
||||
|
||||
errorsController.export = async function (req, res) {
|
||||
const data = await meta.errors.get(false);
|
||||
const csv = await json2csvAsync(data);
|
||||
res.set('Content-Type', 'text/csv').set('Content-Disposition', 'attachment; filename="404.csv"').send(csv);
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
@ -1,58 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
const helpers = require('../helpers');
|
||||
|
||||
var categories = require('../../categories');
|
||||
var privileges = require('../../privileges');
|
||||
var plugins = require('../../plugins');
|
||||
const homePageController = module.exports;
|
||||
|
||||
var homePageController = module.exports;
|
||||
|
||||
homePageController.get = function (req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
categories.getAllCidsFromSet('categories:cid', next);
|
||||
},
|
||||
function (cids, next) {
|
||||
privileges.categories.filterCids('find', cids, 0, next);
|
||||
},
|
||||
function (cids, next) {
|
||||
categories.getCategoriesFields(cids, ['name', 'slug'], next);
|
||||
},
|
||||
function (categoryData, next) {
|
||||
categoryData = categoryData.map(function (category) {
|
||||
return {
|
||||
route: 'category/' + category.slug,
|
||||
name: 'Category: ' + category.name,
|
||||
};
|
||||
});
|
||||
|
||||
plugins.fireHook('filter:homepage.get', { routes: [
|
||||
{
|
||||
route: 'categories',
|
||||
name: 'Categories',
|
||||
},
|
||||
{
|
||||
route: 'recent',
|
||||
name: 'Recent',
|
||||
},
|
||||
{
|
||||
route: 'top',
|
||||
name: 'Top',
|
||||
},
|
||||
{
|
||||
route: 'popular',
|
||||
name: 'Popular',
|
||||
},
|
||||
].concat(categoryData) }, next);
|
||||
},
|
||||
function (data) {
|
||||
data.routes.push({
|
||||
route: '',
|
||||
name: 'Custom',
|
||||
});
|
||||
|
||||
res.render('admin/general/homepage', data);
|
||||
},
|
||||
], next);
|
||||
homePageController.get = async function (req, res) {
|
||||
const routes = await helpers.getHomePageRoutes(req.uid);
|
||||
res.render('admin/general/homepage', { routes: routes });
|
||||
};
|
||||
|
@ -1,26 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
const languages = require('../../languages');
|
||||
const meta = require('../../meta');
|
||||
|
||||
var languages = require('../../languages');
|
||||
var meta = require('../../meta');
|
||||
const languagesController = module.exports;
|
||||
|
||||
var languagesController = module.exports;
|
||||
|
||||
languagesController.get = function (req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
languages.list(next);
|
||||
},
|
||||
function (languages) {
|
||||
languages.forEach(function (language) {
|
||||
languagesController.get = async function (req, res) {
|
||||
const languageData = await languages.list();
|
||||
languageData.forEach(function (language) {
|
||||
language.selected = language.code === meta.config.defaultLang;
|
||||
});
|
||||
|
||||
res.render('admin/general/languages', {
|
||||
languages: languages,
|
||||
languages: languageData,
|
||||
autoDetectLang: meta.config.autoDetectLang,
|
||||
});
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
@ -1,67 +1,58 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
var plugins = require('../../plugins');
|
||||
var meta = require('../../meta');
|
||||
const nconf = require('nconf');
|
||||
const winston = require('winston');
|
||||
const plugins = require('../../plugins');
|
||||
const meta = require('../../meta');
|
||||
|
||||
var pluginsController = module.exports;
|
||||
const pluginsController = module.exports;
|
||||
|
||||
pluginsController.get = function (req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
compatible: function (next) {
|
||||
plugins.list(function (err, plugins) {
|
||||
if (err || !Array.isArray(plugins)) {
|
||||
plugins = [];
|
||||
}
|
||||
|
||||
next(null, plugins);
|
||||
});
|
||||
},
|
||||
all: function (next) {
|
||||
plugins.list(false, function (err, plugins) {
|
||||
if (err || !Array.isArray(plugins)) {
|
||||
plugins = [];
|
||||
}
|
||||
pluginsController.get = async function (req, res) {
|
||||
const [compatible, all] = await Promise.all([
|
||||
getCompatiblePluigns(),
|
||||
getAllPlugins(),
|
||||
]);
|
||||
|
||||
next(null, plugins);
|
||||
});
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (payload) {
|
||||
var compatiblePkgNames = payload.compatible.map(function (pkgData) {
|
||||
return pkgData.name;
|
||||
});
|
||||
var installedPlugins = payload.compatible.filter(function (plugin) {
|
||||
return plugin && plugin.installed;
|
||||
});
|
||||
var activePlugins = payload.all.filter(function (plugin) {
|
||||
return plugin && plugin.installed && plugin.active;
|
||||
});
|
||||
const compatiblePkgNames = compatible.map(pkgData => pkgData.name);
|
||||
const installedPlugins = compatible.filter(plugin => plugin && plugin.installed);
|
||||
const activePlugins = all.filter(plugin => plugin && plugin.installed && plugin.active);
|
||||
|
||||
res.render('admin/extend/plugins', {
|
||||
installed: installedPlugins,
|
||||
installedCount: installedPlugins.length,
|
||||
activeCount: activePlugins.length,
|
||||
inactiveCount: Math.max(0, installedPlugins.length - activePlugins.length),
|
||||
upgradeCount: payload.compatible.reduce(function (count, current) {
|
||||
upgradeCount: compatible.reduce(function (count, current) {
|
||||
if (current.installed && current.outdated) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}, 0),
|
||||
download: payload.compatible.filter(function (plugin) {
|
||||
download: compatible.filter(function (plugin) {
|
||||
return !plugin.installed;
|
||||
}),
|
||||
incompatible: payload.all.filter(function (plugin) {
|
||||
incompatible: all.filter(function (plugin) {
|
||||
return !compatiblePkgNames.includes(plugin.name);
|
||||
}),
|
||||
submitPluginUsage: meta.config.submitPluginUsage,
|
||||
version: nconf.get('version'),
|
||||
});
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
||||
async function getCompatiblePluigns() {
|
||||
return await getPlugins(true);
|
||||
}
|
||||
|
||||
async function getAllPlugins() {
|
||||
return await getPlugins(false);
|
||||
}
|
||||
|
||||
async function getPlugins(matching) {
|
||||
try {
|
||||
const pluginsData = await plugins.list(matching);
|
||||
return pluginsData || [];
|
||||
} catch (err) {
|
||||
winston.error(err);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
const categories = require('../../categories');
|
||||
const privileges = require('../../privileges');
|
||||
|
||||
var categories = require('../../categories');
|
||||
var privileges = require('../../privileges');
|
||||
const privilegesController = module.exports;
|
||||
|
||||
var privilegesController = module.exports;
|
||||
privilegesController.get = async function (req, res) {
|
||||
const cid = req.params.cid ? parseInt(req.params.cid, 10) : 0;
|
||||
const [privilegesData, categoriesData] = await Promise.all([
|
||||
getPrivileges(cid),
|
||||
getCategories(req.uid),
|
||||
]);
|
||||
|
||||
privilegesController.get = function (req, res, callback) {
|
||||
var cid = req.params.cid ? parseInt(req.params.cid, 10) : 0;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
privileges: function (next) {
|
||||
if (!cid) {
|
||||
privileges.global.list(next);
|
||||
} else {
|
||||
privileges.categories.list(cid, next);
|
||||
}
|
||||
},
|
||||
categories: function (next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
categories.getAllCidsFromSet('categories:cid', next);
|
||||
},
|
||||
function (cids, next) {
|
||||
categories.getCategories(cids, req.uid, next);
|
||||
},
|
||||
function (categoriesData, next) {
|
||||
categoriesData = categories.getTree(categoriesData);
|
||||
categories.buildForSelectCategories(categoriesData, next);
|
||||
},
|
||||
], next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (data) {
|
||||
data.categories.unshift({
|
||||
categoriesData.unshift({
|
||||
cid: 0,
|
||||
name: '[[admin/manage/privileges:global]]',
|
||||
icon: 'fa-list',
|
||||
});
|
||||
data.categories.forEach(function (category) {
|
||||
|
||||
let selectedCategory;
|
||||
categoriesData.forEach(function (category) {
|
||||
if (category) {
|
||||
category.selected = category.cid === cid;
|
||||
|
||||
if (category.selected) {
|
||||
data.selected = category;
|
||||
selectedCategory = category;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
res.render('admin/manage/privileges', {
|
||||
privileges: data.privileges,
|
||||
categories: data.categories,
|
||||
selectedCategory: data.selected,
|
||||
privileges: privilegesData,
|
||||
categories: categoriesData,
|
||||
selectedCategory: selectedCategory,
|
||||
cid: cid,
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
async function getPrivileges(cid) {
|
||||
if (!cid) {
|
||||
return await privileges.global.list();
|
||||
}
|
||||
return await privileges.categories.list(cid);
|
||||
}
|
||||
|
||||
async function getCategories(uid) {
|
||||
const cids = await categories.getAllCidsFromSet('categories:cid');
|
||||
const categoriesData = await categories.getCategories(cids, uid);
|
||||
const tree = categories.getTree(categoriesData);
|
||||
return await categories.buildForSelectCategories(tree);
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
|
||||
var Logs = module.exports;
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const util = require('util');
|
||||
const readFileAsync = util.promisify(fs.readFile);
|
||||
const truncateAsync = util.promisify(fs.truncate);
|
||||
const Logs = module.exports;
|
||||
|
||||
Logs.path = path.join(__dirname, '..', '..', 'logs', 'output.log');
|
||||
|
||||
Logs.get = function (callback) {
|
||||
fs.readFile(Logs.path, {
|
||||
encoding: 'utf-8',
|
||||
}, callback);
|
||||
Logs.get = async function () {
|
||||
return await readFileAsync(Logs.path, 'utf-8');
|
||||
};
|
||||
|
||||
Logs.clear = function (callback) {
|
||||
fs.truncate(Logs.path, 0, callback);
|
||||
Logs.clear = async function () {
|
||||
return await truncateAsync(Logs.path, 0);
|
||||
};
|
||||
|
Loading…
Reference in New Issue