1
1
const express = require ( 'express' ) ;
2
2
const router = express . Router ( ) ;
3
- const request = require ( 'request ' ) ;
3
+ const fetch = require ( 'cross-fetch ' ) ;
4
4
5
5
const config = require ( '../config' ) ;
6
6
const { badge } = require ( '../lib/badge' ) ;
@@ -16,48 +16,45 @@ router.get('/', function(req, res) {
16
16
17
17
router . post ( '/invite' , function ( req , res ) {
18
18
if ( req . body . email && ( ! config . inviteToken || ( ! ! config . inviteToken && req . body . token === config . inviteToken ) ) ) {
19
- function doInvite ( ) {
20
- request . post ( {
21
- url : 'https://' + config . slackUrl + '/api/users.admin.invite' ,
22
- form : {
23
- email : req . body . email ,
24
- token : config . slacktoken ,
25
- set_active : true
19
+ async function doInvite ( ) {
20
+ const email = req . body . email ;
21
+ const channelId = config . slackChannel ;
22
+ const body = {
23
+ "channel" : channelId ,
24
+ "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.'
25
+ } ;
26
+ const url = 'https://' + config . slackUrl + '/api/chat.postMessage' ;
27
+ try {
28
+ const response = await fetch ( url , {
29
+ method : 'POST' ,
30
+ body : JSON . stringify ( body ) ,
31
+ headers : {
32
+ "Content-Type" : "application/json;charset=utf-8" ,
33
+ "Authorization" : `Bearer ${ config . slacktoken } `
26
34
}
27
- } , function ( err , httpResponse , body ) {
28
- // body looks like:
29
- // {"ok":true}
30
- // or
31
- // {"ok":false,"error":"already_invited"}
32
- if ( err ) { return res . send ( 'Error:' + err ) ; }
33
- body = JSON . parse ( body ) ;
34
- if ( body . ok ) {
35
+ } ) ;
36
+ if ( response . ok ) {
37
+ const data = await response . json ( ) ;
38
+ if ( data . ok ) {
35
39
res . render ( 'result' , {
36
40
community : config . community ,
37
- message : ' Success! Check “' + req . body . email + '” for an invite from Slack.'
41
+ message : " Success! Once an admin approves your request, you will receive an invite to Slack at<br>" + req . body . email
38
42
} ) ;
39
43
} else {
40
- let error = body . error ;
41
- if ( error === 'already_invited' || error === 'already_in_team' ) {
42
- res . render ( 'result' , {
43
- community : config . community ,
44
- message : 'Success! You were already invited.<br>' +
45
- 'Visit <a href="https://' + config . slackUrl + '">' + config . community + '</a>'
46
- } ) ;
47
- return ;
48
- } else if ( error === 'invalid_email' ) {
49
- error = 'The email you entered is an invalid email.' ;
50
- } else if ( error === 'invalid_auth' ) {
51
- error = 'Something has gone wrong. Please contact a system administrator.' ;
52
- }
53
-
54
- res . render ( 'result' , {
55
- community : config . community ,
56
- message : 'Failed! ' + error ,
57
- isFailed : true
58
- } ) ;
44
+ console . error ( data ) ;
45
+ throw new Error ( 'Non-ok response' )
59
46
}
47
+ }
48
+ } catch ( err ) {
49
+ console . error ( err ) ;
50
+ error = 'Something has gone wrong. Please try again later or contact [email protected] if the problem persists.' ;
51
+ res . render ( 'result' , {
52
+ community : config . community ,
53
+ message : 'Failed! ' + error ,
54
+ isFailed : true
60
55
} ) ;
56
+
57
+ }
61
58
}
62
59
if ( ! ! config . recaptchaSiteKey && ! ! config . recaptchaSecretKey ) {
63
60
request . post ( {
0 commit comments