This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
forked from rauchg/slackin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
182 lines (157 loc) · 4.43 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
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
// es6 runtime requirements
import 'babel-polyfill'
// their code
import express from 'express'
import sockets from 'socket.io'
import { json } from 'body-parser'
import { Server as http } from 'http'
import remail from 'email-regex'
import dom from 'vd'
// our code
import Slack from './slack'
import invite from './slack-invite'
import badge from './badge'
import splash from './splash'
import iframe from './iframe'
import log from './log'
export default function slackin ({
token,
interval = 5000, // jshint ignore:line
org,
css,
coc,
path='/',
channels,
silent = false // jshint ignore:line,
}){
// must haves
if (!token) throw new Error('Must provide a `token`.')
if (!org) throw new Error('Must provide an `org`.')
if (channels) {
// convert to an array
channels = channels.split(',').map((channel) => {
// sanitize channel name
if ('#' === channel[0]) return channel.substr(1)
return channel
})
}
// setup app
let app = express()
let srv = http(app)
let assets = __dirname + '/assets'
// fetch data
let slack = new Slack({ token, interval, org })
slack.setMaxListeners(Infinity)
// capture stats
log(slack, silent)
// middleware for waiting for slack
app.use((req, res, next) => {
if (slack.ready) return next()
slack.once('ready', next)
})
// splash page
app.get('/', (req, res) => {
let { name, logo } = slack.org
let { active, total } = slack.users
if (!name) return res.send(404)
let page = dom('html',
dom('head',
dom('title',
'Join ', name, ' on Slack!'
),
dom('meta name=viewport content="width=device-width,initial-scale=1.0,minimum-scale=1.0,user-scalable=no"'),
dom('link rel="shortcut icon" href=https://slack.global.ssl.fastly.net/272a/img/icons/favicon-32.png'),
css && dom('link rel=stylesheet', { href: css })
),
splash({ coc, path, css, name, org, logo, channels, active, total })
)
res.type('html')
res.send(page.toHTML())
})
// static files
app.use('/assets', express.static(assets))
// invite endpoint
app.post('/invite', json(), (req, res, next) => {
let chanId
if (channels) {
let channel = req.body.channel
if (!channels.includes(channel)) {
return res
.status(400)
.json({ msg: 'Not a permitted channel' })
}
chanId = slack.getChannelId(channel)
if (!chanId) {
return res
.status(400)
.json({ msg: `Channel not found "${channel}"` })
}
}
let email = req.body.email
if (!email) {
return res
.status(400)
.json({ msg: 'No email provided' })
}
if (!remail().test(email)) {
return res
.status(400)
.json({ msg: 'Invalid email' })
}
if (coc && '1' != req.body.coc) {
return res
.status(400)
.json({ msg: 'Agreement to CoC is mandatory' })
}
invite({ token, org, email, channel: chanId }, err => {
if (err) {
if (err.message === `Sending you to Slack...`) {
return res
.status(303)
.json({ msg: err.message, redirectUrl: `https://${org}.slack.com` })
}
return res
.status(400)
.json({ msg: err.message })
}
res
.status(200)
.json({ msg: 'WOOT. Check your email!' })
})
})
// iframe
app.get('/iframe', (req, res) => {
let large = 'large' in req.query
let { active, total } = slack.users
res.type('html')
res.send(iframe({ path, active, total, large }).toHTML())
})
app.get('/iframe/dialog', (req, res) => {
let large = 'large' in req.query
let { name } = slack.org
let { active, total } = slack.users
if (!name) return res.send(404)
let dom = splash({ coc, path, name, org, channels, active, total, large, iframe: true })
res.type('html')
res.send(dom.toHTML())
})
// badge js
app.use('/slackin.js', express.static(assets + '/badge.js'))
// badge rendering
app.get('/badge.svg', (req, res) => {
res.type('svg')
res.set('Cache-Control', 'max-age=0, no-cache')
res.set('Pragma', 'no-cache')
res.send(badge(slack.users).toHTML())
})
// realtime
sockets(srv).on('connection', socket => {
socket.emit('data', slack.users)
let change = (key, val) => socket.emit(key, val)
slack.on('change', change)
socket.on('disconnect', () => {
slack.removeListener('change', change)
})
})
return srv
}