Files
NodeBB/src/controllers/index.js

243 lines
6.0 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'),
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-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'),
2014-07-07 17:36:10 -04:00
search = require('../search'),
2014-05-22 15:23:19 -04:00
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,
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",
content: meta.config.title || 'NodeBB'
}, {
name: "description",
content: meta.config.description || ''
}, {
property: 'og:title',
content: 'Index | ' + (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.search = function(req, res, next) {
2014-03-09 21:43:35 -04:00
if (!req.params.term) {
return res.render('search', {
2014-07-07 17:36:10 -04:00
time: 0,
2014-03-09 21:43:35 -04:00
search_query: '',
posts: [],
topics: []
});
}
2014-07-07 17:36:10 -04:00
var uid = req.user ? req.user.uid : 0;
2014-03-09 21:43:35 -04:00
if (!plugins.hasListeners('filter:search.query')) {
return res.redirect('/404');
}
2014-09-27 19:23:48 -04:00
req.params.term = validator.escape(req.params.term);
2014-09-03 14:40:44 -04:00
2014-07-07 17:36:10 -04:00
search.search(req.params.term, uid, function(err, results) {
2014-03-09 21:43:35 -04:00
if (err) {
return next(err);
}
2014-07-07 17:36:10 -04:00
return res.render('search', results);
});
};
Controllers.reset = function(req, res, next) {
2014-03-21 20:37:53 -04:00
res.render(req.params.code ? 'reset_code' : 'reset', {
reset_code: req.params.code ? req.params.code : null
});
};
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 = {},
login_strategies = auth.get_login_strategies(),
num_strategies = login_strategies.length,
emailersPresent = plugins.hasListeners('action:email.send');
2014-04-08 16:48:13 -04:00
data.alternate_logins = num_strategies > 0;
2014-02-27 16:52:46 -05:00
data.authentication = login_strategies;
2014-09-14 10:21:32 -04:00
data.token = req.csrfToken();
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;
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 = {},
login_strategies = auth.get_login_strategies(),
num_strategies = login_strategies.length;
if (num_strategies === 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
}
data.authentication = login_strategies;
2014-09-14 10:21:32 -04:00
data.token = req.csrfToken();
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;
2014-07-22 22:00:39 -04:00
data.regFormEntry = [];
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-07-17 01:38:20 -04:00
if (meta.config['feeds:disableSitemap'] === '1') {
2014-09-16 16:10:02 -04:00
return res.redirect(nconf.get('relative_path') + '/404');
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,
title: meta.config.title
};
if (url) {
res.render('outgoing', data);
} else {
res.status(404);
res.redirect(nconf.get('relative_path') + '/404');
}
};
2014-04-10 20:31:57 +01:00
module.exports = Controllers;