index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const express = require('express');
  2. const router = express.Router();
  3. const request = require('request');
  4. const config = require('../config');
  5. const { badge } = require('../lib/badge');
  6. router.get('/', function(req, res) {
  7. res.setLocale(config.locale);
  8. res.render('index', { community: config.community,
  9. tokenRequired: !!config.inviteToken,
  10. recaptchaSiteKey: config.recaptchaSiteKey });
  11. });
  12. router.post('/invite', function(req, res) {
  13. if (req.body.email && (!config.inviteToken || (!!config.inviteToken && req.body.token === config.inviteToken))) {
  14. function doInvite() {
  15. request.post({
  16. url: 'https://'+ config.slackUrl + '/api/users.admin.invite',
  17. form: {
  18. email: req.body.email,
  19. token: config.slacktoken,
  20. set_active: true
  21. }
  22. }, function(err, httpResponse, body) {
  23. // body looks like:
  24. // {"ok":true}
  25. // or
  26. // {"ok":false,"error":"already_invited"}
  27. if (err) { return res.send('Error:' + err); }
  28. body = JSON.parse(body);
  29. if (body.ok) {
  30. res.render('result', {
  31. community: config.community,
  32. message: 'Success! Check “'+ req.body.email +'” for an invite from Slack.'
  33. });
  34. } else {
  35. let error = body.error;
  36. if (error === 'already_invited' || error === 'already_in_team') {
  37. res.render('result', {
  38. community: config.community,
  39. message: 'Success! You were already invited.<br>' +
  40. 'Visit <a href="https://'+ config.slackUrl +'">'+ config.community +'</a>'
  41. });
  42. return;
  43. } else if (error === 'invalid_email') {
  44. error = 'The email you entered is an invalid email.';
  45. } else if (error === 'invalid_auth') {
  46. error = 'Something has gone wrong. Please contact a system administrator.';
  47. }
  48. res.render('result', {
  49. community: config.community,
  50. message: 'Failed! ' + error,
  51. isFailed: true
  52. });
  53. }
  54. });
  55. }
  56. if (!!config.recaptchaSiteKey && !!config.recaptchaSecretKey) {
  57. request.post({
  58. url: 'https://www.google.com/recaptcha/api/siteverify',
  59. form: {
  60. response: req.body['g-recaptcha-response'],
  61. secret: config.recaptchaSecretKey
  62. }
  63. }, function(err, httpResponse, body) {
  64. if (typeof body === "string") {
  65. body = JSON.parse(body);
  66. }
  67. if (body.success) {
  68. doInvite();
  69. } else {
  70. error = 'Invalid captcha.';
  71. res.render('result', {
  72. community: config.community,
  73. message: 'Failed! ' + error,
  74. isFailed: true
  75. });
  76. }
  77. });
  78. } else {
  79. doInvite();
  80. }
  81. } else {
  82. const errMsg = [];
  83. if (!req.body.email) {
  84. errMsg.push('your email is required');
  85. }
  86. if (!!config.inviteToken) {
  87. if (!req.body.token) {
  88. errMsg.push('valid token is required');
  89. }
  90. if (req.body.token && req.body.token !== config.inviteToken) {
  91. errMsg.push('the token you entered is wrong');
  92. }
  93. }
  94. res.render('result', {
  95. community: config.community,
  96. message: 'Failed! ' + errMsg.join(' and ') + '.',
  97. isFailed: true
  98. });
  99. }
  100. });
  101. router.get('/badge.svg', (req, res) => {
  102. request.get({
  103. url: 'https://'+ config.slackUrl + '/api/users.list',
  104. qs: {
  105. token: config.slacktoken,
  106. presence: true
  107. }
  108. }, function(err, httpResponse, body) {
  109. try {
  110. body = JSON.parse(body);
  111. } catch(e) {
  112. return res.status(404).send('');
  113. }
  114. if (!body.members) {
  115. return res.status(404).send('');
  116. }
  117. const members = body.members.filter(function(m) {
  118. return !m.is_bot;
  119. });
  120. const total = members.length;
  121. const presence = members.filter(function(m) {
  122. return m.presence === 'active';
  123. }).length;
  124. res.type('svg');
  125. res.set('Cache-Control', 'max-age=0, no-cache');
  126. res.set('Pragma', 'no-cache');
  127. res.send(badge(presence, total));
  128. });
  129. });
  130. module.exports = router;