refactor: async/await, remove dupe code for homepage routes
parent
1a2a381ae3
commit
c9250a01a2
@ -1,33 +1,28 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const json2csv = require('json-2-csv').json2csv;
|
||||||
var json2csv = require('json-2-csv').json2csv;
|
const util = require('util');
|
||||||
|
|
||||||
var meta = require('../../meta');
|
const meta = require('../../meta');
|
||||||
var analytics = require('../../analytics');
|
const analytics = require('../../analytics');
|
||||||
|
const utils = require('../../utils');
|
||||||
|
|
||||||
var errorsController = module.exports;
|
const errorsController = module.exports;
|
||||||
|
|
||||||
errorsController.get = function (req, res, next) {
|
errorsController.get = async function (req, res) {
|
||||||
async.waterfall([
|
const data = await utils.promiseParallel({
|
||||||
function (next) {
|
'not-found': meta.errors.get(true),
|
||||||
async.parallel({
|
analytics: analytics.getErrorAnalytics(),
|
||||||
'not-found': async.apply(meta.errors.get, true),
|
});
|
||||||
analytics: async.apply(analytics.getErrorAnalytics),
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (data) {
|
|
||||||
res.render('admin/advanced/errors', data);
|
res.render('admin/advanced/errors', data);
|
||||||
},
|
|
||||||
], next);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
errorsController.export = function (req, res, next) {
|
const json2csvAsync = util.promisify(function (data, callback) {
|
||||||
async.waterfall([
|
json2csv(data, (err, output) => callback(err, output));
|
||||||
async.apply(meta.errors.get, false),
|
});
|
||||||
async.apply(json2csv),
|
|
||||||
function (csv) {
|
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);
|
res.set('Content-Type', 'text/csv').set('Content-Disposition', 'attachment; filename="404.csv"').send(csv);
|
||||||
},
|
|
||||||
], next);
|
|
||||||
};
|
};
|
||||||
|
@ -1,58 +1,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const helpers = require('../helpers');
|
||||||
|
|
||||||
var categories = require('../../categories');
|
const homePageController = module.exports;
|
||||||
var privileges = require('../../privileges');
|
|
||||||
var plugins = require('../../plugins');
|
|
||||||
|
|
||||||
var homePageController = module.exports;
|
homePageController.get = async function (req, res) {
|
||||||
|
const routes = await helpers.getHomePageRoutes(req.uid);
|
||||||
homePageController.get = function (req, res, next) {
|
res.render('admin/general/homepage', { routes: routes });
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
|
@ -1,26 +1,18 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const languages = require('../../languages');
|
||||||
|
const meta = require('../../meta');
|
||||||
|
|
||||||
var languages = require('../../languages');
|
const languagesController = module.exports;
|
||||||
var meta = require('../../meta');
|
|
||||||
|
|
||||||
var languagesController = module.exports;
|
languagesController.get = async function (req, res) {
|
||||||
|
const languageData = await languages.list();
|
||||||
languagesController.get = function (req, res, next) {
|
languageData.forEach(function (language) {
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
languages.list(next);
|
|
||||||
},
|
|
||||||
function (languages) {
|
|
||||||
languages.forEach(function (language) {
|
|
||||||
language.selected = language.code === meta.config.defaultLang;
|
language.selected = language.code === meta.config.defaultLang;
|
||||||
});
|
});
|
||||||
|
|
||||||
res.render('admin/general/languages', {
|
res.render('admin/general/languages', {
|
||||||
languages: languages,
|
languages: languageData,
|
||||||
autoDetectLang: meta.config.autoDetectLang,
|
autoDetectLang: meta.config.autoDetectLang,
|
||||||
});
|
});
|
||||||
},
|
|
||||||
], next);
|
|
||||||
};
|
};
|
||||||
|
@ -1,67 +1,58 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const nconf = require('nconf');
|
||||||
var nconf = require('nconf');
|
const winston = require('winston');
|
||||||
var plugins = require('../../plugins');
|
const plugins = require('../../plugins');
|
||||||
var meta = require('../../meta');
|
const meta = require('../../meta');
|
||||||
|
|
||||||
var pluginsController = module.exports;
|
const pluginsController = module.exports;
|
||||||
|
|
||||||
pluginsController.get = function (req, res, next) {
|
pluginsController.get = async function (req, res) {
|
||||||
async.waterfall([
|
const [compatible, all] = await Promise.all([
|
||||||
function (next) {
|
getCompatiblePluigns(),
|
||||||
async.parallel({
|
getAllPlugins(),
|
||||||
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 = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
next(null, plugins);
|
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);
|
||||||
}, 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;
|
|
||||||
});
|
|
||||||
|
|
||||||
res.render('admin/extend/plugins', {
|
res.render('admin/extend/plugins', {
|
||||||
installed: installedPlugins,
|
installed: installedPlugins,
|
||||||
installedCount: installedPlugins.length,
|
installedCount: installedPlugins.length,
|
||||||
activeCount: activePlugins.length,
|
activeCount: activePlugins.length,
|
||||||
inactiveCount: Math.max(0, installedPlugins.length - 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) {
|
if (current.installed && current.outdated) {
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
}, 0),
|
}, 0),
|
||||||
download: payload.compatible.filter(function (plugin) {
|
download: compatible.filter(function (plugin) {
|
||||||
return !plugin.installed;
|
return !plugin.installed;
|
||||||
}),
|
}),
|
||||||
incompatible: payload.all.filter(function (plugin) {
|
incompatible: all.filter(function (plugin) {
|
||||||
return !compatiblePkgNames.includes(plugin.name);
|
return !compatiblePkgNames.includes(plugin.name);
|
||||||
}),
|
}),
|
||||||
submitPluginUsage: meta.config.submitPluginUsage,
|
submitPluginUsage: meta.config.submitPluginUsage,
|
||||||
version: nconf.get('version'),
|
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';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const categories = require('../../categories');
|
||||||
|
const privileges = require('../../privileges');
|
||||||
|
|
||||||
var categories = require('../../categories');
|
const privilegesController = module.exports;
|
||||||
var privileges = require('../../privileges');
|
|
||||||
|
|
||||||
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) {
|
categoriesData.unshift({
|
||||||
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({
|
|
||||||
cid: 0,
|
cid: 0,
|
||||||
name: '[[admin/manage/privileges:global]]',
|
name: '[[admin/manage/privileges:global]]',
|
||||||
icon: 'fa-list',
|
icon: 'fa-list',
|
||||||
});
|
});
|
||||||
data.categories.forEach(function (category) {
|
|
||||||
|
let selectedCategory;
|
||||||
|
categoriesData.forEach(function (category) {
|
||||||
if (category) {
|
if (category) {
|
||||||
category.selected = category.cid === cid;
|
category.selected = category.cid === cid;
|
||||||
|
|
||||||
if (category.selected) {
|
if (category.selected) {
|
||||||
data.selected = category;
|
selectedCategory = category;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
res.render('admin/manage/privileges', {
|
res.render('admin/manage/privileges', {
|
||||||
privileges: data.privileges,
|
privileges: privilegesData,
|
||||||
categories: data.categories,
|
categories: categoriesData,
|
||||||
selectedCategory: data.selected,
|
selectedCategory: selectedCategory,
|
||||||
cid: cid,
|
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';
|
'use strict';
|
||||||
|
|
||||||
var path = require('path');
|
const path = require('path');
|
||||||
var fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const util = require('util');
|
||||||
var Logs = module.exports;
|
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.path = path.join(__dirname, '..', '..', 'logs', 'output.log');
|
||||||
|
|
||||||
Logs.get = function (callback) {
|
Logs.get = async function () {
|
||||||
fs.readFile(Logs.path, {
|
return await readFileAsync(Logs.path, 'utf-8');
|
||||||
encoding: 'utf-8',
|
|
||||||
}, callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Logs.clear = function (callback) {
|
Logs.clear = async function () {
|
||||||
fs.truncate(Logs.path, 0, callback);
|
return await truncateAsync(Logs.path, 0);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue