Rewrite warning module for databank [#508]

This commit is contained in:
reality 2013-06-23 11:37:01 +00:00
parent fe76718780
commit 4ffff09381
3 changed files with 93 additions and 55 deletions

View File

@ -1,4 +1,4 @@
{ {
"dependencies": [ "report", "users", "web" ], "dependencies": [ "report", "users", "web" ],
"dbKeys": [ "warnings" ] "dbType": "redis"
} }

View File

@ -1,4 +1,5 @@
var _ = require('underscore')._; var _ = require('underscore')._,
async = require('async');
var pages = function(dbot) { var pages = function(dbot) {
this.warnings = dbot.db.warnings; this.warnings = dbot.db.warnings;
@ -7,37 +8,59 @@ var pages = function(dbot) {
'/warning': function(req, res) { '/warning': function(req, res) {
res.render('servers', { res.render('servers', {
'name': dbot.config.name, 'name': dbot.config.name,
'servers': _.keys(this.warnings) 'servers': _.keys(dbot.config.servers)
}); });
}, },
'/warning/:server': function(req, res) { '/warning/:server': function(req, res) {
var server = req.params.server; var server = req.params.server,
userIds = [],
userNicks = [];
if(_.has(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', { res.render('users', {
'name': dbot.config.name, 'name': dbot.config.name,
'server': server, 'server': server,
'users': _.keys(this.warnings[server]) 'users': userNicks
});
});
}); });
} else {
res.render('error');
}
}, },
'/warning/:server/:user': function(req, res) { '/warning/:server/:user': function(req, res) {
var server = req.params.server, var server = req.params.server,
user = req.params.user; user = req.params.user;
if(_.has(this.warnings, server) && _.has(this.warnings[server], user)) { dbot.api.users.resolveUser(server, user, function(user) {
var warnings = [];
this.db.search('warnings', {
'server': server,
'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', { res.render('warnings', {
'name': dbot.config.name, 'name': dbot.config.name,
'server': server, 'server': server,
'warnings': this.warnings[server][user] 'warnings': warnings
}); });
} else { });
res.render('error'); });
} }.bind(this));
} }
}; };
}; };

View File

@ -1,54 +1,69 @@
var _ = require('underscore')._; var _ = require('underscore')._;
uuid = require('node-uuid');
var warning = function(dbot) { var warning = function(dbot) {
this.warnings = dbot.db.warnings;
this.commands = { this.commands = {
'~warn': function(event) { '~warn': function(event) {
var warner = event.user, var warner = event.rUser,
server = event.server, server = event.server,
warnee = dbot.api.users.resolveUser(server, event.input[1]),
reason = event.input[2], reason = event.input[2],
adminChannel = dbot.config.servers[server].admin_channel; adminChannel = dbot.config.servers[server].admin_channel;
// Store the warn dbot.api.users.resolveUser(server, event.input[1], function(warnee) {
if(!_.has(this.warnings, server)) this.warnings[server] = {}; if(warnee) {
if(!_.has(this.warnings[server], warnee)) this.warnings[server][warnee] = []; var id = uuid.v4();
this.db.save('warnings', id, {
this.warnings[server][warnee].push({ 'id': id,
'warner': warner, 'server': event.server,
'warnee': warnee.id,
'warner': warner.id,
'reason': reason, 'reason': reason,
'time': new Date().getTime() 'time': new Date().getTime()
}); }, function(err) {
// Notify interested parties
var notifyString = dbot.t('warn_notify', { var notifyString = dbot.t('warn_notify', {
'warner': warner, 'warner': warner.primaryNick,
'warnee': warnee, 'warnee': warnee.primaryNick,
'reason': reason, 'reason': reason,
'url': dbot.api.web.getUrl('warning/' + server + '/' + warnee) 'url': dbot.api.web.getUrl('warning/' + server + '/'
+ warnee.primaryNick)
}); });
if(!_.isUndefined(adminChannel)) { if(_.isUndefined(adminChannel)) {
adminChannel = event.channel.name; adminChannel = event.channel.name;
} }
dbot.api.report.notify(server, adminChannel, notifyString); dbot.api.report.notify(server, adminChannel, notifyString);
dbot.say(server, adminChannel, notifyString); dbot.say(server, adminChannel, notifyString);
dbot.say(server, warnee, notifyString); dbot.say(server, warnee.currentNick, notifyString);
});
} else {
event.reply(dbot.t('warnee_not_found', { 'user': event.input[1] }));
}
}.bind(this));
}, },
'~warnings': function(event) { '~warnings': function(event) {
var warnee = event.params[1], var warnee = event.params[1],
server = event.server; server = event.server;
if(_.has(this.warnings, server) && _.has(this.warnings[server], 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', { event.reply(dbot.t('warning_info', {
'user': warnee, 'user': warnee.primaryNick,
'num': this.warnings[server][warnee].length, 'num': warnings,
'url': dbot.api.web.getUrl('warning/' + server + '/' + warnee) 'url': dbot.api.web.getUrl('warning/' + server + '/'
+ warnee.primaryNick)
})); }));
} else { } else {
event.reply(dbot.t('no_warnings', { 'user': warnee })); event.reply(dbot.t('no_warnings', { 'user': warnee }));
} }
});
}.bind(this));
} }
}; };