From d123fa95c62528dc667b45c5854110f3b82fe3f8 Mon Sep 17 00:00:00 2001 From: Sam Nicholls Date: Mon, 28 Jan 2013 01:38:36 +0000 Subject: [PATCH] Profile onLoad to use own createProfile API [#220] --- modules/profile/api.js | 22 ++++++++++++++++++---- modules/profile/profile.js | 21 ++++----------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/modules/profile/api.js b/modules/profile/api.js index 633c993..b1c36d2 100644 --- a/modules/profile/api.js +++ b/modules/profile/api.js @@ -8,11 +8,23 @@ var api = function(dbot) { * If the server does not already exist, create it. */ "createProfile": function(server, primary){ + var primaryLower = primary.toLowerCase(); + if(!_.has(this.profiles, server)){ this.profiles[server] = {}; } - this.profiles[server][primary.toLowerCase()] = {}; - _.defaults(this.profiles[server][primary.toLowerCase()], this.config.schema); + if(!_.has(this.profiles[server], primaryLower)){ + this.profiles[server][primaryLower] = { + "profile": {}, + "preferences": {} + }; + this.profiles[server][primaryLower].profile.primary = primary; + } + + // Ensure all profiles have the keys specified by config.json + //TODO(samstudio8) Currently only handles "top-level" + _.defaults(this.profiles[server][primaryLower].profile, this.config.schema.profile); + _.defaults(this.profiles[server][primaryLower].preferences, this.config.schema.preferences); }, /** @@ -25,10 +37,12 @@ var api = function(dbot) { var profiles = dbot.db.profiles[server]; if(_.has(profiles, alias)){ - var primary = dbot.api.users.resolveUser(server, alias, true).toLowerCase(); + var primary = dbot.api.users.resolveUser(server, alias, true); + var primaryLower = primary.toLowerCase(); alias = alias.trim().toLowerCase(); - profiles[primary] = profiles[alias]; + profiles[primaryLower] = profiles[alias]; + profiles[primaryLower].profile.primary = primary; delete profiles[alias]; } }, diff --git a/modules/profile/profile.js b/modules/profile/profile.js index 5d3b146..5bcf3a1 100644 --- a/modules/profile/profile.js +++ b/modules/profile/profile.js @@ -9,27 +9,14 @@ var profile = function(dbot) { * required properties as defined in the configuation. */ this.onLoad = function(){ + var api = this.api; var schema = this.config.schema; // Ensure all known users have a profile _.each(dbot.api.users.getAllUsers(), function(server, serverName){ - if(!_.has(dbot.db.profiles, serverName)){ - dbot.db.profiles[serverName] = {} - } - _.each(server, function(userName){ - var primary = userName; - userName = userName.toLowerCase(); - // TODO why isn't this calling the profile create API function - if(!_.has(dbot.db.profiles[serverName], userName)){ - dbot.db.profiles[serverName][userName] = { - "profile": {}, - "preferences": {} - }; - } - //TODO(samstudio8) Currently only handles "top-level" - _.defaults(dbot.db.profiles[serverName][userName].profile, schema.profile); - _.defaults(dbot.db.profiles[serverName][userName].preferences, schema.preferences); - dbot.db.profiles[serverName][userName].profile.primary = primary; + _.each(server, function(primary, primaryi){ + console.log(primary); + api.createProfile(serverName, primary); }); }); dbot.save();