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();