Files
NodeBB/src/controllers/index.js

233 lines
6.3 KiB
JavaScript
Raw Normal View History

"use strict";
2014-02-27 15:06:39 -05:00
var topicsController = require('./topics'),
categoriesController = require('./categories'),
tagsController = require('./tags'),
2015-01-07 16:10:11 -05:00
searchController = require('./search'),
usersController = require('./users'),
2014-05-22 15:23:19 -04:00
groupsController = require('./groups'),
accountsController = require('./accounts'),
staticController = require('./static'),
2014-03-03 11:45:40 -05:00
apiController = require('./api'),
adminController = require('./admin'),
2014-11-15 23:22:57 -05:00
helpers = require('./helpers'),
2014-02-27 15:06:39 -05:00
async = require('async'),
2014-02-27 17:16:06 -05:00
nconf = require('nconf'),
2014-09-27 19:23:48 -04:00
validator = require('validator'),
2014-07-07 17:36:10 -04:00
winston = require('winston'),
2014-05-22 15:23:19 -04:00
auth = require('../routes/authentication'),
meta = require('../meta'),
user = require('../user'),
posts = require('../posts'),
topics = require('../topics'),
plugins = require('../plugins'),
categories = require('../categories'),
2014-05-15 20:49:47 -04:00
privileges = require('../privileges');
2014-02-27 14:56:14 -05:00
var Controllers = {
2014-02-27 15:06:39 -05:00
topics: topicsController,
categories: categoriesController,
tags: tagsController,
2015-01-07 16:10:11 -05:00
search: searchController,
users: usersController,
2014-05-22 15:23:19 -04:00
groups: groupsController,
accounts: accountsController,
2014-03-03 11:45:40 -05:00
static: staticController,
api: apiController,
admin: adminController
2014-02-27 14:56:14 -05:00
};
Controllers.home = function(req, res, next) {
async.parallel({
header: function (next) {
res.locals.metaTags = [{
name: "title",
2015-01-04 11:37:20 -05:00
content: validator.escape(meta.config.title || 'NodeBB')
}, {
name: "description",
2015-01-04 11:37:20 -05:00
content: validator.escape(meta.config.description || '')
}, {
property: 'og:title',
2015-01-04 11:37:20 -05:00
content: 'Index | ' + validator.escape(meta.config.title || 'NodeBB')
}, {
2014-04-03 18:15:09 -04:00
property: 'og:type',
content: 'website'
}];
2014-02-27 14:56:14 -05:00
2014-04-03 18:15:09 -04:00
if(meta.config['brand:logo']) {
res.locals.metaTags.push({
property: 'og:image',
content: meta.config['brand:logo']
});
}
2014-02-27 14:56:14 -05:00
next(null);
},
categories: function (next) {
2014-03-24 15:58:02 -04:00
var uid = req.user ? req.user.uid : 0;
2014-08-15 15:45:01 -04:00
categories.getCategoriesByPrivilege(uid, 'find', function (err, categoryData) {
2014-03-24 15:58:02 -04:00
if (err) {
return next(err);
}
2014-09-16 16:10:02 -04:00
var childCategories = [];
2014-03-24 15:58:02 -04:00
2014-09-16 16:10:02 -04:00
for(var i=categoryData.length - 1; i>=0; --i) {
if (Array.isArray(categoryData[i].children) && categoryData[i].children.length) {
childCategories.push.apply(childCategories, categoryData[i].children);
}
2014-09-19 18:59:11 -04:00
if (categoryData[i].parent && categoryData[i].parent.cid) {
2014-09-16 16:10:02 -04:00
categoryData.splice(i, 1);
}
}
2014-09-16 16:10:02 -04:00
async.parallel([
function(next) {
categories.getRecentTopicReplies(categoryData, uid, next);
},
function(next) {
categories.getRecentTopicReplies(childCategories, uid, next);
}
], function(err) {
2014-05-27 12:44:28 -04:00
next(err, categoryData);
2014-02-27 14:56:14 -05:00
});
});
}
}, function (err, data) {
2014-07-07 17:36:10 -04:00
if (err) {
return next(err);
}
res.render('home', data);
2014-02-27 14:56:14 -05:00
});
};
Controllers.reset = function(req, res, next) {
2015-01-29 01:06:48 -05:00
if (req.params.code) {
res.render('reset_code', {
reset_code: req.params.code ? req.params.code : null,
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[reset_password:reset_password]]', url: '/reset'}, {text: '[[reset_password:update_password]]'}])
});
} else {
res.render('reset', {
reset_code: req.params.code ? req.params.code : null,
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[reset_password:reset_password]]'}])
});
}
};
2014-02-27 14:56:14 -05:00
2014-02-27 17:04:41 -05:00
Controllers.login = function(req, res, next) {
2014-02-27 16:52:46 -05:00
var data = {},
2014-11-12 16:15:44 -05:00
loginStrategies = auth.getLoginStrategies(),
2014-02-27 16:52:46 -05:00
emailersPresent = plugins.hasListeners('action:email.send');
2014-11-12 16:15:44 -05:00
data.alternate_logins = loginStrategies.length > 0;
data.authentication = loginStrategies;
2014-02-27 16:52:46 -05:00
data.showResetLink = emailersPresent;
2014-10-03 11:34:07 -04:00
data.allowLocalLogin = parseInt(meta.config.allowLocalLogin, 10) === 1;
data.allowRegistration = parseInt(meta.config.allowRegistration, 10) === 1;
2015-01-29 01:06:48 -05:00
data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[global:login]]'}]);
data.error = req.flash('error')[0];
2014-02-27 16:52:46 -05:00
res.render('login', data);
2014-02-27 16:52:46 -05:00
};
2014-02-27 17:04:41 -05:00
Controllers.register = function(req, res, next) {
2014-09-25 11:29:53 -04:00
if(meta.config.allowRegistration !== undefined && parseInt(meta.config.allowRegistration, 10) === 0) {
return res.redirect(nconf.get('relative_path') + '/403');
}
2014-03-11 04:10:00 -04:00
2014-02-27 16:52:46 -05:00
var data = {},
2014-11-12 16:15:44 -05:00
loginStrategies = auth.getLoginStrategies();
2014-02-27 16:52:46 -05:00
2014-11-12 16:15:44 -05:00
if (loginStrategies.length === 0) {
2014-02-27 16:52:46 -05:00
data = {
'register_window:spansize': 'col-md-12',
'alternate_logins': false
};
} else {
data = {
'register_window:spansize': 'col-md-6',
'alternate_logins': true
};
2014-02-27 16:52:46 -05:00
}
2014-11-12 16:15:44 -05:00
data.authentication = loginStrategies;
2014-02-27 16:52:46 -05:00
data.minimumUsernameLength = meta.config.minimumUsernameLength;
data.maximumUsernameLength = meta.config.maximumUsernameLength;
data.minimumPasswordLength = meta.config.minimumPasswordLength;
data.termsOfUse = meta.config.termsOfUse;
2015-01-29 01:06:48 -05:00
data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[register:register]]'}]);
2014-07-22 22:00:39 -04:00
data.regFormEntry = [];
2014-11-14 13:36:37 -05:00
data.error = req.flash('error')[0];
2014-03-11 04:10:00 -04:00
2014-11-08 14:51:05 -05:00
plugins.fireHook('filter:register.build', {req: req, res: res, templateData: data}, function(err, data) {
if (err && process.env === 'development') {
winston.warn(JSON.stringify(err));
2014-11-08 14:51:05 -05:00
return next(err);
}
2014-11-08 14:51:05 -05:00
res.render('register', data.templateData);
});
2014-02-27 16:52:46 -05:00
};
2014-02-27 17:04:41 -05:00
Controllers.confirmEmail = function(req, res, next) {
user.email.confirm(req.params.code, function (data) {
2014-06-03 16:10:24 -04:00
data.status = data.status === 'ok';
res.render('confirm', data);
2014-02-27 17:04:41 -05:00
});
};
2014-02-27 15:06:39 -05:00
2014-02-27 17:16:06 -05:00
Controllers.sitemap = function(req, res, next) {
2014-12-01 20:28:36 -05:00
if (parseInt(meta.config['feeds:disableSitemap'], 10) === 1) {
return helpers.notFound(req, res);
2014-07-17 01:38:20 -04:00
}
2014-05-22 15:23:19 -04:00
var sitemap = require('../sitemap.js');
2014-02-27 17:16:06 -05:00
sitemap.render(function(xml) {
res.header('Content-Type', 'application/xml');
res.send(xml);
});
};
Controllers.robots = function (req, res) {
res.set('Content-Type', 'text/plain');
if (meta.config["robots.txt"]) {
res.send(meta.config["robots.txt"]);
} else {
res.send("User-agent: *\n" +
2014-03-11 19:44:48 -04:00
"Disallow: " + nconf.get('relative_path') + "/admin/\n" +
2014-02-27 17:16:06 -05:00
"Sitemap: " + nconf.get('url') + "/sitemap.xml");
}
};
Controllers.outgoing = function(req, res, next) {
var url = req.query.url,
data = {
url: url,
2015-01-29 01:06:48 -05:00
title: meta.config.title,
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[notifications:outgoing_link]]'}])
};
if (url) {
res.render('outgoing', data);
} else {
2014-11-16 00:01:20 -05:00
res.status(404).redirect(nconf.get('relative_path') + '/404');
}
};
2014-11-14 12:17:24 -05:00
Controllers.termsOfUse = function(req, res, next) {
if (!meta.config.termsOfUse) {
2014-12-01 20:28:36 -05:00
return helpers.notFound(req, res);
2014-11-14 12:17:24 -05:00
}
res.render('tos', {termsOfUse: meta.config.termsOfUse});
};
2014-04-10 20:31:57 +01:00
module.exports = Controllers;