From d57f183f420cdafb1fa9ac91bf41b653c3c730e1 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 28 Jun 2014 14:59:01 -0400 Subject: [PATCH] privileges.posts.get takes an array of pids now --- src/groups.js | 4 ++++ src/posts.js | 49 +++++++++++++++++++++++++++++++++++++--- src/privileges/posts.js | 50 ++++++++++++++++++++++++++--------------- src/socket.io/posts.js | 12 ++++++---- src/topics.js | 7 ++++++ src/topics/posts.js | 6 ++--- src/user.js | 9 +++++++- 7 files changed, 108 insertions(+), 29 deletions(-) diff --git a/src/groups.js b/src/groups.js index 708de3cd56..4d8dd0cb27 100644 --- a/src/groups.js +++ b/src/groups.js @@ -150,6 +150,10 @@ db.isSetMember('group:' + groupName + ':members', uid, callback); }; + Groups.isMemberOfGroups = function(uid, groups, callback) { + db.isMemberOfSets(groups, uid, callback); + }; + Groups.getMemberCount = function(groupName, callback) { db.setCount('group:' + groupName + ':members', callback); }; diff --git a/src/posts.js b/src/posts.js index 9c86ce4aec..1b0cbe9ca9 100644 --- a/src/posts.js +++ b/src/posts.js @@ -344,6 +344,14 @@ var async = require('async'), }); }; + Posts.getPostsFields = function(pids, fields, callback) { + var keys = pids.map(function(pid) { + return 'post:' + pid; + }); + + db.getObjectsFields(keys, fields, callback); + }; + Posts.getPostField = function(pid, field, callback) { Posts.getPostFields(pid, [field], function(err, data) { if(err) { @@ -382,6 +390,28 @@ var async = require('async'), }); }; + Posts.getCidsByPids = function(pids, callback) { + Posts.getPostsFields(pids, ['tid'], function(err, posts) { + if (err) { + return callback(err); + } + + var tids = posts.map(function(post) { + return post.tid; + }); + + topics.getTopicsFields(tids, ['cid'], function(err, topics) { + if (err) { + return callback(err); + } + var cids = topics.map(function(topic) { + return topic.cid; + }); + callback(null, cids); + }); + }); + }; + Posts.getPostsByUid = function(callerUid, uid, start, end, callback) { user.getPostIds(uid, start, end, function(err, pids) { if (err) { @@ -474,9 +504,22 @@ var async = require('async'), }; Posts.isOwner = function(pid, uid, callback) { - Posts.getPostField(pid, 'uid', function(err, author) { - callback(err, parseInt(author, 10) === parseInt(uid, 10)); - }); + uid = parseInt(uid, 10); + if (Array.isArray(pid)) { + Posts.getPostsFields(pid, ['uid'], function(err, posts) { + if (err) { + return callback(err); + } + posts = posts.map(function(post) { + return post && parseInt(post.uid, 10) === uid; + }); + callback(null, posts); + }); + } else { + Posts.getPostField(pid, 'uid', function(err, author) { + callback(err, parseInt(author, 10) === uid); + }); + } }; Posts.isMain = function(pid, callback) { diff --git a/src/privileges/posts.js b/src/privileges/posts.js index 9b0f4e764d..df73afe97b 100644 --- a/src/privileges/posts.js +++ b/src/privileges/posts.js @@ -14,12 +14,9 @@ module.exports = function(privileges) { privileges.posts = {}; - privileges.posts.get = function(pid, uid, callback) { + privileges.posts.get = function(pids, uid, callback) { async.parallel({ - isOwner: function(next) { - posts.isOwner(pid, uid, next); - }, manage_content: function(next) { helpers.hasEnoughReputationFor('privileges:manage_content', uid, next); }, @@ -29,25 +26,42 @@ module.exports = function(privileges) { isAdministrator: function(next) { user.isAdministrator(uid, next); }, - isModerator: function(next) { - posts.getCidByPid(pid, function(err, cid) { - if (err) { - return next(err); - } - user.isModerator(uid, cid, next); - }); - } - }, function(err, results) { + }, function(err, userResults) { if(err) { return callback(err); } - var editable = results.isAdministrator || results.isModerator || results.manage_content || results.manage_topic || results.isOwner; + var userPriv = userResults.isAdministrator || userResults.manage_topic || userResults.manage_content; - callback(null, { - editable: editable, - view_deleted: editable, - move: results.isAdministrator || results.isModerator + async.parallel({ + isOwner: function(next) { + posts.isOwner(pids, uid, next); + }, + isModerator: function(next) { + posts.getCidsByPids(pids, function(err, cids) { + if (err) { + return next(err); + } + user.isModerator(uid, cids, next); + }); + } + }, function(err, postResults) { + if (err) { + return callback(err); + } + + var privileges = []; + + for (var i=0; i