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) {
this.api.resolveUser(server, nick, function(user) {
var possiNicks = [user].concat(user.aliases);
@ -115,6 +126,9 @@ var api = function(dbot) {
api['getAllUsers'].external = true;
api['getAllUsers'].extMap = [ 'callback' ];
api['getAllChannels'].external = true;
api['getAllChannels'].extMap = [ 'callback' ];
return api;
};

View File

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