diff --git a/src/controllers/tags.js b/src/controllers/tags.js index aba1acae60..c63e220cc8 100644 --- a/src/controllers/tags.js +++ b/src/controllers/tags.js @@ -5,6 +5,7 @@ var tagsController = {}, nconf = require('nconf'), validator = require('validator'), meta = require('../meta'), + user = require('../user'), topics = require('../topics'), helpers = require('./helpers'); @@ -12,43 +13,58 @@ tagsController.getTag = function(req, res, next) { var tag = validator.escape(req.params.tag); var stop = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; - topics.getTagTids(tag, 0, stop, function(err, tids) { + async.waterfall([ + function(next) { + topics.getTagTids(tag, 0, stop, next); + }, + function(tids, next) { + if (Array.isArray(tids) && !tids.length) { + topics.deleteTag(tag); + return res.render('tag', { + topics: [], + tag: tag, + breadcrumbs: helpers.buildBreadcrumbs([{text: '[[tags:tags]]', url: '/tags'}, {text: tag}]) + }); + } + + async.parallel({ + isAdmin: async.apply(user.isAdministrator, req.uid), + topics: async.apply(topics.getTopics, tids, req.uid) + }, next); + } + ], function(err, results) { if (err) { return next(err); } - if (Array.isArray(tids) && !tids.length) { - topics.deleteTag(tag); - return res.render('tag', {topics: [], tag: tag}); + if (!results.isAdmin) { + results.topics = results.topics.filter(function(topic) { + return topic && !topic.deleted; + }); } - topics.getTopics(tids, req.uid, function(err, topics) { - if (err) { - return next(err); + res.locals.metaTags = [ + { + name: 'title', + content: tag + }, + { + property: 'og:title', + content: tag + }, + { + property: 'og:url', + content: nconf.get('url') + '/tags/' + tag } + ]; - res.locals.metaTags = [ - { - name: 'title', - content: tag - }, - { - property: 'og:title', - content: tag - }, - { - property: 'og:url', - content: nconf.get('url') + '/tags/' + tag - } - ]; - var data = { - topics: topics, - tag: tag, - nextStart: stop + 1, - breadcrumbs: helpers.buildBreadcrumbs([{text: '[[tags:tags]]', url: '/tags'}, {text: tag}]) - }; - res.render('tag', data); - }); + var data = { + topics: results.topics, + tag: tag, + nextStart: stop + 1, + breadcrumbs: helpers.buildBreadcrumbs([{text: '[[tags:tags]]', url: '/tags'}, {text: tag}]) + }; + res.render('tag', data); }); };