-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevent.rs
278 lines (256 loc) · 8.16 KB
/
event.rs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use crate::event_api::event::Event;
use crate::payloads::interactive::{InteractivePayload, SlashPayload};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[derive(Deserialize, Serialize, Debug, PartialEq)]
#[serde(tag = "type")]
pub enum SocketModeEvent {
#[serde(rename = "hello")]
HelloEvent(HelloEvent),
#[serde(rename = "disconnect")]
DisconnectEvent(DisconnectEvent),
#[serde(rename = "events_api")]
EventsAPI(EventsAPI),
#[serde(rename = "interactive")]
InteractiveEvent(InteractiveEvent),
#[serde(rename = "slash_commands")]
SlashCommandsEvent(SlashCommandsEvent),
}
impl SocketModeEvent {
pub fn event_type(&self) -> SocketModeEventType {
match self {
SocketModeEvent::HelloEvent(HelloEvent { .. }) => SocketModeEventType::Hello,
SocketModeEvent::DisconnectEvent(DisconnectEvent { .. }) => {
SocketModeEventType::Disconnect
}
SocketModeEvent::EventsAPI(EventsAPI { .. }) => SocketModeEventType::EventsAPI,
SocketModeEvent::InteractiveEvent(InteractiveEvent { .. }) => {
SocketModeEventType::Interactive
}
SocketModeEvent::SlashCommandsEvent(SlashCommandsEvent { .. }) => {
SocketModeEventType::SlashCommands
}
}
}
}
#[derive(Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum SocketModeEventType {
Hello,
Disconnect,
EventsAPI,
Interactive,
SlashCommands,
}
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
pub struct DebugInfo {
pub host: Option<String>,
pub started: Option<String>,
pub build_number: Option<i32>,
pub approximate_connection_time: Option<i32>,
}
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct HelloEvent {
pub connection_info: Option<ConnectionInfo>,
pub num_connections: Option<i32>,
pub debug_info: Option<DebugInfo>,
}
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
pub struct ConnectionInfo {
pub app_id: Option<String>,
}
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct DisconnectEvent {
pub reason: DisconnectReason,
pub debug_info: Option<DebugInfo>,
}
#[derive(Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum DisconnectReason {
LinkDisabled,
Warning,
RefreshRequested,
}
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct EventsAPI {
pub envelope_id: String,
pub accepts_response_payload: bool,
pub payload: Event,
}
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct InteractiveEvent {
pub envelope_id: String,
pub accepts_response_payload: bool,
pub payload: InteractivePayload,
}
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct SlashCommandsEvent {
pub envelope_id: String,
pub accepts_response_payload: bool,
pub payload: SlashPayload,
}
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct AcknowledgeMessage<'s> {
pub envelope_id: &'s str,
}
#[cfg(test)]
mod test {
use super::*;
use crate::event_api::event::*;
use crate::payloads::interactive::InteractiveEventType;
#[test]
fn deserialize_hello_event() {
let json = r##"{
"type": "hello",
"connection_info": {
"app_id": "app_id"
},
"num_connections": 1,
"debug_info": {
"host": "host"
}
}"##;
let event = serde_json::from_str::<SocketModeEvent>(json).unwrap();
match event {
SocketModeEvent::HelloEvent(HelloEvent {
connection_info,
num_connections,
debug_info,
}) => {
assert_eq!(connection_info.unwrap().app_id.unwrap(), "app_id");
assert_eq!(num_connections.unwrap(), 1);
assert_eq!(debug_info.unwrap().host.unwrap(), "host");
}
_ => panic!("Event deserialize into incorrect variant"),
}
}
#[test]
fn deserialize_disconnect_event() {
let json = r##"{
"type": "disconnect",
"reason": "link_disabled",
"debug_info": {
"host": "wss-111.slack.com"
}
}"##;
let event = serde_json::from_str::<SocketModeEvent>(json).unwrap();
match event {
SocketModeEvent::DisconnectEvent(DisconnectEvent { reason, debug_info }) => {
assert_eq!(reason, DisconnectReason::LinkDisabled);
assert_eq!(debug_info.unwrap().host.unwrap(), "wss-111.slack.com");
}
_ => panic!("Event deserialize into incorrect variant"),
}
}
#[test]
fn deserialize_events_api() {
let json = r##"{
"type": "events_api",
"envelope_id": "dbdd0ef3-1543-4f94-bfb4-133d0e6c1545",
"accepts_response_payload": false,
"payload": {
"token": "bHKJ2n9AW6Ju3MjciOHfbA1b",
"team_id": "T1234567890",
"api_app_id": "A0000000000",
"event_id": "Ev0000000000",
"event_time": 1600000000,
"type": "event_callback",
"event": {
"type": "app_home_opened",
"user": "U061F7AUR",
"channel": "D0LAN2Q65",
"event_ts": "1515449522000016",
"tab": "home",
"view": {
"id": "VPASKP233"
}
}
}
}"##;
let event = serde_json::from_str::<SocketModeEvent>(json).unwrap();
match event {
SocketModeEvent::EventsAPI(EventsAPI {
envelope_id,
accepts_response_payload,
payload,
}) => {
assert_eq!(envelope_id, "dbdd0ef3-1543-4f94-bfb4-133d0e6c1545");
assert!(!accepts_response_payload, "false");
match payload.event {
EventCallback::AppHomeOpened { user, .. } => {
assert_eq!(user, "U061F7AUR");
}
_ => panic!("Event callback deserialize into incorrect variant"),
}
}
_ => panic!("Event deserialize into incorrect variant"),
}
}
#[test]
fn deserialize_interactive_event() {
let json = r##"{
"type": "interactive",
"envelope_id": "dbdd0ef3-1543-4f94-bfb4-133d0e6c1545",
"accepts_response_payload": true,
"payload": {
"type": "view_submission"
}
}"##;
let event = serde_json::from_str::<SocketModeEvent>(json).unwrap();
match event {
SocketModeEvent::InteractiveEvent(InteractiveEvent {
envelope_id,
accepts_response_payload,
payload,
}) => {
assert_eq!(envelope_id, "dbdd0ef3-1543-4f94-bfb4-133d0e6c1545");
assert!(accepts_response_payload, "true");
assert_eq!(payload.type_filed, InteractiveEventType::ViewSubmission);
}
_ => panic!("Event deserialize into incorrect variant"),
}
}
#[test]
fn deserialize_slash_commands_event() {
let json = r##"{
"type": "slash_commands",
"envelope_id": "dbdd0ef3-1543-4f94-bfb4-133d0e6c1545",
"accepts_response_payload": true,
"payload": {
"token": "bHKJ2n9AW6Ju3MjciOHfbA1b"
}
}"##;
let event = serde_json::from_str::<SocketModeEvent>(json).unwrap();
match event {
SocketModeEvent::SlashCommandsEvent(SlashCommandsEvent {
envelope_id,
accepts_response_payload,
payload,
}) => {
assert_eq!(envelope_id, "dbdd0ef3-1543-4f94-bfb4-133d0e6c1545");
assert!(accepts_response_payload, "true");
assert_eq!(payload.token.unwrap(), "bHKJ2n9AW6Ju3MjciOHfbA1b");
}
_ => panic!("Event deserialize into incorrect variant"),
}
}
#[test]
fn serialize_acknowledge_message() {
let json = serde_json::to_string_pretty(&AcknowledgeMessage {
envelope_id: "xxxxxxxxxxxxxxxxxxxxx",
})
.unwrap();
let expect = r##"{
"envelope_id": "xxxxxxxxxxxxxxxxxxxxx"
}"##;
assert_eq!(expect, json);
}
}