2013-08-24 19:39:04 +02:00
|
|
|
var _ = require('underscore')._,
|
|
|
|
async = require('async');
|
2013-07-06 20:45:21 +02:00
|
|
|
|
|
|
|
var pages = function(dbot) {
|
2013-08-18 19:32:57 +02:00
|
|
|
var pages = {
|
2013-08-24 15:56:45 +02:00
|
|
|
'/notify': function(req, res) {
|
2013-07-06 20:45:21 +02:00
|
|
|
res.render('servers', {
|
|
|
|
'servers': _.keys(dbot.config.servers)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-24 15:56:45 +02:00
|
|
|
'/notify/:server': function(req, res) {
|
2013-08-24 19:39:04 +02:00
|
|
|
var server = req.params.server,
|
|
|
|
userCount = {},
|
|
|
|
users = [];
|
|
|
|
|
|
|
|
this.db.scan('notifies', function(notify) {
|
|
|
|
if(!_.has(userCount, notify.user)) {
|
|
|
|
userCount[notify.user] = 0;
|
|
|
|
} else {
|
|
|
|
userCount[notify.user]++;
|
|
|
|
}
|
|
|
|
}, 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);
|
|
|
|
next();
|
|
|
|
}
|
2013-08-24 19:39:04 +02:00
|
|
|
});
|
|
|
|
}, function() {
|
|
|
|
res.render('channels', {
|
|
|
|
'server': server,
|
|
|
|
'users': users,
|
|
|
|
'channels': _.keys(dbot.instance.connections[server].channels)
|
|
|
|
});
|
|
|
|
});
|
2013-07-06 20:45:21 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-24 15:56:45 +02:00
|
|
|
'/notify/:server/missing': function(req, res) {
|
2013-08-17 23:04:42 +02:00
|
|
|
var server = req.params.server,
|
|
|
|
user = req.user,
|
|
|
|
notifies = this.pending[user.id];
|
|
|
|
|
|
|
|
res.render('missing_notifies', {
|
|
|
|
'user': user.primaryNick,
|
2013-08-24 16:32:27 +02:00
|
|
|
'notifies': _.sortBy(notifies, 'time')
|
2013-08-17 23:04:42 +02:00
|
|
|
});
|
2013-08-18 18:37:30 +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-24 19:53:16 +02:00
|
|
|
'/notify/:server/:item': function(req, res) {
|
2013-07-06 20:45:21 +02:00
|
|
|
var server = req.params.server,
|
|
|
|
notifies = [];
|
|
|
|
|
2013-08-24 19:53:16 +02:00
|
|
|
if(req.params.item.charAt(0) == '#') {
|
|
|
|
var channel = req.params.item;
|
|
|
|
|
|
|
|
this.db.search('notifies', {
|
|
|
|
'server': server,
|
|
|
|
'channel': channel
|
|
|
|
}, function(notify) {
|
|
|
|
notifies.push(notify);
|
|
|
|
}, 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() {
|
|
|
|
res.render('notifies', {
|
|
|
|
'server': server,
|
|
|
|
'notifies': _.sortBy(notifies, 'time')
|
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() {
|
|
|
|
res.render('notifies', {
|
|
|
|
'server': server,
|
|
|
|
'notifies': _.sortBy(notifies, 'time')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2013-07-06 20:45:21 +02:00
|
|
|
}
|
|
|
|
};
|
2013-08-18 19:32:57 +02:00
|
|
|
|
|
|
|
return pages;
|
2013-07-06 20:45:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return pages(dbot);
|
|
|
|
};
|