2013-01-22 21:44:45 +01:00
|
|
|
var _ = require('underscore')._;
|
|
|
|
|
|
|
|
var api = function(dbot) {
|
|
|
|
return {
|
|
|
|
|
|
|
|
/**
|
2013-04-20 23:51:36 +02:00
|
|
|
* Create a new profile for a given "databanked" user.
|
|
|
|
* Typically only called as a hook to the new_user emit event.
|
|
|
|
* TODO(@samstudio8) Migrate to internalAPI
|
2013-01-22 21:44:45 +01:00
|
|
|
*/
|
2013-04-20 23:51:36 +02:00
|
|
|
"createProfile": function(user, callback){
|
|
|
|
if(user){
|
|
|
|
this.db.create('profiles', user.id, {
|
|
|
|
'id': user.id,
|
2013-06-13 20:59:14 +02:00
|
|
|
'profile': this.config.schema.profile,
|
|
|
|
'preferences': this.config.schema.preferences
|
2013-04-20 23:51:36 +02:00
|
|
|
}, function(err, result){
|
|
|
|
if(err){
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
});
|
2013-01-28 02:38:36 +01:00
|
|
|
}
|
2013-04-20 23:51:36 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
//TODO(samstudio8) Merge Profiles
|
|
|
|
'mergeProfile': function(server, nick, callback){
|
|
|
|
console.log("mergeProfile not implemented");
|
2013-01-22 21:44:45 +01:00
|
|
|
},
|
2013-01-24 00:32:50 +01:00
|
|
|
|
2013-04-20 23:51:36 +02:00
|
|
|
'getProfile': function(server, nick, callback){
|
2014-09-04 16:54:49 +02:00
|
|
|
dbot.api.users.resolveUser(server, nick, function(err, user){
|
2013-04-20 23:51:36 +02:00
|
|
|
if(user){
|
|
|
|
this.db.read('profiles', user.id, function(err, profile){
|
2013-04-21 16:27:08 +02:00
|
|
|
if(!err){
|
|
|
|
callback(false, user, profile);
|
2013-10-24 22:31:49 +02:00
|
|
|
} else {
|
2013-04-21 17:10:24 +02:00
|
|
|
callback(true, user, null);
|
2013-04-21 16:27:08 +02:00
|
|
|
}
|
2013-04-20 23:51:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
callback(true, null, null);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-01-24 00:32:50 +01:00
|
|
|
},
|
|
|
|
|
2013-04-21 17:10:24 +02:00
|
|
|
'getProfileByUUID': function(uuid, callback){
|
2013-10-23 22:15:23 +02:00
|
|
|
this.db.read('profiles', uuid, function(err, profile){
|
|
|
|
callback(profile);
|
|
|
|
});
|
2013-04-21 17:10:24 +02:00
|
|
|
},
|
|
|
|
|
2013-04-20 23:51:36 +02:00
|
|
|
'getAllProfiles': function(callback){
|
|
|
|
var profiles = [];
|
|
|
|
this.db.scan('profiles', function(profile){
|
|
|
|
profiles.push(profile);
|
|
|
|
}, function(err){
|
|
|
|
if(!err){
|
|
|
|
callback(profiles);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2013-01-24 00:32:50 +01:00
|
|
|
|
2013-10-29 16:45:00 +01:00
|
|
|
'getAllProfilesWith': function(item, callback) {
|
|
|
|
var profiles = [];
|
|
|
|
this.db.scan('profiles', function(profile) {
|
2013-10-29 16:52:20 +01:00
|
|
|
if(_.has(profile.profile, item) && profile.profile[item] != null) {
|
2013-10-29 16:45:00 +01:00
|
|
|
profiles.push(profile);
|
|
|
|
}
|
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
callback(profiles);
|
|
|
|
} else {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2014-02-27 02:34:40 +01:00
|
|
|
|
2013-04-20 23:51:36 +02:00
|
|
|
'setProperty': function(server, nick, field, value, callback){
|
|
|
|
this.api.getProfile(server, nick, function(err, user, profile){
|
|
|
|
if(!err){
|
|
|
|
profile.profile[field] = value;
|
|
|
|
this.db.save('profiles', user.id, profile, function(err){
|
|
|
|
if(!err){
|
|
|
|
callback("Ok!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
2013-01-24 00:32:50 +01:00
|
|
|
|
2013-04-20 23:51:36 +02:00
|
|
|
'getProperty': function(server, nick, field, callback){
|
|
|
|
this.api.getProfile(server, nick, function(err, user, profile){
|
|
|
|
if(!err){
|
|
|
|
if(profile.profile[field]){
|
|
|
|
callback(profile.profile[field]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-01-24 00:32:50 +01:00
|
|
|
},
|
2013-01-22 21:44:45 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return api(dbot);
|
|
|
|
};
|