diff --git a/modules/warning/config.json b/modules/warning/config.json index 914c1d7..9fb677f 100644 --- a/modules/warning/config.json +++ b/modules/warning/config.json @@ -1,4 +1,4 @@ { "dependencies": [ "report", "users", "web" ], - "dbKeys": [ "warnings" ] + "dbType": "redis" } diff --git a/modules/warning/pages.js b/modules/warning/pages.js index e60a634..72c5129 100644 --- a/modules/warning/pages.js +++ b/modules/warning/pages.js @@ -1,4 +1,5 @@ -var _ = require('underscore')._; +var _ = require('underscore')._, + async = require('async'); var pages = function(dbot) { this.warnings = dbot.db.warnings; @@ -7,37 +8,59 @@ var pages = function(dbot) { '/warning': function(req, res) { res.render('servers', { 'name': dbot.config.name, - 'servers': _.keys(this.warnings) + 'servers': _.keys(dbot.config.servers) }); }, '/warning/:server': function(req, res) { - var server = req.params.server; + var server = req.params.server, + userIds = [], + userNicks = []; - if(_.has(this.warnings, server)) { - res.render('users', { - 'name': dbot.config.name, - 'server': server, - 'users': _.keys(this.warnings[server]) + this.db.search('warnings', { 'server': server }, function(warning) { + if(!_.include(userIds, warning.warnee)) userIds.push(warning.warnee); + }, function(err) { + async.eachSeries(userIds, function(id, callback) { + dbot.api.users.getUser(id, function(user) { + userNicks.push(user.primaryNick); + callback(false); + }); + }, function(err) { + res.render('users', { + 'name': dbot.config.name, + 'server': server, + 'users': userNicks + }); }); - } else { - res.render('error'); - } + }); }, '/warning/:server/:user': function(req, res) { var server = req.params.server, user = req.params.user; - if(_.has(this.warnings, server) && _.has(this.warnings[server], user)) { - res.render('warnings', { - 'name': dbot.config.name, + dbot.api.users.resolveUser(server, user, function(user) { + var warnings = []; + this.db.search('warnings', { 'server': server, - 'warnings': this.warnings[server][user] + 'warnee': user.id + }, function(warning) { + warnings.push(warning); + }, function(err) { + async.eachSeries(warnings, function(warning, callback) { + dbot.api.users.getUser(warning.warner, function(user) { + warning.warner = user.primaryNick; + callback(false); + }); + }, function(err) { + res.render('warnings', { + 'name': dbot.config.name, + 'server': server, + 'warnings': warnings + }); + }); }); - } else { - res.render('error'); - } + }.bind(this)); } }; }; diff --git a/modules/warning/warning.js b/modules/warning/warning.js index cbea752..34398e0 100644 --- a/modules/warning/warning.js +++ b/modules/warning/warning.js @@ -1,54 +1,69 @@ var _ = require('underscore')._; + uuid = require('node-uuid'); var warning = function(dbot) { - this.warnings = dbot.db.warnings; - this.commands = { '~warn': function(event) { - var warner = event.user, + var warner = event.rUser, server = event.server, - warnee = dbot.api.users.resolveUser(server, event.input[1]), reason = event.input[2], adminChannel = dbot.config.servers[server].admin_channel; - // Store the warn - if(!_.has(this.warnings, server)) this.warnings[server] = {}; - if(!_.has(this.warnings[server], warnee)) this.warnings[server][warnee] = []; - - this.warnings[server][warnee].push({ - 'warner': warner, - 'reason': reason, - 'time': new Date().getTime() - }); - - // Notify interested parties - var notifyString = dbot.t('warn_notify', { - 'warner': warner, - 'warnee': warnee, - 'reason': reason, - 'url': dbot.api.web.getUrl('warning/' + server + '/' + warnee) - }); - if(!_.isUndefined(adminChannel)) { - adminChannel = event.channel.name; - } - dbot.api.report.notify(server, adminChannel, notifyString); - dbot.say(server, adminChannel, notifyString); - dbot.say(server, warnee, notifyString); + dbot.api.users.resolveUser(server, event.input[1], function(warnee) { + if(warnee) { + var id = uuid.v4(); + this.db.save('warnings', id, { + 'id': id, + 'server': event.server, + 'warnee': warnee.id, + 'warner': warner.id, + 'reason': reason, + 'time': new Date().getTime() + }, function(err) { + var notifyString = dbot.t('warn_notify', { + 'warner': warner.primaryNick, + 'warnee': warnee.primaryNick, + 'reason': reason, + 'url': dbot.api.web.getUrl('warning/' + server + '/' + + warnee.primaryNick) + }); + if(_.isUndefined(adminChannel)) { + adminChannel = event.channel.name; + } + dbot.api.report.notify(server, adminChannel, notifyString); + dbot.say(server, adminChannel, notifyString); + dbot.say(server, warnee.currentNick, notifyString); + }); + } else { + event.reply(dbot.t('warnee_not_found', { 'user': event.input[1] })); + } + }.bind(this)); }, '~warnings': function(event) { var warnee = event.params[1], server = event.server; - if(_.has(this.warnings, server) && _.has(this.warnings[server], warnee)) { - event.reply(dbot.t('warning_info', { - 'user': warnee, - 'num': this.warnings[server][warnee].length, - 'url': dbot.api.web.getUrl('warning/' + server + '/' + warnee) - })); - } else { - event.reply(dbot.t('no_warnings', { 'user': warnee })); - } + dbot.api.users.resolveUser(server, warnee, function(warnee) { + var warnings = 0; + this.db.search('warnings', { + 'server': server, + 'warnee': warnee.id + }, function(warning) { + warnings++; + }, function(err) { + if(warnings > 0) { + event.reply(dbot.t('warning_info', { + 'user': warnee.primaryNick, + 'num': warnings, + 'url': dbot.api.web.getUrl('warning/' + server + '/' + + warnee.primaryNick) + })); + } else { + event.reply(dbot.t('no_warnings', { 'user': warnee })); + } + }); + }.bind(this)); } };