forked from anishathalye/offix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffix.coffee
163 lines (139 loc) · 3.99 KB
/
offix.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
158
159
160
161
162
163
# Description:
# A script that interfaces with offix.
#
# Configuration:
# HUBOT_OFFIX_BASEURL - the base URL of the offix installation (which should
# end with a trailing slash).
# HUBOT_OFFIX_KEY - the API key.
# HUBOT_OFFIX_LIMIT - the number of hours to show in the default view
# (defaults to 2).
#
# Commands:
# hubot offix list - show who is in the office
# hubot offix list all - show all people who have been in the office
#
# Author:
# anishathalye
http = require('http')
moment = require('moment')
table = require('text-table')
moment.updateLocale('en', {
relativeTime : {
future: "in %s",
past: "%s ago",
s: "sec",
m: "1 min",
mm: "%d min",
h: "1 hr",
hh: "%d hrs",
d: "1 day",
dd: "%d days",
M: "1 month",
MM: "%d months",
y: "1 year",
yy: "%d years"
}
});
DEFAULT_LIMIT = 2 # Hours
DEFAULT_REFRESH = 15 # Seconds
DEFAULT_ROOM = 'general'
EMPTY_OFFICE = [
"It's quiet...",
"Nobody's here! :(",
"Empty... Perfect for a nap?",
"Nobody here but the cactus.",
"Nobody is here. Come be a trendsetter.",
]
getHttp = (url, callback) ->
try
http.get url, (res) ->
body = ''
res.on 'data', (chunk) ->
body += chunk
res.on 'end', () ->
obj = JSON.parse body
callback(obj)
res.on 'error', (err) ->
callback(null, err)
.on 'error', (err) ->
callback(null, err)
catch err
callback(null, err)
# matches API, which returns [{username, realName, lastSeen}]
# limit in milliseconds
format = (data, limit) ->
line = (elem) ->
if elem.lastSeen?
date = new Date(elem.lastSeen)
date = moment(date).fromNow(true)
else
date = 'never'
[elem.realName, date]
recent = (elem) ->
if limit?
diff = new Date() - new Date(elem.lastSeen)
return diff < limit
else
return true
lines = (line(i) for i in data when recent(i))
if lines.length == 0
randomEmpty = EMPTY_OFFICE[Math.floor(Math.random() * EMPTY_OFFICE.length)]
"```\n#{randomEmpty}\n```"
else
lines.unshift(['---------', '----'])
lines.unshift(['real name', 'seen'])
"```\n#{table(lines)}\n```"
# Returns list of users that have just been seen after not been seen in a while.
# prevUsers = a list of users [{username, realName, lastSeen}]
# nextUsers = a list of users [{username, realName, lastSeen}]
# limit = threshold for oldness (ms)
newUsers = (prevUsers, nextUsers, limit) ->
if prevUsers.length is 0
return []
old = {}
isRecent = (u) ->
if limit
if u.lastSeen?
diff = new Date() - new Date(u.lastSeen)
return diff < limit
else
return false
else
return true
putOld = (u) ->
if u? and not isRecent(u)
old[u.username] = true
putOld(user) for user in prevUsers
recentNew = (u) ->
return isRecent(u) and old[u.username]
return (user for user in nextUsers when recentNew(user))
module.exports = (robot) ->
config = require('hubot-conf')('offix', robot)
usersCache = []
getLimit = ->
return parseInt(config('limit', DEFAULT_LIMIT)) * 60 * 60 * 1000
checkRecentNewUsers = (oldUsers, nextUsers) ->
limit = getLimit()
recent = newUsers(oldUsers, nextUsers, limit)
if recent.length > 0
room = config('room', DEFAULT_ROOM)
for user in recent
if user.shouldBroadcast
robot.messageRoom room, user.realName + ' is in the office!'
refreshUsersCache = ->
baseUrl = config('baseurl')
key = config('key')
url = baseUrl + 'api/users' + '?key=' + key
getHttp url, (data, err) ->
if data?
checkRecentNewUsers(usersCache, data)
usersCache = data
else
return
refreshUsersCache()
setInterval(refreshUsersCache, config('refresh', DEFAULT_REFRESH) * 1000)
robot.respond /offix list$/i, (res) ->
limit = getLimit()
res.send format(usersCache, limit)
robot.respond /offix list all$/i, (res) ->
res.send format(usersCache)