From f69f99911878e37969152fcc5c05ca4fb5c754cd Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 26 May 2013 13:11:09 +0000 Subject: [PATCH] fix various mergeusers hooks [#472] --- modules/poll/config.json | 2 +- modules/poll/poll.js | 28 ++++++++++++++++------------ modules/users/commands.js | 11 ++++++----- modules/users/users.js | 4 ++-- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/modules/poll/config.json b/modules/poll/config.json index 6047e33..ee19c00 100644 --- a/modules/poll/config.json +++ b/modules/poll/config.json @@ -2,7 +2,7 @@ "dbType": "redis", "dbKeys": [ "polls" ], "ignorable": true, - "dependencies": [ "users", "web" ], + "dependencies": [ "users", "web", "event" ], "schema": { "poll": { "pkey": "name", diff --git a/modules/poll/poll.js b/modules/poll/poll.js index d54e8f1..13bebcf 100644 --- a/modules/poll/poll.js +++ b/modules/poll/poll.js @@ -1,22 +1,26 @@ var poll = function(dbot) { this.internalAPI = { - 'updatePollNicks': function(server, oldNick) { - var newNick = dbot.api.users.resolveUser(server, oldNick); - _.each(dbot.db.polls, function(poll) { - if(poll.owner === oldNick) { - poll.owner = newNick; + 'updatePollNicks': function(server, oldUser, newUser) { + this.db.scan('poll', function(poll) { + var needsUpdating = false; + if(poll.owner == oldUser.id) { + poll.owner = newUser.id; + needsUpdating = true; } - if(_.has(poll.votees, oldNick)) { - poll.votees[newNick] = poll.votees[oldNick]; - delete poll.votees[oldNick]; + if(_.has(poll.votees, oldUser.id)) { + poll.votes[poll.votees[oldUser.id]]--; + delete poll.votees[oldUser.id]; + needsUpdating = true; } - }, this); - } + if(needsUpdating) { + this.db.save('poll', poll.name, poll, function(err) {}); + } + }.bind(this), function(err) {}); + }.bind(this) }; this.onLoad = function() { - // dbot.api.command.addHook('~setaliasparent', this.internalAPI.updatePollNicks); - // dbot.api.command.addHook('~mergeusers', this.internalAPI.updatePollNicks); + dbot.api.event.addHook('~mergeusers', this.internalAPI.updatePollNicks); }.bind(this); }; diff --git a/modules/users/commands.js b/modules/users/commands.js index 1a96e53..ac78de1 100644 --- a/modules/users/commands.js +++ b/modules/users/commands.js @@ -98,7 +98,7 @@ var commands = function(dbot) { if(secondaryUser) { user.aliases.push(oldUser.primaryNick); user.aliases.concat(oldUser.aliases); - this.internalAPI.mergeChannelUsers(event.server, oldUser, primaryUser); + this.internalAPI.mergeChannelUsers(event.server, oldUser, user); this.db.del('users', oldUser.id, function(err) { if(!err) { this.db.save('users', user.id, user, function(err) { @@ -108,10 +108,11 @@ var commands = function(dbot) { 'old_user': secondaryUser, 'new_user': primaryUser })); - dbot.api.event.emit('~mergeusers', { - 'server': event.server, - 'secondary': secondaryUser - }); + dbot.api.event.emit('~mergeusers', [ + event.server, + oldUser, + user + ]); } }.bind(this)); } diff --git a/modules/users/users.js b/modules/users/users.js index 5612521..4d372ef 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -84,9 +84,9 @@ var users = function(dbot) { // QQ } }); - }); + }.bind(this)); }, this); - } + }.bind(this) }; this.listener = function(event) {