-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (57 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Commands Plugin for Unibot
* @param {Object} options [description]
* db: {mongoose} the mongodb connection
* bot: {irc} the irc bot
* web: {connect} a connect + connect-rest webserver
* config: {object}
* @return {Function} init function to access shared resources
*/
module.exports = function init(options){
var mongoose = options.db;
var bot = options.bot;
var webserver = options.web;
var config = options.config;
var Logs = new mongoose.Schema({
channel : {
type : String, // channel._id
index : {
unique : true,
dropDups : false
}
},
logs : {
type: [mongoose.Schema.Types.Mixed],
default: []
}
});
var model = mongoose.model('Logs', Logs);
webserver.get('/logs', function(req, res, next){
res.sendFile(__dirname + '/index.html');
});
webserver.get('/logs/:channel', function(req, res, next) {
model.findOne({ channel: req.params.channel }, function(err, logs){
res.send(err || logs);
});
});
return function plugin(channel){
var logs = [];
model.findOne({ channel: channel.id }, function(err, _logs_){
if (err || !_logs_) {
logs = new model({
channel: channel.id
});
logs.save();
} else {
logs = _logs_;
}
});
return {
"(.+)": function(from, matches) {
logs.logs.unshift({ from: from, message: matches[0], timestamp: Date.now() });
logs.markModified('logs');
logs.save();
}
};
};
};