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

@ -579,6 +579,9 @@ var async = require('async'),
Posts.isOwner = function(pid, uid, callback) {
uid = parseInt(uid, 10);
if (Array.isArray(pid)) {
if (!uid) {
return callback(null, pid.map(function() {return false;}));
}
Posts.getPostsFields(pid, ['uid'], function(err, posts) {
if (err) {
return callback(err);
@ -589,12 +592,27 @@ var async = require('async'),
callback(null, posts);
});
} else {
if (!uid) {
return callback(null, false);
}
Posts.getPostField(pid, 'uid', function(err, author) {
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.getPostField(pid, 'tid', function(err, tid) {
if (err) {

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

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

Loading…
Cancel
Save