anon checks

less db calls for uid 0
v1.18.x
barisusakli 10 years ago
parent 0a38a783c7
commit 06f66337fb

@ -195,6 +195,9 @@ var async = require('async'),
}; };
Favourites.hasVoted = function(pid, uid, callback) { Favourites.hasVoted = function(pid, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, {upvoted: false, downvoted: false});
}
db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, function(err, hasVoted) { db.isMemberOfSets(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], uid, function(err, hasVoted) {
if (err) { if (err) {
return callback(err); return callback(err);
@ -205,6 +208,10 @@ var async = require('async'),
}; };
Favourites.getVoteStatusByPostIDs = function(pids, uid, callback) { Favourites.getVoteStatusByPostIDs = function(pids, uid, callback) {
if (!parseInt(uid, 10)) {
var data = pids.map(function() {return false;});
return callback(null, {upvotes: data, downvotes: data});
}
var upvoteSets = [], var upvoteSets = [],
downvoteSets = []; downvoteSets = [];
@ -284,10 +291,17 @@ var async = require('async'),
} }
Favourites.hasFavourited = function(pid, uid, callback) { Favourites.hasFavourited = function(pid, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, false);
}
db.isSetMember('pid:' + pid + ':users_favourited', uid, callback); db.isSetMember('pid:' + pid + ':users_favourited', uid, callback);
}; };
Favourites.getFavouritesByPostIDs = function(pids, uid, callback) { Favourites.getFavouritesByPostIDs = function(pids, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, pids.map(function() {return false;}));
}
var sets = []; var sets = [];
for (var i=0; i<pids.length; ++i) { for (var i=0; i<pids.length; ++i) {
sets.push('pid:' + pids[i] + ':users_favourited'); sets.push('pid:' + pids[i] + ':users_favourited');

@ -579,6 +579,9 @@ var async = require('async'),
Posts.isOwner = function(pid, uid, callback) { Posts.isOwner = function(pid, uid, callback) {
uid = parseInt(uid, 10); uid = parseInt(uid, 10);
if (Array.isArray(pid)) { if (Array.isArray(pid)) {
if (!uid) {
return callback(null, pid.map(function() {return false;}));
}
Posts.getPostsFields(pid, ['uid'], function(err, posts) { Posts.getPostsFields(pid, ['uid'], function(err, posts) {
if (err) { if (err) {
return callback(err); return callback(err);
@ -589,12 +592,27 @@ var async = require('async'),
callback(null, posts); callback(null, posts);
}); });
} else { } else {
if (!uid) {
return callback(null, false);
}
Posts.getPostField(pid, 'uid', function(err, author) { Posts.getPostField(pid, 'uid', function(err, author) {
callback(err, parseInt(author, 10) === uid); callback(err, parseInt(author, 10) === uid);
}); });
} }
}; };
Posts.isModerator = function(pids, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, pids.map(function() {return false;}));
}
Posts.getCidsByPids(pids, function(err, cids) {
if (err) {
return callback(err);
}
user.isModerator(uid, cids, callback);
});
}
Posts.isMain = function(pid, callback) { Posts.isMain = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(err, tid) { Posts.getPostField(pid, 'tid', function(err, tid) {
if (err) { if (err) {

@ -38,12 +38,7 @@ module.exports = function(privileges) {
posts.isOwner(pids, uid, next); posts.isOwner(pids, uid, next);
}, },
isModerator: function(next) { isModerator: function(next) {
posts.getCidsByPids(pids, function(err, cids) { posts.isModerator(pids, uid, next);
if (err) {
return next(err);
}
user.isModerator(uid, cids, next);
});
} }
}, function(err, postResults) { }, function(err, postResults) {
if (err) { if (err) {

@ -402,6 +402,9 @@ var async = require('async'),
User.isModerator = function(uid, cid, callback) { User.isModerator = function(uid, cid, callback) {
if (Array.isArray(cid)) { if (Array.isArray(cid)) {
if (!parseInt(uid, 10)) {
return callback(null, cid.map(function() {return false;}));
}
var uniqueCids = cid.filter(function(cid, index, array) { var uniqueCids = cid.filter(function(cid, index, array) {
return array.indexOf(cid) === index; return array.indexOf(cid) === index;
}); });

Loading…
Cancel
Save