fix various mergeusers hooks [#472]

This commit is contained in:
reality 2013-05-26 13:11:09 +00:00
parent 5dddae4f00
commit f69f999118
4 changed files with 25 additions and 20 deletions

View File

@ -2,7 +2,7 @@
"dbType": "redis", "dbType": "redis",
"dbKeys": [ "polls" ], "dbKeys": [ "polls" ],
"ignorable": true, "ignorable": true,
"dependencies": [ "users", "web" ], "dependencies": [ "users", "web", "event" ],
"schema": { "schema": {
"poll": { "poll": {
"pkey": "name", "pkey": "name",

View File

@ -1,22 +1,26 @@
var poll = function(dbot) { var poll = function(dbot) {
this.internalAPI = { this.internalAPI = {
'updatePollNicks': function(server, oldNick) { 'updatePollNicks': function(server, oldUser, newUser) {
var newNick = dbot.api.users.resolveUser(server, oldNick); this.db.scan('poll', function(poll) {
_.each(dbot.db.polls, function(poll) { var needsUpdating = false;
if(poll.owner === oldNick) { if(poll.owner == oldUser.id) {
poll.owner = newNick; poll.owner = newUser.id;
needsUpdating = true;
} }
if(_.has(poll.votees, oldNick)) { if(_.has(poll.votees, oldUser.id)) {
poll.votees[newNick] = poll.votees[oldNick]; poll.votes[poll.votees[oldUser.id]]--;
delete poll.votees[oldNick]; 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() { this.onLoad = function() {
// dbot.api.command.addHook('~setaliasparent', this.internalAPI.updatePollNicks); dbot.api.event.addHook('~mergeusers', this.internalAPI.updatePollNicks);
// dbot.api.command.addHook('~mergeusers', this.internalAPI.updatePollNicks);
}.bind(this); }.bind(this);
}; };

View File

@ -98,7 +98,7 @@ var commands = function(dbot) {
if(secondaryUser) { if(secondaryUser) {
user.aliases.push(oldUser.primaryNick); user.aliases.push(oldUser.primaryNick);
user.aliases.concat(oldUser.aliases); 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) { this.db.del('users', oldUser.id, function(err) {
if(!err) { if(!err) {
this.db.save('users', user.id, user, function(err) { this.db.save('users', user.id, user, function(err) {
@ -108,10 +108,11 @@ var commands = function(dbot) {
'old_user': secondaryUser, 'old_user': secondaryUser,
'new_user': primaryUser 'new_user': primaryUser
})); }));
dbot.api.event.emit('~mergeusers', { dbot.api.event.emit('~mergeusers', [
'server': event.server, event.server,
'secondary': secondaryUser oldUser,
}); user
]);
} }
}.bind(this)); }.bind(this));
} }

View File

@ -84,9 +84,9 @@ var users = function(dbot) {
// QQ // QQ
} }
}); });
}); }.bind(this));
}, this); }, this);
} }.bind(this)
}; };
this.listener = function(event) { this.listener = function(event) {