added getObjectsFields methods to db class, changed getUsers methods to use the new method, refactor to user.js and mongo.js

v1.18.x
barisusakli 11 years ago
parent 8873963946
commit 3d68c7c6b6

@ -111,7 +111,7 @@ define(function() {
function onUsersLoaded(users, emptyContainer) {
templates.preload_template('users', function() {
templates['useres'].parse({users:[]});
templates['users'].parse({users:[]});
var html = templates.prepare(templates['users'].blocks['users']).parse({
users: users
});

@ -130,7 +130,11 @@ accountsController.getAccount = function(req, res, next) {
});
}
user.isFollowing(callerUID, userData.theirid, function (isFollowing) {
user.isFollowing(callerUID, userData.theirid, function (err, isFollowing) {
if(err) {
return next(err);
}
posts.getPostsByUid(callerUID, userData.theirid, 0, 9, function (err, userPosts) {
if(err) {
return next(err);
@ -175,7 +179,7 @@ accountsController.getFollowing = function(req, res, next) {
}
userData.following = followingData;
userData.followingCount = followingData.length;
res.render('following', userData);
});
@ -200,7 +204,7 @@ accountsController.getFollowers = function(req, res, next) {
}
userData.followers = followersData;
userData.followersCount = followersData.length;
res.render('followers', userData);
});
} else {
@ -286,7 +290,7 @@ accountsController.accountEdit = function(req, res, next) {
if(err) {
return next(err);
}
res.render('accountedit', userData);
});
};
@ -323,12 +327,12 @@ accountsController.accountSettings = function(req, res, next) {
userData.yourid = req.user.uid;
userData.theirid = uid;
userData.settings = settings;
res.render('accountsettings', userData);
});
});
});
});
};
accountsController.uploadPicture = function (req, res, next) {

@ -47,8 +47,8 @@ topicsController.get = function(req, res, next) {
if (parseInt(topicData.deleted, 10) === 1 && parseInt(topicData.expose_tools, 10) === 0) {
return next(new Error('Topic deleted'), null);
}
topicData.currentPage = page;
}
topicData.currentPage = page;
next(err, topicData);
});
});

