diff --git a/package.json b/package.json index f9dcefab..f4ef4068 100644 --- a/package.json +++ b/package.json @@ -8,14 +8,14 @@ "dependencies": { "body-parser": "^1.18.0", "cookie-parser": "^1.4.0", + "cross-fetch": "^3.1.4", "debug": "^3.1.0", "dotenv": "^5.0.1", "express": "^4.13.0", "i18n": "^0.8.3", "morgan": "^1.6.0", "pug": "^2.0.0-rc.4", - "request": "^2.62.0", - "serve-favicon": "^2.3.0", - "sanitize": "^2.1.0" + "sanitize": "^2.1.0", + "serve-favicon": "^2.3.0" } } diff --git a/routes/index.js b/routes/index.js index d2a4ece7..d32bf6e5 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,6 +1,6 @@ const express = require('express'); const router = express.Router(); -const request = require('request'); +const fetch = require('cross-fetch'); const config = require('../config'); const { badge } = require('../lib/badge'); @@ -16,48 +16,45 @@ router.get('/', function(req, res) { 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 + async function doInvite() { + const email = req.body.email; + const channelId = config.slackChannel; + const body = { + "channel": channelId, + "text": req.body.email + ' requested to join R-Ladies Seattle Slack. Please add them (see channel descriptions for instructions) and mark this message when complete.' + }; + const url = 'https://'+ config.slackUrl + '/api/chat.postMessage'; + try { + const response = await fetch(url, { + method: 'POST', + body: JSON.stringify(body), + headers: { + "Content-Type": "application/json;charset=utf-8", + "Authorization": `Bearer ${config.slacktoken}` } - }, 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) { + }); + if(response.ok) { + const data = await response.json(); + if (data.ok) { res.render('result', { community: config.community, - message: 'Success! Check “'+ req.body.email +'” for an invite from Slack.' + message: "Success! Once an admin approves your request, you will receive an invite to Slack at
" + req.body.email }); } 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.
' + - 'Visit '+ config.community +'' - }); - 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 - }); + console.error(data); + throw new Error('Non-ok response') } + } + } catch(err) { + console.error(err); + error = 'Something has gone wrong. Please try again later or contact seattle@rladies.org if the problem persists.'; + res.render('result', { + community: config.community, + message: 'Failed! ' + error, + isFailed: true }); + + } } if (!!config.recaptchaSiteKey && !!config.recaptchaSecretKey) { request.post({