This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
remind.coffee
157 lines (140 loc) · 4.28 KB
/
remind.coffee
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Description:
# Forgetful? Add reminders!
#
# Dependencies:
# "chrono-node": "^0.1.10"
# "moment": "^2.8.1"
# "lodash": "^2.4.1"
#
# Configuration:
# None
#
# Commands:
# hubot remind me (on <date>|in <time>) to <action> - Set a reminder in <time> to do an <action> <time> is in the format 1 day, 2 hours, 5 minutes etc. Time segments are optional, as are commas
# hubot delete reminder <action> - Delete reminder matching <action> (exact match required)
# hubot show reminders
#
# Author:
# whitman
# jtwalters
_ = require('lodash')
moment = require('moment')
chrono = require('chrono-node')
timeoutIds = {}
class Reminders
constructor: (@robot) ->
@cache = []
@currentTimeout = null
# Load reminders from brain, on loaded event
@robot.brain.on('loaded', =>
if @robot.brain.data.reminders
@cache = _.map(@robot.brain.data.reminders, (item) ->
new Reminder(item)
)
console.log("loaded #{@cache.length} reminders")
@queue()
)
# Persist reminders to the brain, on save event
@robot.brain.on('save', =>
@robot.brain.data.reminders = @cache
)
add: (reminder) ->
@cache.push(reminder)
@cache.sort((a, b) -> a.due - b.due)
@queue()
removeFirst: ->
reminder = @cache.shift()
return reminder
queue: ->
return if @cache.length is 0
now = (new Date).getTime()
trigger = =>
reminder = @removeFirst()
@robot.reply(reminder.msg_envelope, 'you asked me to remind you to ' + reminder.action)
@queue()
# setTimeout uses a 32-bit INT
extendTimeout = (timeout, callback) ->
if timeout > 0x7FFFFFFF
setTimeout(->
extendTimeout(timeout - 0x7FFFFFFF, callback)
, 0x7FFFFFFF)
else
setTimeout(callback, timeout)
reminder = @cache[0]
duration = reminder.due - now
duration = 0 if duration < 0
clearTimeout(timeoutIds[reminder])
timeoutIds[reminder] = extendTimeout(reminder.due - now, trigger)
console.log("reminder set with duration of #{duration}")
class Reminder
constructor: (data) ->
{@msg_envelope, @action, @time, @due} = data
if @time and !@due
@time.replace(/^\s+|\s+$/g, '')
periods =
weeks:
value: 0
regex: "weeks?"
days:
value: 0
regex: "days?"
hours:
value: 0
regex: "hours?|hrs?"
minutes:
value: 0
regex: "minutes?|mins?"
seconds:
value: 0
regex: "seconds?|secs?"
for period of periods
pattern = new RegExp('^.*?([\\d\\.]+)\\s*(?:(?:' + periods[period].regex + ')).*$', 'i')
matches = pattern.exec(@time)
periods[period].value = parseInt(matches[1]) if matches
@due = (new Date).getTime()
@due += (
(periods.weeks.value * 604800) +
(periods.days.value * 86400) +
(periods.hours.value * 3600) +
(periods.minutes.value * 60) +
periods.seconds.value
) * 1000
formatDue: ->
dueDate = new Date(@due)
duration = dueDate - new Date
if duration > 0 and duration < 86400000
'in ' + moment.duration(duration).humanize()
else
'on ' + moment(dueDate).format("dddd, MMMM Do YYYY, h:mm:ss a")
module.exports = (robot) ->
reminders = new Reminders(robot)
robot.respond(/show reminders$/i, (msg) ->
text = ''
for reminder in reminders.cache
text += "#{reminder.action} #{reminder.formatDue()}\n"
msg.send(text)
)
robot.respond(/delete reminder (.+)$/i, (msg) ->
query = msg.match[1]
prevLength = reminders.cache.length
reminders.cache = _.reject(reminders.cache, {action: query})
reminders.queue()
msg.send("Deleted reminder #{query}") if reminders.cache.length isnt prevLength
)
robot.respond(/remind me (in|on) (.+?) to (.*)/i, (msg) ->
type = msg.match[1]
time = msg.match[2]
action = msg.match[3]
options =
msg_envelope: msg.envelope,
action: action
time: time
if type is 'on'
# parse the date (convert to timestamp)
due = chrono.parseDate(time).getTime()
if due.toString() isnt 'Invalid Date'
options.due = due
reminder = new Reminder(options)
reminders.add(reminder)
msg.send "I'll remind you to #{action} #{reminder.formatDue()}"
)