more refactors

v1.18.x
barisusakli 9 years ago
parent 1f4857585d
commit 5950f97f96

@ -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);
});
};
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);
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], 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);
}

@ -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);
};
};

@ -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) {

@ -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);

@ -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) {

@ -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) {

@ -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);
};

@ -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 = [];

@ -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) {

@ -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});
});
});

@ -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);

@ -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);

@ -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);

@ -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) {

@ -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();
}

@ -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) {

@ -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) {

@ -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);
}

@ -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);

@ -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<tids.length; ++i) {
keys.push('topic:' + tids[i]);
}
db.getObjects(keys, function(err, topics) {
if (err) {
return callback(err);
}
async.map(topics, modifyTopic, callback);
});
};
function modifyTopic(topic, callback) {
if (!topic) {
return callback(null, topic);
}
topic.title = validator.escape(topic.title);
topic.relativeTime = utils.toISOString(topic.timestamp);
topic.lastposttimeISO = utils.toISOString(topic.lastposttime);
callback(null, topic);
}
Topics.getPageCount = function(tid, uid, callback) {
Topics.getTopicField(tid, 'postcount', function(err, postCount) {
if (err) {
@ -105,16 +73,6 @@ var async = require('async'),
});
};
Topics.getCategoryData = function(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if (err) {
return callback(err);
}
categories.getCategoryData(cid, callback);
});
};
Topics.getTopicsFromSet = function(set, uid, start, stop, callback) {
async.waterfall([
function(next) {
@ -166,10 +124,10 @@ var async = require('async'),
async.parallel({
users: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
categories: function(next) {
categories.getMultipleCategoryFields(cids, ['cid', 'name', 'slug', 'icon', 'bgColor', 'color', 'disabled'], next);
categories.getCategoriesFields(cids, ['cid', 'name', 'slug', 'icon', 'bgColor', 'color', 'disabled'], next);
},
hasRead: function(next) {
Topics.hasReadTopics(tids, uid, next);
@ -342,28 +300,6 @@ var async = require('async'),
db.sortedSetAdd('tid:' + tid + ':bookmarks', index, uid, callback);
};
Topics.getTopicField = function(tid, field, callback) {
db.getObjectField('topic:' + tid, field, callback);
};
Topics.getTopicFields = function(tid, fields, callback) {
db.getObjectFields('topic:' + tid, fields, callback);
};
Topics.getTopicsFields = function(tids, fields, callback) {
if (!Array.isArray(tids) || !tids.length) {
return callback(null, []);
}
var keys = tids.map(function(tid) {
return 'topic:' + tid;
});
db.getObjectsFields(keys, fields, callback);
};
Topics.setTopicField = function(tid, field, value, callback) {
db.setObjectField('topic:' + tid, field, value, callback);
};
Topics.isLocked = function(tid, callback) {
Topics.getTopicField(tid, 'locked', function(err, locked) {
callback(err, parseInt(locked, 10) === 1);

@ -0,0 +1,78 @@
'use strict';
var async = require('async');
var validator = require('validator');
var db = require('../database');
var categories = require('../categories');
var utils = require('../../public/src/utils');
module.exports = function(Topics) {
Topics.getTopicField = function(tid, field, callback) {
db.getObjectField('topic:' + tid, field, callback);
};
Topics.getTopicFields = function(tid, fields, callback) {
db.getObjectFields('topic:' + tid, fields, callback);
};
Topics.getTopicsFields = function(tids, fields, callback) {
if (!Array.isArray(tids) || !tids.length) {
return callback(null, []);
}
var keys = tids.map(function(tid) {
return 'topic:' + tid;
});
db.getObjectsFields(keys, fields, 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<tids.length; ++i) {
keys.push('topic:' + tids[i]);
}
db.getObjects(keys, function(err, topics) {
if (err) {
return callback(err);
}
async.map(topics, modifyTopic, callback);
});
};
function modifyTopic(topic, callback) {
if (!topic) {
return callback(null, topic);
}
topic.title = validator.escape(topic.title);
topic.relativeTime = utils.toISOString(topic.timestamp);
topic.lastposttimeISO = utils.toISOString(topic.lastposttime);
callback(null, topic);
}
Topics.getCategoryData = function(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if (err) {
return callback(err);
}
categories.getCategoryData(cid, callback);
});
};
Topics.setTopicField = function(tid, field, value, callback) {
db.setObjectField('topic:' + tid, field, value, callback);
};
};

@ -99,7 +99,7 @@ module.exports = function(Topics) {
}
}
user.getMultipleUserFields(editors, ['uid', 'username', 'userslug'], function(err, editors) {
user.getUsersFields(editors, ['uid', 'username', 'userslug'], function(err, editors) {
if (err) {
return next(err);
}

@ -44,7 +44,7 @@ module.exports = function(Topics) {
return array.indexOf(uid) === index;
});
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
function(usersData, next) {
var users = {};

@ -103,7 +103,7 @@ var async = require('async'),
});
async.parallel({
userData: function(next) {
User.getMultipleUserFields(uids, data.fields, next);
User.getUsersFields(uids, data.fields, next);
},
isAdmin: function(next) {
User.isAdministrator(uids, next);
@ -193,7 +193,7 @@ var async = require('async'),
};
User.getUsernamesByUids = function(uids, callback) {
User.getMultipleUserFields(uids, ['username'], function(err, users) {
User.getUsersFields(uids, ['username'], function(err, users) {
if (err) {
return callback(err);
}

@ -38,7 +38,7 @@ module.exports = function(User) {
var uids = users.map(function(user) {
return user.score;
});
User.getMultipleUserFields(uids, ['uid', 'email', 'username'], next);
User.getUsersFields(uids, ['uid', 'email', 'username'], next);
},
function(usersData, next) {
usersData.forEach(function(user, index) {

@ -16,7 +16,7 @@ module.exports = function(User) {
};
User.getUserFields = function(uid, fields, callback) {
User.getMultipleUserFields([uid], fields, function(err, users) {
User.getUsersFields([uid], fields, function(err, users) {
callback(err, users ? users[0] : null);
});
};

@ -77,7 +77,7 @@ var async = require('async'),
Digest.send = function(data, callback) {
var now = new Date();
user.getMultipleUserFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline'], function(err, users) {
user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline'], function(err, users) {
if (err) {
winston.error('[user/jobs] Could not send digests (' + data.interval + '): ' + err.message);
return callback(err);

@ -103,7 +103,7 @@ module.exports = function(User) {
async.parallel({
userData: function(next) {
User.getMultipleUserFields(uids, fields, next);
User.getUsersFields(uids, fields, next);
},
isOnline: function(next) {
if (data.onlineOnly) {

Loading…
Cancel
Save