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) {
|
||||
res.render('admin/advanced/errors', data);
|
||||
},
|
||||
], next);
|
||||
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);
|
||||
};
|
||||
|
||||
errorsController.export = function (req, res, next) {
|
||||
async.waterfall([
|
||||
async.apply(meta.errors.get, false),
|
||||
async.apply(json2csv),
|
||||
function (csv) {
|
||||
res.set('Content-Type', 'text/csv').set('Content-Disposition', 'attachment; filename="404.csv"').send(csv);
|
||||
},
|
||||
], next);
|
||||
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);
|
||||
};
|
||||
|
@ -1,55 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
const db = require('../../database');
|
||||
const events = require('../../events');
|
||||
const pagination = require('../../pagination');
|
||||
|
||||
var db = require('../../database');
|
||||
var events = require('../../events');
|
||||
var pagination = require('../../pagination');
|
||||
const eventsController = module.exports;
|
||||
|
||||
var eventsController = module.exports;
|
||||
|
||||
eventsController.get = function (req, res, next) {
|
||||
var page = parseInt(req.query.page, 10) || 1;
|
||||
var itemsPerPage = parseInt(req.query.perPage, 10) || 20;
|
||||
var start = (page - 1) * itemsPerPage;
|
||||
var stop = start + itemsPerPage - 1;
|
||||
eventsController.get = async function (req, res) {
|
||||
const page = parseInt(req.query.page, 10) || 1;
|
||||
const itemsPerPage = parseInt(req.query.perPage, 10) || 20;
|
||||
const start = (page - 1) * itemsPerPage;
|
||||
const stop = start + itemsPerPage - 1;
|
||||
|
||||
// Limit by date
|
||||
var from = req.query.start ? new Date(req.query.start) || undefined : undefined;
|
||||
var to = req.query.end ? new Date(req.query.end) || undefined : new Date();
|
||||
let from = req.query.start ? new Date(req.query.start) || undefined : undefined;
|
||||
let to = req.query.end ? new Date(req.query.end) || undefined : new Date();
|
||||
from = from && from.setHours(0, 0, 0, 0); // setHours returns a unix timestamp (Number, not Date)
|
||||
to = to && to.setHours(23, 59, 59, 999); // setHours returns a unix timestamp (Number, not Date)
|
||||
|
||||
var currentFilter = req.query.type || '';
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
eventCount: function (next) {
|
||||
db.sortedSetCount('events:time' + (currentFilter ? ':' + currentFilter : ''), from || '-inf', to, next);
|
||||
},
|
||||
events: function (next) {
|
||||
events.getEvents(currentFilter, start, stop, from || '-inf', to, next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (results) {
|
||||
var types = [''].concat(events.types).map(function (type) {
|
||||
return {
|
||||
value: type,
|
||||
name: type || 'all',
|
||||
selected: type === currentFilter,
|
||||
};
|
||||
});
|
||||
|
||||
var pageCount = Math.max(1, Math.ceil(results.eventCount / itemsPerPage));
|
||||
|
||||
res.render('admin/advanced/events', {
|
||||
events: results.events,
|
||||
pagination: pagination.create(page, pageCount, req.query),
|
||||
types: types,
|
||||
query: req.query,
|
||||
});
|
||||
},
|
||||
], next);
|
||||
const currentFilter = req.query.type || '';
|
||||
|
||||
const [eventCount, eventData] = await Promise.all([
|
||||
db.sortedSetCount('events:time' + (currentFilter ? ':' + currentFilter : ''), from || '-inf', to),
|
||||
events.getEvents(currentFilter, start, stop, from || '-inf', to),
|
||||
]);
|
||||
|
||||
const types = [''].concat(events.types).map(function (type) {
|
||||
return {
|
||||
value: type,
|
||||
name: type || 'all',
|
||||
selected: type === currentFilter,
|
||||
};
|
||||
});
|
||||
|
||||
const pageCount = Math.max(1, Math.ceil(eventCount / itemsPerPage));
|
||||
|
||||
res.render('admin/advanced/events', {
|
||||
events: eventData,
|
||||
pagination: pagination.create(page, pageCount, req.query),
|
||||
types: types,
|
||||
query: req.query,
|
||||
});
|
||||
};
|
||||
|
@ -1,91 +1,62 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var validator = require('validator');
|
||||
|
||||
var db = require('../../database');
|
||||
var groups = require('../../groups');
|
||||
var meta = require('../../meta');
|
||||
var pagination = require('../../pagination');
|
||||
|
||||
var groupsController = module.exports;
|
||||
|
||||
groupsController.list = function (req, res, next) {
|
||||
var page = parseInt(req.query.page, 10) || 1;
|
||||
var groupsPerPage = 20;
|
||||
var pageCount = 0;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
getGroupNames(next);
|
||||
},
|
||||
function (groupNames, next) {
|
||||
pageCount = Math.ceil(groupNames.length / groupsPerPage);
|
||||
|
||||
var start = (page - 1) * groupsPerPage;
|
||||
var stop = start + groupsPerPage - 1;
|
||||
|
||||
groupNames = groupNames.slice(start, stop + 1);
|
||||
groups.getGroupsData(groupNames, next);
|
||||
},
|
||||
function (groupData) {
|
||||
res.render('admin/manage/groups', {
|
||||
groups: groupData,
|
||||
pagination: pagination.create(page, pageCount),
|
||||
yourid: req.uid,
|
||||
});
|
||||
},
|
||||
], next);
|
||||
const validator = require('validator');
|
||||
|
||||
const db = require('../../database');
|
||||
const groups = require('../../groups');
|
||||
const meta = require('../../meta');
|
||||
const pagination = require('../../pagination');
|
||||
|
||||
const groupsController = module.exports;
|
||||
|
||||
groupsController.list = async function (req, res) {
|
||||
const page = parseInt(req.query.page, 10) || 1;
|
||||
const groupsPerPage = 20;
|
||||
|
||||
let groupNames = await getGroupNames();
|
||||
const pageCount = Math.ceil(groupNames.length / groupsPerPage);
|
||||
const start = (page - 1) * groupsPerPage;
|
||||
const stop = start + groupsPerPage - 1;
|
||||
|
||||
groupNames = groupNames.slice(start, stop + 1);
|
||||
const groupData = await groups.getGroupsData(groupNames);
|
||||
res.render('admin/manage/groups', {
|
||||
groups: groupData,
|
||||
pagination: pagination.create(page, pageCount),
|
||||
yourid: req.uid,
|
||||
});
|
||||
};
|
||||
|
||||
groupsController.get = function (req, res, callback) {
|
||||
var groupName = req.params.name;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
groupNames: function (next) {
|
||||
getGroupNames(next);
|
||||
},
|
||||
group: function (next) {
|
||||
groups.get(groupName, { uid: req.uid, truncateUserList: true, userListCount: 20 }, next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (result) {
|
||||
if (!result.group) {
|
||||
return callback();
|
||||
}
|
||||
result.group.isOwner = true;
|
||||
|
||||
result.groupNames = result.groupNames.map(function (name) {
|
||||
return {
|
||||
encodedName: encodeURIComponent(name),
|
||||
displayName: validator.escape(String(name)),
|
||||
selected: name === groupName,
|
||||
};
|
||||
});
|
||||
|
||||
res.render('admin/manage/group', {
|
||||
group: result.group,
|
||||
groupNames: result.groupNames,
|
||||
allowPrivateGroups: meta.config.allowPrivateGroups,
|
||||
maximumGroupNameLength: meta.config.maximumGroupNameLength,
|
||||
maximumGroupTitleLength: meta.config.maximumGroupTitleLength,
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
groupsController.get = async function (req, res, next) {
|
||||
const groupName = req.params.name;
|
||||
const [groupNames, group] = await Promise.all([
|
||||
getGroupNames(),
|
||||
groups.get(groupName, { uid: req.uid, truncateUserList: true, userListCount: 20 }),
|
||||
]);
|
||||
|
||||
if (!group) {
|
||||
return next();
|
||||
}
|
||||
group.isOwner = true;
|
||||
|
||||
const groupNameData = groupNames.map(function (name) {
|
||||
return {
|
||||
encodedName: encodeURIComponent(name),
|
||||
displayName: validator.escape(String(name)),
|
||||
selected: name === groupName,
|
||||
};
|
||||
});
|
||||
|
||||
res.render('admin/manage/group', {
|
||||
group: group,
|
||||
groupNames: groupNameData,
|
||||
allowPrivateGroups: meta.config.allowPrivateGroups,
|
||||
maximumGroupNameLength: meta.config.maximumGroupNameLength,
|
||||
maximumGroupTitleLength: meta.config.maximumGroupTitleLength,
|
||||
});
|
||||
};
|
||||
|
||||
function getGroupNames(callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getSortedSetRange('groups:createtime', 0, -1, next);
|
||||
},
|
||||
function (groupNames, next) {
|
||||
groupNames = groupNames.filter(function (name) {
|
||||
return name !== 'registered-users' && !groups.isPrivilegeGroup(name);
|
||||
});
|
||||
next(null, groupNames);
|
||||
},
|
||||
], callback);
|
||||
async function getGroupNames() {
|
||||
const groupNames = await db.getSortedSetRange('groups:createtime', 0, -1);
|
||||
return groupNames.filter(name => name !== 'registered-users' && !groups.isPrivilegeGroup(name));
|
||||
}
|
||||
|
@ -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 = async function (req, res) {
|
||||
const languageData = await languages.list();
|
||||
languageData.forEach(function (language) {
|
||||
language.selected = language.code === meta.config.defaultLang;
|
||||
});
|
||||
|
||||
languagesController.get = function (req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
languages.list(next);
|
||||
},
|
||||
function (languages) {
|
||||
languages.forEach(function (language) {
|
||||
language.selected = language.code === meta.config.defaultLang;
|
||||
});
|
||||
|
||||
res.render('admin/general/languages', {
|
||||
languages: languages,
|
||||
autoDetectLang: meta.config.autoDetectLang,
|
||||
});
|
||||
},
|
||||
], next);
|
||||
res.render('admin/general/languages', {
|
||||
languages: languageData,
|
||||
autoDetectLang: meta.config.autoDetectLang,
|
||||
});
|
||||
};
|
||||
|
@ -1,43 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
|
||||
var navigationAdmin = require('../../navigation/admin');
|
||||
const navigationAdmin = require('../../navigation/admin');
|
||||
const groups = require('../../groups');
|
||||
|
||||
var navigationController = module.exports;
|
||||
|
||||
navigationController.get = function (req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
admin: async.apply(navigationAdmin.getAdmin),
|
||||
groups: async.apply(groups.getNonPrivilegeGroups, 'groups:createtime', 0, -1),
|
||||
}, next);
|
||||
},
|
||||
function (result) {
|
||||
result.groups.sort((a, b) => b.system - a.system);
|
||||
result.groups = result.groups.map(group => ({ name: group.name, displayName: group.displayName }));
|
||||
|
||||
result.admin.enabled.forEach(function (enabled, index) {
|
||||
enabled.index = index;
|
||||
enabled.selected = index === 0;
|
||||
|
||||
enabled.groups = result.groups.map(function (group) {
|
||||
return {
|
||||
displayName: group.displayName,
|
||||
selected: enabled.groups.includes(group.name),
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
result.admin.available.forEach(function (available) {
|
||||
available.groups = result.groups;
|
||||
});
|
||||
|
||||
result.admin.navigation = result.admin.enabled.slice();
|
||||
|
||||
res.render('admin/general/navigation', result.admin);
|
||||
},
|
||||
], next);
|
||||
const navigationController = module.exports;
|
||||
|
||||
navigationController.get = async function (req, res) {
|
||||
const [admin, allGroups] = await Promise.all([
|
||||
navigationAdmin.getAdmin(),
|
||||
groups.getNonPrivilegeGroups('groups:createtime', 0, -1),
|
||||
]);
|
||||
|
||||
allGroups.sort((a, b) => b.system - a.system);
|
||||
const groupsData = allGroups.map(group => ({ name: group.name, displayName: group.displayName }));
|
||||
|
||||
admin.enabled.forEach(function (enabled, index) {
|
||||
enabled.index = index;
|
||||
enabled.selected = index === 0;
|
||||
|
||||
enabled.groups = groupsData.map(function (group) {
|
||||
return {
|
||||
displayName: group.displayName,
|
||||
selected: enabled.groups.includes(group.name),
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
admin.available.forEach(function (available) {
|
||||
available.groups = groups;
|
||||
});
|
||||
|
||||
admin.navigation = admin.enabled.slice();
|
||||
|
||||
res.render('admin/general/navigation', admin);
|
||||
};
|
||||
|
@ -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 = [];
|
||||
}
|
||||
pluginsController.get = async function (req, res) {
|
||||
const [compatible, all] = await Promise.all([
|
||||
getCompatiblePluigns(),
|
||||
getAllPlugins(),
|
||||
]);
|
||||
|
||||
next(null, plugins);
|
||||
});
|
||||
},
|
||||
all: function (next) {
|
||||
plugins.list(false, function (err, plugins) {
|
||||
if (err || !Array.isArray(plugins)) {
|
||||
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(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;
|
||||
});
|
||||
|
||||
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) {
|
||||
if (current.installed && current.outdated) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}, 0),
|
||||
download: payload.compatible.filter(function (plugin) {
|
||||
return !plugin.installed;
|
||||
}),
|
||||
incompatible: payload.all.filter(function (plugin) {
|
||||
return !compatiblePkgNames.includes(plugin.name);
|
||||
}),
|
||||
submitPluginUsage: meta.config.submitPluginUsage,
|
||||
version: nconf.get('version'),
|
||||
});
|
||||
},
|
||||
], next);
|
||||
res.render('admin/extend/plugins', {
|
||||
installed: installedPlugins,
|
||||
installedCount: installedPlugins.length,
|
||||
activeCount: activePlugins.length,
|
||||
inactiveCount: Math.max(0, installedPlugins.length - activePlugins.length),
|
||||
upgradeCount: compatible.reduce(function (count, current) {
|
||||
if (current.installed && current.outdated) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}, 0),
|
||||
download: compatible.filter(function (plugin) {
|
||||
return !plugin.installed;
|
||||
}),
|
||||
incompatible: all.filter(function (plugin) {
|
||||
return !compatiblePkgNames.includes(plugin.name);
|
||||
}),
|
||||
submitPluginUsage: meta.config.submitPluginUsage,
|
||||
version: nconf.get('version'),
|
||||
});
|
||||
};
|
||||
|
||||
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,115 +1,76 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var validator = require('validator');
|
||||
const validator = require('validator');
|
||||
|
||||
var db = require('../../database');
|
||||
var user = require('../../user');
|
||||
var topics = require('../../topics');
|
||||
var categories = require('../../categories');
|
||||
var pagination = require('../../pagination');
|
||||
var plugins = require('../../plugins');
|
||||
var utils = require('../../utils');
|
||||
const db = require('../../database');
|
||||
const user = require('../../user');
|
||||
const topics = require('../../topics');
|
||||
const categories = require('../../categories');
|
||||
const pagination = require('../../pagination');
|
||||
const plugins = require('../../plugins');
|
||||
const utils = require('../../utils');
|
||||
|
||||
var postQueueController = module.exports;
|
||||
const postQueueController = module.exports;
|
||||
|
||||
postQueueController.get = function (req, res, next) {
|
||||
var page = parseInt(req.query.page, 10) || 1;
|
||||
var postsPerPage = 20;
|
||||
var results;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
ids: function (next) {
|
||||
db.getSortedSetRange('post:queue', 0, -1, next);
|
||||
},
|
||||
isAdminOrGlobalMod: function (next) {
|
||||
user.isAdminOrGlobalMod(req.uid, next);
|
||||
},
|
||||
moderatedCids: function (next) {
|
||||
user.getModeratedCids(req.uid, next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (_results, next) {
|
||||
results = _results;
|
||||
getQueuedPosts(results.ids, next);
|
||||
},
|
||||
function (postData) {
|
||||
postData = postData.filter(function (postData) {
|
||||
return postData && (results.isAdminOrGlobalMod || results.moderatedCids.includes(String(postData.category.cid)));
|
||||
});
|
||||
postQueueController.get = async function (req, res) {
|
||||
const page = parseInt(req.query.page, 10) || 1;
|
||||
const postsPerPage = 20;
|
||||
|
||||
var pageCount = Math.max(1, Math.ceil(postData.length / postsPerPage));
|
||||
var start = (page - 1) * postsPerPage;
|
||||
var stop = start + postsPerPage - 1;
|
||||
postData = postData.slice(start, stop + 1);
|
||||
const [ids, isAdminOrGlobalMod, moderatedCids] = await Promise.all([
|
||||
db.getSortedSetRange('post:queue', 0, -1),
|
||||
user.isAdminOrGlobalMod(req.uid),
|
||||
user.getModeratedCids(req.uid),
|
||||
]);
|
||||
|
||||
res.render('admin/manage/post-queue', {
|
||||
title: '[[pages:post-queue]]',
|
||||
posts: postData,
|
||||
pagination: pagination.create(page, pageCount),
|
||||
});
|
||||
},
|
||||
], next);
|
||||
let postData = await getQueuedPosts(ids);
|
||||
postData = postData.filter(p => p && (isAdminOrGlobalMod || moderatedCids.includes(String(p.category.cid))));
|
||||
|
||||
const pageCount = Math.max(1, Math.ceil(postData.length / postsPerPage));
|
||||
const start = (page - 1) * postsPerPage;
|
||||
const stop = start + postsPerPage - 1;
|
||||
postData = postData.slice(start, stop + 1);
|
||||
|
||||
res.render('admin/manage/post-queue', {
|
||||
title: '[[pages:post-queue]]',
|
||||
posts: postData,
|
||||
pagination: pagination.create(page, pageCount),
|
||||
});
|
||||
};
|
||||
|
||||
function getQueuedPosts(ids, callback) {
|
||||
var postData;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
const keys = ids.map(id => 'post:queue:' + id);
|
||||
db.getObjects(keys, next);
|
||||
},
|
||||
function (data, next) {
|
||||
postData = data;
|
||||
data.forEach(function (data) {
|
||||
if (data) {
|
||||
data.data = JSON.parse(data.data);
|
||||
data.data.timestampISO = utils.toISOString(data.data.timestamp);
|
||||
}
|
||||
return data;
|
||||
});
|
||||
const uids = data.map(data => data && data.uid);
|
||||
user.getUsersFields(uids, ['username', 'userslug', 'picture'], next);
|
||||
},
|
||||
function (userData, next) {
|
||||
postData.forEach(function (postData, index) {
|
||||
if (postData) {
|
||||
postData.user = userData[index];
|
||||
}
|
||||
});
|
||||
async function getQueuedPosts(ids) {
|
||||
const keys = ids.map(id => 'post:queue:' + id);
|
||||
const postData = await db.getObjects(keys);
|
||||
postData.forEach(function (data) {
|
||||
if (data) {
|
||||
data.data = JSON.parse(data.data);
|
||||
data.data.timestampISO = utils.toISOString(data.data.timestamp);
|
||||
}
|
||||
});
|
||||
const uids = postData.map(data => data && data.uid);
|
||||
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
|
||||
postData.forEach(function (postData, index) {
|
||||
if (postData) {
|
||||
postData.user = userData[index];
|
||||
postData.data.rawContent = validator.escape(String(postData.data.content));
|
||||
postData.data.title = validator.escape(String(postData.data.title || ''));
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(postData.map(p => addMetaData(p)));
|
||||
return postData;
|
||||
}
|
||||
|
||||
async.map(postData, function (postData, next) {
|
||||
if (!postData) {
|
||||
return next(null, postData);
|
||||
}
|
||||
postData.data.rawContent = validator.escape(String(postData.data.content));
|
||||
postData.data.title = validator.escape(String(postData.data.title || ''));
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (postData.data.cid) {
|
||||
next(null, { cid: postData.data.cid });
|
||||
} else if (postData.data.tid) {
|
||||
topics.getTopicFields(postData.data.tid, ['title', 'cid'], next);
|
||||
} else {
|
||||
next(null, { cid: 0 });
|
||||
}
|
||||
},
|
||||
function (topicData, next) {
|
||||
postData.topic = topicData;
|
||||
categories.getCategoryData(topicData.cid, next);
|
||||
},
|
||||
function (categoryData, next) {
|
||||
postData.category = categoryData;
|
||||
plugins.fireHook('filter:parse.post', { postData: postData.data }, next);
|
||||
},
|
||||
function (result, next) {
|
||||
postData.data.content = result.postData.content;
|
||||
next(null, postData);
|
||||
},
|
||||
], next);
|
||||
}, next);
|
||||
},
|
||||
], callback);
|
||||
async function addMetaData(postData) {
|
||||
if (!postData) {
|
||||
return;
|
||||
}
|
||||
postData.topic = { cid: 0 };
|
||||
if (postData.data.cid) {
|
||||
postData.topic = { cid: postData.data.cid };
|
||||
} else if (postData.data.tid) {
|
||||
postData.topic = await topics.getTopicFields(postData.data.tid, ['title', 'cid']);
|
||||
}
|
||||
postData.category = await categories.getCategoryData(postData.topic.cid);
|
||||
const result = await plugins.fireHook('filter:parse.post', { postData: postData.data });
|
||||
postData.data.content = result.postData.content;
|
||||
}
|
||||
|
@ -1,62 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
|
||||
var categories = require('../../categories');
|
||||
var privileges = require('../../privileges');
|
||||
|
||||
var privilegesController = module.exports;
|
||||
|
||||
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({
|
||||
cid: 0,
|
||||
name: '[[admin/manage/privileges:global]]',
|
||||
icon: 'fa-list',
|
||||
});
|
||||
data.categories.forEach(function (category) {
|
||||
if (category) {
|
||||
category.selected = category.cid === cid;
|
||||
|
||||
if (category.selected) {
|
||||
data.selected = category;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
res.render('admin/manage/privileges', {
|
||||
privileges: data.privileges,
|
||||
categories: data.categories,
|
||||
selectedCategory: data.selected,
|
||||
cid: cid,
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
const categories = require('../../categories');
|
||||
const privileges = require('../../privileges');
|
||||
|
||||
const 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),
|
||||
]);
|
||||
|
||||
categoriesData.unshift({
|
||||
cid: 0,
|
||||
name: '[[admin/manage/privileges:global]]',
|
||||
icon: 'fa-list',
|
||||
});
|
||||
|
||||
let selectedCategory;
|
||||
categoriesData.forEach(function (category) {
|
||||
if (category) {
|
||||
category.selected = category.cid === cid;
|
||||
|
||||
if (category.selected) {
|
||||
selectedCategory = category;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
res.render('admin/manage/privileges', {
|
||||
privileges: privilegesData,
|
||||
categories: categoriesData,
|
||||
selectedCategory: selectedCategory,
|
||||
cid: cid,
|
||||
});
|
||||
};
|
||||
|
||||
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