3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-30 16:09:27 +01:00
dbot/modules/users/api.js

75 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-09-04 11:36:57 +02:00
var _ = require('underscore')._;
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) {
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));
}
2014-07-07 13:06:38 +02:00
}.bind(this));
},
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) {
if(!err) {
2014-09-04 11:36:57 +02:00
callback(null, result);
} else {
callback(true, null);
}
});
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) {
2017-07-18 04:16:07 +02:00
this.api.getUser(id, function(err, user) {
if(!err) {
callback(null, user.aliases);
} else {
callback(true, null);
}
});
2014-09-05 12:42:30 +02:00
},
2014-09-05 12:42:30 +02:00
// Check if a nick is online under a given alias
'isOnline': function(server, channel, nick, callback) {
this.api.resolveUser(server, nick, function(err, user) {
if(user) {
this.api.getUserAliases(user.id, function(err, aliases) {
aliases.push(nick);
var onlineNicks = _.keys(dbot.instance.connections[server].channels[channel].nicks);
var isOnline = _.any(onlineNicks, function(nick) {
return _.include(aliases, nick);
}, this);
callback(null, user, isOnline);
});
} else {
callback(true, null, null);
}
}.bind(this));
}
};
2014-09-04 12:15:52 +02:00
2014-09-15 23:19:41 +02:00
api['getUserAliases'].external = true;
api['getUserAliases'].extMap = [ 'id', 'callback' ];
2014-09-04 12:15:52 +02:00
return api;
};
exports.fetch = function(dbot) {
return api(dbot);
};