-
Notifications
You must be signed in to change notification settings - Fork 0
/
Note.js
90 lines (64 loc) · 2.02 KB
/
Note.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
module.exports = function(bot, chat, mongoose, db, constants, privates) {
// Setup our properties.
this.bot = bot;
this.chat = chat;
this.mongoose = mongoose;
this.constants = constants;
this.privates = privates;
this.moment = require('moment');
// Sample object.
/*
j = {
from: "protocoldoug",
to: "protocoldoug",
note: "sample note",
sent: 1377802447,
};
*/
// Setup a schema.
this.noteSchema = this.mongoose.Schema({
from: String,
to: String,
note: String,
sent: Number
}, { collection: 'notes' });
// Compile it to a model.
this.Note = this.mongoose.model('Note', this.noteSchema);
// Here's the note handler.
// When someone speaks, we check if we have any notes for them.
this.handler = function(speaker) {
// console.log('note handler',speaker);
this.Note.find({to: speaker},function (err, notes) {
for (i in notes) {
// Ok, given each note for this person... Send them a mesasge.
// How long ago was that?
ago = this.moment.unix(notes[i].sent).fromNow();
// Now, let's send that note to the person it's intended for.
this.chat.say("note_send",[speaker,notes[i].from,ago,notes[i].note]);
// Now you can delete that from mongo.
this.Note.findOne({ _id: notes[i]._id }).remove();
}
}.bind(this));
};
// Additionally, we can make a note.
this.leaveAMessage = function(from,arguments) {
// Ok, so let's get the nick off the arguments.
// console.log('arguments',arguments);
if (/\s/.test(arguments)) {
to = arguments.replace(/^(.+?)\s.+$/,'$1');
message = arguments.replace(/^.+?\s(.+)$/,'$1');
// Ok, looks like this is a valid message.
// Nice little way to get a unix-time from 'moment': http://momentjs.com/docs/#/displaying/format/
newnote = new this.Note({
from: from,
to: to,
note: message,
sent: this.moment().format('X')
});
newnote.save();
this.chat.say("note_saved",[from]);
} else {
this.chat.say("note_format",[from]);
}
};
};