-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ls
211 lines (184 loc) · 10.2 KB
/
app.ls
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
require! {
'koa'
'koa-router'
'koa-logger'
'koa-bodyparser'
'koa-jsonp'
'koa-static'
'sendgrid'
'btoa'
}
jsyaml = require 'js-yaml'
normalize_space = require 'normalize-space'
text_clipper = require 'text-clipper'
kapp = koa()
kapp.use(koa-jsonp())
kapp.use(koa-logger())
kapp.use(koa-bodyparser({jsonLimit: '20mb'}))
app = koa-router()
helper = sendgrid.mail
getsecret = require('getsecret')
sg = sendgrid(getsecret('sendgrid_api_key'))
default_from_email = getsecret('from_email')
default_to_email = getsecret('to_email')
cloudinary = require('cloudinary')
cloudinary.config({
cloud_name: getsecret('cloudinary_cloud_name')
api_key: getsecret('cloudinary_api_key')
api_secret: getsecret('cloudinary_api_secret')
})
Gitter = require 'node-gitter'
gitter = new Gitter getsecret('gitter_api_key')
Octokat = require 'octokat'
octo = new Octokat({token: getsecret('github_api_key')})
{add_noerr, cfy} = require 'cfy'
co = require 'co'
#img_data_base64 = 'iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABHNCSVQICAgIfAhkiAAAAAl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cfRcRHjnlkVmUs6E3R7ph/zGhXZsmeTQTJ5OiVWUjQHWMxqrKuxvMYhVAMScyx8rIy0GsQqAFbNbnOYsthjEKgA+wr5+oOy8GdoAbe4g/ItVWWQB8ddApmYQ8BXhnRu7dACHZfRx9LThJ4g3u/inZvZuBkJsBrUBrwGb59DxF/AAUhyxGHljPhA4EpvTN2YivQq7Nm92RC7cJjl01oHTgfvyGFYWNkTanma5S96l5y6h2yM7fUXdoe3IFvGqGIBsPWe9809u4rPKMZL07wRv0/wcoOHoHeawoiRpzHhZSp198uJ3MZLkhzG9RfJDoIooWH02xbyS1gp0ACel0FtJRiDJGr056nXStV7ph+65Pg3Sn3F4eRN9y4ATU+qsLGsBN7Fyydh84BLkoMm0TEc3ADbOYMMkpLFldz115OU1ikYXsSUj9kf2wgcju4efI07LwlTkhC8tNkDq/7OwEZJu1gHMQebmFIx2ufZwW/NtqFTSgZOeKgdA1p8OK31RUOUA0G7ZukRZXxRUOQA028rPwbezS0cb8C86L4CWlceOIlPIf/E/JnwXEicjLUjJeJ6LP8LcakedvYBnSFbX344c/XIe4Y+lLZzYVgItqNH7vGM9kMpxHMdxHMdxHMdxHMdxHMdxHMdxHMdxkvIfPKhlO+FtoloAAAAASUVORK5CYII='
upload_to_cloudinary = cfy (img_data_url) ->*
#img_data_url = 'data:image/png;base64,' + img_data
result = yield -> cloudinary.v2.uploader.upload img_data_url, {}, it
#throw new Error('cloudinary failed')
return result.url
upload_to_cloudinary_json = cfy (json_data) ->*
public_id = [Math.floor(Math.random()*10) for i from 0 til 28].join('') + '.txt'
result = yield -> cloudinary.v2.uploader.upload 'data:text/plain;base64,' + btoa(JSON.stringify(json_data)), {public_id, resource_type: 'raw'}, it
return result.url
app.get '/ping', ->*
this.body = 'hi'
return
app.post '/report_bug', ->*
this.type = 'json'
{message, screenshot, other} = this.request.body
user_email = this.request.body.email
is_gitter = this.request.body['gitter']
is_github = this.request.body['github']
if not message?
this.body = JSON.stringify {response: 'error', error: 'need parameter message'}
return
screenshot_url = null
if not other?
other = {}
if other.extra?
extra = other.extra
delete other.extra
else
extra = {}
extra.ip_address = this.request.ip
extra.server_timestamp = Date.now()
extra.server_localtime = new Date().toString()
if screenshot?
try
screenshot_url = yield upload_to_cloudinary(screenshot)
catch err
other.screenshot_upload_error = 'Error occurred while uploading screenshot'
other.screenshot_upload_error_message = err.toString()
email_message = message.split('\n').join('<br>')
if screenshot_url?
email_message += '<br><br>' + '<img src="' + screenshot_url + '"></img>'
if other?
email_message += '<br><br><pre>' + (jsyaml.safeDump(other)) + '</pre>'
#if extra?
# email_message += '<br><br><pre>' + (JSON.stringify(extra)) + '</pre>'
if extra?
try
data_url = yield upload_to_cloudinary_json(extra)
email_message += '<br><br><a href="' + data_url + '">' + data_url + '</a><br><br>'
catch err
other.extra_upload_error = 'Error occurred while uploading extra data'
other.extra_upload_error = err.toString()
from_email = new helper.Email(default_from_email)
to_email = new helper.Email(default_to_email)
title = text_clipper(normalize_space(message), 80)
subject = '[User Feedback] ' + title
#var img_data = 'data:image/png;base64,'
content = new helper.Content('text/html', email_message)
mail = new helper.Mail(from_email, subject, to_email, content)
if user_email? and user_email.length > 0 and user_email.indexOf('@') != -1 and user_email.indexOf('.') != -1
#mail.setReplyTo(user_email)
if not mail.personalizations?
mail.personalizations = []
mail.personalizations[0].addCc(new helper.Email(user_email))
request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON(),
})
sendgrid_response = yield -> sg.API request, it
github_issue_url = 'https://github.com/habitlab/habitlab/issues/'
if is_github
github_message = 'A user submitted the following via HabitLab\'s built-in Feedback form:\n\n' + (message.split('\n').join('\n\n'))
if screenshot_url?
github_message += '\n\n' + "<img src=\"#{screenshot_url}\"></img>"
if other?
github_message += '\n\n```\n' + jsyaml.safeDump(other) + '\n```\n'
github_title = subject
new_issue = {
title: github_title
body: github_message
}
result = yield octo.repos('habitlab', 'habitlab').issues.create(new_issue)
if result.htmlUrl? and result.htmlUrl.startsWith? and (result.htmlUrl.startsWith('https://github.com/habitlab/habitlab/issues') or result.htmlUrl.startsWith('http://github.com/habitlab/habitlab/issues'))
github_issue_url = result.htmlUrl
gitter_message = 'A user submitted the following via HabitLab\'s built-in Feedback form:'
if is_github
gitter_message += '\n\nGitHub Issue: ' + "[#{github_issue_url}](#{github_issue_url})"
gitter_message += '\n\n' + (message.split('\n').join('\n\n'))
if screenshot_url?
gitter_message += '\n\n' + "[#{screenshot_url}](#{screenshot_url})"
if other?
gitter_message += '\n\n```\n' + jsyaml.safeDump(other) + '\n```\n'
if is_gitter
room = yield gitter.rooms.join('habitlab/habitlab')
room.send(gitter_message)
response_message = 'Your message has been sent to <a href="mailto:' + default_to_email + '" target="_blank">' + default_to_email + '</a> <br><br>'
if is_gitter
response_message += 'It has also been posted to the support chat at <a href="https://gitter.im/habitlab/habitlab" target="_blank">https://gitter.im/habitlab/habitlab</a> <br><br>'
else
response_message += 'If you need help, try the support chat at <a href="https://gitter.im/habitlab/habitlab" target="_blank">https://gitter.im/habitlab/habitlab</a> <br><br>'
if is_github
response_message += 'You can track progress at <a href="' + github_issue_url + '" target="_blank">' + github_issue_url + '</a> <br><br>'
#else
# response_message += 'You can file a bug at <a href="' + github_issue_url + '">' + github_issue_url + '</a> <br><br>'
this.body = {response: 'success', message: response_message}
/*
app.get '/send_test_gitter', ->*
this.type = 'json'
img_url = yield upload_to_cloudinary(img_data_base64)
room = yield gitter.rooms.join('habitlab/habitlab')
room.send("""
Here is a long message.
And more.
Multiline supported right?
[#{img_url}](#{img_url})
""")
this.body = img_url
# based off https://github.com/habitlab/habitlab-website/blob/master/app.ls
app.get '/send_test_email', ->*
this.type = 'json'
from_email = new helper.Email(default_from_email)
to_email = new helper.Email(default_to_email)
subject = 'Hello World from the SendGrid Node.js Library!'
#var img_data = 'data:image/png;base64,'
content = new helper.Content('text/html', 'See screenshot below<br><br><img src="cid:screenshot"></img>')
mail = new helper.Mail(from_email, subject, to_email, content)
attachment = new helper.Attachment()
attachment.setContent(img_data_base64)
attachment.setType("image/png")
attachment.setFilename("screenshot.png")
attachment.setDisposition("inline")
attachment.setContentId("screenshot")
mail.addAttachment(attachment)
request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON(),
})
response = yield -> sg.API request, it
this.body = JSON.stringify(response)
*/
kapp.use(app.routes())
kapp.use(app.allowedMethods())
kapp.use(koa-static(__dirname + '/www'))
port = process.env.PORT ? 5000
kapp.listen(port)
console.log "listening to port #{port} visit http://localhost:#{port}"