Files
NodeBB/src/controllers/helpers.js
Julian Lam c4fbed24f8 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 5a7f81185e
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 f159d0d9ef
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Thu Jul 6 12:16:38 2017 -0300

    Prevent form submit

    Required for theme change

commit d37b95cb71
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 9fd542d897
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Wed Jul 5 19:57:56 2017 -0300

    Fix tests

commit cdad5bf8c2
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Wed Jul 5 19:09:17 2017 -0300

    Update error handling

commit 4ff11cd136
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Wed Jul 5 17:29:08 2017 -0300

    Remove async waterfall

commit df01d44e82
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Wed Jul 5 16:59:43 2017 -0300

    Set noscript compose as noscript at start

commit 4bcc380da7
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Wed Jul 5 16:59:12 2017 -0300

    Remove last useless next

commit b5eac6fea1
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Sun Jul 2 18:35:08 2017 -0300

    Last function requires no next

commit 20a5cce6e6
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Sun Jul 2 18:06:58 2017 -0300

    Remove more useless next calls

commit 85ee22a79b
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Sun Jul 2 17:46:07 2017 -0300

    Remove useless next calls

commit 7d984c47ad
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Sun Jul 2 15:45:31 2017 -0300

    Support old themes

commit 4a09dfbd08
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Sun Jul 2 15:37:23 2017 -0300

    Moved all error handling into helpers function

commit 391aa6e67e
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Thu Jun 8 15:37:37 2017 -0300

    ESLint - Fix mixed conditionals

commit 80ccc6fd58
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Sat Jun 3 18:08:15 2017 -0300

    Compose without scripts

commit 2aca811256
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Sat Jun 3 18:00:44 2017 -0300

    Register without scripts

commit 097bb51577
Author: André Zanghelini <an_dz@simutrans-forum>
Date:   Sat Jun 3 16:42:15 2017 -0300

    Logout without scripts

commit d497e08109
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

199 lines
5.0 KiB
JavaScript

'use strict';
var nconf = require('nconf');
var async = require('async');
var validator = require('validator');
var winston = require('winston');
var user = require('../user');
var privileges = require('../privileges');
var categories = require('../categories');
var plugins = require('../plugins');
var meta = require('../meta');
var middleware = require('../middleware');
var helpers = module.exports;
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,
loggedIn: true,
error: error,
returnLink: true,
title: '[[global:' + httpStatusString + '.title]]',
});
});
};
helpers.notAllowed = function (req, res, error) {
plugins.fireHook('filter:helpers.notAllowed', {
req: req,
res: res,
error: error,
}, function (err) {
if (err) {
return winston.error(err);
}
if (req.uid) {
if (res.locals.isAPI) {
res.status(403).json({
path: req.path.replace(/^\/api/, ''),
loggedIn: !!req.uid,
error: error,
title: '[[global:403.title]]',
});
} else {
middleware.buildHeader(req, res, function () {
res.status(403).render('403', {
path: req.path,
loggedIn: !!req.uid,
error: error,
title: '[[global:403.title]]',
});
});
}
} else if (res.locals.isAPI) {
req.session.returnTo = nconf.get('relative_path') + req.url.replace(/^\/api/, '');
res.status(401).json('not-authorized');
} else {
req.session.returnTo = nconf.get('relative_path') + req.url;
res.redirect(nconf.get('relative_path') + '/login');
}
});
};
helpers.redirect = function (res, url) {
if (res.locals.isAPI) {
res.set('X-Redirect', encodeURI(url)).status(200).json(url);
} else {
res.redirect(nconf.get('relative_path') + encodeURI(url));
}
};
helpers.buildCategoryBreadcrumbs = function (cid, callback) {
var breadcrumbs = [];
async.whilst(function () {
return parseInt(cid, 10);
}, function (next) {
categories.getCategoryFields(cid, ['name', 'slug', 'parentCid', 'disabled', 'isSection'], function (err, data) {
if (err) {
return next(err);
}
if (!parseInt(data.disabled, 10) && !parseInt(data.isSection, 10)) {
breadcrumbs.unshift({
text: validator.escape(String(data.name)),
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.homePageCustom) {
breadcrumbs.unshift({
text: '[[global:header.categories]]',
url: nconf.get('relative_path') + '/categories',
});
}
breadcrumbs.unshift({
text: '[[global:home]]',
url: nconf.get('relative_path') + '/',
});
callback(null, breadcrumbs);
});
};
helpers.buildBreadcrumbs = function (crumbs) {
var breadcrumbs = [
{
text: '[[global:home]]',
url: nconf.get('relative_path') + '/',
},
];
crumbs.forEach(function (crumb) {
if (crumb) {
if (crumb.url) {
crumb.url = nconf.get('relative_path') + crumb.url;
}
breadcrumbs.push(crumb);
}
});
return breadcrumbs;
};
helpers.buildTitle = function (pageTitle) {
var titleLayout = meta.config.titleLayout || '{pageTitle} | {browserTitle}';
var browserTitle = validator.escape(String(meta.config.browserTitle || meta.config.title || 'NodeBB'));
pageTitle = pageTitle || '';
var title = titleLayout.replace('{pageTitle}', function () {
return pageTitle;
}).replace('{browserTitle}', function () {
return browserTitle;
});
return title;
};
helpers.getWatchedCategories = function (uid, selectedCid, callback) {
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;
});
var selectedCategory;
categoryData.forEach(function (category) {
category.selected = parseInt(category.cid, 10) === parseInt(selectedCid, 10);
if (category.selected) {
selectedCategory = category;
}
});
var categoriesData = [];
var tree = categories.getTree(categoryData, 0);
tree.forEach(function (category) {
recursive(category, categoriesData, '');
});
next(null, { categories: categoriesData, selectedCategory: selectedCategory });
},
], 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);
});
}