From b12e3562d5b2f7b706feba239af167f2d6f79e23 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 30 Oct 2014 18:04:45 -0400 Subject: [PATCH] hide category icons if they are not set --- src/categories.js | 39 +++++++++++++++++++++++++++------------ src/posts.js | 10 +++++----- src/topics.js | 1 - 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/categories.js b/src/categories.js index f3c9ee7316..f91309b04c 100644 --- a/src/categories.js +++ b/src/categories.js @@ -314,20 +314,30 @@ var db = require('./database'), return callback(null, []); } - async.map(categories, function(category, next) { - if (!category || !parseInt(category.cid, 10)) { - return next(null, null); - } - category.name = validator.escape(category.name); - category.description = validator.escape(category.description); - category.backgroundImage = category.image ? nconf.get('relative_path') + category.image : ''; - category.disabled = parseInt(category.disabled, 10) === 1; - - next(null, category); - }, callback); + async.map(categories, modifyCategory, callback); }); }; + function modifyCategory(category, callback) { + if (!category || !parseInt(category.cid, 10)) { + return callback(null, null); + } + + category.name = validator.escape(category.name); + category.disabled = parseInt(category.disabled, 10) === 1; + category.icon = category.icon || 'hidden'; + + if (category.description) { + category.description = validator.escape(category.description); + } + + if (category.image) { + category.backgroundImage = category.image ? nconf.get('relative_path') + category.image : ''; + } + + callback(null, category); + } + Categories.getCategoryField = function(cid, field, callback) { db.getObjectField('category:' + cid, field, callback); }; @@ -336,7 +346,12 @@ var db = require('./database'), var keys = cids.map(function(cid) { return 'category:' + cid; }); - db.getObjectsFields(keys, fields, callback); + db.getObjectsFields(keys, fields, function(err, categories) { + if (err) { + return callback(err); + } + async.map(categories, modifyCategory, callback); + }); }; Categories.getCategoryFields = function(cid, fields, callback) { diff --git a/src/posts.js b/src/posts.js index a12dd12ad3..a1d1b1a93a 100644 --- a/src/posts.js +++ b/src/posts.js @@ -270,13 +270,13 @@ var async = require('async'), return next(err); } - var cidKeys = topics.map(function(topic) { - return 'category:' + topic.cid; + var cids = topics.map(function(topic) { + return topic && topic.cid; }).filter(function(value, index, array) { - return array.indexOf(value) === index; + return value && array.indexOf(value) === index; }); - db.getObjectsFields(cidKeys, ['cid', 'name', 'icon', 'slug'], function(err, categories) { + categories.getMultipleCategoryFields(cids, ['cid', 'name', 'icon', 'slug'], function(err, categories) { next(err, {topics: topics, categories: categories}); }); }); @@ -320,9 +320,9 @@ var async = require('async'), async.map(posts, function(post, next) { post.user = results.users[post.uid]; post.topic = results.topics[post.tid]; + post.topic.title = validator.escape(post.topic.title); post.category = results.categories[post.topic.cid]; - post.topic.title = validator.escape(post.topic.title); post.relativeTime = utils.toISOString(post.timestamp); if (!post.content || !options.parse) { diff --git a/src/topics.js b/src/topics.js index 023d537b82..b1bfcdeeaa 100644 --- a/src/topics.js +++ b/src/topics.js @@ -226,7 +226,6 @@ var async = require('async'), for (var i=0; i