users module almost fully operational with databank (few kinks in moderation commands still) [#272]

This commit is contained in:
reality 2013-03-24 16:04:01 +00:00
parent ef9f5f2646
commit 3814df98f6
3 changed files with 144 additions and 133 deletions

View File

@ -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) {

View File

@ -3,97 +3,94 @@ var _ = require('underscore')._;
var commands = function(dbot) {
var commands = {
'~alias': function(event) {
var knownUsers = this.getServerUsers(event.server),
alias = event.params[1].trim();
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 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(user.aliases.length != 0) {
event.reply(dbot.t('primary', {
'user': alias,
'count': aliasCount
'user': nick,
'count': user.aliases.length,
}) + including);
} else {
event.reply(dbot.t('primary', {
'user': alias,
'count': aliasCount
'user': nick,
'count': user.aliases.length
}).slice(0, -2) + ".");
}
} 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 }));
event.reply(dbot.t('alias', {
'alias': nick,
'user': user.primaryNick
}));
}
} else {
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
}
});
},
'~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 {
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 }));
}
return false;
}.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.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
}));
return {
dbot.api.event.emit('~mergeusers', {
'server': event.server,
'secondary': secondaryUser
};
});
}
});
}
});
} else {
event.reply(dbot.t('unprimary_error'));
}
return false;
});
} else {
event.reply(dbot.t('unprimary_error'));
}
});
}
};

View File

@ -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);
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') {
if(!this.api.isKnownUser(event.newNick)) {
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, function(err) {
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,25 +115,29 @@ 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);
this.internalAPI.addChannelUser(user, event.channel.name);
}
});
} else {
if(!_.include(user.channels, event.channel)) {
this.internalAPI.addChannelUser(user, event.channel.name);
}
}
}.bind(this));
}, this);
});
}.bind(this));
}.bind(this));
var connections = dbot.instance.connections;
_.each(connections, function(connection) {
connection.updateNickLists();
});
});
};
};