new middleware to add slug to topic and category

v1.18.x
barisusakli 11 years ago
parent 1eafc6ba3d
commit 37e8856861

@ -65,16 +65,6 @@ categoriesController.get = function(req, res, next) {
page = req.query.page || 1,
uid = req.user ? req.user.uid : 0;
if (!req.params.slug && !res.locals.isAPI) {
categories.getCategoryField(cid, 'slug', function(err, slug) {
if (err) {
return next(err);
}
res.redirect('/category/' + slug);
});
return;
}
async.waterfall([
function(next) {
categoryTools.privileges(cid, uid, function(err, categoryPrivileges) {

@ -18,16 +18,6 @@ topicsController.get = function(req, res, next) {
uid = req.user ? req.user.uid : 0,
privileges;
if (!req.params.slug && !res.locals.isAPI) {
topics.getTopicField(tid, 'slug', function(err, slug) {
if (err) {
return next(err);
}
res.redirect('/topic/' + slug);
});
return;
}
async.waterfall([
function(next) {
threadTools.privileges(tid, ((req.user) ? req.user.uid || 0 : 0), function(err, userPrivileges) {

@ -14,6 +14,8 @@ var app,
translator = require('./../../public/src/translator'),
user = require('./../user'),
db = require('./../database'),
categories = require('./../categories'),
topics = require('./../topics'),
controllers = {
api: require('./../controllers/api')
@ -49,9 +51,31 @@ middleware.redirectToAccountIfLoggedIn = function(req, res, next) {
} else {
next();
}
}
middleware.addSlug = function(req, res, next) {
function redirect(method, id, name) {
method(id, 'slug', function(err, slug) {
if (err || !slug) {
return next(err);
}
res.redirect(name + slug);
});
}
if (!req.params.slug) {
if (req.params.category_id) {
redirect(categories.getCategoryField, req.params.category_id, '/category/');
} else if (req.params.topic_id) {
redirect(topics.getTopicField, req.params.topic_id, '/topic/');
} else {
return next();
}
return;
}
next();
};
middleware.prepareAPI = function(req, res, next) {
res.locals.isAPI = true;
next();

@ -48,7 +48,7 @@ function staticRoutes(app, middleware, controllers) {
}
function topicRoutes(app, middleware, controllers) {
app.get('/topic/:topic_id/:slug?', middleware.buildHeader, controllers.topics.get);
app.get('/topic/:topic_id/:slug?', middleware.buildHeader, middleware.addSlug, controllers.topics.get);
app.get('/api/topic/:topic_id/:slug?', controllers.topics.get);
}
@ -65,7 +65,7 @@ function categoryRoutes(app, middleware, controllers) {
app.get('/unread/total', middleware.buildHeader, middleware.authenticate, controllers.categories.unreadTotal);
app.get('/api/unread/total', middleware.authenticate, controllers.categories.unreadTotal);
app.get('/category/:category_id/:slug?', middleware.buildHeader, controllers.categories.get);
app.get('/category/:category_id/:slug?', middleware.buildHeader, middleware.addSlug, controllers.categories.get);
app.get('/api/category/:category_id/:slug?', controllers.categories.get);
}

Loading…
Cancel
Save