From 61bd1312a6fde11ab4f4a4569cecf11b030d02a7 Mon Sep 17 00:00:00 2001 From: reality Date: Tue, 21 May 2013 20:17:22 +0000 Subject: [PATCH] Fix fucking async hell and add getAllChannels --- modules/users/api.js | 14 ++++++++++++++ modules/users/users.js | 24 ++++++++++++++---------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/modules/users/api.js b/modules/users/api.js index 1d5ffc3..8fc537a 100644 --- a/modules/users/api.js +++ b/modules/users/api.js @@ -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; }; diff --git a/modules/users/users.js b/modules/users/users.js index 03d2fc5..b40f937 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -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