3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-18 08:32:42 +01:00
dbot/modules/report/api.js

89 lines
3.7 KiB
JavaScript
Raw Normal View History

2013-10-21 17:40:45 +02:00
var uuid = require('node-uuid'),
_ = require('underscore')._,
async = require('async');
var api = function(dbot) {
var api = {
'notify': function(type, server, user, cName, message) {
2014-05-25 20:24:13 +02:00
var id = uuid.v4(),
tags = [];
_.each(message.match(/(#\w+)/g), function(match) {
tags.push(match.toLowerCase());
});
2013-10-21 17:40:45 +02:00
this.db.save('notifies', id, {
'id': id,
'server': server,
'type': type,
'channel': cName,
'user': user.id,
'time': new Date().getTime(),
2014-05-25 19:16:53 +02:00
'message': message,
2014-05-25 20:24:13 +02:00
'tags': tags
2014-07-15 00:54:23 +02:00
}, function(err, notify) {
dbot.api.event.emit('new_notify', [ notify, user.primaryNick ]);
});
2013-10-21 17:40:45 +02:00
var channel = dbot.instance.connections[server].channels[cName];
var ops = _.filter(channel.nicks, function(user) {
if(this.config.notifyVoice) {
return user.op || user.voice;
} else {
return user.op;
}
}, this);
ops = _.pluck(ops, 'name');
dbot.api.users.resolveChannel(server, cName, function(channel) {
if(channel) {
var perOps = channel.op;
if(this.config.notifyVoice) perOps = _.union(perOps, channel.voice);
this.db.read('nunsubs', channel.id, function(err, nunsubs) {
async.eachSeries(ops, function(nick, next) {
dbot.api.users.resolveUser(server, nick, function(user) {
if(!_.include(user.mobile, user.currentNick)) {
perOps = _.without(perOps, user.id);
}
if(nunsubs && _.include(nunsubs.users, user.id)) {
ops = _.without(ops, user.currentNick);
}
next();
});
}, function() {
offlineUsers = perOps;
if(!_.include(this.config.noMissingChans, cName)) {
_.each(offlineUsers, function(id) {
if(!this.pending[id]) this.pending[id] = [];
this.pending[id].push({
'time': new Date().getTime(),
'channel': cName,
'user': user.primaryNick,
'message': message
});
this.pNotify[id] = true;
}.bind(this));
}
message = this.internalAPI.formatNotify(type, server,
user, cName, message);
this.internalAPI.notify(server, ops, message);
if(_.has(this.config.chan_redirs, cName)) {
dbot.say(server, this.config.chan_redirs[cName], message);
}
}.bind(this));
}.bind(this));
}
}.bind(this));
},
'notifyUsers': function(server, users, message) {
this.internalAPI.notify(server, users, message);
}
};
return api;
};
exports.fetch = function(dbot) {
return api(dbot);
};