moved user search to its own file

v1.18.x
barisusakli 11 years ago
parent 383b68e7fc
commit d8e00f95a2

@ -1,3 +1,5 @@
'use strict';
var async = require('async'), var async = require('async'),
user = require('../user'), user = require('../user'),
topics = require('../topics'), topics = require('../topics'),
@ -93,8 +95,9 @@ SocketUser.updateProfile = function(socket, data, callback) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
if(!isAdmin) { if(!isAdmin) {
return callback(new Error('not allowed!')) return callback(new Error('not allowed!'));
} }
user.updateProfile(data.uid, data, callback); user.updateProfile(data.uid, data, callback);

@ -27,6 +27,7 @@ var bcrypt = require('bcryptjs'),
require('./user/profile')(User); require('./user/profile')(User);
require('./user/admin')(User); require('./user/admin')(User);
require('./user/settings')(User); require('./user/settings')(User);
require('./user/search')(User);
User.create = function(userData, callback) { User.create = function(userData, callback) {
userData = userData || {}; userData = userData || {};
@ -338,44 +339,6 @@ var bcrypt = require('bcryptjs'),
}); });
}; };
User.search = function(query, callback) {
if (!query || query.length === 0) {
return callback(null, {timing:0, users:[]});
}
var start = process.hrtime();
db.getObject('username:uid', function(err, usernamesHash) {
if (err) {
return callback(null, {timing: 0, users:[]});
}
query = query.toLowerCase();
var usernames = Object.keys(usernamesHash),
uids = [];
uids = usernames.filter(function(username) {
return username.toLowerCase().indexOf(query) === 0;
})
.slice(0, 10)
.sort(function(a, b) {
return a > b;
})
.map(function(username) {
return usernamesHash[username];
});
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});
});
});
};
User.onNewPostMade = function(uid, tid, pid, timestamp) { User.onNewPostMade = function(uid, tid, pid, timestamp) {
User.addPostIdToUser(uid, pid, timestamp); User.addPostIdToUser(uid, pid, timestamp);

@ -0,0 +1,45 @@
'use strict';
var db = require('./../database');
module.exports = function(User) {
User.search = function(query, callback) {
if (!query || query.length === 0) {
return callback(null, {timing:0, users:[]});
}
var start = process.hrtime();
db.getObject('username:uid', function(err, usernamesHash) {
if (err) {
return callback(null, {timing: 0, users:[]});
}
query = query.toLowerCase();
var usernames = Object.keys(usernamesHash),
uids = [];
uids = usernames.filter(function(username) {
return username.toLowerCase().indexOf(query) === 0;
})
.slice(0, 10)
.sort(function(a, b) {
return a > b;
})
.map(function(username) {
return usernamesHash[username];
});
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});
});
});
};
};
Loading…
Cancel
Save