implement rUser cache [#559]

This commit is contained in:
reality 2013-08-19 17:11:07 +00:00
parent 5b82ab8690
commit 0f53693c28

View File

@ -7,6 +7,7 @@ var _ = require('underscore')._,
async = require('async'); async = require('async');
var users = function(dbot) { var users = function(dbot) {
this.userCache = {};
/*** Internal API ***/ /*** Internal API ***/
this.internalAPI = { this.internalAPI = {
@ -138,20 +139,30 @@ var users = function(dbot) {
}.bind(this)); }.bind(this));
}.bind(this); }.bind(this);
var checkUser = function(done) { var checkUser = function(done) {
this.api.resolveUser(event.server, event.user, function(user) { var checkCurrentNick = function(user) {
if(!user) { if(event.user != user.currentNick) {
this.internalAPI.createUser(event.server, event.user, done); user.currentNick = event.user;
} else { this.db.save('users', user.id, user, function() {
if(event.user != user.currentNick) {
user.currentNick = event.user;
this.db.save('users', user.id, user, function() {
done(user);
});
} else {
done(user); 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);
console.log('got cached user');
} else {
this.api.resolveUser(event.server, event.user, function(user) {
if(!user) {
this.internalAPI.createUser(event.server,
event.user, checkCurrentNick);
} else {
checkCurrentNick(user);
}
}.bind(this));
}
}.bind(this); }.bind(this);
var checkChannelUsers = function(done) { var checkChannelUsers = function(done) {
var needsUpdating = false; var needsUpdating = false;
@ -197,11 +208,19 @@ var users = function(dbot) {
event.rChannel = channel; event.rChannel = channel;
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)) {
this.userCache[event.server][event.rUser.currentNick] = event.rUser.id;
} else if(this.userCache[event.server][event.rUser.currentNick] != event.rUser.id) {
this.userCache[event.server][event.rUser.currentNick] = event.rUser.id;
}
checkChannelUsers(function() { checkChannelUsers(function() {
callback(); callback();
}); });
}); }.bind(this));
}); }.bind(this));
} else if(event.user) { } else if(event.user) {
this.api.resolveUser(event.server, event.user, function(user) { this.api.resolveUser(event.server, event.user, function(user) {
if(user) event.rUser = user; if(user) event.rUser = user;