Changed users API to databank [#272]

Behavioural changes:

* API functions no longer return values, callbacks must be submitted. These will be documented soon.
* resolveUser now returns the user record instead of just a resolved username. This means many less search operations.
* resolveUser now returns false instead of the original nick passed to it in the case of being unable to find a user.
* getServerUsers has been removed, as it is now unnecessary.
This commit is contained in:
reality 2013-03-24 11:34:52 +00:00
parent cbc42e6fc3
commit d30aa145aa

View File

@ -11,19 +11,18 @@ var api = function(dbot) {
}; };
var api = { var api = {
// Return a user record given a primary nick or an alias
'resolveUser': function(server, nick, callback) { 'resolveUser': function(server, nick, callback) {
if(this.api.isPrimaryUser(nick)) {
callback(nick);
} else {
var user = false; var user = false;
this.db.search('user', { 'server': server }, function(user) { this.db.search('user', { 'server': server }, function(result) {
if(_.include(user.aliases, nick)) user = user.primaryNick; if(result.primaryNick == nick || _.include(result.aliases, nick)) {
user = result;
}
}, function(err) { }, function(err) {
if(!err) { if(!err) {
callback(user); callback(user);
} }
}); });
}
}, },
'getRandomChannelUser': function(server, channel, callback) { 'getRandomChannelUser': function(server, channel, callback) {
@ -36,7 +35,10 @@ var api = function(dbot) {
}, function(err) { }, function(err) {
if(!err) { if(!err) {
if(!_.isUndefined(channel.users)) { if(!_.isUndefined(channel.users)) {
callback(channel.users[_.random(0, channel.users.length - 1)]); var randomUser = channel.users[_.random(0, channel.users.length - 1)];
this.api.resolveUser(server, randomUser, function(user) {
callback(user);
});
} else { } else {
callback(false); callback(false);
} }
@ -44,17 +46,6 @@ var api = function(dbot) {
}); });
}, },
'getServerUsers': function(server, callback) {
var users = [];
this.db.search('users', { 'server': server }, function(user) {
users.push(user);
}, function(err) {
if(!err) {
callback(users);
}
});
},
'getAllUsers': function(callback) { 'getAllUsers': function(callback) {
var users = []; var users = [];
this.db.scan('users', function(user) { this.db.scan('users', function(user) {
@ -102,29 +93,27 @@ var api = function(dbot) {
}); });
}, },
'isOnline': function(server, user, channel, callback) { 'isOnline': function(server, nick, channel, callback) {
var user = this.api.resolveUser(server, user, useLowerCase); this.api.resolveUser(server, nick, function(user) {
var possiNicks = [user].concat(this.api.getAliases(server, user)); var possiNicks = [user].concat(user.aliases);
if(!_.has(dbot.instance.connections[server].channels, channel)) return false; if(_.has(dbot.instance.connections[server].channels, channel)) {
var onlineNicks = dbot.instance.connections[server].channels[channel].nicks; var onlineNicks = dbot.instance.connections[server].channels[channel].nicks;
var isOnline = _.any(onlineNicks, function(nick) { var isOnline = _.any(onlineNicks, function(nick) {
nick = nick.name; nick = nick.name;
return _.include(possiNicks, nick); return _.include(possiNicks, nick);
}, this); }, this);
callback(isOnline); callback(isOnline);
}
});
}, },
'isChannelUser': function(server, user, channel) { 'isChannelUser': function(server, nick, channel, callback) {
var knownUsers = this.getServerUsers(server); this.api.resolveUser(server, nick, function(user) {
var user = this.api.resolveUser(server, user, useLowerCase); var isChannelUser = _.include(user.channels, channel);
callback(isChannelUser);
if(!_.has(knownUsers.channelUsers, channel)) { });
return false;
}
return _.include(knownUsers.channelUsers[channel], user);
} }
}; };