From da5e79ebeb760a16a033fb363340c43186c251b0 Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 2 May 2013 18:25:10 +0000 Subject: [PATCH] Efficiency improvements in users, return of isKnownUser API function [#331] --- modules/users/api.js | 10 ++++++++++ modules/users/strings.json | 2 +- modules/users/users.js | 40 +++++++++++++++++++++++++------------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/modules/users/api.js b/modules/users/api.js index 53f3727..1d5ffc3 100644 --- a/modules/users/api.js +++ b/modules/users/api.js @@ -93,6 +93,16 @@ var api = function(dbot) { callback(isOnline); } }); + }, + + 'isKnownUser': function(server, nick, callback) { + this.api.resolveUser(server, nick, function(isKnown) { + if(isKnown == false) { + callback(false); + } else { + callback(true); + } + }); } }; diff --git a/modules/users/strings.json b/modules/users/strings.json index 96a1883..a8f60f1 100644 --- a/modules/users/strings.json +++ b/modules/users/strings.json @@ -5,7 +5,7 @@ "nl": "{alias} is een alias van {user}" }, "primary": { - "en": "{user} ({currentNick}) is a primary user with {count} aliases, ", + "en": "{user} (currently {currentNick}) is a primary user with {count} aliases, ", "na'vi": "{user} ({currentNick}) lu txin ulte {count}a stxo lu poru, ", "nl": "{user} ({currentNick}) is een primaire gebruiker met {count} aliassen, " }, diff --git a/modules/users/users.js b/modules/users/users.js index 3fdab59..03d2fc5 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -73,22 +73,34 @@ var users = function(dbot) { this.listener = function(event) { if(event.action == 'JOIN' && event.user != dbot.config.name) { this.api.resolveUser(event.server, event.user, function(user) { - if(!user) { // User does not yet exist - this.internalAPI.createUser(event.server, event.user, event.channel.name, function(result) { - this.internalAPI.addChannelUser(result, event.channel.name, function(err) { }); - }.bind(this)); - } else { - if(!_.include(user.channels, event.channel.name)) { // User not yet channel user - user.channels.push(event.channel.name); - this.internalAPI.addChannelUser(user, event.channel.name, function(err) { }); - } + var needsUpdating = false; + if(!user) { // User does not yet exist + user = { + 'id': uuid.v4(), + 'primaryNick': event.user, + 'currentNick': event.user, + 'server': event.server, + 'channels': [], + 'aliases': [] + }; + this.internalAPI.addChannelUser(user, event.channel.name, function(err) { }); + needsUpdating = true; + } + + if(!_.include(user.channels, event.channel.name)) { // User not yet channel user + user.channels.push(event.channel.name); + this.internalAPI.addChannelUser(user, event.channel.name, function(err) { }); + needsUpdating = true; + } + + if(user.currentNick != event.user) { user.currentNick = event.user; - this.db.save('users', user.id, user, function(err) { - if(err) { - // QQ - } - }); + needsUpdating = true; + } + + if(needsUpdating == true) { + this.db.save('users', user.id, user, function(err) { }); } }.bind(this)); } else if(event.action == 'NICK') {