3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-25 04:02:39 +01:00

Profile onLoad to use own createProfile API [#220]

This commit is contained in:
Sam Nicholls 2013-01-28 01:38:36 +00:00
parent 13e24bbdbb
commit d123fa95c6
2 changed files with 22 additions and 21 deletions

View File

@ -8,11 +8,23 @@ var api = function(dbot) {
* If the server does not already exist, create it. * If the server does not already exist, create it.
*/ */
"createProfile": function(server, primary){ "createProfile": function(server, primary){
var primaryLower = primary.toLowerCase();
if(!_.has(this.profiles, server)){ if(!_.has(this.profiles, server)){
this.profiles[server] = {}; this.profiles[server] = {};
} }
this.profiles[server][primary.toLowerCase()] = {}; if(!_.has(this.profiles[server], primaryLower)){
_.defaults(this.profiles[server][primary.toLowerCase()], this.config.schema); 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]; var profiles = dbot.db.profiles[server];
if(_.has(profiles, alias)){ 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(); alias = alias.trim().toLowerCase();
profiles[primary] = profiles[alias]; profiles[primaryLower] = profiles[alias];
profiles[primaryLower].profile.primary = primary;
delete profiles[alias]; delete profiles[alias];
} }
}, },

View File

@ -9,27 +9,14 @@ var profile = function(dbot) {
* required properties as defined in the configuation. * required properties as defined in the configuation.
*/ */
this.onLoad = function(){ this.onLoad = function(){
var api = this.api;
var schema = this.config.schema; var schema = this.config.schema;
// Ensure all known users have a profile // Ensure all known users have a profile
_.each(dbot.api.users.getAllUsers(), function(server, serverName){ _.each(dbot.api.users.getAllUsers(), function(server, serverName){
if(!_.has(dbot.db.profiles, serverName)){ _.each(server, function(primary, primaryi){
dbot.db.profiles[serverName] = {} console.log(primary);
} api.createProfile(serverName, primary);
_.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;
}); });
}); });
dbot.save(); dbot.save();