small optimizations

user.isOnline works with an array of uids
do not make 2 trips to db to get main post and first 10 posts
v1.18.x
barisusakli 11 years ago
parent c818a37f0f
commit c07d9121df

@ -93,7 +93,7 @@ var async = require('async'),
};
Posts.getPostsByTid = function(tid, set, start, end, reverse, callback) {
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, end, function(err, pids) {
Posts.getPidsFromSet(set, start, end, reverse, function(err, pids) {
if(err) {
return callback(err);
}
@ -102,31 +102,15 @@ var async = require('async'),
return callback(null, []);
}
Posts.getPostsByPids(pids, function(err, posts) {
if(err) {
return callback(err);
}
if(!Array.isArray(posts) || !posts.length) {
return callback(null, []);
}
plugins.fireHook('filter:post.getPosts', {tid: tid, posts: posts}, function(err, data) {
if(err) {
return callback(err);
}
if(!data || !Array.isArray(data.posts)) {
return callback(null, []);
}
callback(null, data.posts);
});
});
Posts.getPostsByPids(pids, tid, callback);
});
};
Posts.getPostsByPids = function(pids, callback) {
Posts.getPidsFromSet = function(set, start, end, reverse, callback) {
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, end, callback);
};
Posts.getPostsByPids = function(pids, tid, callback) {
var keys = [];
for(var x=0, numPids=pids.length; x<numPids; ++x) {
@ -155,7 +139,23 @@ var async = require('async'),
next(null, postData);
});
}, callback);
}, function(err, posts) {
if (err) {
return callback(err);
}
plugins.fireHook('filter:post.getPosts', {tid: tid, posts: posts}, function(err, data) {
if (err) {
return callback(err);
}
if (!data || !Array.isArray(data.posts)) {
return callback(null, []);
}
callback(null, data.posts);
});
});
});
};

@ -217,24 +217,22 @@ SocketUser.setTopicSort = function(socket, sort, callback) {
}
};
SocketUser.getOnlineUsers = function(socket, data, callback) {
SocketUser.getOnlineUsers = function(socket, uids, callback) {
var returnData = {};
if(!data) {
if (!uids) {
return callback(new Error('[[error:invalid-data]]'));
}
function getUserStatus(uid, next) {
SocketUser.isOnline(socket, uid, function(err, data) {
SocketUser.isOnline(socket, uids, function(err, userData) {
if (err) {
return next(err);
return callback(err);
}
returnData[uid] = data;
next();
});
userData.forEach(function(user) {
if (user) {
returnData[user.uid] = user;
}
async.each(data, getUserStatus, function(err) {
callback(err, returnData);
});
callback(null, returnData);
});
};

@ -256,11 +256,24 @@ var async = require('async'),
}
async.parallel({
mainPost: function(next) {
Topics.getMainPost(tid, uid, next);
},
posts: function(next) {
Topics.getTopicPosts(tid, set, start, end, uid, reverse, next);
posts.getPidsFromSet(set, start, end, reverse, function(err, pids) {
if (err) {
return next(err);
}
pids = [topicData.mainPid].concat(pids);
posts.getPostsByPids(pids, tid, function(err, posts) {
if (err) {
return next(err);
}
start = parseInt(start, 10);
for(var i=0; i<posts.length; ++i) {
posts[i].index = start + i + 1;
}
Topics.addPostData(posts, uid, next);
});
});
},
category: function(next) {
Topics.getCategoryData(tid, next);
@ -280,7 +293,7 @@ var async = require('async'),
}
topicData.category = results.category;
topicData.posts = results.mainPost.concat(results.posts);
topicData.posts = results.posts;
topicData.tags = results.tags;
topicData.thread_tools = results.threadTools;
topicData.pageCount = results.pageCount;
@ -302,7 +315,7 @@ var async = require('async'),
if (!parseInt(mainPid, 10)) {
return callback(null, []);
}
posts.getPostsByPids([mainPid], function(err, postData) {
posts.getPostsByPids([mainPid], tid, function(err, postData) {
if (err) {
return callback(err);
}

@ -416,26 +416,33 @@ var bcrypt = require('bcryptjs'),
}
};
User.isOnline = function(uid, callback) {
User.getUserFields(uid, ['username', 'userslug', 'picture', 'status', 'reputation', 'postcount'] , function(err, data) {
User.isOnline = function(uids, callback) {
if (!Array.isArray(uids)) {
uids = [uids];
}
User.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'reputation', 'postcount'] , function(err, userData) {
if (err) {
return callback(err);
}
var websockets = require('./socket.io');
var online = websockets.isUserOnline(uid);
data.status = online ? (data.status || 'online') : 'offline';
userData = userData.map(function(user) {
var online = websockets.isUserOnline(user.uid);
user.status = online ? (user.status || 'online') : 'offline';
if(data.status === 'offline') {
if (user.status === 'offline') {
online = false;
}
data.online = online;
data.uid = uid;
data.timestamp = Date.now();
data.rooms = websockets.getUserRooms(uid);
user.online = online;
user.timestamp = Date.now();
user.rooms = websockets.getUserRooms(user.uid);
return user;
});
callback(null, data);
callback(null, userData);
});
};

Loading…
Cancel
Save