admin controllers

v1.18.x
barisusakli 10 years ago
parent 3858f6777d
commit 244c75f809

@ -1,423 +1,31 @@
"use strict";
var async = require('async'),
fs = require('fs'),
path = require('path'),
nconf = require('nconf'),
user = require('../user'),
categories = require('../categories'),
privileges = require('../privileges'),
posts = require('../posts'),
topics = require('../topics'),
meta = require('../meta'),
db = require('../database'),
events = require('../events'),
languages = require('../languages'),
plugins = require('../plugins'),
validator = require('validator');
var adminController = {
dashboard: require('./admin/dashboard'),
categories: require('./admin/categories'),
tags: {},
flags: {},
topics: {},
tags: require('./admin/tags'),
flags: require('./admin/flags'),
groups: require('./admin/groups'),
appearance: {},
appearance: require('./admin/appearance'),
extend: {
widgets: {}
widgets: require('./admin/widgets'),
rewards: require('./admin/rewards')
},
events: {},
logs: {},
database: {},
postCache: {},
plugins: {},
languages: {},
settings: {},
logger: {},
sounds: {},
homepage: {},
navigation: {},
themes: {},
events: require('./admin/events'),
logs: require('./admin/logs'),
database: require('./admin/database'),
postCache: require('./admin/postCache'),
plugins: require('./admin/plugins'),
languages: require('./admin/languages'),
settings: require('./admin/settings'),
logger: require('./admin/logger'),
sounds: require('./admin/sounds'),
homepage: require('./admin/homepage'),
navigation: require('./admin/navigation'),
themes: require('./admin/themes'),
users: require('./admin/users'),
uploads: require('./admin/uploads')
};
adminController.home = function(req, res, next) {
async.parallel({
stats: function(next) {
getStats(next);
},
notices: function(next) {
var notices = [
{
done: !meta.reloadRequired,
doneText: 'Reload not required',
notDoneText:'Reload required'
},
{
done: plugins.hasListeners('action:email.send'),
doneText: 'Emailer Installed',
notDoneText:'Emailer not installed',
tooltip:'Install an emailer plugin from the plugin page in order to activate registration emails and email digests',
link:'/admin/extend/plugins'
},
{
done: plugins.hasListeners('filter:search.query'),
doneText: 'Search Plugin Installed',
notDoneText:'Search Plugin not installed',
tooltip: 'Install a search plugin from the plugin page in order to activate search functionality',
link:'/admin/extend/plugins'
}
];
plugins.fireHook('filter:admin.notices', notices, next);
}
}, function(err, results) {
if (err) {
return next(err);
}
res.render('admin/general/dashboard', {
version: nconf.get('version'),
notices: results.notices,
stats: results.stats
});
});
};
function getStats(callback) {
async.parallel([
function(next) {
getStatsForSet('ip:recent', 'uniqueIPCount', next);
},
function(next) {
getStatsForSet('users:joindate', 'userCount', next);
},
function(next) {
getStatsForSet('posts:pid', 'postCount', next);
},
function(next) {
getStatsForSet('topics:tid', 'topicCount', next);
}
], function(err, results) {
if (err) {
return callback(err);
}
results[0].name = 'Unique Visitors';
results[1].name = 'Users';
results[2].name = 'Posts';
results[3].name = 'Topics';
callback(null, results);
});
}
function getStatsForSet(set, field, callback) {
var terms = {
day: 86400000,
week: 604800000,
month: 2592000000
};
var now = Date.now();
async.parallel({
day: function(next) {
db.sortedSetCount(set, now - terms.day, now, next);
},
week: function(next) {
db.sortedSetCount(set, now - terms.week, now, next);
},
month: function(next) {
db.sortedSetCount(set, now - terms.month, now, next);
},
alltime: function(next) {
getGlobalField(field, next);
}
}, callback);
}
function getGlobalField(field, callback) {
db.getObjectField('global', field, function(err, count) {
callback(err, parseInt(count, 10) || 0);
});
}
adminController.tags.get = function(req, res, next) {
topics.getTags(0, 199, function(err, tags) {
if (err) {
return next(err);
}
res.render('admin/manage/tags', {tags: tags});
});
};
adminController.flags.get = function(req, res, next) {
function done(err, posts) {
if (err) {
return next(err);
}
res.render('admin/manage/flags', {posts: posts, next: stop + 1, byUsername: byUsername});
}
var sortBy = req.query.sortBy || 'count';
var byUsername = req.query.byUsername || '';
var start = 0;
var stop = 19;
if (byUsername) {
posts.getUserFlags(byUsername, sortBy, req.uid, start, stop, done);
} else {
var set = sortBy === 'count' ? 'posts:flags:count' : 'posts:flagged';
posts.getFlags(set, req.uid, start, stop, done);
}
};
adminController.database.get = function(req, res, next) {
async.parallel({
redis: function(next) {
if (nconf.get('redis')) {
var rdb = require('../database/redis');
var cxn = rdb.connect();
rdb.info(cxn, next);
} else {
next();
}
},
mongo: function(next) {
if (nconf.get('mongo')) {
var mdb = require('../database/mongo');
mdb.info(mdb.client, next);
} else {
next();
}
}
}, function(err, results) {
if (err) {
return next(err);
}
res.render('admin/advanced/database', results);
});
};
adminController.events.get = function(req, res, next) {
events.getEvents(0, 19, function(err, events) {
if(err || !events) {
return next(err);
}
res.render('admin/advanced/events', {
events: events,
next: 20
});
});
};
adminController.logs.get = function(req, res, next) {
meta.logs.get(function(err, logs) {
res.render('admin/advanced/logs', {
data: validator.escape(logs)
});
});
};
adminController.postCache.get = function(req, res, next) {
var cache = require('../posts/cache');
var avgPostSize = 0;
var percentFull = 0;
if (cache.itemCount > 0) {
avgPostSize = parseInt((cache.length / cache.itemCount), 10);
percentFull = ((cache.length / cache.max) * 100).toFixed(2);
}
res.render('admin/advanced/post-cache', {
cache: {
length: cache.length,
max: cache.max,
itemCount: cache.itemCount,
percentFull: percentFull,
avgPostSize: avgPostSize
}
});
};
adminController.plugins.get = function(req, res, 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 = [];
}
next(null, plugins);
});
}
}, function(err, payload) {
var compatiblePkgNames = payload.compatible.map(function(pkgData) {
return pkgData.name;
});
res.render('admin/extend/plugins' , {
installed: payload.compatible.filter(function(plugin) {
return plugin.installed;
}),
download: payload.compatible.filter(function(plugin) {
return !plugin.installed;
}),
incompatible: payload.all.filter(function(plugin) {
return compatiblePkgNames.indexOf(plugin.name) === -1;
})
});
});
};
adminController.languages.get = function(req, res, next) {
languages.list(function(err, languages) {
if (err) {
return next(err);
}
languages.forEach(function(language) {
language.selected = language.code === (meta.config.defaultLang || 'en_GB');
});
res.render('admin/general/languages', {
languages: languages
});
});
};
adminController.sounds.get = function(req, res, next) {
meta.sounds.getFiles(function(err, sounds) {
sounds = Object.keys(sounds).map(function(name) {
return {
name: name
};
});
res.render('admin/general/sounds', {
sounds: sounds
});
});
};
adminController.navigation.get = function(req, res, next) {
require('../navigation/admin').getAdmin(function(err, data) {
if (err) {
return next(err);
}
res.render('admin/general/navigation', data);
});
};
adminController.homepage.get = function(req, res, next) {
async.waterfall([
function(next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
},
function(cids, next) {
privileges.categories.filterCids('find', cids, 0, next);
},
function(cids, next) {
categories.getMultipleCategoryFields(cids, ['name', 'slug'], next);
},
function(categoryData, next) {
categoryData = categoryData.map(function(category) {
return {
route: 'category/' + category.slug,
name: 'Category: ' + category.name
};
});
next(null, categoryData);
}
], function(err, categoryData) {
if (err || !categoryData) categoryData = [];
plugins.fireHook('filter:homepage.get', {routes: [
{
route: 'categories',
name: 'Categories'
},
{
route: 'recent',
name: 'Recent'
},
{
route: 'popular',
name: 'Popular'
}
].concat(categoryData)}, function(err, data) {
data.routes.push({
route: '',
name: 'Custom'
});
res.render('admin/general/homepage', data);
});
});
};
adminController.settings.get = function(req, res, next) {
var term = req.params.term ? req.params.term : 'general';
res.render('admin/settings/' + term);
};
adminController.logger.get = function(req, res, next) {
res.render('admin/development/logger', {});
};
adminController.appearance.get = function(req, res, next) {
var term = req.params.term ? req.params.term : 'themes';
res.render('admin/appearance/' + term, {});
};
adminController.extend.widgets = function(req, res, next) {
require('../widgets/admin').get(function(err, data) {
if (err) {
return next(err);
}
res.render('admin/extend/widgets', data);
});
};
adminController.extend.rewards = function(req, res, next) {
require('../rewards/admin').get(function(err, data) {
if (err) {
return next(err);
}
res.render('admin/extend/rewards', data);
});
};
adminController.themes.get = function(req, res, next) {
var themeDir = path.join(__dirname, '../../node_modules/' + req.params.theme);
fs.exists(themeDir, function(exists) {
if (exists) {
var themeConfig = require(path.join(themeDir, 'theme.json')),
screenshotPath = path.join(themeDir, themeConfig.screenshot);
if (themeConfig.screenshot && fs.existsSync(screenshotPath)) {
res.sendFile(screenshotPath);
} else {
res.sendFile(path.join(__dirname, '../../public/images/themes/default.png'));
}
} else {
return next();
}
});
};
module.exports = adminController;

