forked from GitHub/dbot
users onLoad and nick/join listener now databankised [#272]
This commit is contained in:
parent
aa6a5fce42
commit
edcbe501e8
@ -24,40 +24,52 @@ var users = function(dbot) {
|
|||||||
}, this);
|
}, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.internalAPI = {
|
||||||
|
'createUser': function(server, nick, channel, callback) {
|
||||||
|
var id = uuid.v4();
|
||||||
|
this.db.create('users', id, {
|
||||||
|
'uuid': id,
|
||||||
|
'primaryNick': nick,
|
||||||
|
'currentNick': nick,
|
||||||
|
'server': server,
|
||||||
|
'channels': [ channel ],
|
||||||
|
'aliases': []
|
||||||
|
}, function(err, result) {
|
||||||
|
if(!err) {
|
||||||
|
dbot.api.event.emit('new_user', [ user ]);
|
||||||
|
callback(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
'addChannelUser': function(user, channelName) {
|
||||||
|
user.channels.push(channelName);
|
||||||
|
this.db.save('users', user.id, user, function(err) {
|
||||||
|
if(!err) {
|
||||||
|
this.api.getChannel(user.server, channelName, function(channel) {
|
||||||
|
channel.users.push(user.primaryNick);
|
||||||
|
this.db.save('channel_users', channel.id, channel, function(err) {
|
||||||
|
if(!err) {
|
||||||
|
dbot.api.event.emit('new_channel_user', [ user, channel]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
this.listener = function(event) {
|
this.listener = function(event) {
|
||||||
if(event.action == 'JOIN' && nick != dbot.config.name) {
|
if(event.action == 'JOIN' && nick != 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
|
||||||
var id = uuid.v4();
|
this.internalAPI.createUser(event.server, event.user, event.channel, function(result) {
|
||||||
this.db.create('users', id, {
|
user = result;
|
||||||
'uuid': id,
|
|
||||||
'primaryNick': event.user,
|
|
||||||
'currentNick': event.user,
|
|
||||||
'server': event.server,
|
|
||||||
'channels': [ event.channel ],
|
|
||||||
'aliases': []
|
|
||||||
}, function(err, result) {
|
|
||||||
if(!err) {
|
|
||||||
user = result;
|
|
||||||
dbot.api.event.emit('new_user', [ user ]);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!_.include(user.channels, event.channel)) { // User not yet channel user
|
if(!_.include(user.channels, event.channel)) { // User not yet channel user
|
||||||
user.channels.push(event.channel);
|
this.internalAPI.addChannelUser(user, event.channel);
|
||||||
this.db.save('users', user.id, user, function(err) {
|
|
||||||
if(!err) {
|
|
||||||
this.api.getChannel(event.server, event.channel, function(channel) {
|
|
||||||
channel.users.push(user.primaryNick);
|
|
||||||
this.db.save('channel_users', channel.id, channel, function(err)) {
|
|
||||||
if(!err) {
|
|
||||||
dbot.api.event.emit('new_channel_user', [ user ]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(event.action == 'NICK') {
|
} else if(event.action == 'NICK') {
|
||||||
@ -76,32 +88,43 @@ var users = function(dbot) {
|
|||||||
this.on = ['JOIN', 'NICK'];
|
this.on = ['JOIN', 'NICK'];
|
||||||
|
|
||||||
this.onLoad = function() {
|
this.onLoad = function() {
|
||||||
/* Trigger updateNickLists to stat current users in channel
|
|
||||||
dbot.instance.addListener('366', 'users', function(event) {
|
dbot.instance.addListener('366', 'users', function(event) {
|
||||||
var knownUsers = this.getServerUsers(event.server);
|
this.api.getChannel(event.server, event.channel, function(channel) {
|
||||||
if(!_.has(knownUsers.channelUsers, event.channel.name)) {
|
if(!channel) { // Channel does not yet exist
|
||||||
knownUsers.channelUsers[event.channel.name] = [];
|
var id = uuid.v4();
|
||||||
}
|
this.db.create('channel_users', id, {
|
||||||
var channelUsers = knownUsers.channelUsers[event.channel.name];
|
'uuid': id,
|
||||||
|
'server': event.server,
|
||||||
_.each(event.channel.nicks, function(nick) {
|
'name': event.channel,
|
||||||
nick = nick.name;
|
'users': []
|
||||||
if(this.api.isKnownUser(event.server, nick)) {
|
}, function(err, result) {
|
||||||
nick = this.api.resolveUser(event.server, nick);
|
if(!err) {
|
||||||
} else {
|
channel = result;
|
||||||
knownUsers.users.push(nick);
|
dbot.api.event.emit('new_channel', [ channel ]);
|
||||||
dbot.api.event.emit('new_user', [ event.server, nick ]);
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if(!_.include(channelUsers, nick)) {
|
|
||||||
channelUsers.push(nick);
|
|
||||||
}
|
|
||||||
}, this);
|
|
||||||
}.bind(this));
|
|
||||||
|
|
||||||
var connections = dbot.instance.connections;
|
_.each(event.channel.nicks, function(nick) {
|
||||||
_.each(connections, function(connection) {
|
this.api.resolveUser(event.server, nick, function(user) {
|
||||||
connection.updateNickLists();
|
if(!user) {
|
||||||
});*/
|
this.internalAPI.createUser(event.server, nick, event.channel, function(result) {
|
||||||
|
user = result;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!_.include(user.channels, event.channel)) {
|
||||||
|
this.internalAPI.addChannelUser(user, event.channel);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, this);
|
||||||
|
});
|
||||||
|
|
||||||
|
var connections = dbot.instance.connections;
|
||||||
|
_.each(connections, function(connection) {
|
||||||
|
connection.updateNickLists();
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user