rewrite JOIN users listener

This commit is contained in:
reality 2013-06-22 16:25:31 +00:00
parent b3b91f189f
commit 66f5b1df43

View File

@ -41,16 +41,20 @@ var users = function(dbot) {
}); });
}.bind(this), }.bind(this),
'addChannelUser': function(user, channelName, callback) { 'addChannelUser': function(channel, user, callback) {
this.api.getChannel(user.server, channelName, function(channel) { if(!_.include(channel.users, user.id)) {
channel.users.push(user.id); channel.users.push(user.id);
}
if(!_.include(user.channels, channel.id) {
user.channels.push(channel.id);
}
this.db.save('users', user.id, user, function(err) {
this.db.save('channel_users', channel.id, channel, function(err) { this.db.save('channel_users', channel.id, channel, function(err) {
if(!err) { dbot.api.event.emit('new_channel_user', [ user, channel ]);
dbot.api.event.emit('new_channel_user', [ user, channel ]); callback(err);
callback();
}
}); });
}.bind(this)); });
}.bind(this), }.bind(this),
'updateChannelPrimaryUser': function(server, oldUser, newUser) { 'updateChannelPrimaryUser': function(server, oldUser, newUser) {
@ -90,52 +94,26 @@ var users = function(dbot) {
}; };
this.listener = function(event) { this.listener = function(event) {
// TODO: If user joins channel with alias
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) { if(!event.rUser) {
var needsUpdating = false; this.internalAPI.createUser(event.server, event.user, event.channel.name, function(user) {
this.internalAPI.addChannelUser(channel, user, function() {});
if(!user) { // User does not yet exist });
user = { } else if(!_.include(event.rUser.channels, event.rChannel.id)) {
'id': uuid.v4(), this.internalAPI.addChannelUser(channel, user, function() {});
'primaryNick': event.user, }
'currentNick': event.user,
'server': event.server,
'channels': [],
'aliases': []
};
needsUpdating = true;
}
if(!_.include(user.channels, event.channel.name)) { // User not yet channel user
user.channels.push(event.channel.name);
this.internalAPI.addChannelUser(user, event.channel.name, function(err) { });
needsUpdating = true;
}
if(user.currentNick != event.user) {
user.currentNick = event.user;
needsUpdating = true;
}
if(needsUpdating == true) {
this.db.save('users', user.id, user, function(err) { });
}
}.bind(this));
} else if(event.action == 'NICK') { } else if(event.action == 'NICK') {
this.api.resolveUser(event.server, event.user, function(user) { this.api.isKnownUser(event.server, event.newNick, function(isKnown) {
this.api.isKnownUser(event.server, event.newNick, function(isKnown) { event.rUser.currentNick = event.newNick;
user.currentNick = event.newNick;
if(!isKnown) { if(!isKnown) {
user.aliases.push(event.newNick); event.rUser.aliases.push(event.newNick);
} }
this.db.save('users', user.id, user, function(err) { this.db.save('users', event.rUser.id, event.rUser, function(err) {
if(!err) { dbot.api.event.emit('new_user_alias', [ event.rUser, event.newNick ]);
dbot.api.event.emit('new_user_alias', [ user, event.newNick ]); });
}
});
}.bind(this));
}.bind(this)); }.bind(this));
} }
}.bind(this); }.bind(this);
@ -182,8 +160,7 @@ var users = function(dbot) {
}; };
}.bind(this)); }.bind(this));
var connections = dbot.instance.connections; _.each(dbot.instance.connections, function(connection) {
_.each(connections, function(connection) {
connection.updateNickLists(); connection.updateNickLists();
}); });
}; };