forked from GitHub/dbot
modify data structures and fix a bunch of broken async stuff in users [#331]
This commit is contained in:
parent
77f67d64e1
commit
5420bd846b
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -26,19 +26,30 @@ 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) {
|
||||||
this.api.getChannel(user.server, channelName, function(channel) {
|
dbot.api.event.emit('new_channel', [ result ]);
|
||||||
channel.users.push(user.primaryNick);
|
callback(result);
|
||||||
this.db.save('channel_users', channel.id, channel, function(err) {
|
|
||||||
if(!err) {
|
|
||||||
dbot.api.event.emit('new_channel_user', [ user, channel]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}.bind(this));
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}.bind(this),
|
||||||
|
|
||||||
|
'addChannelUser': function(user, channelName, callback) {
|
||||||
|
this.api.getChannel(user.server, channelName, function(channel) {
|
||||||
|
channel.users.push(user.id);
|
||||||
|
this.db.save('channel_users', channel.id, channel, function(err) {
|
||||||
|
if(!err) {
|
||||||
|
dbot.api.event.emit('new_channel_user', [ user, channel ]);
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
});
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}.bind(this),
|
}.bind(this),
|
||||||
|
|
||||||
@ -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 ]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
_.each(event.channel.nicks, function(nick) {
|
var checkChannelUsers = function(channel) {
|
||||||
var nick = nick.name;
|
_.each(event.channel.nicks, function(nick) {
|
||||||
this.api.resolveUser(event.server, nick, function(user) {
|
var nick = nick.name;
|
||||||
if(!user) {
|
this.api.resolveUser(event.server, nick, function(user) {
|
||||||
this.internalAPI.createUser(event.server, nick, event.channel, function(result) {
|
if(!user) {
|
||||||
user = result;
|
this.internalAPI.createUser(event.server, nick, event.channel.name, function(result) {
|
||||||
|
channel.users.push(result.id);
|
||||||
|
}.bind(this));
|
||||||
|
} 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);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} else {
|
|
||||||
if(!_.include(user.channels, event.channel)) {
|
|
||||||
this.internalAPI.addChannelUser(user, event.channel.name);
|
|
||||||
}
|
}
|
||||||
}
|
}.bind(this));
|
||||||
|
}, this);
|
||||||
|
|
||||||
|
process.nextTick(function() {
|
||||||
|
this.db.save('channel_users', channel.id, channel, function(err) { });
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}, 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));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user