-
Notifications
You must be signed in to change notification settings - Fork 5
/
redisred.coffee
147 lines (127 loc) · 4.5 KB
/
redisred.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
# Description:
# A script that allows users to make redisred links through hubot
#
# Configuration:
# HUBOT_REDISRED_URL - The base URL for redirects, not including the API path.
# HUBOT_REDISRED_TOKEN - The API token to be used for the requests.
# HUBOT_REDISRED_PREFIX - (requires restart) The prefix to auto-expand links. "" to disable.
#
# Commands:
# hubot redisred list - list all redirects
# hubot redisred list <regex> - list all redirects whose key matches regex
# hubot redisred info <key> - get info for redirect whose key is exactly key
# hubot redisred create <key> <url> - create redirect from key and url
# hubot redisred delete <key> - delete a redirect
#
# Author:
# Detry322
filter = (arr, func) ->
filtered = []
for item in arr
if func item
filtered.push item
filtered
module.exports = (robot) ->
config = require('hubot-conf')('redisred', robot)
cache = {}
formatRedirect = (redirect) ->
"*#{redirect.key}* (#{redirect.clicks} clicks): #{redirect.url}"
formatRedirects = (redirects) ->
message = "*Redirects:*"
for redirect in redirects
message += "\n" + formatRedirect(redirect)
message
formatShortlinks = (redirects) ->
message = ""
for redirect in redirects
message += "Link: #{config('url')}/#{redirect.key}\n"
message.trim()
modifyRedirects = (action, data, callback) ->
postData = JSON.stringify(data)
robot.http(config('url') + "/admin/api/" + action)
.header('Content-Type', 'application/json')
.header('Accept', 'application/json')
.header('x-access-token', config("token"))
.post(postData) (err, httpResponse, body) ->
if not err and httpResponse.statusCode is 200
try
redirect = JSON.parse body
callback(false, redirect)
catch error
callback(error)
else
callback(err || httpResponse)
fetchRedirects = (callback) ->
callback = callback or () -> undefined
robot.http(config('url') + "/admin/api/")
.header('Accept', 'application/json')
.header('x-access-token', config("token"))
.get() (err, httpResponse, body) ->
if not err and httpResponse.statusCode is 200
try
redirects = JSON.parse body
cache = {}
for redirect in redirects
cache[redirect.key] = redirect
callback(false, redirects)
catch error
callback(error)
else
callback(err || httpResponse)
fetchRedirects()
setInterval(fetchRedirects, 5*60*1000) # Fetch redirects every 5 minutes
createRedirect = (res, key, url) ->
data = {key: key, url: url}
modifyRedirects 'create', data, (err, redirect) ->
if (err)
res.send "Error creating redirect."
else
cache[redirect.key] = redirect
res.send "*Redirect Created:*\n" + formatRedirect(redirect)
deleteRedirect = (res, key) ->
data = {key: key}
modifyRedirects 'delete', data, (err, redirect) ->
if (err)
res.send "Error deleting redirect."
else
delete cache[key]
res.send "Deleted redirect: #{key}"
listRedirects = (res, search) ->
fetchRedirects (err, redirects) ->
if (err)
res.send "Error fetching redirects."
else
filtered = filter redirects, (redirect) ->
redirect.key.match search
res.send formatRedirects(filtered)
fetchInfo = (res, key) ->
fetchRedirects (err, redirects) ->
if (err)
res.send "Error fetching redirect."
else
filtered = filter redirects, (redirect) ->
redirect.key == key
res.send formatRedirect(filtered[0])
robot.hear /.*/i, (res) ->
redirects = []
prefix = config('prefix')
for key, value of cache
if ///(^|\s)#{prefix}/#{key}($|\s)///i.test res.message.text
redirects.push value
if config('prefix') and redirects.length != 0
res.send formatShortlinks(redirects)
robot.respond /redisred list$/i, (res) ->
listRedirects(res, "")
robot.respond /redisred list (.+)$/i, (res) ->
search = res.match[1]
listRedirects(res, search)
robot.respond /redisred info ([^ \n]+)$/i, (res) ->
key = res.match[1]
fetchInfo(res, key)
robot.respond /redisred create ([^ \n]+) (.+)$/i, (res) ->
key = res.match[1]
url = res.match[2]
createRedirect(res, key, url)
robot.respond /redisred delete ([^ \n]+)/i, (res) ->
key = res.match[1]
deleteRedirect(res, key)