You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.7 KiB
JavaScript

'use strict';
var async = require('async');
7 years ago
var url = require('url');
var plugins = require('../plugins');
var meta = require('../meta');
var user = require('../user');
function adminHomePageRoute() {
return (meta.config.homePageRoute || meta.config.homePageCustom || '').replace(/^\/+/, '') || 'categories';
}
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') {
7 years ago
route = (settings.homePageRoute || route).replace(/^\/+/, '');
}
next(null, route);
},
], callback);
}
function rewrite(req, res, next) {
if (req.path !== '/' && req.path !== '/api/' && req.path !== '/api') {
return next();
}
async.waterfall([
function (next) {
if (meta.config.allowUserHomePage) {
getUserHomeRoute(req.uid, next);
} else {
next(null, adminHomePageRoute());
}
},
function (route, next) {
7 years ago
var parsedUrl;
try {
parsedUrl = url.parse(route, true);
} catch (err) {
return next(err);
}
7 years ago
var pathname = parsedUrl.pathname;
var hook = 'action:homepage.get:' + pathname;
if (!plugins.hasListeners(hook)) {
7 years ago
req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + pathname;
} else {
7 years ago
res.locals.homePageRoute = pathname;
}
7 years ago
req.query = Object.assign(parsedUrl.query, req.query);
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;