From 3814df98f62a71b670addf6e7cd421c4d63b1732 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 24 Mar 2013 16:04:01 +0000 Subject: [PATCH] users module almost fully operational with databank (few kinks in moderation commands still) [#272] --- modules/users/api.js | 7 +- modules/users/commands.js | 155 +++++++++++++++++++------------------- modules/users/users.js | 115 +++++++++++++++------------- 3 files changed, 144 insertions(+), 133 deletions(-) diff --git a/modules/users/api.js b/modules/users/api.js index 7d6c30d..b187e8c 100644 --- a/modules/users/api.js +++ b/modules/users/api.js @@ -14,7 +14,8 @@ var api = function(dbot) { // Return a user record given a primary nick or an alias 'resolveUser': function(server, nick, callback) { var user = false; - this.db.search('user', { 'server': server }, function(result) { + this.db.search('users', { 'server': server }, function(result) { + console.log(result.primaryNick); if(result.primaryNick == nick || _.include(result.aliases, nick)) { user = result; } @@ -25,11 +26,11 @@ var api = function(dbot) { }); }, - 'getChannel': function(server, channel, callback) { + 'getChannel': function(server, channelName, callback) { var channel = false; this.db.search('channel_users', { 'server': server, - 'name': channel + 'name': channelName }, function(result) { channel = result; }, function(err) { diff --git a/modules/users/commands.js b/modules/users/commands.js index 57452c1..4b5fc46 100644 --- a/modules/users/commands.js +++ b/modules/users/commands.js @@ -3,98 +3,95 @@ var _ = require('underscore')._; var commands = function(dbot) { var commands = { '~alias': function(event) { - var knownUsers = this.getServerUsers(event.server), - alias = event.params[1].trim(); + var nick = event.params[1].trim(); + this.api.resolveUser(event.server, nick, function(user) { + if(user) { + if(nick == user.primaryNick) { + var aliases = _.first(user.aliases, 10); + var including = 'including: ' + aliases.join(', ') + '.'; - if(_.include(knownUsers.users, alias)) { - var aliases = this.api.getAliases(event.server, alias); - var aliasCount = aliases.length; - - if(aliasCount != 0) { - var aliases = _.first(aliases, 10); - var including = 'including: ' + aliases.join(', ') + '.'; - - event.reply(dbot.t('primary', { - 'user': alias, - 'count': aliasCount - }) + including); + if(user.aliases.length != 0) { + event.reply(dbot.t('primary', { + 'user': nick, + 'count': user.aliases.length, + }) + including); + } else { + event.reply(dbot.t('primary', { + 'user': nick, + 'count': user.aliases.length + }).slice(0, -2) + "."); + } + } else { + event.reply(dbot.t('alias', { + 'alias': nick, + 'user': user.primaryNick + })); + } } else { - event.reply(dbot.t('primary', { - 'user': alias, - 'count': aliasCount - }).slice(0, -2) + "."); + event.reply(dbot.t('unknown_alias', { 'alias': nick })); } - } else if(_.has(knownUsers.aliases, alias)) { - event.reply(dbot.t('alias', { - 'alias': alias, - 'user': knownUsers.aliases[alias] - })); - } else { - event.reply(dbot.t('unknown_alias', { 'alias': alias })); - } + }); }, '~setaliasparent': function(event) { - var knownUsers = this.getServerUsers(event.server); - var newParent = event.params[1]; + var newPrimary = event.params[1].trim(); + this.api.resolveUser(event.server, newPrimary, function(user) { + if(user) { + var newAlias = user.primaryNick; + user.primaryNick = newPrimary; + user.aliases = _.without(user.aliases, newPrimary).push(newAlias); + this.internalAPI.updateChannelPrimaryUser(event.server, newAlias, newPrimary); - if(_.has(knownUsers.aliases, newParent)) { - var newAlias = knownUsers.aliases[newParent]; - - // Replace user entry - knownUsers.users = _.without(knownUsers.users, newAlias); - knownUsers.users.push(newParent); - - // Replace channels entries with new primary user - this.updateChannels(event, newAlias, newParent); - - // Remove alias for new parent & add alias for new alias - delete knownUsers.aliases[newParent]; - knownUsers.aliases[newAlias] = newParent; - - // Update aliases to point to new primary user - this.updateAliases(event, newAlias, newParent); - - event.reply(dbot.t('aliasparentset', { - 'newParent': newParent, - 'newAlias': newAlias - })); - - return { - 'server': event.server, - 'alias': newAlias - }; - } else { - event.reply(dbot.t('unknown_alias', { 'alias': newParent })); - } - return false; + this.db.save('users', user.id, user, function(err) { + if(!err) { + dbot.api.event.emit('~setaliasparent', { + 'server': event.server, + 'alias': newAlias + }); + } + }); + } else { + event.reply(dbot.t('unknown_alias', { 'alias': newParent })); + } + }.bind(this)); }, '~mergeusers': function(event) { - var knownUsers = this.getServerUsers(event.server); var primaryUser = event.params[1]; var secondaryUser = event.params[2]; - if(_.include(knownUsers.users, primaryUser) && _.include(knownUsers.users, secondaryUser)) { - knownUsers.users = _.without(knownUsers.users, secondaryUser); - knownUsers.aliases[secondaryUser] = primaryUser; - this.updateAliases(event, secondaryUser, primaryUser); - this.updateChannels(event, secondaryUser, primaryUser); - - event.reply(dbot.t('merged_users', { - 'old_user': secondaryUser, - 'new_user': primaryUser - })); - - return { - 'server': event.server, - 'secondary': secondaryUser - }; - } else { - event.reply(dbot.t('unprimary_error')); - } - return false; - } + this.api.resolveUser(event.server, primaryUser, function(user) { + if(user) { + this.api.resolveUser(event.server, secondaryUser, function(secondaryUser) { + if(secondaryUser) { + user.aliases.push(secondaryUser.primaryNick); + user.aliases.concat(secondaryUser.aliases); + this.db.del('users', secondaryUser.id, function(err) { + if(!err) { + this.db.save('users', user.id, user, function(err) { + if(!err) { + this.updateChannels(event, secondaryUser, primaryUser); + event.reply(dbot.t('merged_users', { + 'old_user': secondaryUser, + 'new_user': primaryUser + })); + dbot.api.event.emit('~mergeusers', { + 'server': event.server, + 'secondary': secondaryUser + }); + } + }); + } + }); + } else { + event.reply(dbot.t('unprimary_error')); + } + }); + } else { + event.reply(dbot.t('unprimary_error')); + } + }); + } }; commands['~alias'].regex = [/^~alias ([\d\w[\]{}^|\\`_-]+?)/, 2]; diff --git a/modules/users/users.js b/modules/users/users.js index e83dd6b..b1d1177 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -2,28 +2,12 @@ * Name: Users * Description: Track known users */ -var _ = require('underscore')._; +var _ = require('underscore')._, + uuid = require('node-uuid'); var users = function(dbot) { - this.knownUsers = dbot.db.knownUsers; - - this.updateAliases = function(event, oldUser, newUser) { - var knownUsers = this.getServerUsers(event.server); - _.each(knownUsers.aliases, function(user, alias) { - if(user == oldUser) { - knownUsers.aliases[alias] = newUser; - } - }, this); - }; - - this.updateChannels = function(event, oldUser, newUser) { - var channelUsers = this.getServerUsers(event.server).channelUsers; - channelUsers = _.each(channelUsers, function(channel, channelName) { - channelUsers[channelName] = _.without(channel, oldUser); - channelUsers[channelName].push(newUser); - }, this); - }; + /*** Internal API ***/ this.internalAPI = { 'createUser': function(server, nick, channel, callback) { var id = uuid.v4(); @@ -36,14 +20,17 @@ var users = function(dbot) { 'aliases': [] }, function(err, result) { if(!err) { - dbot.api.event.emit('new_user', [ user ]); + dbot.api.event.emit('new_user', [ result ]); callback(result); + dbot.say('aberwiki', '#realitest', result.server); + dbot.say('aberwiki', '#realitest', result.primaryNick); } }); - }, + }.bind(this), 'addChannelUser': function(user, channelName) { user.channels.push(channelName); + dbot.say('aberwiki', '#realitest', user.id); this.db.save('users', user.id, user, function(err) { if(!err) { this.api.getChannel(user.server, channelName, function(channel) { @@ -56,46 +43,68 @@ var users = function(dbot) { }); } }); - } + }.bind(this), + + 'updateChannelPrimaryUser': function(server, oldUser, newUser) { + this.db.search('channel_users', { 'server': server }, function(channel) { + channel.users = _.without(channel.users, oldUser); + if(!_.include(channel.users, newUser)) channel.users.push(newUser); + this.db.save('channel_users', channel.id, channel, function(err) { + if(err) { + // QQ + } + }); + }.bind(this), function(err) { + if(err) { + // QQ + } + }); + }.bind(this) }; this.listener = function(event) { - if(event.action == 'JOIN' && nick != dbot.config.name) { + if(event.action == 'JOIN' && event.user != dbot.config.name) { this.api.resolveUser(event.server, event.user, function(user) { if(!user) { // User does not yet exist this.internalAPI.createUser(event.server, event.user, event.channel, function(result) { user = result; - }); - } - - if(!_.include(user.channels, event.channel)) { // User not yet channel user - this.internalAPI.addChannelUser(user, event.channel); - } - } - } else if(event.action == 'NICK') { - if(!this.api.isKnownUser(event.newNick)) { - this.api.resolveUser(event.server, event.user, function(user) { - user.aliases.push(event.newNick); - this.db.save('users', user.id, function(err) { - if(!err) { - dbot.api.event.emit('new_user_alias', [ user, event.newNick ]); + if(!_.include(user.channels, event.channel)) { // User not yet channel user + this.internalAPI.addChannelUser(user, event.channel.name); } }); - }); - } + } else { + if(!_.include(user.channels, event.channel)) { // User not yet channel user + this.internalAPI.addChannelUser(user, event.channel.name); + } + } + }.bind(this)); + } else if(event.action == 'NICK') { + this.api.isKnownUser(event.server, event.newNick, function(isKnown) { + if(!isKnown) { + this.api.resolveUser(event.server, event.user, function(user) { + dbot.say('aberwiki', '#realitest', event.newNick); + user.aliases.push(event.newNick); + this.db.save('users', user.id, user, function(err) { + if(!err) { + dbot.api.event.emit('new_user_alias', [ user, event.newNick ]); + } + }); + }.bind(this)); + } + }.bind(this)); } }.bind(this); this.on = ['JOIN', 'NICK']; this.onLoad = function() { dbot.instance.addListener('366', 'users', function(event) { - this.api.getChannel(event.server, event.channel, 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, { 'uuid': id, 'server': event.server, - 'name': event.channel, + 'name': event.channel.name, 'users': [] }, function(err, result) { if(!err) { @@ -106,24 +115,28 @@ var users = function(dbot) { } _.each(event.channel.nicks, function(nick) { + var nick = nick.name; this.api.resolveUser(event.server, nick, function(user) { 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.name); + } }); + } else { + if(!_.include(user.channels, event.channel)) { + this.internalAPI.addChannelUser(user, event.channel.name); + } } - - if(!_.include(user.channels, event.channel)) { - this.internalAPI.addChannelUser(user, event.channel); - } - }); + }.bind(this)); }, this); - }); + }.bind(this)); + }.bind(this)); - var connections = dbot.instance.connections; - _.each(connections, function(connection) { - connection.updateNickLists(); - }); + var connections = dbot.instance.connections; + _.each(connections, function(connection) { + connection.updateNickLists(); }); }; };