@ -0,0 +1,12 @@
"use strict";
var appearanceController = {};
appearanceController.get = function(req, res, next) {
var term = req.params.term ? req.params.term : 'themes';
res.render('admin/appearance/' + term, {});
};
module.exports = appearanceController;

@ -0,0 +1,111 @@
'use strict';
var async = require('async');
var nconf = require('nconf');
var db = require('../../database');
var meta = require('../../meta');
var plugins = require('../../plugins');
var dashboardController = {};
dashboardController.get = function(req, res, next) {
async.parallel({
stats: function(next) {
getStats(next);
},
notices: function(next) {
var notices = [
{
done: !meta.reloadRequired,
doneText: 'Reload not required',
notDoneText:'Reload required'
},
{
done: plugins.hasListeners('action:email.send'),
doneText: 'Emailer Installed',
notDoneText:'Emailer not installed',
tooltip:'Install an emailer plugin from the plugin page in order to activate registration emails and email digests',
link:'/admin/extend/plugins'
},
{
done: plugins.hasListeners('filter:search.query'),
doneText: 'Search Plugin Installed',
notDoneText:'Search Plugin not installed',
tooltip: 'Install a search plugin from the plugin page in order to activate search functionality',
link:'/admin/extend/plugins'
}
];
plugins.fireHook('filter:admin.notices', notices, next);
}
}, function(err, results) {
if (err) {
return next(err);
}
res.render('admin/general/dashboard', {
version: nconf.get('version'),
notices: results.notices,
stats: results.stats
});
});
};
function getStats(callback) {
async.parallel([
function(next) {
getStatsForSet('ip:recent', 'uniqueIPCount', next);
},
function(next) {
getStatsForSet('users:joindate', 'userCount', next);
},
function(next) {
getStatsForSet('posts:pid', 'postCount', next);
},
function(next) {
getStatsForSet('topics:tid', 'topicCount', next);
}
], function(err, results) {
if (err) {
return callback(err);
}
results[0].name = 'Unique Visitors';
results[1].name = 'Users';
results[2].name = 'Posts';
results[3].name = 'Topics';
callback(null, results);
});
}
function getStatsForSet(set, field, callback) {
var terms = {
day: 86400000,
week: 604800000,
month: 2592000000
};
var now = Date.now();
async.parallel({
day: function(next) {
db.sortedSetCount(set, now - terms.day, now, next);
},
week: function(next) {
db.sortedSetCount(set, now - terms.week, now, next);
},
month: function(next) {
db.sortedSetCount(set, now - terms.month, now, next);
},
alltime: function(next) {
getGlobalField(field, next);
}
}, callback);
}
function getGlobalField(field, callback) {
db.getObjectField('global', field, function(err, count) {
callback(err, parseInt(count, 10) || 0);
});
}
module.exports = dashboardController;

