-
Notifications
You must be signed in to change notification settings - Fork 886
/
index.js
154 lines (140 loc) · 4.48 KB
/
index.js
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
const express = require('express');
const router = express.Router();
const request = require('request');
const config = require('../config');
const { badge } = require('../lib/badge');
const sanitize = require('sanitize');
router.get('/', function(req, res) {
res.setLocale(config.locale);
res.render('index', { community: config.community,
tokenRequired: !!config.inviteToken,
recaptchaSiteKey: config.recaptchaSiteKey });
});
router.post('/invite', function(req, res) {
if (req.body.email && (!config.inviteToken || (!!config.inviteToken && req.body.token === config.inviteToken))) {
function doInvite() {
request.post({
url: 'https://'+ config.slackUrl + '/api/users.admin.invite',
form: {
email: req.body.email,
token: config.slacktoken,
set_active: true
}
}, function(err, httpResponse, body) {
// body looks like:
// {"ok":true}
// or
// {"ok":false,"error":"already_invited"}
if (err) { return res.send('Error:' + err); }
body = JSON.parse(body);
if (body.ok) {
res.render('result', {
community: config.community,
message: 'Success! Check “'+ req.body.email +'” for an invite from Slack.'
});
} else {
let error = body.error;
if (error === 'already_invited' || error === 'already_in_team') {
res.render('result', {
community: config.community,
message: 'Success! You were already invited.<br>' +
'Visit <a href="https://'+ config.slackUrl +'">'+ config.community +'</a>'
});
return;
} else if (error === 'invalid_email') {
error = 'The email you entered is an invalid email.';
} else if (error === 'invalid_auth') {
error = 'Something has gone wrong. Please contact a system administrator.';
}
res.render('result', {
community: config.community,
message: 'Failed! ' + error,
isFailed: true
});
}
});
}
if (!!config.recaptchaSiteKey && !!config.recaptchaSecretKey) {
request.post({
url: 'https://www.google.com/recaptcha/api/siteverify',
form: {
response: req.body['g-recaptcha-response'],
secret: config.recaptchaSecretKey
}
}, function(err, httpResponse, body) {
if (typeof body === "string") {
body = JSON.parse(body);
}
if (body.success) {
doInvite();
} else {
error = 'Invalid captcha.';
res.render('result', {
community: config.community,
message: 'Failed! ' + error,
isFailed: true
});
}
});
} else {
doInvite();
}
} else {
const errMsg = [];
if (!req.body.email) {
errMsg.push('your email is required');
}
if (!!config.inviteToken) {
if (!req.body.token) {
errMsg.push('valid token is required');
}
if (req.body.token && req.body.token !== config.inviteToken) {
errMsg.push('the token you entered is wrong');
}
}
res.render('result', {
community: config.community,
message: 'Failed! ' + errMsg.join(' and ') + '.',
isFailed: true
});
}
});
router.get('/badge.svg', (req, res) => {
request.get({
url: 'https://'+ config.slackUrl + '/api/users.list',
qs: {
token: config.slacktoken,
presence: true
}
}, function(err, httpResponse, body) {
try {
body = JSON.parse(body);
} catch(e) {
return res.status(404).send('');
}
if (!body.members) {
return res.status(404).send('');
}
const members = body.members.filter(function(m) {
return !m.is_bot;
});
const total = members.length;
const presence = members.filter(function(m) {
return m.presence === 'active';
}).length;
const hexColor = /^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
sanitize.middleware.mixinFilters(req);
res.type('svg');
res.set('Cache-Control', 'max-age=0, no-cache');
res.set('Pragma', 'no-cache');
res.send(
badge(
presence,
total,
req.queryPattern('colorA', hexColor),
req.queryPattern('colorB', hexColor)
)
);
});
});
module.exports = router;