2017-11-04 08:51:44 -06:00
|
|
|
'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');
|
2017-11-04 08:51:44 -06:00
|
|
|
|
2018-01-03 15:06:41 -05:00
|
|
|
function adminHomePageRoute() {
|
2021-01-07 20:44:02 -06:00
|
|
|
return ((meta.config.homePageRoute === 'custom' ? meta.config.homePageCustom : meta.config.homePageRoute) || 'categories').replace(/^\//, '');
|
2017-11-04 08:51:44 -06:00
|
|
|
}
|
|
|
|
|
|
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;
|
2017-11-04 08:51:44 -06:00
|
|
|
}
|
|
|
|
|
|
2019-07-31 12:39:15 -04:00
|
|
|
async function rewrite(req, res, next) {
|
2017-11-15 14:48:28 -05:00
|
|
|
if (req.path !== '/' && req.path !== '/api/' && req.path !== '/api') {
|
2017-11-04 08:51:44 -06:00
|
|
|
return next();
|
|
|
|
|
}
|
2019-07-31 12:39:15 -04:00
|
|
|
let route = adminHomePageRoute();
|
|
|
|
|
if (meta.config.allowUserHomePage) {
|
|
|
|
|
route = await getUserHomeRoute(req.uid, next);
|
|
|
|
|
}
|
2017-11-04 08:51:44 -06:00
|
|
|
|
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}`;
|
2020-11-20 16:06:26 -05:00
|
|
|
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();
|
2017-11-17 06:11:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.rewrite = rewrite;
|
|
|
|
|
|
|
|
|
|
function pluginHook(req, res, next) {
|
2021-02-04 00:06:15 -07:00
|
|
|
const hook = `action:homepage.get:${res.locals.homePageRoute}`;
|
2017-11-17 06:11:33 -07:00
|
|
|
|
2020-11-20 16:06:26 -05:00
|
|
|
plugins.hooks.fire(hook, {
|
2017-11-17 06:11:33 -07:00
|
|
|
req: req,
|
|
|
|
|
res: res,
|
|
|
|
|
next: next,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.pluginHook = pluginHook;
|