dbot/modules/report/pages.js

196 lines
7.3 KiB
JavaScript
Raw Normal View History

2013-08-24 19:39:04 +02:00
var _ = require('underscore')._,
2013-08-29 23:12:45 +02:00
async = require('async'),
moment = require('moment-timezone');
2013-07-06 20:45:21 +02:00
var pages = function(dbot) {
var pages = {
'/notify': function(req, res) {
2013-08-31 14:17:06 +02:00
var server = req.user.server,
2013-08-24 19:39:04 +02:00
userCount = {},
2013-08-24 22:55:57 +02:00
users = [],
2014-05-25 19:54:46 +02:00
channelCount = {},
tags = {};
2013-08-24 19:39:04 +02:00
this.db.scan('notifies', function(notify) {
if(!_.has(userCount, notify.user)) {
2013-08-24 22:55:57 +02:00
userCount[notify.user] = 0;
2013-08-24 19:39:04 +02:00
}
2013-08-24 22:55:57 +02:00
if(!_.has(channelCount, notify.channel)) {
channelCount[notify.channel] = 0;
}
userCount[notify.user]++;
channelCount[notify.channel]++;
2014-05-25 19:54:46 +02:00
_.each(notify.tags, function(tag) {
if(!_.has(tags, tag)) tags[tag] = 0;
tags[tag]++;
});
2013-08-24 19:39:04 +02:00
}, function() {
userCount = _.map(userCount, function(value, key) {
return {
'id': key,
'count': value
}
});
async.eachSeries(userCount, function(userCount, next) {
dbot.api.users.getUser(userCount.id, function(user) {
2013-08-24 19:56:11 +02:00
if(user) {
userCount['name'] = user.primaryNick;
users.push(userCount);
}
2013-08-24 19:58:17 +02:00
next();
2013-08-24 19:39:04 +02:00
});
}, function() {
res.render('channels', {
'server': server,
'users': users,
2014-05-25 19:54:46 +02:00
'channels': channelCount,
'tags': tags
2013-08-24 19:39:04 +02:00
});
});
2013-07-06 20:45:21 +02:00
});
},
2013-08-31 14:53:45 +02:00
'/notify/stream': function(req, res) {
var staffedChannels = [],
notifications = [];
async.each(req.user.channels, function(cId, done) {
dbot.api.users.getChannel(cId, function(channel) {
if(_.include(channel.op, req.user.id) ||
(this.config.notifyVoice && _.include(channel.voice, req.user.id))) {
staffedChannels.push(channel.name);
}
done();
}.bind(this));
}.bind(this), function() {
async.each(staffedChannels, function(cName, done) {
this.db.search('notifies', { 'server': req.user.server, 'channel': cName }, function(notify) {
notifications.push(notify);
}, function() {
done();
});
}.bind(this), function() {
async.each(notifications, function(notify, done) {
dbot.api.users.getUser(notify.user, function(user) {
if(user) notify.user = user.primaryNick;
done();
});
}, function() {
2013-08-31 14:57:34 +02:00
var timezone = req.user.timezone || 'Europe/London';
2013-08-31 14:53:45 +02:00
notifications = _.sortBy(notifications, 'time').reverse();
2013-08-31 14:57:34 +02:00
_.each(notifications, function(v, k) {
v.time = moment(v.time).tz(timezone);
});
2013-08-31 14:53:45 +02:00
res.render('notifies', {
'server': req.user.server,
'notifies': notifications
});
});
}.bind(this));
}.bind(this));
},
'/notify/missing': function(req, res) {
var server = req.user.server,
2013-08-17 23:04:42 +02:00
user = req.user,
notifies = this.pending[user.id];
2013-08-31 14:57:34 +02:00
var timezone = req.user.timezone || 'Europe/London';
2013-08-29 23:12:45 +02:00
notifies = _.sortBy(notifies, 'time').reverse();
2013-08-31 14:57:34 +02:00
_.each(notifies, function(v, k) {
v.time = moment(v.time).tz(timezone);
});
2013-08-29 23:12:45 +02:00
2013-08-17 23:04:42 +02:00
res.render('missing_notifies', {
'user': user.primaryNick,
2013-08-29 23:12:45 +02:00
'notifies': notifies
2013-08-17 23:04:42 +02:00
});
if(_.has(dbot.modules, 'log')) {
dbot.api.log.log(server, user.primaryNick,
'Checked their missing notifications.');
}
2013-08-17 23:04:42 +02:00
},
2013-08-31 14:17:06 +02:00
'/notify/:item': function(req, res) {
var server = req.user.server,
2014-05-25 19:54:46 +02:00
type = req.query.t,
2013-07-06 20:45:21 +02:00
notifies = [];
2014-05-25 19:54:46 +02:00
console.log(type);
2013-08-24 19:53:16 +02:00
2014-05-25 19:54:46 +02:00
if(req.params.item.charAt(0) == '#') {
var item = req.params.item,
search = {
2013-08-24 19:53:16 +02:00
'server': server,
2014-05-25 19:54:46 +02:00
};
if(type != 'tag') search.channel = item;
this.db.search('notifies', search, function(notify) {
if(type == 'tag') {
if(_.include(notify.tags, item)) notifies.push(notify);
} else {
notifies.push(notify);
}
2013-08-24 19:53:16 +02:00
}, function(err) {
var pNickCache = {};
async.eachSeries(notifies, function(notify, next) {
if(!_.has(pNickCache, notify.user)) {
dbot.api.users.getUser(notify.user, function(user) {
pNickCache[notify.user] = user.primaryNick;
notify.user = user.primaryNick;
next();
});
} else {
notify.user = pNickCache[notify.user];
2013-08-24 19:39:04 +02:00
next();
2013-08-24 19:53:16 +02:00
}
}, function() {
2013-08-31 14:57:34 +02:00
var timezone = req.user.timezone || 'Europe/London';
2013-08-29 23:12:45 +02:00
notifies = _.sortBy(notifies, 'time').reverse();
2013-08-31 14:57:34 +02:00
_.each(notifies, function(v, k) {
v.time = moment(v.time).tz(timezone);
});
2013-08-29 23:12:45 +02:00
2013-08-24 19:53:16 +02:00
res.render('notifies', {
'server': server,
2013-08-29 23:12:45 +02:00
'notifies': notifies
2013-08-24 19:39:04 +02:00
});
});
2013-07-06 20:45:21 +02:00
});
2013-08-24 19:53:16 +02:00
} else {
var username = req.params.item;
dbot.api.users.resolveUser(server, username, function(user) {
this.db.search('notifies', {
'user': user.id
}, function(notify) {
notify.user = user.primaryNick;
notifies.push(notify);
}, function() {
2013-08-31 14:57:34 +02:00
var timezone = req.user.timezone || 'Europe/London';
2013-08-29 23:12:45 +02:00
notifies = _.sortBy(notifies, 'time').reverse();
2013-08-31 14:57:34 +02:00
_.each(notifies, function(v, k) {
v.time = moment(v.time).tz(timezone);
});
2013-08-29 23:12:45 +02:00
2013-08-24 19:53:16 +02:00
res.render('notifies', {
'server': server,
2013-08-29 23:12:45 +02:00
'notifies': notifies
2013-08-24 19:53:16 +02:00
});
});
}.bind(this));
}
2013-08-31 14:38:25 +02:00
},
2013-07-06 20:45:21 +02:00
};
return pages;
2013-07-06 20:45:21 +02:00
};
exports.fetch = function(dbot) {
return pages(dbot);
};