Skip to content

Node.js based forum software built for the modern web

privileges.posts.get takes an array of pids now

barisusaklid57f183f420c
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<pids.length; ++i) {
+ var editable = userPriv || postResults.isModerator[i] || postResults.isOwner[i];
+ privileges.push({
+ editable: editable,
+ view_deleted: editable,
+ move: userResults.isAdministrator || postResults.isModerator[i]
+ });
+ }
+
+ callback(null, privileges);
});
});
};
diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js
index 1980d21927..6d425c7b8c 100644
--- a/src/socket.io/posts.js
+++ b/src/socket.io/posts.js
@@ -226,12 +226,16 @@ SocketPosts.purge = function(socket, data, callback) {
};
SocketPosts.getPrivileges = function(socket, pid, callback) {
- privileges.posts.get(pid, socket.uid, function(err, privileges) {
- if(err) {
+ privileges.posts.get([pid], socket.uid, function(err, privileges) {
+ if (err) {
return callback(err);
}
- privileges.pid = parseInt(pid, 10);
- callback(null, privileges);
+ if (!Array.isArray(privileges) || !privileges.length) {
+ return callback(new Error('[[error:invalid-data]]'));
+ }
+
+ privileges[0].pid = parseInt(pid, 10);
+ callback(null, privileges[0]);
});
};
diff --git a/src/topics.js b/src/topics.js
index 9157ab8f48..e1e9a1c90f 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -386,6 +386,13 @@ var async = require('async'),
db.getObjectFields('topic:' + tid, fields, callback);
};
+ Topics.getTopicsFields = function(tids, fields, callback) {
+ 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);
};
diff --git a/src/topics/posts.js b/src/topics/posts.js
index de64f430c1..593cae4e18 100644
--- a/src/topics/posts.js
+++ b/src/topics/posts.js
@@ -55,6 +55,7 @@ module.exports = function(Topics) {
},
userData: function(next) {
var uids = [];
+
for(var i=0; i<postData.length; ++i) {
if (uids.indexOf(postData[i].uid) === -1) {
uids.push(postData[i].uid);
@@ -70,6 +71,7 @@ module.exports = function(Topics) {
users.forEach(function(user) {
userData[user.uid] = user;
});
+
next(null, userData);
});
},
@@ -93,9 +95,7 @@ module.exports = function(Topics) {
});
},
privileges: function(next) {
- async.map(pids, function (pid, next) {
- privileges.posts.get(pid, uid, next);
- }, next);
+ privileges.posts.get(pids, uid, next);
}
}, function(err, results) {
if(err) {
diff --git a/src/user.js b/src/user.js
index 11efb9ff60..60b7fe9394 100644
--- a/src/user.js
+++ b/src/user.js
@@ -405,7 +405,14 @@ var bcrypt = require('bcryptjs'),
};
User.isModerator = function(uid, cid, callback) {
- groups.isMember(uid, 'cid:' + cid + ':privileges:mods', callback);
+ if (Array.isArray(cid)) {
+ var groupNames = cid.map(function(cid) {
+ return 'cid:' + cid + ':privileges:mods';
+ });
+ groups.isMemberOfGroups(uid, groupNames, callback);
+ } else {
+ groups.isMember(uid, 'cid:' + cid + ':privileges:mods', callback);
+ }
};
User.isAdministrator = function(uid, callback) {