3
0
mirror of https://github.com/reality/dbot.git synced 2025-01-11 20:42:37 +01:00

Fix fucking async hell and add getAllChannels

This commit is contained in:
reality 2013-05-21 20:17:22 +00:00
parent e8757347ba
commit 61bd1312a6
2 changed files with 28 additions and 10 deletions

View File

@ -79,6 +79,17 @@ var api = function(dbot) {
}); });
}, },
'getAllChannels': function(callback) {
var channels = [];
this.db.scan('channel_users', function(channel) {
channels.push(channel);
}, function(err) {
if(!err) {
callback(channels);
}
});
},
'isOnline': function(server, nick, channel, callback) { 'isOnline': function(server, nick, channel, callback) {
this.api.resolveUser(server, nick, function(user) { this.api.resolveUser(server, nick, function(user) {
var possiNicks = [user].concat(user.aliases); var possiNicks = [user].concat(user.aliases);
@ -115,6 +126,9 @@ var api = function(dbot) {
api['getAllUsers'].external = true; api['getAllUsers'].external = true;
api['getAllUsers'].extMap = [ 'callback' ]; api['getAllUsers'].extMap = [ 'callback' ];
api['getAllChannels'].external = true;
api['getAllChannels'].extMap = [ 'callback' ];
return api; return api;
}; };

View File

@ -84,7 +84,6 @@ var users = function(dbot) {
'channels': [], 'channels': [],
'aliases': [] 'aliases': []
}; };
this.internalAPI.addChannelUser(user, event.channel.name, function(err) { });
needsUpdating = true; needsUpdating = true;
} }
@ -127,26 +126,31 @@ var users = function(dbot) {
dbot.instance.addListener('366', 'users', function(event) { dbot.instance.addListener('366', 'users', function(event) {
this.api.getChannel(event.server, event.channel.name, function(channel) { this.api.getChannel(event.server, event.channel.name, function(channel) {
var checkChannelUsers = function(channel) { var checkChannelUsers = function(channel) {
_.each(event.channel.nicks, function(nick) { var i = 0;
var nick = nick.name; var checkChannelNicks = function(chanicks) {
if(i == chanicks.length) return;
var nick = chanicks[i]; i++;
this.api.resolveUser(event.server, nick, function(user) { this.api.resolveUser(event.server, nick, function(user) {
if(!user) { if(!user) {
this.internalAPI.createUser(event.server, nick, event.channel.name, function(result) { this.internalAPI.createUser(event.server, nick, event.channel.name, function(result) {
channel.users.push(result.id); this.internalAPI.addChannelUser(result, event.channel.name, function(err) {
checkChannelNicks(chanicks);
});
}.bind(this)); }.bind(this));
} else { } else {
if(!_.include(user.channels, event.channel.name)) { if(!_.include(user.channels, event.channel.name)) {
channel.users.push(user.id); this.internalAPI.addChannelUser(user, event.channel.name, function(err) {
checkChannelNicks(chanicks);
});
user.channels.push(event.channel.name); user.channels.push(event.channel.name);
this.db.save('users', user.id, user, function(err) { }); this.db.save('users', user.id, user, function(err) { });
} else {
checkChannelNicks(chanicks);
} }
} }
}.bind(this)); }.bind(this));
}, this); }.bind(this);
checkChannelNicks(_.keys(event.channel.nicks));
process.nextTick(function() {
this.db.save('channel_users', channel.id, channel, function(err) { });
}.bind(this));
}.bind(this); }.bind(this);
if(!channel) { // Channel does not yet exist if(!channel) { // Channel does not yet exist