privileges.posts.get takes an array of pids now

v1.18.x
barisusakli 11 years ago
parent 6cd29a31a4
commit d57f183f42

@ -150,6 +150,10 @@
db.isSetMember('group:' + groupName + ':members', uid, callback); db.isSetMember('group:' + groupName + ':members', uid, callback);
}; };
Groups.isMemberOfGroups = function(uid, groups, callback) {
db.isMemberOfSets(groups, uid, callback);
};
Groups.getMemberCount = function(groupName, callback) { Groups.getMemberCount = function(groupName, callback) {
db.setCount('group:' + groupName + ':members', callback); db.setCount('group:' + groupName + ':members', callback);
}; };

@ -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.getPostField = function(pid, field, callback) {
Posts.getPostFields(pid, [field], function(err, data) { Posts.getPostFields(pid, [field], function(err, data) {
if(err) { 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) { Posts.getPostsByUid = function(callerUid, uid, start, end, callback) {
user.getPostIds(uid, start, end, function(err, pids) { user.getPostIds(uid, start, end, function(err, pids) {
if (err) { if (err) {
@ -474,9 +504,22 @@ var async = require('async'),
}; };
Posts.isOwner = function(pid, uid, callback) { Posts.isOwner = function(pid, uid, callback) {
Posts.getPostField(pid, 'uid', function(err, author) { uid = parseInt(uid, 10);
callback(err, parseInt(author, 10) === 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) { Posts.isMain = function(pid, callback) {

@ -14,12 +14,9 @@ module.exports = function(privileges) {
privileges.posts = {}; privileges.posts = {};
privileges.posts.get = function(pid, uid, callback) { privileges.posts.get = function(pids, uid, callback) {
async.parallel({ async.parallel({
isOwner: function(next) {
posts.isOwner(pid, uid, next);
},
manage_content: function(next) { manage_content: function(next) {
helpers.hasEnoughReputationFor('privileges:manage_content', uid, next); helpers.hasEnoughReputationFor('privileges:manage_content', uid, next);
}, },
@ -29,25 +26,42 @@ module.exports = function(privileges) {
isAdministrator: function(next) { isAdministrator: function(next) {
user.isAdministrator(uid, next); user.isAdministrator(uid, next);
}, },
isModerator: function(next) { }, function(err, userResults) {
posts.getCidByPid(pid, function(err, cid) {
if (err) {
return next(err);
}
user.isModerator(uid, cid, next);
});
}
}, function(err, results) {
if(err) { if(err) {
return callback(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, { async.parallel({
editable: editable, isOwner: function(next) {
view_deleted: editable, posts.isOwner(pids, uid, next);
move: results.isAdministrator || results.isModerator },
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);
}); });
}); });
}; };

@ -226,12 +226,16 @@ SocketPosts.purge = function(socket, data, callback) {
}; };
SocketPosts.getPrivileges = function(socket, pid, callback) { SocketPosts.getPrivileges = function(socket, pid, callback) {
privileges.posts.get(pid, socket.uid, function(err, privileges) { privileges.posts.get([pid], socket.uid, function(err, privileges) {
if(err) { if (err) {
return callback(err); return callback(err);
} }
privileges.pid = parseInt(pid, 10); if (!Array.isArray(privileges) || !privileges.length) {
callback(null, privileges); return callback(new Error('[[error:invalid-data]]'));
}
privileges[0].pid = parseInt(pid, 10);
callback(null, privileges[0]);
}); });
}; };

@ -386,6 +386,13 @@ var async = require('async'),
db.getObjectFields('topic:' + tid, fields, callback); 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) { Topics.setTopicField = function(tid, field, value, callback) {
db.setObjectField('topic:' + tid, field, value, callback); db.setObjectField('topic:' + tid, field, value, callback);
}; };

@ -55,6 +55,7 @@ module.exports = function(Topics) {
}, },
userData: function(next) { userData: function(next) {
var uids = []; var uids = [];
for(var i=0; i<postData.length; ++i) { for(var i=0; i<postData.length; ++i) {
if (uids.indexOf(postData[i].uid) === -1) { if (uids.indexOf(postData[i].uid) === -1) {
uids.push(postData[i].uid); uids.push(postData[i].uid);
@ -70,6 +71,7 @@ module.exports = function(Topics) {
users.forEach(function(user) { users.forEach(function(user) {
userData[user.uid] = user; userData[user.uid] = user;
}); });
next(null, userData); next(null, userData);
}); });
}, },
@ -93,9 +95,7 @@ module.exports = function(Topics) {
}); });
}, },
privileges: function(next) { privileges: function(next) {
async.map(pids, function (pid, next) { privileges.posts.get(pids, uid, next);
privileges.posts.get(pid, uid, next);
}, next);
} }
}, function(err, results) { }, function(err, results) {
if(err) { if(err) {

@ -405,7 +405,14 @@ var bcrypt = require('bcryptjs'),
}; };
User.isModerator = function(uid, cid, callback) { 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) { User.isAdministrator = function(uid, callback) {

Loading…
Cancel
Save