2014-09-04 11:36:57 +02:00
|
|
|
var _ = require('underscore')._;
|
2013-01-14 22:03:47 +01:00
|
|
|
|
|
|
|
var api = function(dbot) {
|
2014-09-04 12:15:52 +02:00
|
|
|
var api = {
|
2014-09-04 11:36:57 +02:00
|
|
|
// Retrieve a user record given a server and nickname
|
|
|
|
'resolveUser': function(server, nick, callback) {
|
|
|
|
var id = nick + '.' + server;
|
|
|
|
this.api.getUser(id, function(err, result) {
|
2013-03-24 13:26:33 +01:00
|
|
|
if(!err) {
|
2014-09-04 11:36:57 +02:00
|
|
|
callback(null, result);
|
|
|
|
} else {
|
|
|
|
this.db.read('user_aliases', id, function(err, result) {
|
|
|
|
if(!err) {
|
|
|
|
this.api.getUser(result.user, callback);
|
|
|
|
} else {
|
|
|
|
callback(true, null);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-03-24 13:26:33 +01:00
|
|
|
}
|
2014-07-07 13:06:38 +02:00
|
|
|
}.bind(this));
|
2013-03-24 13:26:33 +01:00
|
|
|
},
|
|
|
|
|
2014-09-04 11:36:57 +02:00
|
|
|
// Retrive a user record given its ID
|
|
|
|
'getUser': function(id, callback) {
|
|
|
|
this.db.read('users', id, function(err, result) {
|
2013-03-24 12:09:40 +01:00
|
|
|
if(!err) {
|
2014-09-04 11:36:57 +02:00
|
|
|
callback(null, result);
|
|
|
|
} else {
|
|
|
|
callback(true, null);
|
2013-03-24 12:09:40 +01:00
|
|
|
}
|
|
|
|
});
|
2013-01-22 01:24:12 +01:00
|
|
|
},
|
|
|
|
|
2014-09-04 11:36:57 +02:00
|
|
|
// Retrieve user aliases given a user ID
|
|
|
|
'getUserAliases': function(id, callback) {
|
|
|
|
var aliases = [];
|
|
|
|
this.db.search('user_aliases', { 'user': id }, function(result) {
|
|
|
|
aliases.push(result.alias);
|
2013-05-21 22:17:22 +02:00
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
2014-09-04 11:36:57 +02:00
|
|
|
callback(null, aliases);
|
|
|
|
} else {
|
|
|
|
callback(true, null);
|
2013-05-21 22:17:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-09-04 11:36:57 +02:00
|
|
|
// Retrieve a channel record given a server and a channel name
|
|
|
|
'resolveChannel': function(server, channel, callback) {
|
|
|
|
var id = channel + '.' + server;
|
|
|
|
this.api.getChannel(id, callback);
|
2013-05-02 20:25:10 +02:00
|
|
|
},
|
|
|
|
|
2014-09-04 11:36:57 +02:00
|
|
|
// Retrieve a channel record given its ID
|
|
|
|
'getChannel': function(id, callback) {
|
|
|
|
this.db.read('channels', id, function(err, result) {
|
|
|
|
if(!err) {
|
|
|
|
callback(null, result);
|
2013-05-02 20:25:10 +02:00
|
|
|
} else {
|
2014-09-04 11:36:57 +02:00
|
|
|
callback(true, null);
|
2013-05-02 20:25:10 +02:00
|
|
|
}
|
|
|
|
});
|
2013-01-14 22:03:47 +01:00
|
|
|
}
|
|
|
|
};
|
2014-09-04 12:15:52 +02:00
|
|
|
|
|
|
|
return api;
|
2013-01-14 22:03:47 +01:00
|
|
|
};
|
2014-09-04 11:51:39 +02:00
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return api(dbot);
|
|
|
|
};
|