Files
NodeBB/src/controllers/home.js

65 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
2019-07-31 12:39:15 -04:00
const url = require('url');
2018-07-06 14:20:10 -04:00
2019-07-31 12:39:15 -04:00
const plugins = require('../plugins');
const meta = require('../meta');
const user = require('../user');
2018-01-03 15:06:41 -05:00
function adminHomePageRoute() {
return ((meta.config.homePageRoute === 'custom' ? meta.config.homePageCustom : meta.config.homePageRoute) || 'categories').replace(/^\//, '');
}
2019-07-31 12:39:15 -04:00
async function getUserHomeRoute(uid) {
const settings = await user.getSettings(uid);
let route = adminHomePageRoute();
2018-01-03 15:06:41 -05:00
2019-07-31 12:39:15 -04:00
if (settings.homePageRoute !== 'undefined' && settings.homePageRoute !== 'none') {
route = (settings.homePageRoute || route).replace(/^\/+/, '');
}
2018-01-03 15:06:41 -05:00
2019-07-31 12:39:15 -04:00
return route;
}
2019-07-31 12:39:15 -04:00
async function rewrite(req, res, next) {
if (req.path !== '/' && req.path !== '/api/' && req.path !== '/api') {
return next();
}
2019-07-31 12:39:15 -04:00
let route = adminHomePageRoute();
if (meta.config.allowUserHomePage) {
route = await getUserHomeRoute(req.uid, next);
}
2019-07-31 12:39:15 -04:00
let parsedUrl;
try {
parsedUrl = url.parse(route, true);
} catch (err) {
return next(err);
}
2018-01-03 15:06:41 -05:00
2019-07-31 12:39:15 -04:00
const pathname = parsedUrl.pathname;
2021-02-03 23:59:08 -07:00
const hook = `action:homepage.get:${pathname}`;
if (!plugins.hooks.hasListeners(hook)) {
2019-07-31 12:39:15 -04:00
req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + pathname;
} else {
res.locals.homePageRoute = pathname;
}
req.query = Object.assign(parsedUrl.query, req.query);
2018-01-03 15:06:41 -05:00
2019-07-31 12:39:15 -04:00
next();
}
exports.rewrite = rewrite;
function pluginHook(req, res, next) {
2021-02-04 00:06:15 -07:00
const hook = `action:homepage.get:${res.locals.homePageRoute}`;
plugins.hooks.fire(hook, {
req: req,
res: res,
next: next,
});
}
exports.pluginHook = pluginHook;