@ -0,0 +1,37 @@
'use strict';
var async = require('async');
var nconf = require('nconf');
var databaseController = {};
databaseController.get = function(req, res, next) {
async.parallel({
redis: function(next) {
if (nconf.get('redis')) {
var rdb = require('../../database/redis');
var cxn = rdb.connect();
rdb.info(cxn, next);
} else {
next();
}
},
mongo: function(next) {
if (nconf.get('mongo')) {
var mdb = require('../../database/mongo');
mdb.info(mdb.client, next);
} else {
next();
}
}
}, function(err, results) {
if (err) {
return next(err);
}
res.render('admin/advanced/database', results);
});
};
module.exports = databaseController;

@ -0,0 +1,22 @@
'use strict';
var events = require('../../events');
var eventsController = {};
eventsController.get = function(req, res, next) {
events.getEvents(0, 19, function(err, events) {
if (err) {
return next(err);
}
res.render('admin/advanced/events', {
events: events,
next: 20
});
});
};
module.exports = eventsController;

@ -0,0 +1,29 @@
"use strict";
var posts = require('../../posts');
var flagsController = {};
flagsController.get = function(req, res, next) {
function done(err, posts) {
if (err) {
return next(err);
}
res.render('admin/manage/flags', {posts: posts, next: stop + 1, byUsername: byUsername});
}
var sortBy = req.query.sortBy || 'count';
var byUsername = req.query.byUsername || '';
var start = 0;
var stop = 19;
if (byUsername) {
posts.getUserFlags(byUsername, sortBy, req.uid, start, stop, done);
} else {
var set = sortBy === 'count' ? 'posts:flags:count' : 'posts:flagged';
posts.getFlags(set, req.uid, start, stop, done);
}
};
module.exports = flagsController;

