|
|
|
@ -1,61 +1,56 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var plugins = require('../plugins');
|
|
|
|
|
var meta = require('../meta');
|
|
|
|
|
var user = require('../user');
|
|
|
|
|
var pubsub = require('../pubsub');
|
|
|
|
|
|
|
|
|
|
var adminHomePageRoute;
|
|
|
|
|
var getRoute;
|
|
|
|
|
|
|
|
|
|
function configUpdated() {
|
|
|
|
|
adminHomePageRoute = (meta.config.homePageRoute || meta.config.homePageCustom || '').replace(/^\/+/, '') || 'categories';
|
|
|
|
|
getRoute = parseInt(meta.config.allowUserHomePage, 10) ? getRouteAllowUserHomePage : getRouteDisableUserHomePage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRouteDisableUserHomePage(uid, next) {
|
|
|
|
|
next(null, adminHomePageRoute);
|
|
|
|
|
function adminHomePageRoute() {
|
|
|
|
|
return (meta.config.homePageRoute || meta.config.homePageCustom || '').replace(/^\/+/, '') || 'categories';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRouteAllowUserHomePage(uid, next) {
|
|
|
|
|
user.getSettings(uid, function (err, settings) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var route = adminHomePageRoute;
|
|
|
|
|
|
|
|
|
|
if (settings.homePageRoute !== 'undefined' && settings.homePageRoute !== 'none') {
|
|
|
|
|
route = settings.homePageRoute || route;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(null, route);
|
|
|
|
|
});
|
|
|
|
|
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') {
|
|
|
|
|
route = settings.homePageRoute || route;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(null, route);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pubsub.on('config:update', configUpdated);
|
|
|
|
|
configUpdated();
|
|
|
|
|
|
|
|
|
|
function rewrite(req, res, next) {
|
|
|
|
|
if (req.path !== '/' && req.path !== '/api/' && req.path !== '/api') {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRoute(req.uid, function (err, route) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hook = 'action:homepage.get:' + route;
|
|
|
|
|
|
|
|
|
|
if (!plugins.hasListeners(hook)) {
|
|
|
|
|
req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + route;
|
|
|
|
|
} else {
|
|
|
|
|
res.locals.homePageRoute = route;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
if (parseInt(meta.config.allowUserHomePage, 10)) {
|
|
|
|
|
getUserHomeRoute(req.uid, next);
|
|
|
|
|
} else {
|
|
|
|
|
next(null, adminHomePageRoute());
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function (route, next) {
|
|
|
|
|
var hook = 'action:homepage.get:' + route;
|
|
|
|
|
|
|
|
|
|
if (!plugins.hasListeners(hook)) {
|
|
|
|
|
req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + route;
|
|
|
|
|
} else {
|
|
|
|
|
res.locals.homePageRoute = route;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
], next);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.rewrite = rewrite;
|
|
|
|
|