diff --git a/src/controllers/sitemap.js b/src/controllers/sitemap.js index 453aed4f6e..c87b1565a9 100644 --- a/src/controllers/sitemap.js +++ b/src/controllers/sitemap.js @@ -1,68 +1,61 @@ 'use strict'; +var async = require('async'); + var sitemap = require('../sitemap'); var meta = require('../meta'); -var sitemapController = {}; -sitemapController.render = function (req, res, next) { - sitemap.render(function (err, tplData) { - if (err) { - return next(err); - } +var sitemapController = module.exports; - req.app.render('sitemap', tplData, function (err, xml) { - if (err) { - return next(err); - } +sitemapController.render = function (req, res, next) { + async.waterfall([ + function (next) { + sitemap.render(next); + }, + function (tplData, next) { + req.app.render('sitemap', tplData, next); + }, + function (xml) { res.header('Content-Type', 'application/xml'); res.send(xml); - }); - }); + }, + ], next); }; sitemapController.getPages = function (req, res, next) { - if (parseInt(meta.config['feeds:disableSitemap'], 10) === 1) { - return next(); - } - - sitemap.getPages(function (err, xml) { - if (err) { - return next(err); - } - res.header('Content-Type', 'application/xml'); - res.send(xml); - }); + sendSitemap(function (callback) { + sitemap.getPages(callback); + }, res, next); }; sitemapController.getCategories = function (req, res, next) { - if (parseInt(meta.config['feeds:disableSitemap'], 10) === 1) { - return next(); - } - - sitemap.getCategories(function (err, xml) { - if (err) { - return next(err); - } - res.header('Content-Type', 'application/xml'); - res.send(xml); - }); + sendSitemap(function (callback) { + sitemap.getCategories(callback); + }, res, next); }; sitemapController.getTopicPage = function (req, res, next) { + sendSitemap(function (callback) { + sitemap.getTopicPage(parseInt(req.params[0], 10), callback); + }, res, next); +}; + +function sendSitemap(method, res, callback) { if (parseInt(meta.config['feeds:disableSitemap'], 10) === 1) { - return next(); + return callback(); } + async.waterfall([ + function (next) { + method(next); + }, + function (xml) { + if (!xml) { + return callback(); + } - sitemap.getTopicPage(parseInt(req.params[0], 10), function (err, xml) { - if (err) { - return next(err); - } else if (!xml) { - return next(); - } - - res.header('Content-Type', 'application/xml'); - res.send(xml); - }); -}; + res.header('Content-Type', 'application/xml'); + res.send(xml); + }, + ], callback); +} -module.exports = sitemapController;