You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.2 KiB
JavaScript

"use strict";
11 years ago
var topicsController = require('./topics'),
categoriesController = require('./categories'),
tagsController = require('./tags'),
usersController = require('./users'),
11 years ago
groupsController = require('./groups'),
accountsController = require('./accounts'),
staticController = require('./static'),
apiController = require('./api'),
adminController = require('./admin'),
helpers = require('./helpers'),
11 years ago
async = require('async'),
nconf = require('nconf'),
11 years ago
validator = require('validator'),
11 years ago
winston = require('winston'),
11 years ago
auth = require('../routes/authentication'),
meta = require('../meta'),
user = require('../user'),
posts = require('../posts'),
topics = require('../topics'),
11 years ago
search = require('../search'),
11 years ago
plugins = require('../plugins'),
categories = require('../categories'),
11 years ago
privileges = require('../privileges');
11 years ago
var Controllers = {
11 years ago
topics: topicsController,
categories: categoriesController,
tags: tagsController,
users: usersController,
11 years ago
groups: groupsController,
accounts: accountsController,
static: staticController,
api: apiController,
admin: adminController
11 years ago
};
Controllers.home = function(req, res, next) {
async.parallel({
header: function (next) {
res.locals.metaTags = [{
name: "title",
10 years ago
content: validator.escape(meta.config.title || 'NodeBB')
}, {
name: "description",
10 years ago
content: validator.escape(meta.config.description || '')
}, {
property: 'og:title',
10 years ago
content: 'Index | ' + validator.escape(meta.config.title || 'NodeBB')
}, {
11 years ago
property: 'og:type',
content: 'website'
}];
11 years ago
11 years ago
if(meta.config['brand:logo']) {
res.locals.metaTags.push({
property: 'og:image',
content: meta.config['brand:logo']
});
}
11 years ago
next(null);
},
categories: function (next) {
var uid = req.user ? req.user.uid : 0;
11 years ago
categories.getCategoriesByPrivilege(uid, 'find', function (err, categoryData) {
if (err) {
return next(err);
}
11 years ago
var childCategories = [];
11 years ago
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);
}
11 years ago
if (categoryData[i].parent && categoryData[i].parent.cid) {
11 years ago
categoryData.splice(i, 1);
}
}
11 years ago
async.parallel([
function(next) {
categories.getRecentTopicReplies(categoryData, uid, next);
},
function(next) {
categories.getRecentTopicReplies(childCategories, uid, next);
}
], function(err) {
next(err, categoryData);
11 years ago
});
});
}
}, function (err, data) {
11 years ago
if (err) {
return next(err);
}
res.render('home', data);
11 years ago
});
};
Controllers.search = function(req, res, next) {
10 years ago
if (!plugins.hasListeners('filter:search.query')) {
return helpers.notFound(req, res);
}
if (!req.params.term) {
return res.render('search', {
11 years ago
time: 0,
search_query: '',
posts: [],
topics: []
});
}
11 years ago
var uid = req.user ? req.user.uid : 0;
11 years ago
req.params.term = validator.escape(req.params.term);
11 years ago
11 years ago
search.search(req.params.term, uid, function(err, results) {
if (err) {
return next(err);
}
11 years ago
return res.render('search', results);
});
};
Controllers.reset = function(req, res, next) {
res.render(req.params.code ? 'reset_code' : 'reset', {
reset_code: req.params.code ? req.params.code : null
});
};
11 years ago
Controllers.login = function(req, res, next) {
var data = {},
10 years ago
loginStrategies = auth.getLoginStrategies(),
emailersPresent = plugins.hasListeners('action:email.send');
10 years ago
data.alternate_logins = loginStrategies.length > 0;
data.authentication = loginStrategies;
data.showResetLink = emailersPresent;
10 years ago
data.allowLocalLogin = parseInt(meta.config.allowLocalLogin, 10) === 1;
data.allowRegistration = parseInt(meta.config.allowRegistration, 10) === 1;
data.error = req.flash('error')[0];
res.render('login', data);
};
Controllers.register = function(req, res, next) {
11 years ago
if(meta.config.allowRegistration !== undefined && parseInt(meta.config.allowRegistration, 10) === 0) {
return res.redirect(nconf.get('relative_path') + '/403');
}
11 years ago
var data = {},
10 years ago
loginStrategies = auth.getLoginStrategies();
10 years ago
if (loginStrategies.length === 0) {
data = {
'register_window:spansize': 'col-md-12',
'alternate_logins': false
};
} else {
data = {
'register_window:spansize': 'col-md-6',
'alternate_logins': true
};
}
10 years ago
data.authentication = loginStrategies;
data.minimumUsernameLength = meta.config.minimumUsernameLength;
data.maximumUsernameLength = meta.config.maximumUsernameLength;
data.minimumPasswordLength = meta.config.minimumPasswordLength;
data.termsOfUse = meta.config.termsOfUse;
data.regFormEntry = [];
10 years ago
data.error = req.flash('error')[0];
11 years ago
plugins.fireHook('filter:register.build', {req: req, res: res, templateData: data}, function(err, data) {
if (err && process.env === 'development') {
winston.warn(JSON.stringify(err));
return next(err);
}
res.render('register', data.templateData);
});
};
Controllers.confirmEmail = function(req, res, next) {
user.email.confirm(req.params.code, function (data) {
data.status = data.status === 'ok';
res.render('confirm', data);
});
};
11 years ago
Controllers.sitemap = function(req, res, next) {
10 years ago
if (parseInt(meta.config['feeds:disableSitemap'], 10) === 1) {
return helpers.notFound(req, res);
}
11 years ago
var sitemap = require('../sitemap.js');
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" +
"Disallow: " + nconf.get('relative_path') + "/admin/\n" +
"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).redirect(nconf.get('relative_path') + '/404');
}
};
10 years ago
Controllers.termsOfUse = function(req, res, next) {
if (!meta.config.termsOfUse) {
10 years ago
return helpers.notFound(req, res);
10 years ago
}
res.render('tos', {termsOfUse: meta.config.termsOfUse});
};
module.exports = Controllers;