@ -0,0 +1,62 @@
'use strict';
var async = require('async');
var db = require('../../database');
var categories = require('../../categories');
var privileges = require('../../privileges');
var plugins = require('../../plugins');
var homePageController = {};
homePageController.get = function(req, res, next) {
async.waterfall([
function(next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
},
function(cids, next) {
privileges.categories.filterCids('find', cids, 0, next);
},
function(cids, next) {
categories.getMultipleCategoryFields(cids, ['name', 'slug'], next);
},
function(categoryData, next) {
categoryData = categoryData.map(function(category) {
return {
route: 'category/' + category.slug,
name: 'Category: ' + category.name
};
});
next(null, categoryData);
}
], function(err, categoryData) {
if (err || !categoryData) {
categoryData = [];
}
plugins.fireHook('filter:homepage.get', {routes: [
{
route: 'categories',
name: 'Categories'
},
{
route: 'recent',
name: 'Recent'
},
{
route: 'popular',
name: 'Popular'
}
].concat(categoryData)}, function(err, data) {
data.routes.push({
route: '',
name: 'Custom'
});
res.render('admin/general/homepage', data);
});
});
};
module.exports = homePageController;

@ -0,0 +1,25 @@
'use strict';
var languages = require('../../languages');
var meta = require('../../meta');
var languagesController = {};
languagesController.get = function(req, res, next) {
languages.list(function(err, languages) {
if (err) {
return next(err);
}
languages.forEach(function(language) {
language.selected = language.code === (meta.config.defaultLang || 'en_GB');
});
res.render('admin/general/languages', {
languages: languages
});
});
};
module.exports = languagesController;

@ -0,0 +1,9 @@
'use strict';
var loggerController = {};
loggerController.get = function(req, res) {
res.render('admin/development/logger', {});
};
module.exports = loggerController;

@ -0,0 +1,22 @@
'use strict';
var validator = require('validator');
var meta = require('../../meta');
var logsController = {};
logsController.get = function(req, res, next) {
meta.logs.get(function(err, logs) {
if (err) {
return next(err);
}
res.render('admin/advanced/logs', {
data: validator.escape(logs)
});
});
};
module.exports = logsController;

@ -0,0 +1,15 @@
'use strict';
var navigationController = {};
navigationController.get = function(req, res, next) {
require('../../navigation/admin').getAdmin(function(err, data) {
if (err) {
return next(err);
}
res.render('admin/general/navigation', data);
});
};
module.exports = navigationController;

@ -0,0 +1,50 @@
'use strict';
var async = require('async');
var plugins = require('../../plugins');
var pluginsController = {};
pluginsController.get = function(req, res, 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 = [];
}
next(null, plugins);
});
}
}, function(err, payload) {
if (err) {
return next(err);
}
var compatiblePkgNames = payload.compatible.map(function(pkgData) {
return pkgData.name;
});
res.render('admin/extend/plugins' , {
installed: payload.compatible.filter(function(plugin) {
return plugin.installed;
}),
download: payload.compatible.filter(function(plugin) {
return !plugin.installed;
}),
incompatible: payload.all.filter(function(plugin) {
return compatiblePkgNames.indexOf(plugin.name) === -1;
})
});
});
};
module.exports = pluginsController;

