Files
NodeBB/src/controllers/helpers.js

244 lines
6.2 KiB
JavaScript
Raw Normal View History

2014-11-15 23:22:57 -05:00
'use strict';
var nconf = require('nconf');
var async = require('async');
var validator = require('validator');
2016-07-11 13:03:47 +03:00
var winston = require('winston');
2016-11-03 13:06:21 +03:00
var user = require('../user');
var privileges = require('../privileges');
var categories = require('../categories');
var plugins = require('../plugins');
var meta = require('../meta');
2017-05-12 17:53:23 -04:00
var middleware = require('../middleware');
2014-11-15 23:22:57 -05:00
2017-05-12 17:53:23 -04:00
var helpers = module.exports;
2014-11-15 23:22:57 -05:00
Squashed commit of the following: commit 9c86d9b2904e14927cd7e9679b92aec0951d1063 Merge: ebfa63a 5a7f811 Author: Julian Lam <julian@nodebb.org> Date: Thu Jul 20 08:41:39 2017 -0400 Merge branch 'noscript-login' of https://github.com/An-dz/NodeBB into noscript commit 5a7f81185e8f9bd7d2d011c3d495988be7e437a3 Author: André Zanghelini <an_dz@simutrans-forum> Date: Mon Jul 17 23:07:14 2017 -0300 Rename clashing variable 'next' commit ebfa63a984073a58c17aa408c363cdb03ef89985 Merge: c1801cd f159d0d Author: Julian Lam <julian@nodebb.org> Date: Mon Jul 17 16:30:40 2017 -0400 Merge branch 'noscript-logout' of https://github.com/An-dz/NodeBB into noscript commit c1801cda14e6363491e30b659902e2ae71f7e1f7 Merge: 7a5f9f3 9fd542d Author: Julian Lam <julian@nodebb.org> Date: Mon Jul 17 16:30:31 2017 -0400 Merge branch 'noscript-register' of https://github.com/An-dz/NodeBB into noscript commit 7a5f9f35abc834bb72ddddc9ca07d34f2fde8353 Merge: 44851f9 d37b95c Author: Julian Lam <julian@nodebb.org> Date: Mon Jul 17 16:30:10 2017 -0400 Merge branch 'noscript-compose' of https://github.com/An-dz/NodeBB into noscript commit f159d0d9ef1b7f600e830a96fdb4b9c87c79bb4a Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 12:16:38 2017 -0300 Prevent form submit Required for theme change commit d37b95cb71d32d4483190609798e244c331db165 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 01:49:52 2017 -0300 Prevent link action with scripts Required for the theme change that changes the buttons to `a` tags. commit 9fd542d8970b7d1a4126f4edc4b44eab7d708fb0 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:57:56 2017 -0300 Fix tests commit cdad5bf8c2891ad76f7441fd4d8a74b058a14e6d Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:09:17 2017 -0300 Update error handling commit 4ff11cd136a4fb98483f837e2cebc741380dfe76 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 17:29:08 2017 -0300 Remove async waterfall commit df01d44e821a70c984b89e9585a325c3e02c6e37 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:43 2017 -0300 Set noscript compose as noscript at start commit 4bcc380da72239b8315cc849a77a3036e06e4a12 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:12 2017 -0300 Remove last useless next commit b5eac6fea11e209934c0648a7e75ad07a2167123 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:35:08 2017 -0300 Last function requires no next commit 20a5cce6e6e32a454c304c448383707ec44c75a8 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:06:58 2017 -0300 Remove more useless next calls commit 85ee22a79bcbbb1995106f43d4c74d6ba9206cab Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 17:46:07 2017 -0300 Remove useless next calls commit 7d984c47ad24faac1fe537dee4a5a7d697e8634c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:45:31 2017 -0300 Support old themes commit 4a09dfbd08253115c342a9e829c4e6940cecb8cc Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:37:23 2017 -0300 Moved all error handling into helpers function commit 391aa6e67ef9ab67304005e14ac0633cdb630713 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jun 8 15:37:37 2017 -0300 ESLint - Fix mixed conditionals commit 80ccc6fd581d791f31e7ab62de8de611837bfc3c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:08:15 2017 -0300 Compose without scripts commit 2aca811256721238ca0cede4954213d369009885 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:00:44 2017 -0300 Register without scripts commit 097bb51577fb26f8e22f86dc274cb670ab606a8a Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:42:15 2017 -0300 Logout without scripts commit d497e08109891079656fee1c145043a9c0e55f2e Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:27:10 2017 -0300 Login without script
2017-07-20 08:51:04 -04:00
helpers.noScriptErrors = function (req, res, error, httpStatus) {
if (req.body.noscript !== 'true') {
return res.status(httpStatus).send(error);
}
var middleware = require('../middleware');
var httpStatusString = httpStatus.toString();
middleware.buildHeader(req, res, function () {
res.status(httpStatus).render(httpStatusString, {
path: req.path,
2018-01-31 15:20:17 -05:00
loggedIn: req.loggedIn,
Squashed commit of the following: commit 9c86d9b2904e14927cd7e9679b92aec0951d1063 Merge: ebfa63a 5a7f811 Author: Julian Lam <julian@nodebb.org> Date: Thu Jul 20 08:41:39 2017 -0400 Merge branch 'noscript-login' of https://github.com/An-dz/NodeBB into noscript commit 5a7f81185e8f9bd7d2d011c3d495988be7e437a3 Author: André Zanghelini <an_dz@simutrans-forum> Date: Mon Jul 17 23:07:14 2017 -0300 Rename clashing variable 'next' commit ebfa63a984073a58c17aa408c363cdb03ef89985 Merge: c1801cd f159d0d Author: Julian Lam <julian@nodebb.org> Date: Mon Jul 17 16:30:40 2017 -0400 Merge branch 'noscript-logout' of https://github.com/An-dz/NodeBB into noscript commit c1801cda14e6363491e30b659902e2ae71f7e1f7 Merge: 7a5f9f3 9fd542d Author: Julian Lam <julian@nodebb.org> Date: Mon Jul 17 16:30:31 2017 -0400 Merge branch 'noscript-register' of https://github.com/An-dz/NodeBB into noscript commit 7a5f9f35abc834bb72ddddc9ca07d34f2fde8353 Merge: 44851f9 d37b95c Author: Julian Lam <julian@nodebb.org> Date: Mon Jul 17 16:30:10 2017 -0400 Merge branch 'noscript-compose' of https://github.com/An-dz/NodeBB into noscript commit f159d0d9ef1b7f600e830a96fdb4b9c87c79bb4a Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 12:16:38 2017 -0300 Prevent form submit Required for theme change commit d37b95cb71d32d4483190609798e244c331db165 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 01:49:52 2017 -0300 Prevent link action with scripts Required for the theme change that changes the buttons to `a` tags. commit 9fd542d8970b7d1a4126f4edc4b44eab7d708fb0 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:57:56 2017 -0300 Fix tests commit cdad5bf8c2891ad76f7441fd4d8a74b058a14e6d Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:09:17 2017 -0300 Update error handling commit 4ff11cd136a4fb98483f837e2cebc741380dfe76 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 17:29:08 2017 -0300 Remove async waterfall commit df01d44e821a70c984b89e9585a325c3e02c6e37 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:43 2017 -0300 Set noscript compose as noscript at start commit 4bcc380da72239b8315cc849a77a3036e06e4a12 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:12 2017 -0300 Remove last useless next commit b5eac6fea11e209934c0648a7e75ad07a2167123 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:35:08 2017 -0300 Last function requires no next commit 20a5cce6e6e32a454c304c448383707ec44c75a8 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:06:58 2017 -0300 Remove more useless next calls commit 85ee22a79bcbbb1995106f43d4c74d6ba9206cab Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 17:46:07 2017 -0300 Remove useless next calls commit 7d984c47ad24faac1fe537dee4a5a7d697e8634c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:45:31 2017 -0300 Support old themes commit 4a09dfbd08253115c342a9e829c4e6940cecb8cc Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:37:23 2017 -0300 Moved all error handling into helpers function commit 391aa6e67ef9ab67304005e14ac0633cdb630713 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jun 8 15:37:37 2017 -0300 ESLint - Fix mixed conditionals commit 80ccc6fd581d791f31e7ab62de8de611837bfc3c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:08:15 2017 -0300 Compose without scripts commit 2aca811256721238ca0cede4954213d369009885 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:00:44 2017 -0300 Register without scripts commit 097bb51577fb26f8e22f86dc274cb670ab606a8a Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:42:15 2017 -0300 Logout without scripts commit d497e08109891079656fee1c145043a9c0e55f2e Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:27:10 2017 -0300 Login without script
2017-07-20 08:51:04 -04:00
error: error,
returnLink: true,
Squashed commit of the following: commit 9c86d9b2904e14927cd7e9679b92aec0951d1063 Merge: ebfa63a 5a7f811 Author: Julian Lam <julian@nodebb.org> Date: Thu Jul 20 08:41:39 2017 -0400 Merge branch 'noscript-login' of https://github.com/An-dz/NodeBB into noscript commit 5a7f81185e8f9bd7d2d011c3d495988be7e437a3 Author: André Zanghelini <an_dz@simutrans-forum> Date: Mon Jul 17 23:07:14 2017 -0300 Rename clashing variable 'next' commit ebfa63a984073a58c17aa408c363cdb03ef89985 Merge: c1801cd f159d0d Author: Julian Lam <julian@nodebb.org> Date: Mon Jul 17 16:30:40 2017 -0400 Merge branch 'noscript-logout' of https://github.com/An-dz/NodeBB into noscript commit c1801cda14e6363491e30b659902e2ae71f7e1f7 Merge: 7a5f9f3 9fd542d Author: Julian Lam <julian@nodebb.org> Date: Mon Jul 17 16:30:31 2017 -0400 Merge branch 'noscript-register' of https://github.com/An-dz/NodeBB into noscript commit 7a5f9f35abc834bb72ddddc9ca07d34f2fde8353 Merge: 44851f9 d37b95c Author: Julian Lam <julian@nodebb.org> Date: Mon Jul 17 16:30:10 2017 -0400 Merge branch 'noscript-compose' of https://github.com/An-dz/NodeBB into noscript commit f159d0d9ef1b7f600e830a96fdb4b9c87c79bb4a Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 12:16:38 2017 -0300 Prevent form submit Required for theme change commit d37b95cb71d32d4483190609798e244c331db165 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jul 6 01:49:52 2017 -0300 Prevent link action with scripts Required for the theme change that changes the buttons to `a` tags. commit 9fd542d8970b7d1a4126f4edc4b44eab7d708fb0 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:57:56 2017 -0300 Fix tests commit cdad5bf8c2891ad76f7441fd4d8a74b058a14e6d Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 19:09:17 2017 -0300 Update error handling commit 4ff11cd136a4fb98483f837e2cebc741380dfe76 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 17:29:08 2017 -0300 Remove async waterfall commit df01d44e821a70c984b89e9585a325c3e02c6e37 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:43 2017 -0300 Set noscript compose as noscript at start commit 4bcc380da72239b8315cc849a77a3036e06e4a12 Author: André Zanghelini <an_dz@simutrans-forum> Date: Wed Jul 5 16:59:12 2017 -0300 Remove last useless next commit b5eac6fea11e209934c0648a7e75ad07a2167123 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:35:08 2017 -0300 Last function requires no next commit 20a5cce6e6e32a454c304c448383707ec44c75a8 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 18:06:58 2017 -0300 Remove more useless next calls commit 85ee22a79bcbbb1995106f43d4c74d6ba9206cab Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 17:46:07 2017 -0300 Remove useless next calls commit 7d984c47ad24faac1fe537dee4a5a7d697e8634c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:45:31 2017 -0300 Support old themes commit 4a09dfbd08253115c342a9e829c4e6940cecb8cc Author: André Zanghelini <an_dz@simutrans-forum> Date: Sun Jul 2 15:37:23 2017 -0300 Moved all error handling into helpers function commit 391aa6e67ef9ab67304005e14ac0633cdb630713 Author: André Zanghelini <an_dz@simutrans-forum> Date: Thu Jun 8 15:37:37 2017 -0300 ESLint - Fix mixed conditionals commit 80ccc6fd581d791f31e7ab62de8de611837bfc3c Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:08:15 2017 -0300 Compose without scripts commit 2aca811256721238ca0cede4954213d369009885 Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 18:00:44 2017 -0300 Register without scripts commit 097bb51577fb26f8e22f86dc274cb670ab606a8a Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:42:15 2017 -0300 Logout without scripts commit d497e08109891079656fee1c145043a9c0e55f2e Author: André Zanghelini <an_dz@simutrans-forum> Date: Sat Jun 3 16:27:10 2017 -0300 Login without script
2017-07-20 08:51:04 -04:00
title: '[[global:' + httpStatusString + '.title]]',
});
});
};
2017-10-19 13:53:05 -04:00
helpers.validFilters = { '': true, new: true, watched: true, unreplied: true };
helpers.buildFilters = function (url, filter) {
return [{
name: '[[unread:all-topics]]',
url: url,
selected: filter === '',
filter: '',
}, {
name: '[[unread:new-topics]]',
url: url + '/new',
selected: filter === 'new',
filter: 'new',
}, {
name: '[[unread:watched-topics]]',
url: url + '/watched',
selected: filter === 'watched',
filter: 'watched',
}, {
name: '[[unread:unreplied-topics]]',
url: url + '/unreplied',
selected: filter === 'unreplied',
filter: 'unreplied',
}];
};
helpers.notAllowed = function (req, res, error) {
2016-06-22 14:58:46 -04:00
plugins.fireHook('filter:helpers.notAllowed', {
req: req,
res: res,
2017-02-17 19:31:21 -07:00
error: error,
2017-02-18 15:05:36 -07:00
}, function (err) {
2016-07-11 13:03:47 +03:00
if (err) {
return winston.error(err);
}
2018-01-31 15:20:17 -05:00
if (req.loggedIn) {
2016-06-22 14:58:46 -04:00
if (res.locals.isAPI) {
res.status(403).json({
path: req.path.replace(/^\/api/, ''),
2018-01-31 15:20:17 -05:00
loggedIn: req.loggedIn,
error: error,
2017-02-17 19:31:21 -07:00
title: '[[global:403.title]]',
2016-06-22 14:58:46 -04:00
});
} else {
2017-05-12 17:53:23 -04:00
middleware.buildHeader(req, res, function () {
res.status(403).render('403', {
path: req.path,
2018-01-31 15:20:17 -05:00
loggedIn: req.loggedIn,
2017-05-12 17:53:23 -04:00
error: error,
title: '[[global:403.title]]',
});
2016-06-22 14:58:46 -04:00
});
}
2017-02-18 14:32:35 -07:00
} else if (res.locals.isAPI) {
req.session.returnTo = nconf.get('relative_path') + req.url.replace(/^\/api/, '');
res.status(401).json('not-authorized');
2014-11-15 23:22:57 -05:00
} else {
2017-02-18 14:32:35 -07:00
req.session.returnTo = nconf.get('relative_path') + req.url;
res.redirect(nconf.get('relative_path') + '/login');
2014-11-15 23:22:57 -05:00
}
2016-06-22 14:58:46 -04:00
});
2014-11-15 23:22:57 -05:00
};
helpers.redirect = function (res, url) {
2015-03-09 18:22:44 -04:00
if (res.locals.isAPI) {
2017-07-05 11:36:35 -04:00
res.set('X-Redirect', encodeURI(url)).status(200).json(url);
2015-03-09 18:22:44 -04:00
} else {
2016-03-20 15:11:32 -05:00
res.redirect(nconf.get('relative_path') + encodeURI(url));
2015-03-09 18:22:44 -04:00
}
};
helpers.buildCategoryBreadcrumbs = function (cid, callback) {
var breadcrumbs = [];
async.whilst(function () {
return parseInt(cid, 10);
}, function (next) {
2017-05-29 17:12:02 -04:00
categories.getCategoryFields(cid, ['name', 'slug', 'parentCid', 'disabled', 'isSection'], function (err, data) {
if (err) {
return next(err);
}
2017-05-29 17:12:02 -04:00
if (!parseInt(data.disabled, 10) && !parseInt(data.isSection, 10)) {
breadcrumbs.unshift({
text: validator.escape(String(data.name)),
2017-02-17 19:31:21 -07:00
url: nconf.get('relative_path') + '/category/' + data.slug,
});
}
cid = data.parentCid;
next();
});
}, function (err) {
if (err) {
return callback(err);
}
if (meta.config.homePageRoute && meta.config.homePageRoute !== 'categories') {
2016-04-21 11:40:40 -04:00
breadcrumbs.unshift({
text: '[[global:header.categories]]',
2017-02-17 19:31:21 -07:00
url: nconf.get('relative_path') + '/categories',
2016-04-21 11:40:40 -04:00
});
}
2015-01-29 01:06:48 -05:00
breadcrumbs.unshift({
text: '[[global:home]]',
2017-02-17 19:31:21 -07:00
url: nconf.get('relative_path') + '/',
});
2015-01-29 01:06:48 -05:00
callback(null, breadcrumbs);
});
};
helpers.buildBreadcrumbs = function (crumbs) {
2015-01-29 01:06:48 -05:00
var breadcrumbs = [
{
text: '[[global:home]]',
2017-02-17 19:31:21 -07:00
url: nconf.get('relative_path') + '/',
},
2015-01-29 01:06:48 -05:00
];
crumbs.forEach(function (crumb) {
2015-01-29 01:06:48 -05:00
if (crumb) {
if (crumb.url) {
crumb.url = nconf.get('relative_path') + crumb.url;
}
breadcrumbs.push(crumb);
}
});
2015-01-29 01:06:48 -05:00
return breadcrumbs;
};
2014-11-15 23:22:57 -05:00
helpers.buildTitle = function (pageTitle) {
2015-09-23 01:59:13 -04:00
var titleLayout = meta.config.titleLayout || '{pageTitle} | {browserTitle}';
var browserTitle = validator.escape(String(meta.config.browserTitle || meta.config.title || 'NodeBB'));
2015-09-23 01:59:13 -04:00
pageTitle = pageTitle || '';
var title = titleLayout.replace('{pageTitle}', function () {
2016-03-22 11:19:33 +02:00
return pageTitle;
}).replace('{browserTitle}', function () {
2016-03-22 11:19:33 +02:00
return browserTitle;
});
2015-09-23 01:59:13 -04:00
return title;
};
2016-11-03 13:25:29 +03:00
helpers.getWatchedCategories = function (uid, selectedCid, callback) {
2017-10-30 15:26:12 -04:00
if (selectedCid && !Array.isArray(selectedCid)) {
selectedCid = [selectedCid];
}
2016-11-03 13:06:21 +03:00
async.waterfall([
function (next) {
user.getWatchedCategories(uid, next);
},
function (cids, next) {
privileges.categories.filterCids('read', cids, uid, next);
},
function (cids, next) {
categories.getCategoriesFields(cids, ['cid', 'name', 'slug', 'icon', 'link', 'color', 'bgColor', 'parentCid'], next);
},
function (categoryData, next) {
categoryData = categoryData.filter(function (category) {
return category && !category.link;
});
2017-10-30 15:26:12 -04:00
var selectedCategory = [];
var selectedCids = [];
2016-11-03 13:06:21 +03:00
categoryData.forEach(function (category) {
2017-10-30 15:26:12 -04:00
category.selected = selectedCid ? selectedCid.indexOf(String(category.cid)) !== -1 : false;
2016-11-03 13:06:21 +03:00
if (category.selected) {
2017-10-30 15:26:12 -04:00
selectedCategory.push(category);
selectedCids.push(parseInt(category.cid, 10));
2016-11-03 13:06:21 +03:00
}
});
2017-10-30 15:26:12 -04:00
selectedCids.sort(function (a, b) {
return a - b;
});
if (selectedCategory.length > 1) {
selectedCategory = {
icon: 'fa-plus',
name: '[[unread:multiple-categories-selected]]',
bgColor: '#ddd',
};
} else if (selectedCategory.length === 1) {
selectedCategory = selectedCategory[0];
} else {
selectedCategory = undefined;
}
2016-11-03 13:06:21 +03:00
var categoriesData = [];
var tree = categories.getTree(categoryData, 0);
tree.forEach(function (category) {
recursive(category, categoriesData, '');
});
2017-10-30 15:26:12 -04:00
next(null, { categories: categoriesData, selectedCategory: selectedCategory, selectedCids: selectedCids });
2017-02-17 19:31:21 -07:00
},
2016-11-03 13:06:21 +03:00
], callback);
};
function recursive(category, categoriesData, level) {
category.level = level;
categoriesData.push(category);
category.children.forEach(function (child) {
recursive(child, categoriesData, '&nbsp;&nbsp;&nbsp;&nbsp;' + level);
});
}