-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
86 lines (77 loc) · 3.04 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
const bodyParser = require('body-parser');
const express = require('express');
const { AccountManager } = require('./app/controllers/account-manager');
const { FileManager } = require('./app/controllers/file-manager');
const { solidLogin, solidLogout } = require('./app/controllers/solid');
const { httpStatus } = require('./app/util/http');
const { commandVerify } = require('./app/middlewares/commands');
const { slackVerify } = require('./app/middlewares/slack');
const { solidVerify } = require('./app/middlewares/solid');
// Main app
const app = express();
const PORT = process.env.PORT || 3000;
// Handle app entrypoint
const entryHandler = async (req, res) => {
const commands = req.commands;
const command = commands[0];
switch (command) {
case 'profile':
const profileResponse = await FileManager.exec(req, res, command);
return profileResponse;
case 'account':
const accountResponse = await FileManager.exec(req, res, command);
return accountResponse;
case 'file':
const fileResponse = await FileManager.exec(req, res, command);
return fileResponse;
case 'logout':
const logoutResponse = await solidLogout(req, res);
return logoutResponse;
default:
return res.send(`Sorry, I don't recognize your command: \`${command}\`. For the complete set of available commands, please type the following command: \`/solid help\``);
}
};
// Handle interactive components, like modal buttons
const actionHandler = async (req, res) => {
// this is a necessary acknowledgment response
// for most interactions in Slack apps
res.status(httpStatus.OK).send();
const payload = JSON.parse(req.body.payload);
const type = payload.type;
const callbackId = payload.view.callback_id;
let command;
switch (callbackId) {
case 'login-manager':
const loginResponse = await solidLogin(req, res);
return loginResponse;
case 'file-manager':
command = payload.actions[0].action_id;
const contentResponse = await FileManager.exec(req, res, command);
return contentResponse;
case 'save-content':
command = callbackId;
const saveResponse = await FileManager.exec(req, res, command);
return saveResponse;
default:
return res.status(httpStatus.OK).send(`Unrecognized interactive component \`callback_id\`: \`${callbackId}\``);
}
};
// Parse body as json
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Slack verification middleware
app.use(slackVerify);
// TODO: Currently, this does not work well
// with solidVerify middleware, so we have placed
// it before it since we expect to hit this
// endpoint exclusively through the entry point.
// It may be worth fixing that middleware method
// to work with this endpoint.
app.post('/action', actionHandler);
// Command verification middleware
app.use(commandVerify);
// Solid verification middleware
app.use(solidVerify);
// SolidSlack entrypoint
app.post('/entry', entryHandler);
app.listen(PORT, () => console.log(`Solid Slack is listening at http://0.0.0.0:${PORT}`));