@ -100,6 +100,26 @@
return null;
}
function fieldToString(field) {
if(field === null || field === undefined) {
return field;
}
if(typeof field !== 'string') {
field = field.toString();
}
// if there is a '.' in the field name it inserts subdocument in mongo, replace '.'s with \uff0E
field = field.replace(/\./g, '\uff0E');
return field;
}
function toString(value) {
if(value === null || value === undefined) {
return value;
}
return value.toString();
}
//
// Exported functions
@ -235,11 +255,7 @@
module.setObjectField = function(key, field, value, callback) {
var data = {};
if(typeof field !== 'string') {
field = field.toString();
}
// if there is a '.' in the field name it inserts subdocument in mongo, replace '.'s with \uff0E
field = field.replace(/\./g, '\uff0E');
field = fieldToString(field);
data[field] = value;
db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, function(err, result) {
if(typeof callback === 'function') {
@ -271,51 +287,69 @@
};
module.getObjectField = function(key, field, callback) {
if(typeof field !== 'string') {
field = field.toString();
}
field = field.replace(/\./g, '\uff0E');
field = fieldToString(field);
module.getObjectFields(key, [field], function(err, data) {
if(err) {
return callback(err);
}
callback(null, data[field]);
callback(null, data ? data[field] : null);
});
};
module.getObjectFields = function(key, fields, callback) {
module.getObjectsFields([key], fields, function(err, items) {
callback(err, items ? items[0] : null);
});
};
module.getObjectsFields = function(keys, fields, callback) {
var _fields = {
_id: 0
_id: 0,
_key: 1
};
for(var i=0; i<fields.length; ++i) {
if (typeof fields[i] !== 'string') {
fields[i] = fields[i].toString();
}
fields[i] = fields[i].replace(/\./g, '\uff0E');
for(var i=0; i<fields.length; ++i) {
fields[i] = fieldToString(fields[i]);
_fields[fields[i]] = 1;
}
keys = keys.map(function(key) {
return { _key : key};
});
db.collection('objects').findOne({_key:key}, _fields, function(err, item) {
if(err) {
db.collection('objects').find({$or: keys}, _fields).toArray(function(err, items) {
if (err) {
return callback(err);
}
if(item === null) {
item = {};
if (items === null) {
items = [];
}
for(var i=0; i<fields.length; ++i) {
if(item[fields[i]] === null || item[fields[i]] === undefined) {
item[fields[i]] = null;
var returnData = [],
index = 0,
item;
for (var i=0; i<keys.length; ++i) {
if (items[index] && items[index]._key === keys[i]._key) {
item = items[index];
index++;
} else {
item = {};
}
returnData.push(item);
for (var k=0; k<fields.length; ++k) {
if (item[fields[k]] === null || item[fields[k]] === undefined) {
item[fields[k]] = null;
}
}
}
callback(null, item);
callback(null, returnData);
});
};
@ -341,7 +375,9 @@
var values = [];
for(var key in data) {
values.push(data[key]);
if (data && data.hasOwnProperty(key)) {
values.push(data[key]);
}
}
callback(null, values);
});
@ -349,10 +385,7 @@
module.isObjectField = function(key, field, callback) {
var data = {};
if(typeof field !== 'string') {
field = field.toString();
}
field = field.replace(/\./g, '\uff0E');
field = fieldToString(field);
data[field] = '';
db.collection('objects').findOne({_key:key}, {fields:data}, function(err, item) {
if(err) {
@ -364,11 +397,8 @@
module.deleteObjectField = function(key, field, callback) {
var data = {};
if(typeof field !== 'string') {
field = field.toString();
}
field = field.replace(/\./g, '\uff0E');
data[field] = "";
field = fieldToString(field);
data[field] = '';
db.collection('objects').update({_key:key}, {$unset : data}, function(err, result) {
if(typeof callback === 'function') {
callback(err, result);
@ -386,10 +416,7 @@
module.incrObjectFieldBy = function(key, field, value, callback) {
var data = {};
if(typeof field !== 'string') {
field = field.toString();
}
field = field.replace(/\./g, '\uff0E');
field = fieldToString(field);
data[field] = value;
db.collection('objects').findAndModify({_key:key}, {}, {$inc: data}, {new:true, upsert:true}, function(err, result) {
@ -408,10 +435,9 @@
}
value.forEach(function(element, index, array) {
array[index] = element ? element.toString() : element;
array[index] = toString(element);
});
db.collection('objects').update({_key: key}, { $addToSet: { members: { $each: value } } }, { upsert: true, w: 1 }, function(err, result) {
if(typeof callback === 'function') {
callback(err, result);
@ -425,7 +451,7 @@
}
value.forEach(function(element, index, array) {
array[index] = element ? element.toString() : element;
array[index] = toString(element);
});
db.collection('objects').update( { _key: key }, { $pullAll: { members: value } }, function(err, result) {
@ -436,9 +462,7 @@
};
module.isSetMember = function(key, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
db.collection('objects').findOne({_key:key, members: value}, function(err, item) {
callback(err, item !== null && item !== undefined);
@ -447,9 +471,7 @@
module.isMemberOfSets = function(sets, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
db.collection('objects').find({_key: {$in : sets}, members: value}).toArray(function(err, result) {
if(err) {
@ -525,9 +547,7 @@
// sorted sets
module.sortedSetAdd = function(key, score, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
var data = {
score: parseInt(score, 10),
value: value
@ -541,9 +561,7 @@
};
module.sortedSetRemove = function(key, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
db.collection('objects').remove({_key:key, value:value}, function(err, result) {
if(typeof callback === 'function') {
@ -640,9 +658,7 @@
};
module.sortedSetRank = function(key, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
module.getSortedSetRange(key, 0, -1, function(err, result) {
if(err) {
return callback(err);
@ -657,9 +673,7 @@
};
module.sortedSetRevRank = function(key, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
module.getSortedSetRevRange(key, 0, -1, function(err, result) {
if(err) {
return callback(err);
@ -676,18 +690,9 @@
};
module.sortedSetScore = function(key, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
db.collection('objects').findOne({_key:key, value: value}, {fields:{score:1}}, function(err, result) {
if(err) {
return callback(err);
}
if(result) {
return callback(null, result.score);
}
callback(err, null);
callback(err, result ? result.score : null);
});
};
@ -698,9 +703,7 @@
};
module.sortedSetsScore = function(keys, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
db.collection('objects').find({_key:{$in:keys}, value: value}).toArray(function(err, result) {
if(err) {
return callback(err);
@ -720,9 +723,7 @@
// lists
module.listPrepend = function(key, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
module.isObjectField(key, 'array', function(err, exists) {
if(err) {
@ -746,9 +747,7 @@
};
module.listAppend = function(key, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
db.collection('objects').update({ _key: key }, { $push: { array: value } }, {upsert:true, w:1}, function(err, result) {
if(typeof callback === 'function') {
callback(err, result);
@ -782,9 +781,7 @@
};
module.listRemoveAll = function(key, value, callback) {
if(value !== null && value !== undefined) {
value = value.toString();
}
value = toString(value);
db.collection('objects').update({_key: key }, { $pull: { array: value } }, function(err, result) {
if(typeof callback === 'function') {

@ -55,7 +55,7 @@
return reds.client || (reds.client = redisClient);
};
postSearch = reds.createSearch('nodebbpostsearch'),
postSearch = reds.createSearch('nodebbpostsearch');
topicSearch = reds.createSearch('nodebbtopicsearch');
var db = parseInt(nconf.get('redis:database'), 10);
@ -69,7 +69,9 @@
});
}
callback();
if(typeof callback === 'function') {
callback();
}
};
module.close = function() {
@ -221,18 +223,34 @@
};
module.getObjectFields = function(key, fields, callback) {
redisClient.hmget(key, fields, function(err, data) {
if(err) {
return callback(err);
}
module.getObjectsFields([key], fields, function(err, results) {
callback(err, results ? results[0]: null);
});
};
module.getObjectsFields = function(keys, fields, callback) {
var multi = redisClient.multi();
for(var x=0; x<keys.length; ++x) {
multi.hmget.apply(multi, [keys[x]].concat(fields));
}
var returnData = {};
function makeObject(array) {
var obj = {};
for (var i = 0, ii = fields.length; i < ii; ++i) {
returnData[fields[i]] = data[i];
obj[fields[i]] = array[i];
}
return obj;
}
multi.exec(function(err, results) {
if (err) {
return callback(err);
}
callback(null, returnData);
results = results.map(makeObject);
callback(null, results);
});
};

@ -52,5 +52,9 @@ module.exports = function(app, middleware, controllers) {
}
});
});
app.get('/test', function(req, res) {
res.send(200);
});
});
};

@ -230,7 +230,7 @@ SocketPosts.getFavouritedUsers = function(socket, pid, callback) {
}
var max = 5; //hardcoded
var usernames = "";
var finalText = "";
var pid_uids = data[0];
var rest_amount = 0;
@ -240,15 +240,15 @@ SocketPosts.getFavouritedUsers = function(socket, pid, callback) {
pid_uids = pid_uids.slice(0, max);
}
user.getUsernamesByUids(pid_uids, function(err, result) {
user.getUsernamesByUids(pid_uids, function(err, usernames) {
if(err) {
return callback(err);
}
usernames = result.join(', ') + (rest_amount > 0
finalText = usernames.join(', ') + (rest_amount > 0
? " and " + rest_amount + (rest_amount > 1 ? " others" : " other")
: "");
callback(null, usernames);
callback(null, finalText);
});
});
};

@ -168,32 +168,51 @@ var bcrypt = require('bcryptjs'),
};
User.getMultipleUserFields = function(uids, fields, callback) {
if (uids.length === 0) {
if (!Array.isArray(uids) || !uids.length) {
return callback(null, []);
}
function getFields(uid, next) {
User.getUserFields(uid, fields, next);
}
var keys = uids.map(function(uid) {
return 'user:' + uid;
});
async.map(uids, getFields, callback);
db.getObjectsFields(keys, fields, callback);
};
User.getUserData = function(uid, callback) {
db.getObject('user:' + uid, function(err, data) {
if(err) {
User.getUsersData([uid], function(err, users) {
callback(err, users ? users[0] : null);
});
};
User.getUsersData = function(uids, callback) {
if (!Array.isArray(uids) || !uids.length) {
return callback(null, []);
}
var keys = uids.map(function(uid) {
return 'user:' + uid;
});
db.getObjects(keys, function(err, users) {
if (err) {
return callback(err);
}
if (data) {
if (data.password) {
data.password = null;
data.hasPassword = true;
} else {
data.hasPassword = false;
users.forEach(function(user) {
if (user) {
if (user.password) {
user.password = null;
user.hasPassword = true;
} else {
user.hasPassword = false;
}
}
}
callback(err, data);
});
callback(null, users);
});
};
@ -508,36 +527,48 @@ var bcrypt = require('bcryptjs'),
db.getSortedSetRevRange(set, start, stop, function(err, uids) {
if (err) {
return callback(err, null);
return callback(err);
}
function getUserData(uid, callback) {
User.getUserData(uid, function(err, userData) {
if(!userData.status) {
userData.status = 'online';
User.getUsersData(uids, function(err, users) {
if (err) {
return callback(err);
}
async.map(users, function(user, next) {
if (!user) {
return next(null, user);
}
User.isAdministrator(uid, function(err, isAdmin) {
if (userData) {
userData.administrator = isAdmin ? '1':'0';
if (!user.status) {
user.status = 'online';
}
User.isAdministrator(user.uid, function(err, isAdmin) {
if (err) {
return next(err);
}
user.administrator = isAdmin ? '1':'0';
if(set === 'users:online') {
return callback(null, userData);
return next(null, user);
}
db.sortedSetScore('users:online', uid, function(err, score) {
db.sortedSetScore('users:online', user.uid, function(err, score) {
if (err) {
return next(err);
}
if(!score) {
userData.status = 'offline';
user.status = 'offline';
}
callback(null, userData);
next(null, user);
});
});
});
}
async.map(uids, getUserData, callback);
}, callback);
});
});
};
@ -568,24 +599,25 @@ var bcrypt = require('bcryptjs'),
};
User.getUsersCSV = function(callback) {
var csvContent = "";
var csvContent = '';
db.getObjectValues('username:uid', function(err, uids) {
if(err) {
if (err) {
return callback(err);
}
async.each(uids, function(uid, next) {
User.getUserFields(uid, ['email', 'username'], function(err, userData) {
if(err) {
return next(err);
}
User.getMultipleUserFields(uids, ['email', 'username'], function(err, usersData) {
if (err) {
return callback(err);
}
csvContent += userData.email + ',' + userData.username + ',' + uid + '\n';
next();
usersData.forEach(function(user, index) {
if (user) {
csvContent += user.email + ',' + user.username + ',' + uids[index] + '\n';
}
});
}, function(err) {
callback(err, csvContent);
callback(null, csvContent);
});
});
};
@ -604,9 +636,9 @@ var bcrypt = require('bcryptjs'),
query = query.toLowerCase();
var usernames = Object.keys(usernamesHash),
results = [];
uids = [];
results = usernames.filter(function(username) {
uids = usernames.filter(function(username) {
return username.toLowerCase().indexOf(query) === 0;
})
.slice(0, 10)
@ -617,7 +649,10 @@ var bcrypt = require('bcryptjs'),
return usernamesHash[username];
});
User.getDataForUsers(results, function(err, userdata) {
User.getUsersData(uids, function(err, userdata) {
if (err) {
return callback(err);
}
var diff = process.hrtime(start);
var timing = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(1);
callback(null, {timing: timing, users: userdata});
@ -657,70 +692,48 @@ var bcrypt = require('bcryptjs'),
});
};
User.follow = function(uid, followid, callback) {
db.setAdd('following:' + uid, followid, function(err, data) {
if(err) {
return callback(err);
}
User.follow = function(uid, followuid, callback) {
toggleFollow('follow', uid, followuid, callback);
};
db.setAdd('followers:' + followid, uid, callback);
});
User.unfollow = function(uid, unfollowuid, callback) {
toggleFollow('unfollow', uid, unfollowuid, callback);
};
User.unfollow = function(uid, unfollowid, callback) {
db.setRemove('following:' + uid, unfollowid, function(err, data) {
function toggleFollow(type, uid, theiruid, callback) {
var command = type === 'follow' ? 'setAdd' : 'setRemove';
db[command]('following:' + uid, theiruid, function(err) {
if(err) {
return callback(err);
}
db.setRemove('followers:' + unfollowid, uid, callback);
db[command]('followers:' + theiruid, uid, callback);
});
};
}
User.getFollowing = function(uid, callback) {
db.getSetMembers('following:' + uid, function(err, userIds) {
if(err) {
return callback(err);
}
User.getDataForUsers(userIds, callback);
});
getFollow('following:' + uid, callback);
};
User.getFollowers = function(uid, callback) {
db.getSetMembers('followers:' + uid, function(err, userIds) {
getFollow('followers:' + uid, callback);
};
function getFollow(set, callback) {
db.getSetMembers(set, function(err, uids) {
if(err) {
return callback(err);
}
User.getDataForUsers(userIds, callback);
User.getUsersData(uids, callback);
});
};
}
User.getFollowingCount = function(uid, callback) {
db.getSetMembers('following:' + uid, function(err, userIds) {
if (err) {
return callback(err);
}
userIds = userIds.filter(function(value) {
return parseInt(value, 10) !== 0;
});
callback(null, userIds.length);
});
db.setCount('following:' + uid, callback);
};
User.getFollowerCount = function(uid, callback) {
db.getSetMembers('followers:' + uid, function(err, userIds) {
if(err) {
return callback(err);
}
userIds = userIds.filter(function(value) {
return parseInt(value, 10) !== 0;
});
callback(null, userIds.length);
});
db.setCount('followers:' + uid, callback);
};
User.getFollowStats = function (uid, callback) {
@ -734,22 +747,6 @@ var bcrypt = require('bcryptjs'),
}, callback);
};
User.getDataForUsers = function(uids, callback) {
if (!uids || !Array.isArray(uids) || uids.length === 0) {
return callback(null, []);
}
function getUserData(uid, next) {
if(parseInt(uid, 10) === 0) {
return next(null, null);
}
User.getUserData(uid, next);
}
async.map(uids, getUserData, callback);
};
User.sendPostNotificationToFollowers = function(uid, tid, pid) {
User.getUserField(uid, 'username', function(err, username) {
@ -773,13 +770,7 @@ var bcrypt = require('bcryptjs'),
};
User.isFollowing = function(uid, theirid, callback) {
db.isSetMember('following:' + uid, theirid, function(err, isMember) {
if (!err) {
callback(isMember);
} else {
console.log(err);
}
});
db.isSetMember('following:' + uid, theirid, callback);
};
User.exists = function(userslug, callback) {
@ -809,29 +800,17 @@ var bcrypt = require('bcryptjs'),
};
User.getUsernamesByUids = function(uids, callback) {
User.getMultipleUserFields(uids, ['username'], function(err, users) {
if (err) {
return callback(err);
}
if (!Array.isArray(uids)) {
return callback(null, []);
}
function getUserName(uid, next) {
User.getUserField(uid, 'username', next);
}
async.map(uids, getUserName, callback);
};
User.getUserSlugsByUids = function(uids, callback) {
if (!Array.isArray(uids)) {
return callback(null, []);
}
function getUserSlug(uid, next) {
User.getUserField(uid, 'userslug', next);
}
users = users.map(function(user) {
return user.username;
});
async.map(uids, getUserSlug, callback);
callback(null, users);
});
};
User.getUsernameByUserslug = function(slug, callback) {
@ -846,12 +825,7 @@ var bcrypt = require('bcryptjs'),
};
User.getUidByEmail = function(email, callback) {
db.getObjectField('email:uid', email, function(err, data) {
if (err) {
return callback(err);
}
callback(null, data);
});
db.getObjectField('email:uid', email, callback);
};
User.isModerator = function(uid, cid, callback) {

Loading…
Cancel
Save