3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 14:29:29 +01:00

some more efficiency improvements in users

This commit is contained in:
reality 2013-08-25 15:37:32 +00:00
parent 31f32ca8da
commit 5cf79312f4
2 changed files with 15 additions and 23 deletions

View File

@ -10,15 +10,19 @@ var api = function(dbot) {
var api = { var api = {
// Return a user record given a primary nick or an alias // Return a user record given a primary nick or an alias
'resolveUser': function(server, nick, callback) { 'resolveUser': function(server, nick, callback) {
var found = false;
if(_.has(this.userCache[server], nick)) { if(_.has(this.userCache[server], nick)) {
this.api.getUser(this.userCache[server][nick], callback); this.api.getUser(this.userCache[server][nick], callback);
} else { } else {
this.db.search('users', { 'server': server }, function(result) { this.db.search('users', { 'server': server }, function(result) {
if(result.primaryNick == nick || _.include(result.aliases, nick)) { if(result.primaryNick == nick || _.include(result.aliases, nick)) {
this.userCache[server][nick] = result.id; this.userCache[server][nick] = result.id;
return callback(result); found = true;
callback(result);
} }
}.bind(this), function(err) {}); }.bind(this), function(err) {
if(!found) callback(false);
});
} }
}, },

View File

@ -127,29 +127,18 @@ var users = function(dbot) {
}.bind(this)); }.bind(this));
}.bind(this); }.bind(this);
var checkUser = function(done) { var checkUser = function(done) {
var checkCurrentNick = function(user) {
if(event.user != user.currentNick) {
user.currentNick = event.user;
this.db.save('users', user.id, user, function() {
done(user);
});
} else {
done(user);
}
}.bind(this);
if(_.has(this.userCache, event.server) && _.has(this.userCache[event.server], event.user)) {
this.api.getUser(this.userCache[event.server][event.user], checkCurrentNick);
} else {
this.api.resolveUser(event.server, event.user, function(user) { this.api.resolveUser(event.server, event.user, function(user) {
if(!user) { if(!user) {
this.internalAPI.createUser(event.server, this.internalAPI.createUser(event.server, event.user, done);
event.user, checkCurrentNick);
} else { } else {
checkCurrentNick(user); if(event.user != user.currentNick) {
user.currentNick = event.user;
this.db.save('users', user.id, user, function() { done(user); });
} else {
done(user);
} }
}.bind(this));
} }
});
}.bind(this); }.bind(this);
var checkChannelUsers = function(done) { var checkChannelUsers = function(done) {
var needsUpdating = false; var needsUpdating = false;
@ -201,7 +190,6 @@ var users = function(dbot) {
checkUser(function(user) { checkUser(function(user) {
event.rUser = user; event.rUser = user;
if(!_.has(this.userCache, event.server)) this.userCache[event.server] = {};
if(!_.has(this.userCache[event.server], event.rUser.currentNick)) { if(!_.has(this.userCache[event.server], event.rUser.currentNick)) {
this.userCache[event.server][event.rUser.currentNick] = event.rUser.id; this.userCache[event.server][event.rUser.currentNick] = event.rUser.id;
} else if(this.userCache[event.server][event.rUser.currentNick] != event.rUser.id) { } else if(this.userCache[event.server][event.rUser.currentNick] != event.rUser.id) {