-
Notifications
You must be signed in to change notification settings - Fork 0
/
discourse.group.info.coffee
44 lines (40 loc) · 1.55 KB
/
discourse.group.info.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
# Description:
# Query Discourse for Group Information
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_DISCOURSE_URL
#
# Commands:
# hubot show <group> group info [from discourse]
#
# Author:
# cpradio
last_checkin_date = new Date();
last_checkin_date.setHours(0,0,0,0);
dif = (last_checkin_date.getDay() + 6) % 7;
last_checkin_date = new Date(last_checkin_date - dif * 24*60*60*1000);
module.exports = (robot) ->
robot.respond /show (.+) group info( from discourse)?/i, (res) ->
groupname = res.match[1]
user_url = process.env.HUBOT_DISCOURSE_URL + "/groups/#{encodeURIComponent(groupname)}/members.json"
res.http(user_url)
.get() (err, _, body) ->
return res.send "Sorry, the tubes are broken." if err
try
data = JSON.parse(body.toString("utf8"))
checkin_date_str = last_checkin_date.toDateString()
output = "Discourse Info: #{groupname} (last check-in date: #{checkin_date_str})\r\n"
for own key, user of data.members
username = user.username
last_seen = new Date(user.last_seen_at)
last_seen_as_date = new Date(last_seen)
checked_in = last_seen_as_date > last_checkin_date
checked_in_str = if checked_in then "(checked in)" else "(not checked in)"
prefix_suffix = if checked_in then "" else "*"
output += " #{prefix_suffix}User #{username} was last seen on #{last_seen} #{checked_in_str}#{prefix_suffix}\r\n"
res.send output
catch e
res.send "Discourse data for #{groupname} group is unavailable."