modify data structures and fix a bunch of broken async stuff in users [#331]

This commit is contained in:
reality 2013-04-14 20:00:53 +00:00
parent 77f67d64e1
commit 5420bd846b
2 changed files with 56 additions and 46 deletions

View File

@ -139,6 +139,12 @@ var api = function(dbot) {
} }
}; };
api['resolveUser'].external = true;
api['resolveUser'].extMap = [ 'server', 'nick', 'callback' ];
api['getChannel'].external = true;
api['getChannel'].extMap = [ 'server', 'channel', 'callback' ];
return api; return api;
}; };

View File

@ -26,20 +26,31 @@ var users = function(dbot) {
}); });
}.bind(this), }.bind(this),
'addChannelUser': function(user, channelName) { 'createChannel': function(server, name, callback) {
user.channels.push(channelName); var id = uuid.v4();
this.db.save('users', user.id, user, function(err) { this.db.create('channel_users', id, {
'id': id,
'server': server,
'name': name,
'users': []
}, function(err, result) {
if(!err) { if(!err) {
dbot.api.event.emit('new_channel', [ result ]);
callback(result);
}
});
}.bind(this),
'addChannelUser': function(user, channelName, callback) {
this.api.getChannel(user.server, channelName, function(channel) { this.api.getChannel(user.server, channelName, function(channel) {
channel.users.push(user.primaryNick); channel.users.push(user.id);
this.db.save('channel_users', channel.id, channel, function(err) { this.db.save('channel_users', channel.id, channel, function(err) {
if(!err) { if(!err) {
dbot.api.event.emit('new_channel_user', [ user, channel ]); dbot.api.event.emit('new_channel_user', [ user, channel ]);
callback();
} }
}); });
}.bind(this)); }.bind(this));
}
}.bind(this));
}.bind(this), }.bind(this),
'updateChannelPrimaryUser': function(server, oldUser, newUser) { 'updateChannelPrimaryUser': function(server, oldUser, newUser) {
@ -63,15 +74,13 @@ var users = function(dbot) {
if(event.action == 'JOIN' && event.user != dbot.config.name) { if(event.action == 'JOIN' && event.user != dbot.config.name) {
this.api.resolveUser(event.server, event.user, function(user) { this.api.resolveUser(event.server, event.user, function(user) {
if(!user) { // User does not yet exist if(!user) { // User does not yet exist
this.internalAPI.createUser(event.server, event.user, event.channel, function(result) { this.internalAPI.createUser(event.server, event.user, event.channel.name, function(result) {
user = result; this.internalAPI.addChannelUser(result, event.channel.name, function(err) { });
if(!_.include(user.channels, event.channel)) { // User not yet channel user }.bind(this));
this.internalAPI.addChannelUser(user, event.channel.name);
}
});
} else { } else {
if(!_.include(user.channels, event.channel)) { // User not yet channel user if(!_.include(user.channels, event.channel.name)) { // User not yet channel user
this.internalAPI.addChannelUser(user, event.channel.name); users.channels.push(event.channel.name);
this.internalAPI.addChannelUser(user, event.channel.name, function(err) { });
} }
user.currentNick = event.user; user.currentNick = event.user;
@ -105,38 +114,33 @@ var users = function(dbot) {
this.onLoad = function() { this.onLoad = function() {
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) {
if(!channel) { // Channel does not yet exist
var id = uuid.v4();
this.db.create('channel_users', id, {
'id': id,
'server': event.server,
'name': event.channel.name,
'users': []
}, function(err, result) {
if(!err) {
channel = result;
dbot.api.event.emit('new_channel', [ channel ]);
}
});
}
var checkChannelUsers = function(channel) {
_.each(event.channel.nicks, function(nick) { _.each(event.channel.nicks, function(nick) {
var nick = nick.name; var nick = nick.name;
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, function(result) { this.internalAPI.createUser(event.server, nick, event.channel.name, function(result) {
user = result; channel.users.push(result.id);
if(!_.include(user.channels, event.channel)) { }.bind(this));
this.internalAPI.addChannelUser(user, event.channel.name);
}
});
} else { } else {
if(!_.include(user.channels, event.channel)) { if(!_.include(user.channels, event.channel)) {
this.internalAPI.addChannelUser(user, event.channel.name); channel.users.push(user.id);
} }
} }
}.bind(this)); }.bind(this));
}, this); }, this);
process.nextTick(function() {
this.db.save('channel_users', channel.id, channel, function(err) { });
}.bind(this));
}.bind(this);
if(!channel) { // Channel does not yet exist
this.internalAPI.createChannel(event.server, event.channel.name, checkChannelUsers);
} else {
checkChannelUsers(channel);
}
}.bind(this)); }.bind(this));
}.bind(this)); }.bind(this));