Files
NodeBB/src/controllers/home.js

79 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
2018-01-03 15:06:41 -05:00
var async = require('async');
2018-07-06 14:20:10 -04:00
var url = require('url');
var plugins = require('../plugins');
var meta = require('../meta');
var user = require('../user');
2018-01-03 15:06:41 -05:00
function adminHomePageRoute() {
return (meta.config.homePageRoute || meta.config.homePageCustom || '').replace(/^\/+/, '') || 'categories';
}
2018-01-03 15:06:41 -05:00
function getUserHomeRoute(uid, callback) {
async.waterfall([
function (next) {
user.getSettings(uid, next);
},
function (settings, next) {
var route = adminHomePageRoute();
if (settings.homePageRoute !== 'undefined' && settings.homePageRoute !== 'none') {
2018-07-06 14:20:10 -04:00
route = (settings.homePageRoute || route).replace(/^\/+/, '');
2018-01-03 15:06:41 -05:00
}
next(null, route);
},
], callback);
}
function rewrite(req, res, next) {
if (req.path !== '/' && req.path !== '/api/' && req.path !== '/api') {
return next();
}
2018-01-03 15:06:41 -05:00
async.waterfall([
function (next) {
if (meta.config.allowUserHomePage) {
2018-01-03 15:06:41 -05:00
getUserHomeRoute(req.uid, next);
} else {
next(null, adminHomePageRoute());
}
},
function (route, next) {
2018-07-06 14:20:10 -04:00
var parsedUrl;
try {
parsedUrl = url.parse(route, true);
} catch (err) {
return next(err);
}
2018-01-03 15:06:41 -05:00
2018-07-06 14:20:10 -04:00
var pathname = parsedUrl.pathname;
var hook = 'action:homepage.get:' + pathname;
2018-01-03 15:06:41 -05:00
if (!plugins.hasListeners(hook)) {
2018-07-06 14:20:10 -04:00
req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + pathname;
2018-01-03 15:06:41 -05:00
} else {
2018-07-06 14:20:10 -04:00
res.locals.homePageRoute = pathname;
2018-01-03 15:06:41 -05:00
}
2018-07-06 14:20:10 -04:00
req.query = Object.assign(parsedUrl.query, req.query);
2018-01-03 15:06:41 -05:00
next();
},
], next);
}
exports.rewrite = rewrite;
function pluginHook(req, res, next) {
var hook = 'action:homepage.get:' + res.locals.homePageRoute;
plugins.fireHook(hook, {
req: req,
res: res,
next: next,
});
}
exports.pluginHook = pluginHook;