From 1037070bff607584eac142f974d125cd09d339ef Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 4 Sep 2014 12:15:11 +0000 Subject: [PATCH] fixes for merge and setaliasparent, basic sstats functionality --- modules/sstats/commands.js | 69 ++++++++++++++++++-------------------- modules/sstats/sstats.js | 37 ++++++++++---------- modules/users/commands.js | 12 +++---- modules/users/users.js | 29 ++++++++++------ 4 files changed, 73 insertions(+), 74 deletions(-) diff --git a/modules/sstats/commands.js b/modules/sstats/commands.js index 7176198..20e71c9 100644 --- a/modules/sstats/commands.js +++ b/modules/sstats/commands.js @@ -5,46 +5,41 @@ var _ = require('underscore')._, var commands = function(dbot) { var commands = { '~words': function(event) { - var getWords = function(user) { - this.api.getUserStats(user.id, function(uStats) { - if(uStats) { - var output = dbot.t('sstats_uwords', { - 'user': user.primaryNick, - 'words': uStats.words, - 'curses': uStats.curses, - 'capitals': uStats.capitals, - 'date': moment(uStats.creation).format('DD/MM/YYYY') - }); - if(event.rChannel && _.has(uStats.channels, event.rChannel.id)) { - ucStats = uStats.channels[event.rChannel.id]; - output += dbot.t('sstats_ucwords', { - 'channel': event.channel, - 'words': ucStats.words, - 'curses': ucStats.curses, - 'capitals': ucStats.capitals - }); - } - event.reply(output); - } else { - event.reply(dbot.t('sstats_noustats')); - } - }); - }.bind(this); - + var id = event.rUser.primaryNick + '.' + event.server, + user = event.rUser.currentNick, + cId = event.channel + '.' + event.server; if(event.params[1]) { - dbot.api.users.resolveUser(event.server, event.params[1], function(user) { - if(user) { - getWords(user); - } else { - event.reply(dbot.t('sstats_unknown_user')); - } - }); - } else { - getWords(event.rUser); + id = event.params[1] + '.' + event.server; + user = event.params[1]; } + + this.api.getUserStats(id, function(uStats) { + if(uStats) { + var output = dbot.t('sstats_uwords', { + 'user': user, + 'words': uStats.words, + 'curses': uStats.curses, + 'capitals': uStats.capitals, + 'date': moment(uStats.creation).format('DD/MM/YYYY') + }); + if(_.has(uStats.channels, cId)) { + ucStats = uStats.channels[cId]; + output += dbot.t('sstats_ucwords', { + 'channel': event.channel, + 'words': ucStats.words, + 'curses': ucStats.curses, + 'capitals': ucStats.capitals + }); + } + event.reply(output); + } else { + event.reply(dbot.t('sstats_noustats')); + } + }); }, '~lines': function(event) { + var cId = event.channel + '.' + event.server; var getLines = function(user) { this.api.getUserStats(user.id, function(uStats) { if(uStats) { @@ -53,10 +48,10 @@ var commands = function(dbot) { 'lines': uStats.lines, 'date': moment(uStats.creation).format('DD/MM/YYYY') }); - if(event.rChannel && _.has(uStats.channels, event.rChannel.id)) { + if(_.has(uStats.channels, cId)) { output += dbot.t('sstats_uclines', { 'channel': event.channel, - 'lines': uStats.channels[event.rChannel.id].lines + 'lines': uStats.channels[cId].lines }); } event.reply(output); diff --git a/modules/sstats/sstats.js b/modules/sstats/sstats.js index cc9e70b..504c24f 100644 --- a/modules/sstats/sstats.js +++ b/modules/sstats/sstats.js @@ -44,7 +44,7 @@ var sstats = function(dbot) { .value(); async.eachSeries(pCounts, function(pCount, next) { - dbot.api.users.getUser(pCount[0], function(user) { + dbot.api.users.getUser(pCount[0], function(err, user) { pCount[0] = user.primaryNick; next(); }); }, function() { @@ -54,14 +54,9 @@ var sstats = function(dbot) { }.bind(this), 'channelHighscore': function(key, server, channel, property, callback) { - dbot.api.users.resolveChannel(server, channel, function(channel) { - if(channel) { - var newProperty = 'channels.' + channel.id + '.' + property; - this.internalAPI.highscore(key, newProperty, callback); - } else { - callback(null); - } - }.bind(this)); + var cId = channel + '.' + server; + var newProperty = 'channels.' + cId + '.' + property; + this.internalAPI.highscore(key, newProperty, callback); }.bind(this), 'formatHighscore': function(string, pCounts) { @@ -78,6 +73,7 @@ var sstats = function(dbot) { event.uStats.lines++; var message = event.message.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g, ""); + var cId = event.channel + '.' + event.server; var words = message.split(' '), wCount = words.length, capitals = 0, @@ -98,18 +94,18 @@ var sstats = function(dbot) { event.cStats.capitals += capitals; event.cStats.curses += curses; - if(!_.has(event.uStats.channels, event.rChannel.id)) { - event.uStats.channels[event.rChannel.id] = { + if(!_.has(event.uStats.channels, cId)) { + event.uStats.channels[cId] = { 'lines': 1, 'words': wCount, 'capitals': capitals, 'curses': curses }; } else { - event.uStats.channels[event.rChannel.id].lines++; - event.uStats.channels[event.rChannel.id].words += wCount; - event.uStats.channels[event.rChannel.id].capitals += capitals; - event.uStats.channels[event.rChannel.id].curses += curses; + event.uStats.channels[cId].lines++; + event.uStats.channels[cId].words += wCount; + event.uStats.channels[cId].capitals += capitals; + event.uStats.channels[cId].curses += curses; } // Look for tracked words. @@ -124,9 +120,9 @@ var sstats = function(dbot) { this.api.getTrackedWord(word, function(tWord) { if(tWord) { tWord.total += count; - if(!_.has(tWord.channels, event.rChannel.id)) tWord.channels[event.rChannel.id] = 0; + if(!_.has(tWord.channels, cId)) tWord.channels[cId] = 0; if(!_.has(tWord.users, event.rUser.id)) tWord.users[event.rUser.id] = 0; - tWord.channels[event.rChannel.id] += count; + tWord.channels[cId] += count; tWord.users[event.rUser.id] += count; this.db.save('tracked_words', word, tWord, function() {}); } @@ -158,13 +154,14 @@ var sstats = function(dbot) { // Preload channel stats dbot.instance.addPreEmitHook(function(event, callback) { - if(!event.rChannel) return callback(); - this.api.getChannelStats(event.rChannel.id, function(cStats) { + if(!event.channel) return callback(); + var id = event.channel + '.' + event.server; + this.api.getChannelStats(id, function(cStats) { if(cStats) { event.cStats = cStats; callback(); } else { - this.api.createChannelStats(event.rChannel.id, function(cStats) { + this.api.createChannelStats(id, function(cStats) { event.cStats = cStats; callback(); }); diff --git a/modules/users/commands.js b/modules/users/commands.js index 2d4cd41..0bfcb14 100644 --- a/modules/users/commands.js +++ b/modules/users/commands.js @@ -65,7 +65,7 @@ var commands = function(dbot) { '~setaliasparent': function(event) { var newPrimary = event.params[1]; - this.api.resolveUser(event.server, newPrimary, function(user) { + this.api.resolveUser(event.server, newPrimary, function(err, user) { if(user) { if(user.primaryNick !== newPrimary) { this.internalAPI.reparentUser(user, newPrimary, function() { @@ -78,9 +78,9 @@ var commands = function(dbot) { event.reply(dbot.t('already_primary', { 'user': newPrimary })); } } else { - event.reply(dbot.t('unknown_alias', { 'alias': nick })); + event.reply(dbot.t('unknown_alias', { 'alias': newPrimary })); } - }); + }.bind(this)); }, '~rmalias': function(event) { @@ -107,11 +107,11 @@ var commands = function(dbot) { this.api.resolveUser(event.server, oldNick, function(err, oldUser) { if(oldUser) { this.api.resolveUser(event.server, newNick, function(err, newUser) { - if(newUser) { + if(newUser && newUser.id !== oldUser.id) { this.internalAPI.mergeUsers(oldUser, newUser, function() { event.reply(dbot.t('merged_users', { - 'old_user': secondaryUser, - 'new_user': primaryUser + 'old_user': oldNick, + 'new_user': newNick })); }); } else { diff --git a/modules/users/users.js b/modules/users/users.js index 1cbf32f..ad76cfc 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -35,7 +35,7 @@ var users = function(dbot) { 'user': user.id }, function(err, result) { if(!err) { - dbot.api.event.emit('new_user_alias', [ event.rUser, event.newNick ]); + dbot.api.event.emit('new_user_alias', [ user, alias ]); callback(null, result); } else { callback(true, null); @@ -67,9 +67,15 @@ var users = function(dbot) { // Merge two user records and aliases 'mergeUsers': function(oldUser, newUser, callback) { this.db.search('user_aliases', { 'user': oldUser.id }, function(alias) { - alias.user = newUser.id; - this.db.save('user_aliases', alias.id, alias, function(){}); - }.bind(this), function(){}); + if(alias.alias === newUser.primaryNick) { + this.db.del('user_aliases', alias.id, function(){}); + } else { + alias.user = newUser.id; + this.db.save('user_aliases', alias.id, alias, function(){}); + } + }.bind(this), function(){ + this.internalAPI.createAlias(oldUser.primaryNick, newUser, function(){}); + }.bind(this)); this.db.del('users', oldUser.id, function(err) { if(!err) { @@ -99,13 +105,14 @@ var users = function(dbot) { // Track nick changes this.listener = function(event) { // Update current nick - this.internalAPI.updateCurrentNick(event.rUser, event.newNick, function(){}); - - // Add new alias record if nick is not already claimed - this.api.resolveUser(event.server, event.newNick, function(err, user) { - if(!user) { - this.internalAPI.createAlias(event.newNick, event.rUser, function(){}); - } + this.api.resolveUser(event.server, event.user, function(err, user) { + console.log(user); + this.internalAPI.updateCurrentNick(user, event.newNick, function(){}); + this.api.resolveUser(event.server, event.newNick, function(err, eUser) { + if(!eUser) { + this.internalAPI.createAlias(event.newNick, user, function(){}); + } + }.bind(this)); }.bind(this)); }.bind(this); this.on = ['NICK'];