From 48aba5fc6447907365a0fced181275b7240e7965 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 27 May 2014 12:44:28 -0400 Subject: [PATCH] category widget changes --- public/src/forum/topic/move.js | 4 ++-- public/src/forum/unread.js | 4 ++-- src/categories.js | 35 +++++++++++++++++++++++++++++++--- src/controllers/admin.js | 6 +++--- src/controllers/index.js | 18 +++-------------- src/install.js | 6 +++--- src/sitemap.js | 16 +++++++--------- src/socket.io/categories.js | 2 +- 8 files changed, 53 insertions(+), 38 deletions(-) diff --git a/public/src/forum/topic/move.js b/public/src/forum/topic/move.js index c1a0fe2d59..ed8d959521 100644 --- a/public/src/forum/topic/move.js +++ b/public/src/forum/topic/move.js @@ -38,12 +38,12 @@ define(function() { socket.emit('categories.get', onCategoriesLoaded); } - function onCategoriesLoaded(err, data) { + function onCategoriesLoaded(err, categories) { if (err) { return app.alertError(err.message); } - renderCategories(data.categories); + renderCategories(categories); modal.find('.category-list').on('click', 'li[data-cid]', function(e) { selectCategory($(this)); diff --git a/public/src/forum/unread.js b/public/src/forum/unread.js index b2ca82be7c..a289290710 100644 --- a/public/src/forum/unread.js +++ b/public/src/forum/unread.js @@ -122,8 +122,8 @@ define(['forum/recent', 'topicSelect', 'forum/infinitescroll'], function(recent, } } - function onCategoriesLoaded(err, data) { - createCategoryLinks(data.categories); + function onCategoriesLoaded(err, categories) { + createCategoryLinks(categories); } function createCategoryLinks(categories) { diff --git a/src/categories.js b/src/categories.js index a4cbce57bf..feb8a9b927 100644 --- a/src/categories.js +++ b/src/categories.js @@ -11,6 +11,7 @@ var db = require('./database'), meta = require('./meta'), emitter = require('./emitter'), validator = require('validator'), + privileges = require('./privileges'), async = require('async'), winston = require('winston'), @@ -175,7 +176,7 @@ var db = require('./database'), db.sortedSetCard('categories:recent_posts:cid:' + cid, callback); }; - Categories.getAllCategories = function(uid, callback) { + Categories.getAllCategories = function(callback) { db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { if (err) { return callback(err); @@ -185,7 +186,35 @@ var db = require('./database'), return callback(null, {categories : []}); } - Categories.getCategories(cids, uid, callback); + Categories.getCategoriesData(cids, callback); + }); + }; + + Categories.getVisibleCategories = function(uid, callback) { + db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { + if (err) { + return callback(err); + } + + if (!Array.isArray(cids) || !cids.length) { + return callback(null, {categories : []}); + } + + Categories.getCategories(cids, uid, function(err, categories) { + if (err) { + return callback(err); + } + async.filter(categories, function (category, next) { + if (category.disabled) { + return next(false); + } + privileges.categories.can('find', category.cid, uid, function(err, findable) { + next(!err && findable); + }); + }, function(visibleCategories) { + callback(null, visibleCategories); + }); + }); }); }; @@ -312,7 +341,7 @@ var db = require('./database'), categories[i]['unread-class'] = (parseInt(categories[i].topic_count, 10) === 0 || (hasRead[i] && uid !== 0)) ? '' : 'unread'; } - callback(null, {categories: categories}); + callback(null, categories); }); }; diff --git a/src/controllers/admin.js b/src/controllers/admin.js index de1308fbbf..1c5053fba2 100644 --- a/src/controllers/admin.js +++ b/src/controllers/admin.js @@ -51,12 +51,12 @@ adminController.categories.disabled = function(req, res, next) { }; function filterAndRenderCategories(req, res, next, active) { - categories.getAllCategories(0, function (err, data) { - data.categories = data.categories.filter(function (category) { + categories.getAllCategories(function (err, categoryData) { + categoryData = categoryData.filter(function (category) { return active ? !category.disabled : category.disabled; }); - res.render('admin/categories', data); + res.render('admin/categories', {categories: categoryData}); }); } diff --git a/src/controllers/index.js b/src/controllers/index.js index 12ffef2132..2cc7c6cf15 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -62,15 +62,11 @@ Controllers.home = function(req, res, next) { }, categories: function (next) { var uid = req.user ? req.user.uid : 0; - categories.getAllCategories(uid, function (err, data) { + categories.getVisibleCategories(uid, function (err, categoryData) { if (err) { return next(err); } - data.categories = data.categories.filter(function (category) { - return !category.disabled; - }); - function getRecentReplies(category, callback) { categories.getRecentReplies(category.cid, uid, parseInt(category.numRecentReplies, 10), function (err, posts) { if (err) { @@ -81,16 +77,8 @@ Controllers.home = function(req, res, next) { }); } - async.filter(data.categories, function (category, next) { - privileges.categories.can('find', category.cid, uid, function(err, findable) { - next(!err && findable); - }); - }, function(visibleCategories) { - data.categories = visibleCategories; - - async.each(data.categories, getRecentReplies, function (err) { - next(err, data.categories); - }); + async.each(categoryData, getRecentReplies, function (err) { + next(err, categoryData); }); }); } diff --git a/src/install.js b/src/install.js index ef78d10dba..3576777271 100644 --- a/src/install.js +++ b/src/install.js @@ -364,8 +364,8 @@ function createAdmin(callback) { function createCategories(next) { var Categories = require('./categories'); - Categories.getAllCategories(0, function (err, data) { - if (data.categories.length === 0) { + Categories.getAllCategories(function (err, categoryData) { + if (categoryData.length === 0) { winston.warn('No categories found, populating instance with default categories'); fs.readFile(path.join(__dirname, '../', 'install/data/categories.json'), function (err, default_categories) { @@ -382,7 +382,7 @@ function createCategories(next) { }); }); } else { - winston.info('Categories OK. Found ' + data.categories.length + ' categories.'); + winston.info('Categories OK. Found ' + categoryData.length + ' categories.'); next(); } }); diff --git a/src/sitemap.js b/src/sitemap.js index 90f98c1a47..4ee7cfc7f2 100644 --- a/src/sitemap.js +++ b/src/sitemap.js @@ -29,23 +29,21 @@ var path = require('path'), async.parallel([ function(next) { var categoryUrls = []; - categories.getAllCategories(0, function(err, data) { + categories.getVisibleCategories(0, function(err, data) { if (err) { return next(err); } data.categories.forEach(function(category) { - if (!category.disabled) { - categoryUrls.push({ - url: path.join('/category', category.slug), - changefreq: 'weekly', - priority: '0.4' - }); - } + categoryUrls.push({ + url: path.join('/category', category.slug), + changefreq: 'weekly', + priority: '0.4' + }); }); next(null, categoryUrls); - }, 0); + }); }, function(next) { var topicUrls = []; diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index e6d9ca52a6..41f64d8457 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -23,7 +23,7 @@ SocketCategories.getRecentReplies = function(socket, cid, callback) { }; SocketCategories.get = function(socket, data, callback) { - categories.getAllCategories(0, callback); + categories.getAllCategories(callback); }; SocketCategories.loadMore = function(socket, data, callback) {