@ -0,0 +1,26 @@
'use strict';
var postCacheController = {};
postCacheController.get = function(req, res, next) {
var cache = require('../../posts/cache');
var avgPostSize = 0;
var percentFull = 0;
if (cache.itemCount > 0) {
avgPostSize = parseInt((cache.length / cache.itemCount), 10);
percentFull = ((cache.length / cache.max) * 100).toFixed(2);
}
res.render('admin/advanced/post-cache', {
cache: {
length: cache.length,
max: cache.max,
itemCount: cache.itemCount,
percentFull: percentFull,
avgPostSize: avgPostSize
}
});
};
module.exports = postCacheController;

@ -0,0 +1,17 @@
'use strict';
var rewardsController = {};
rewardsController.get = function(req, res, next) {
require('../../rewards/admin').get(function(err, data) {
if (err) {
return next(err);
}
res.render('admin/extend/rewards', data);
});
};
module.exports = rewardsController;

@ -0,0 +1,11 @@
'use strict';
var settingsController = {};
settingsController.get = function(req, res, next) {
var term = req.params.term ? req.params.term : 'general';
res.render('admin/settings/' + term);
};
module.exports = settingsController;

@ -0,0 +1,21 @@
'use strict';
var meta = require('../../meta');
var soundsController = {};
soundsController.get = function(req, res, next) {
meta.sounds.getFiles(function(err, sounds) {
sounds = Object.keys(sounds).map(function(name) {
return {
name: name
};
});
res.render('admin/general/sounds', {
sounds: sounds
});
});
};
module.exports = soundsController;

@ -0,0 +1,18 @@
"use strict";
var topics = require('../../topics');
var tagsController = {};
tagsController.get = function(req, res, next) {
topics.getTags(0, 199, function(err, tags) {
if (err) {
return next(err);
}
res.render('admin/manage/tags', {tags: tags});
});
};
module.exports = tagsController;

@ -0,0 +1,25 @@
'use strict';
var path = require('path');
var fs = require('fs');
var themesController = {};
themesController.get = function(req, res, next) {
var themeDir = path.join(__dirname, '../../node_modules/' + req.params.theme);
fs.exists(themeDir, function(exists) {
if (exists) {
var themeConfig = require(path.join(themeDir, 'theme.json')),
screenshotPath = path.join(themeDir, themeConfig.screenshot);
if (themeConfig.screenshot && fs.existsSync(screenshotPath)) {
res.sendFile(screenshotPath);
} else {
res.sendFile(path.join(__dirname, '../../public/images/themes/default.png'));
}
} else {
return next();
}
});
};
module.exports = themesController;

@ -0,0 +1,16 @@
'use strict';
var widgetsController = {};
widgetsController.get = function(req, res, next) {
require('../../widgets/admin').get(function(err, data) {
if (err) {
return next(err);
}
res.render('admin/extend/widgets', data);
});
};
module.exports = widgetsController;

@ -40,8 +40,8 @@ function apiRouter(middleware, controllers) {
function addRoutes(router, middleware, controllers) {
var middlewares = [middleware.pluginHooks];
router.get('/', middlewares, controllers.admin.home);
router.get('/general/dashboard', middlewares, controllers.admin.home);
router.get('/', middlewares, controllers.admin.dashboard.get);
router.get('/general/dashboard', middlewares, controllers.admin.dashboard.get);
router.get('/general/languages', middlewares, controllers.admin.languages.get);
router.get('/general/sounds', middlewares, controllers.admin.sounds.get);
router.get('/general/navigation', middlewares, controllers.admin.navigation.get);
@ -70,8 +70,8 @@ function addRoutes(router, middleware, controllers) {
router.get('/appearance/:term?', middlewares, controllers.admin.appearance.get);
router.get('/extend/plugins', middlewares, controllers.admin.plugins.get);
router.get('/extend/widgets', middlewares, controllers.admin.extend.widgets);
router.get('/extend/rewards', middlewares, controllers.admin.extend.rewards);
router.get('/extend/widgets', middlewares, controllers.admin.extend.widgets.get);
router.get('/extend/rewards', middlewares, controllers.admin.extend.rewards.get);
router.get('/advanced/database', middlewares, controllers.admin.database.get);
router.get('/advanced/events', middlewares, controllers.admin.events.get);

Loading…
Cancel
Save