forked from pusher-community/customer-support-chat-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (73 loc) · 3.78 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
// ----------------------------------------------------------------------------
// Load dependencies...
// ----------------------------------------------------------------------------
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser');
const Chatkit = require('pusher-chatkit-server')
// ----------------------------------------------------------------------------
// Instantiate Express and Chatkit
// ----------------------------------------------------------------------------
const app = express()
const chatkit = new Chatkit.default(require('./config.js'));
// ----------------------------------------------------------------------------
// Load Express Middlewares
// ----------------------------------------------------------------------------
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'assets')))
// ----------------------------------------------------------------------------
// Define Routes
// ----------------------------------------------------------------------------
app.post('/session/load', (req, res, next) => {
// Attempt to create a new user with the email will serving as the ID of the user.
// If there is no user matching the ID, we create one but if there is one we skip
// creating and go straight into fetching the chat room for that user
chatkit.createUser(req.body.email, req.body.name)
.then(() => getUserRoom(req, res, next, false))
.catch(err => {
(err.error_type === 'services/chatkit/user/user_already_exists')
? getUserRoom(req, res, next, true)
: next(err)
})
function getUserRoom(req, res, next, existingAccount) {
const name = req.body.name
const email = req.body.email
// Get the list of users the user belongs to. Check within that room list for one whos
// name matches the users ID. If we find one, we return that as the response, else
// we create the room and return it as the response.
chatkit.apiRequest({method: 'GET', 'path': `/users/${email}/rooms`})
.then(rooms => {
let clientRoom = false
// Loop through user rooms to see if there is already a room for the client
rooms.forEach(room => (room.name === email ? (clientRoom = room) : false))
if (clientRoom && clientRoom.id) {
return res.json(clientRoom)
}
const createRoomRequest = {
method: 'POST',
path: '/rooms',
jwt: chatkit.generateAccessToken({userId: email}).token,
body: { name: email, private: true, user_ids: ['chatkit-dashboard'] },
};
// Since we can't find a client room, we will create one and return that.
chatkit.apiRequest(createRoomRequest)
.then(room => res.json(room))
.catch(err => next(new Error(`${err.error_type} - ${err.error_description}`)))
})
.catch(err => next(new Error(`ERROR: ${err.error_type} - ${err.error_description}`)))
}
})
app.post('/session/auth', (req, res) => {
res.json(chatkit.authenticate(req.body, req.query.user_id))
})
app.get('/admin', (req, res) => {
res.sendFile('admin.html', {root: __dirname + '/views'})
})
app.get('/', (req, res) => {
res.sendFile('index.html', {root: __dirname + '/views'})
})
// ----------------------------------------------------------------------------
// Start Express Application
// ----------------------------------------------------------------------------
app.listen(3000, () => console.log("Application listening on port 3000!!!"))