diff --git a/modules/profile/api.js b/modules/profile/api.js index 25a6c9c..b19a693 100644 --- a/modules/profile/api.js +++ b/modules/profile/api.js @@ -14,11 +14,7 @@ var api = function(dbot) { 'id': user.id, 'profile': this.config.schema.profile, 'preferences': this.config.schema.preferences - }, function(err, result){ - if(err){ - console.log(err); - } - }); + }, callback); } }, @@ -32,11 +28,17 @@ var api = function(dbot) { if(user){ this.db.read('profiles', user.id, function(err, profile){ if(!err){ - callback(false, user, profile); + if(profile) { + callback(false, user, profile); + } else { + this.api.createProfile(user, function(err, profile) { + callback(null, user, profile); + }); + } } else { callback(true, user, null); } - }); + }.bind(this)); } else{ callback(true, null, null); @@ -44,10 +46,16 @@ var api = function(dbot) { }.bind(this)); }, - 'getProfileByUUID': function(uuid, callback){ - this.db.read('profiles', uuid, function(err, profile){ - callback(profile); - }); + 'getProfileByUser': function(user, callback){ + this.db.read('profiles', user.id, function(err, profile){ + if(profile) { + callback(profile); + } else { + this.api.createProfile(user, function(err, profile) { + callback(profile); + }); + } + }.bind(this)); }, 'getAllProfiles': function(callback){ diff --git a/modules/profile/profile.js b/modules/profile/profile.js index c2f264c..9b9a6ef 100644 --- a/modules/profile/profile.js +++ b/modules/profile/profile.js @@ -5,30 +5,16 @@ var profile = function(dbot) { this.onLoad = function(){ var schema = this.config.schema; - // Ensure all users have a profile - dbot.api.users.getAllUsers(function(users){ - if(users){ - _.each(users, function(user){ - this.api.getProfileByUUID(user.id, function(err, uuid, profile){ - // If function returns an error and uuid, create a new profile - if(err && uuid){ - this.api.createProfile(user); - } - }.bind(this)); - }.bind(this)); - } - }.bind(this)); - // Add API Hooks dbot.api.event.addHook('new_user', this.api.createProfile); dbot.instance.addPreEmitHook(function(event, callback) { if(!event.rUser) return callback(); - this.api.getProfileByUUID(event.rUser.id, function(uProfile) { + this.api.getProfileByUser(event.rUser, function(uProfile) { if(uProfile) { event.rProfile = uProfile.profile; - callback(); } + callback(); }.bind(this)); }.bind(this)); diff --git a/modules/users/api.js b/modules/users/api.js index bbc171d..c35878e 100644 --- a/modules/users/api.js +++ b/modules/users/api.js @@ -44,7 +44,7 @@ var api = function(dbot) { } }); }, - + // Check if a nick is online under a given alias 'isOnline': function(server, channel, nick, callback) { this.api.resolveUser(server, nick, function(err, user) {