/
server.js
191 lines (173 loc) · 5.19 KB
/
server.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
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
require('dotenv').config()
const express = require('express')
const app = express()
const axios = require('axios')
const path = require('path')
const nodemailer = require('nodemailer')
const createMail = require('./createmail')
const urlcrypt = require('url-crypt')('~{ry*I)44==yU/]9<7DPk!Hj"R#:-/Z7(hTBnlRS=4CXF')
const mail = process.env.EMAIL
const password = process.env.PASSWORD
const token = process.env.SECRET
const b13 = process.env.B13
const b14 = process.env.B14
const b15 = process.env.B15
const b16 = process.env.B16
const b17 = process.env.B17
const b18 = process.env.B18
const outs = process.env.OUTS
const slack = process.env.SLACK_TOKEN
const webhookURL = process.env.INVITE_CHANNEL_WEBHOOK
// Get transporter services
const emailHost = process.env.EMAIL_HOST || 'smtp.gmail.com'
const emailSecure = process.env.EMAIL_SECURE || true
const emailPort = process.env.PORT || 465
const emailPool = process.env.POOL || true
app.use(express.static('public'))
app.get('/', (request, response) => {
response.sendFile(path.join(__dirname, '/views/index.html'))
})
const dict = {}
dict['2013'] = b13
dict['2014'] = b14
dict['2015'] = b15
dict['2016'] = b16
dict['2017'] = b17
dict['2018'] = b18
dict['outsider'] = outs
// Send the mail to the given email
app.get('/sendmail/:username/:id', (request, response, next) => {
const username = request.params.username
const id = request.params.id
const base64 = urlcrypt.cryptObj({
email: id,
username: username
})
// Invite to Slack
const slackUrl = `https://slack.com/api/users.admin.invite?token=${slack}&email=${id}`
axios.post(slackUrl)
// Post invitation message on Slack
const time = Math.round((new Date()).getTime() / 1000)
const message = `${username} got invited to iiitv organization on GitHub and Slack`
const options = {
'text': 'Welcome to IIITV',
'attachments': [
{
'color': '#36a64f',
'title': 'Invitation from IIITV',
'title_link': 'https://github.com/orgs/iiitv/people',
'text': message,
'footer': 'Slack API',
'ts': time
}
]
}
function sendMessage () {
return new Promise((resolve, reject) => {
axios.post(webhookURL, JSON.stringify(options))
.then(response => {
return resolve('SUCCESS: Sent slack webhook', response.data)
})
.catch(error => {
return reject(new Error('FAILED: Sent slack webhook', error))
})
})
}
const loop = async () => {
for (let i = 0; i < 3; i++) {
console.log('retrying sending message ', i)
try {
const res = await sendMessage()
console.log(res)
break
} catch (err) {
console.log(err)
}
}
}
loop()
const verificationurl = `https://${request.get('host')}/verify/${base64}`
var transporter = nodemailer.createTransport({
host: emailHost,
secure: emailSecure,
port: emailPort,
pool: emailPool,
auth: {
user: mail,
pass: password
}
})
const mailOptions = {
from: '"IIITV Coding Club" <codingclub@iiitv.ac.in>',
to: id,
cc: mail,
subject: 'Invitation to join IIITV Organization on GitHub',
html: createMail.createMail(username, verificationurl)
}
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error)
response.send({ status: 500 })
response.end()
} else {
response.send({ status: 200 })
response.end()
}
})
})
// Verify the email id through the link, and add as member
app.get('/verify/:base64', (request, response, next) => {
const encryptedData = request.params.base64
try {
const data = urlcrypt.decryptObj(encryptedData)
addMember(data)
.then((status) => {
response.status(status)
response.redirect('https://github.com/orgs/iiitv/teams')
})
.catch((err) => {
console.log(err)
response.status(400).send('Error occured. Please try again later.')
response.end()
})
} catch (e) {
response.status(400).send('Invalid Link.')
}
})
// Add the member as per their email id
const addMember = (data) => {
const promise = new Promise((resolve, reject) => {
let pref = data.email.substring(0, 4)
const checkInsti = data.email.split('@')[1]
if (checkInsti === 'iiitv.ac.in' || checkInsti === 'iiitvadodara.ac.in') {
console.log('IIITian')
const removeURL = `https://api.github.com/teams/${dict['outsider']}/memberships/${data.username}?access_token=${token}`
axios.delete(removeURL)
.then(response => {
console.log(response.data.url)
resolve(204)
})
.catch(error => {
reject(error)
})
} else {
pref = 'outsider'
}
console.log(pref)
const url = `https://api.github.com/teams/${dict[pref]}/memberships/${data.username}?access_token=${token}`
console.log(url)
axios.put(url)
.then(response => {
console.log(response.data.url)
resolve(200)
})
.catch(error => {
reject(error)
})
})
return promise
}
// listen for requests :)
const listener = app.listen(3000 || process.env.PORT, () => {
console.log(`Your app is listening on port ${listener.address().port}`)
})