3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-23 20:39:25 +01:00

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" ],
"dbKeys": [ "warnings" ]
"dbType": "redis"
}

View File

@ -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));
}
};
};

View File

@ -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));
}
};