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) {
res.render('users', { if(!_.include(userIds, warning.warnee)) userIds.push(warning.warnee);
'name': dbot.config.name, }, function(err) {
'server': server, async.eachSeries(userIds, function(id, callback) {
'users': _.keys(this.warnings[server]) 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) { '/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) {
res.render('warnings', { var warnings = [];
'name': dbot.config.name, this.db.search('warnings', {
'server': server, '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 { }.bind(this));
res.render('error');
}
} }
}; };
}; };

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,
'reason': reason, 'warnee': warnee.id,
'time': new Date().getTime() 'warner': warner.id,
}); 'reason': reason,
'time': new Date().getTime()
// Notify interested parties }, function(err) {
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)) { });
adminChannel = event.channel.name; if(_.isUndefined(adminChannel)) {
} adminChannel = event.channel.name;
dbot.api.report.notify(server, adminChannel, notifyString); }
dbot.say(server, adminChannel, notifyString); dbot.api.report.notify(server, adminChannel, notifyString);
dbot.say(server, warnee, 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) { '~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) {
event.reply(dbot.t('warning_info', { var warnings = 0;
'user': warnee, this.db.search('warnings', {
'num': this.warnings[server][warnee].length, 'server': server,
'url': dbot.api.web.getUrl('warning/' + server + '/' + warnee) 'warnee': warnee.id
})); }, function(warning) {
} else { warnings++;
event.reply(dbot.t('no_warnings', { 'user': warnee })); }, 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));
} }
}; };