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);
};
Groups.isMemberOfGroups = function(uid, groups, callback) {
db.isMemberOfSets(groups, uid, callback);
};
Groups.getMemberCount = function(groupName, 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.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) {
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) === parseInt(uid, 10));
callback(err, parseInt(author, 10) === uid);
});
}
};
Posts.isMain = function(pid, callback) {

@ -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);
},
}, function(err, userResults) {
if(err) {
return callback(err);
}
var userPriv = userResults.isAdministrator || userResults.manage_topic || userResults.manage_content;
async.parallel({
isOwner: function(next) {
posts.isOwner(pids, uid, next);
},
isModerator: function(next) {
posts.getCidByPid(pid, function(err, cid) {
posts.getCidsByPids(pids, function(err, cids) {
if (err) {
return next(err);
}
user.isModerator(uid, cid, next);
user.isModerator(uid, cids, next);
});
}
}, function(err, results) {
if(err) {
}, function(err, postResults) {
if (err) {
return callback(err);
}
var editable = results.isAdministrator || results.isModerator || results.manage_content || results.manage_topic || results.isOwner;
var privileges = [];
callback(null, {
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: results.isAdministrator || results.isModerator
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) {
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]);
});
};

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

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

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

Loading…
Cancel
Save