From 5950f97f963c8cd2abe63c980bad7fc7fdccf672 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 25 Sep 2015 17:38:58 -0400 Subject: [PATCH] more refactors --- src/categories.js | 109 ++-------------------------- src/categories/data.js | 116 ++++++++++++++++++++++++++++++ src/controllers/admin/homepage.js | 2 +- src/controllers/categories.js | 4 +- src/controllers/unread.js | 2 +- src/events.js | 2 +- src/groups/membership.js | 2 +- src/groups/search.js | 2 +- src/messaging.js | 4 +- src/posts/summary.js | 4 +- src/posts/user.js | 2 +- src/privileges/categories.js | 4 +- src/privileges/topics.js | 2 +- src/privileges/users.js | 2 +- src/search.js | 2 +- src/socket.io/admin/user.js | 6 +- src/socket.io/categories.js | 2 +- src/socket.io/index.js | 2 +- src/socket.io/posts/favourites.js | 4 +- src/topics.js | 72 ++----------------- src/topics/data.js | 78 ++++++++++++++++++++ src/topics/posts.js | 2 +- src/topics/teaser.js | 2 +- src/user.js | 4 +- src/user/admin.js | 2 +- src/user/data.js | 2 +- src/user/digest.js | 2 +- src/user/search.js | 2 +- 28 files changed, 235 insertions(+), 204 deletions(-) create mode 100644 src/categories/data.js create mode 100644 src/topics/data.js diff --git a/src/categories.js b/src/categories.js index aef6e2021f..1cfea81bda 100644 --- a/src/categories.js +++ b/src/categories.js @@ -13,6 +13,7 @@ var async = require('async'), (function(Categories) { + require('./categories/data')(Categories); require('./categories/create')(Categories); require('./categories/delete')(Categories); require('./categories/topics')(Categories); @@ -40,9 +41,6 @@ var async = require('async'), topics: function(next) { Categories.getCategoryTopics(data, next); }, - pageCount: function(next) { - Categories.getPageCount(data.cid, data.uid, next); - }, isIgnored: function(next) { Categories.isIgnored([data.cid], data.uid, next); } @@ -53,7 +51,6 @@ var async = require('async'), category.topics = results.topics.topics; category.nextStart = results.topics.nextStart; - category.pageCount = results.pageCount; category.isIgnored = results.isIgnored[0]; plugins.fireHook('filter:category.get', {category: category, uid: data.uid}, function(err, data) { @@ -123,108 +120,10 @@ var async = require('async'), return callback(err, []); } - user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], callback); - }); - }; - - Categories.getCategoryData = function(cid, callback) { - Categories.getCategoriesData([cid], function(err, categories) { - callback(err, categories ? categories[0] : null); - }); - }; - - Categories.getCategoriesData = function(cids, callback) { - if (!Array.isArray(cids) || !cids.length) { - return callback(null, []); - } - var keys = cids.map(function(cid) { - return 'category:' + cid; - }); - - db.getObjects(keys, function(err, categories) { - if (err || !Array.isArray(categories) || !categories.length) { - return callback(err, []); - } - - async.map(categories, modifyCategory, callback); + user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], callback); }); }; - function modifyCategory(category, callback) { - if (!category) { - return callback(null, null); - } - - category.name = validator.escape(category.name); - category.disabled = category.hasOwnProperty('disabled') ? parseInt(category.disabled, 10) === 1 : undefined; - category.icon = category.icon || 'hidden'; - if (category.hasOwnProperty('post_count')) { - category.post_count = category.totalPostCount = category.post_count || 0; - } - - if (category.hasOwnProperty('topic_count')) { - category.topic_count = category.totalTopicCount = category.topic_count || 0; - } - - if (category.image) { - category.backgroundImage = category.image; - } - - if (category.description) { - plugins.fireHook('filter:parse.raw', category.description, function(err, parsedDescription) { - if (err) { - return callback(err); - } - category.descriptionParsed = parsedDescription; - category.description = validator.escape(category.description); - callback(null, category); - }); - } else { - callback(null, category); - } - } - - Categories.getCategoryField = function(cid, field, callback) { - db.getObjectField('category:' + cid, field, callback); - }; - - Categories.getMultipleCategoryFields = function(cids, fields, callback) { - if (!Array.isArray(cids) || !cids.length) { - return callback(null, []); - } - - var keys = cids.map(function(cid) { - return 'category:' + cid; - }); - - db.getObjectsFields(keys, fields, function(err, categories) { - if (err) { - return callback(err); - } - async.map(categories, modifyCategory, callback); - }); - }; - - Categories.getAllCategoryFields = function(fields, callback) { - async.waterfall([ - async.apply(db.getSortedSetRange, 'categories:cid', 0, -1), - function(cids, next) { - Categories.getMultipleCategoryFields(cids, fields, next); - } - ], callback); - }; - - Categories.getCategoryFields = function(cid, fields, callback) { - db.getObjectFields('category:' + cid, fields, callback); - }; - - Categories.setCategoryField = function(cid, field, value, callback) { - db.setObjectField('category:' + cid, field, value, callback); - }; - - Categories.incrementCategoryFieldBy = function(cid, field, value, callback) { - db.incrObjectFieldBy('category:' + cid, field, value, callback); - }; Categories.getCategories = function(cids, uid, callback) { if (!Array.isArray(cids)) { @@ -297,7 +196,7 @@ var async = require('async'), var parentCids; async.waterfall([ function (next) { - Categories.getMultipleCategoryFields(cids, ['parentCid'], next); + Categories.getCategoriesFields(cids, ['parentCid'], next); }, function (_categoriesData, next) { categoriesData = _categoriesData; @@ -396,7 +295,7 @@ var async = require('async'), category.parentCid = 0; } - if (category.parentCid == parentCid){ + if (parseInt(category.parentCid, 10) === parseInt(parentCid, 10)){ tree.push(category); category.children = Categories.getTree(categories, category.cid); } diff --git a/src/categories/data.js b/src/categories/data.js new file mode 100644 index 0000000000..c844e632ad --- /dev/null +++ b/src/categories/data.js @@ -0,0 +1,116 @@ +'use strict'; + +var async = require('async'); +var validator = require('validator'); +var winston = require('winston'); + +var db = require('../database'); +var plugins = require('../plugins'); + +module.exports = function(Categories) { + + Categories.getCategoryData = function(cid, callback) { + Categories.getCategoriesData([cid], function(err, categories) { + callback(err, categories ? categories[0] : null); + }); + }; + + Categories.getCategoriesData = function(cids, callback) { + if (!Array.isArray(cids) || !cids.length) { + return callback(null, []); + } + var keys = cids.map(function(cid) { + return 'category:' + cid; + }); + + db.getObjects(keys, function(err, categories) { + if (err || !Array.isArray(categories) || !categories.length) { + return callback(err, []); + } + + async.map(categories, modifyCategory, callback); + }); + }; + + function modifyCategory(category, callback) { + if (!category) { + return callback(null, null); + } + + category.name = validator.escape(category.name); + category.disabled = category.hasOwnProperty('disabled') ? parseInt(category.disabled, 10) === 1 : undefined; + category.icon = category.icon || 'hidden'; + if (category.hasOwnProperty('post_count')) { + category.post_count = category.totalPostCount = category.post_count || 0; + } + + if (category.hasOwnProperty('topic_count')) { + category.topic_count = category.totalTopicCount = category.topic_count || 0; + } + + if (category.image) { + category.backgroundImage = category.image; + } + + if (category.description) { + plugins.fireHook('filter:parse.raw', category.description, function(err, parsedDescription) { + if (err) { + return callback(err); + } + category.descriptionParsed = parsedDescription; + category.description = validator.escape(category.description); + callback(null, category); + }); + } else { + callback(null, category); + } + } + + Categories.getCategoryField = function(cid, field, callback) { + db.getObjectField('category:' + cid, field, callback); + }; + + Categories.getCategoriesFields = function(cids, fields, callback) { + if (!Array.isArray(cids) || !cids.length) { + return callback(null, []); + } + + var keys = cids.map(function(cid) { + return 'category:' + cid; + }); + + db.getObjectsFields(keys, fields, function(err, categories) { + if (err) { + return callback(err); + } + async.map(categories, modifyCategory, callback); + }); + }; + + Categories.getMultipleCategoryFields = function(cids, fields, callback) { + winston.warn('[deprecated] Categories.getMultipleCategoryFields is deprecated please use Categories.getCategoriesFields'); + Categories.getCategoriesFields(cids, fields, callback); + }; + + Categories.getAllCategoryFields = function(fields, callback) { + async.waterfall([ + async.apply(db.getSortedSetRange, 'categories:cid', 0, -1), + function(cids, next) { + Categories.getCategoriesFields(cids, fields, next); + } + ], callback); + }; + + Categories.getCategoryFields = function(cid, fields, callback) { + db.getObjectFields('category:' + cid, fields, callback); + }; + + Categories.setCategoryField = function(cid, field, value, callback) { + db.setObjectField('category:' + cid, field, value, callback); + }; + + Categories.incrementCategoryFieldBy = function(cid, field, value, callback) { + db.incrObjectFieldBy('category:' + cid, field, value, callback); + }; + +}; \ No newline at end of file diff --git a/src/controllers/admin/homepage.js b/src/controllers/admin/homepage.js index 834ada501d..f503dd8865 100644 --- a/src/controllers/admin/homepage.js +++ b/src/controllers/admin/homepage.js @@ -19,7 +19,7 @@ homePageController.get = function(req, res, next) { privileges.categories.filterCids('find', cids, 0, next); }, function(cids, next) { - categories.getMultipleCategoryFields(cids, ['name', 'slug'], next); + categories.getCategoriesFields(cids, ['name', 'slug'], next); }, function(categoryData, next) { categoryData = categoryData.map(function(category) { diff --git a/src/controllers/categories.js b/src/controllers/categories.js index 89a10d16a2..ce1edc0532 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -82,6 +82,7 @@ categoriesController.list = function(req, res, next) { categoriesController.get = function(req, res, callback) { var cid = req.params.category_id, page = parseInt(req.query.page, 10) || 1, + pageCount = 1, userPrivileges; if ((req.params.topic_index && !utils.isNumber(req.params.topic_index)) || !utils.isNumber(cid)) { @@ -120,7 +121,7 @@ categoriesController.get = function(req, res, callback) { var settings = results.userSettings; var topicIndex = utils.isNumber(req.params.topic_index) ? parseInt(req.params.topic_index, 10) - 1 : 0; var topicCount = parseInt(results.categoryData.topic_count, 10); - var pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage)); + pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage)); if (topicIndex < 0 || topicIndex > Math.max(topicCount - 1, 0)) { return helpers.redirect(res, '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : '')); @@ -249,6 +250,7 @@ categoriesController.get = function(req, res, callback) { } data.currentPage = page; + data.pageCount = pageCount; data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1; data.rssFeedUrl = nconf.get('relative_path') + '/category/' + data.cid + '.rss'; data.pagination = pagination.create(data.currentPage, data.pageCount); diff --git a/src/controllers/unread.js b/src/controllers/unread.js index f9cccf3d3a..af7bf816ef 100644 --- a/src/controllers/unread.js +++ b/src/controllers/unread.js @@ -34,7 +34,7 @@ unreadController.get = function(req, res, next) { privileges.categories.filterCids('read', results.watchedCategories, req.uid, next); }, function(cids, next) { - categories.getMultipleCategoryFields(cids, ['cid', 'name', 'slug', 'icon', 'link'], next); + categories.getCategoriesFields(cids, ['cid', 'name', 'slug', 'icon', 'link'], next); }, function(categories, next) { categories = categories.filter(function(category) { diff --git a/src/events.js b/src/events.js index 682e9549db..968e1b2f39 100644 --- a/src/events.js +++ b/src/events.js @@ -77,7 +77,7 @@ var async = require('async'), user.isAdministrator(uids, next); }, userData: function(next) { - user.getMultipleUserFields(uids, ['username', 'userslug', 'picture'], next); + user.getUsersFields(uids, ['username', 'userslug', 'picture'], next); } }, function(err, results) { if (err) { diff --git a/src/groups/membership.js b/src/groups/membership.js index 46dd756607..663b3d7b14 100644 --- a/src/groups/membership.js +++ b/src/groups/membership.js @@ -240,7 +240,7 @@ module.exports = function(Groups) { return next(err); } - user.getMultipleUserFields(uids, ['uid', 'username', 'picture', 'userslug'], next); + user.getUsersFields(uids, ['uid', 'username', 'picture', 'userslug'], next); }); }, callback); }; diff --git a/src/groups/search.js b/src/groups/search.js index 89c9eba1d2..482f7c97ff 100644 --- a/src/groups/search.js +++ b/src/groups/search.js @@ -77,7 +77,7 @@ module.exports = function(Groups) { Groups.getMembers(data.groupName, 0, -1, next); }, function(members, next) { - user.getMultipleUserFields(members, ['uid'].concat([searchBy]), next); + user.getUsersFields(members, ['uid'].concat([searchBy]), next); }, function(users, next) { var uids = []; diff --git a/src/messaging.js b/src/messaging.js index 81f8b3b818..8ffb3a579e 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -145,7 +145,7 @@ var db = require('./database'), }; function getMessages(mids, fromuid, touid, isNew, callback) { - user.getMultipleUserFields([fromuid, touid], ['uid', 'username', 'userslug', 'picture', 'status'], function(err, userData) { + user.getUsersFields([fromuid, touid], ['uid', 'username', 'userslug', 'picture', 'status'], function(err, userData) { if(err) { return callback(err); } @@ -263,7 +263,7 @@ var db = require('./database'), db.isSortedSetMembers('uid:' + uid + ':chats:unread', uids, next); }, users: function(next) { - user.getMultipleUserFields(uids, ['uid', 'username', 'picture', 'status'] , next); + user.getUsersFields(uids, ['uid', 'username', 'picture', 'status'] , next); }, teasers: function(next) { async.map(uids, function(fromuid, next) { diff --git a/src/posts/summary.js b/src/posts/summary.js index 582354499d..eaa1f5b2ef 100644 --- a/src/posts/summary.js +++ b/src/posts/summary.js @@ -46,7 +46,7 @@ module.exports = function(Posts) { async.parallel({ users: function(next) { - user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next); + user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next); }, topicsAndCategories: function(next) { getTopicAndCategories(topicKeys, next); @@ -124,7 +124,7 @@ module.exports = function(Posts) { return topic && array.indexOf(topic) === index; }); - categories.getMultipleCategoryFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], function(err, categories) { + categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], function(err, categories) { callback(err, {topics: topics, categories: categories}); }); }); diff --git a/src/posts/user.js b/src/posts/user.js index 46e132a186..8b40d8bd73 100644 --- a/src/posts/user.js +++ b/src/posts/user.js @@ -20,7 +20,7 @@ module.exports = function(Posts) { user.getMultipleUserSettings(uids, next); }, userData: function(next) { - user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status'], next); + user.getUsersFields(uids, ['uid', 'username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status'], next); }, online: function(next) { require('../socket.io').isUsersOnline(uids, next); diff --git a/src/privileges/categories.js b/src/privileges/categories.js index 3e454ccf9f..bad14d3294 100644 --- a/src/privileges/categories.js +++ b/src/privileges/categories.js @@ -60,7 +60,7 @@ module.exports = function(privileges) { var members = _.unique(_.flatten(memberSets)); - user.getMultipleUserFields(members, ['picture', 'username'], function(err, memberData) { + user.getUsersFields(members, ['picture', 'username'], function(err, memberData) { if (err) { return next(err); } @@ -244,7 +244,7 @@ module.exports = function(privileges) { async.parallel({ categories: function(next) { - categories.getMultipleCategoryFields(cids, ['disabled'], next); + categories.getCategoriesFields(cids, ['disabled'], next); }, allowedTo: function(next) { helpers.isUserAllowedTo(privilege, uid, cids, next); diff --git a/src/privileges/topics.js b/src/privileges/topics.js index b642f34355..d9ce26a183 100644 --- a/src/privileges/topics.js +++ b/src/privileges/topics.js @@ -86,7 +86,7 @@ module.exports = function(privileges) { async.parallel({ categories: function(next) { - categories.getMultipleCategoryFields(cids, ['disabled'], next); + categories.getCategoriesFields(cids, ['disabled'], next); }, allowedTo: function(next) { helpers.isUserAllowedTo(privilege, uid, cids, next); diff --git a/src/privileges/users.js b/src/privileges/users.js index 1a0427b766..4562beb4cd 100644 --- a/src/privileges/users.js +++ b/src/privileges/users.js @@ -38,7 +38,7 @@ module.exports = function(privileges) { function isModeratorOfCategories(cids, uid, callback) { if (!parseInt(uid, 10)) { - return filterIsModerator(null, cids.map(function() {return false;})); + return filterIsModerator(cids, uid, cids.map(function() {return false;}), callback); } var uniqueCids = cids.filter(function(cid, index, array) { diff --git a/src/search.js b/src/search.js index ac8dd2090e..05948cf593 100644 --- a/src/search.js +++ b/src/search.js @@ -191,7 +191,7 @@ function getMatchedPosts(pids, data, callback) { var uids = posts.map(function(post) { return post.uid; }); - user.getMultipleUserFields(uids, ['username'], next); + user.getUsersFields(uids, ['username'], next); } else { next(); } diff --git a/src/socket.io/admin/user.js b/src/socket.io/admin/user.js index b48746b0a1..d0eb477301 100644 --- a/src/socket.io/admin/user.js +++ b/src/socket.io/admin/user.js @@ -16,7 +16,7 @@ User.makeAdmins = function(socket, uids, callback) { return callback(new Error('[[error:invalid-data]]')); } - user.getMultipleUserFields(uids, ['banned'], function(err, userData) { + user.getUsersFields(uids, ['banned'], function(err, userData) { if (err) { return callback(err); } @@ -133,7 +133,7 @@ User.sendValidationEmail = function(socket, uids, callback) { return callback(new Error('[[error:email-confirmations-are-disabled]]')); } - user.getMultipleUserFields(uids, ['uid', 'email'], function(err, usersData) { + user.getUsersFields(uids, ['uid', 'email'], function(err, usersData) { if (err) { return callback(err); } @@ -216,7 +216,7 @@ User.search = function(socket, data, callback) { async.parallel({ users: function(next) { - user.getMultipleUserFields(uids, ['email'], next); + user.getUsersFields(uids, ['email'], next); }, flagCounts: function(next) { var sets = uids.map(function(uid) { diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index d351d5f334..4f6e5c14c0 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -121,7 +121,7 @@ SocketCategories.getTopicCount = function(socket, cid, callback) { SocketCategories.getUsersInCategory = function(socket, cid, callback) { var uids = websockets.getUidsInRoom('category_' + cid); - user.getMultipleUserFields(uids, ['uid', 'userslug', 'username', 'picture'], callback); + user.getUsersFields(uids, ['uid', 'userslug', 'username', 'picture'], callback); }; SocketCategories.getCategoriesByPrivilege = function(socket, privilege, callback) { diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 5a8e68bce3..ba1acc5408 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -281,7 +281,7 @@ Sockets.getUsersInRoom = function (uid, roomName, start, stop, callback) { if (!uids.length) { return callback(null, {users: [], total: 0 , room: roomName}); } - user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], function(err, users) { + user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], function(err, users) { if (err) { return callback(err); } diff --git a/src/socket.io/posts/favourites.js b/src/socket.io/posts/favourites.js index e24564bb1d..72ed90aa0b 100644 --- a/src/socket.io/posts/favourites.js +++ b/src/socket.io/posts/favourites.js @@ -37,13 +37,13 @@ module.exports = function(SocketPosts) { function (results, next) { async.parallel({ upvoters: function(next) { - user.getMultipleUserFields(results.upvoteUids, ['username', 'userslug', 'picture'], next); + user.getUsersFields(results.upvoteUids, ['username', 'userslug', 'picture'], next); }, upvoteCount: function(next) { next(null, results.upvoteUids.length); }, downvoters: function(next) { - user.getMultipleUserFields(results.downvoteUids, ['username', 'userslug', 'picture'], next); + user.getUsersFields(results.downvoteUids, ['username', 'userslug', 'picture'], next); }, downvoteCount: function(next) { next(null, results.downvoteUids.length); diff --git a/src/topics.js b/src/topics.js index cf03f5c349..c18314a03d 100644 --- a/src/topics.js +++ b/src/topics.js @@ -15,6 +15,8 @@ var async = require('async'), (function(Topics) { + + require('./topics/data')(Topics); require('./topics/create')(Topics); require('./topics/delete')(Topics); require('./topics/unread')(Topics); @@ -33,40 +35,6 @@ var async = require('async'), db.isSortedSetMember('topics:tid', tid, callback); }; - Topics.getTopicData = function(tid, callback) { - db.getObject('topic:' + tid, function(err, topic) { - if (err || !topic) { - return callback(err); - } - modifyTopic(topic, callback); - }); - }; - - Topics.getTopicsData = function(tids, callback) { - var keys = []; - - for (var i=0; i