-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
132 lines (120 loc) · 3.86 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
const rootPrefix = '.',
configProvider = require(rootPrefix + '/lib/configProvider'),
slackAppConstants = require(rootPrefix + '/lib/constants/slackApp'),
slackWrapper = require(rootPrefix + '/lib/slack/Wrapper'),
CommonMiddlewares = require(rootPrefix + '/lib/middlewareMethods/Common'),
InteractiveMiddlewares = require(rootPrefix + '/lib/middlewareMethods/Interactive'),
SlashCommandMiddlewares = require(rootPrefix + '/lib/middlewareMethods/SlashCommand'),
Message = require(rootPrefix + '/lib/slack/Message'),
Modal = require(rootPrefix + '/lib/slack/Modal');
/**
* Class for Slack Admin.
*
* @class SlackAdmin
*
* @param {array} appConfigs
* @param {object} whitelistedChannelIds
* @param {string} domain
* @param {array} whitelistedUsers
*
*/
class SlackAdmin {
constructor(appConfigs, whitelistedChannelIds, whitelistedUsers) {
configProvider.set('app_config', appConfigs);
configProvider.set('whitelisted_channel_ids', whitelistedChannelIds);
configProvider.set('whitelisted_users', whitelistedUsers);
slackAppConstants.setSlackAppConfigById();
slackWrapper.init();
}
/**
* Validator methods : Exposed methods for common, slash command and interactive endpoint validations
*
* @returns {{common: *, slashCommands: *, interactive: *}}
*/
get validators() {
return {
common: CommonMiddlewares.CommonMiddleWareMethod,
interactive: InteractiveMiddlewares.InteractiveMiddleWareMethod,
slashCommands: SlashCommandMiddlewares.SlashCommandMiddlewareMethod
};
}
/**
* Slack admin common middlewares
*
* @returns {function(...[*]=)}
*/
get commonMiddlewares() {
const oThis = this;
return async function(req, res, next) {
try {
const response = await oThis.validators.common(req.body, req.query, req.headers, req.method);
req.body = response.requestBody;
req.query = response.requestQuery;
req.internalDecodedParams = response.internalDecodedParams;
req.decodedParams = response.decodedParams;
next();
} catch (errorMessage) {
console.error('Common middleaware error:', errorMessage);
return res.status(200).json('Something went wrong.');
}
};
}
/**
* Slack admin interactive endpoints middlewares
*
* @returns {function(...[*]=)}
*/
get interactiveEndpointMiddlewares() {
const oThis = this;
return async function(req, res, next) {
try {
const response = await oThis.validators.interactive(
req.params,
req.body,
req.headers,
req.decodedParams,
req.internalDecodedParams
);
req.params = response.requestParams;
req.sanitizedHeaders = response.internalDecodedParams.headers;
req.internalDecodedParams = response.internalDecodedParams;
req.decodedParams = response.decodedParams;
next();
} catch (err) {
console.error('Interactive endpoint middleware error:', JSON.stringify(err));
return res.status(200).json('something_went_wrong');
}
};
}
/**
* Slack command middlewares
*
* @returns {function(...[*]=)}
*/
get slashCommandMiddlewares() {
const oThis = this;
return async function(req, res, next) {
try {
const response = await oThis.validators.slashCommands(req.body, req.rawBody, req.headers, req.decodedParams);
req.body = response.requestBody;
req.decodedParams = response.decodedParams;
next();
} catch (err) {
console.error('Slash command middleware error:', JSON.stringify(err));
return res.status(200).json('Something went wrong.');
}
};
}
/**
* Slack interactive element middlewares
*
* @returns {{Message, Modal}}
*/
get interactiveElements() {
return {
Message: Message,
Modal: Modal
};
}
}
module.exports = SlackAdmin;