Skip to content

Commit 6b7030d

Browse files
authored
Merge pull request #490 from seratch/issue-489
Fix #489 Event type extraction failure in slack-app-backend (no effect to Bolt)
2 parents 05f9c58 + fe88a05 commit 6b7030d

File tree

2 files changed

+86
-82
lines changed

2 files changed

+86
-82
lines changed
Lines changed: 21 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,39 @@
11
package com.slack.api.app_backend.events;
22

3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonParser;
5+
import com.google.gson.JsonSyntaxException;
6+
import lombok.extern.slf4j.Slf4j;
7+
8+
@Slf4j
39
public class EventTypeExtractorImpl implements EventTypeExtractor {
410

511
@Override
612
public String extractEventType(String json) {
7-
StringBuilder sb = new StringBuilder();
8-
char[] chars = json.toCharArray();
9-
boolean isInsideEventData = false;
10-
for (int idx = 0; idx < (chars.length - 7); idx++) {
11-
if (!isInsideEventData && chars[idx] == '"'
12-
&& chars[idx + 1] == 'e'
13-
&& chars[idx + 2] == 'v'
14-
&& chars[idx + 3] == 'e'
15-
&& chars[idx + 4] == 'n'
16-
&& chars[idx + 5] == 't'
17-
&& chars[idx + 6] == '"'
18-
&& chars[idx + 7] == ':') {
19-
idx = idx + 8;
20-
isInsideEventData = true;
21-
}
22-
23-
if (isInsideEventData && chars[idx] == '"'
24-
&& chars[idx + 1] == 't'
25-
&& chars[idx + 2] == 'y'
26-
&& chars[idx + 3] == 'p'
27-
&& chars[idx + 4] == 'e'
28-
&& chars[idx + 5] == '"'
29-
&& chars[idx + 6] == ':') {
30-
idx = idx + 7;
31-
int doubleQuoteCount = 0;
32-
boolean isPreviousCharEscape = false;
33-
while (doubleQuoteCount < 2 && idx < chars.length) {
34-
char c = chars[idx];
35-
if (c == '"' && !isPreviousCharEscape) {
36-
doubleQuoteCount++;
37-
} else {
38-
if (doubleQuoteCount == 1) {
39-
sb.append(c);
40-
}
41-
}
42-
isPreviousCharEscape = c == '\\';
43-
idx++;
44-
}
45-
break;
46-
}
47-
}
48-
return sb.toString();
13+
return extractFieldUnderEvent(json, "type");
4914
}
5015

5116
@Override
5217
public String extractEventSubtype(String json) {
53-
StringBuilder sb = new StringBuilder();
54-
char[] chars = json.toCharArray();
55-
boolean isInsideEventData = false;
56-
for (int idx = 0; idx < (chars.length - 7); idx++) {
57-
if (!isInsideEventData && chars[idx] == '"'
58-
&& chars[idx + 1] == 'e'
59-
&& chars[idx + 2] == 'v'
60-
&& chars[idx + 3] == 'e'
61-
&& chars[idx + 4] == 'n'
62-
&& chars[idx + 5] == 't'
63-
&& chars[idx + 6] == '"'
64-
&& chars[idx + 7] == ':') {
65-
idx = idx + 8;
66-
isInsideEventData = true;
67-
}
18+
return extractFieldUnderEvent(json, "subtype");
19+
}
6820

69-
if (isInsideEventData && chars[idx] == '"'
70-
&& chars[idx + 1] == 's'
71-
&& chars[idx + 2] == 'u'
72-
&& chars[idx + 3] == 'b'
73-
&& chars[idx + 4] == 't'
74-
&& chars[idx + 5] == 'y'
75-
&& chars[idx + 6] == 'p'
76-
&& chars[idx + 7] == 'e'
77-
&& chars[idx + 8] == '"'
78-
&& chars[idx + 9] == ':') {
79-
idx = idx + 10;
80-
int doubleQuoteCount = 0;
81-
boolean isPreviousCharEscape = false;
82-
while (doubleQuoteCount < 2 && idx < chars.length) {
83-
char c = chars[idx];
84-
if (c == '"' && !isPreviousCharEscape) {
85-
doubleQuoteCount++;
86-
} else {
87-
if (doubleQuoteCount == 1) {
88-
sb.append(c);
89-
}
21+
private static String extractFieldUnderEvent(String json, String fieldName) {
22+
try {
23+
JsonElement root = JsonParser.parseString(json);
24+
if (root != null && root.isJsonObject() && root.getAsJsonObject().has("event")) {
25+
JsonElement event = root.getAsJsonObject().get("event");
26+
if (event.isJsonObject() && event.getAsJsonObject().has(fieldName)) {
27+
JsonElement eventType = event.getAsJsonObject().get(fieldName);
28+
if (eventType.isJsonPrimitive()) {
29+
return eventType.getAsString();
9030
}
91-
isPreviousCharEscape = c == '\\';
92-
idx++;
9331
}
94-
break;
9532
}
33+
} catch (JsonSyntaxException e) {
34+
log.debug("Failed to parse {} as a JSON data", json, e);
9635
}
97-
return sb.toString();
36+
return "";
9837
}
9938

10039
}

slack-app-backend/src/test/java/test_locally/app_backend/events/EventTypeDetectorImplTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ public class EventTypeDetectorImplTest {
1111

1212
private EventTypeExtractor eventTypeExtractor = new EventTypeExtractorImpl();
1313

14+
@Test
15+
public void notFound() {
16+
{
17+
String eventType = eventTypeExtractor.extractEventType("{}");
18+
assertThat(eventType, is(""));
19+
}
20+
{
21+
String eventType = eventTypeExtractor.extractEventType("{\"event\": {}}");
22+
assertThat(eventType, is(""));
23+
}
24+
{
25+
String eventType = eventTypeExtractor.extractEventSubtype("{}");
26+
assertThat(eventType, is(""));
27+
}
28+
{
29+
String eventType = eventTypeExtractor.extractEventSubtype("{\"event\": {}}");
30+
assertThat(eventType, is(""));
31+
}
32+
}
33+
1434
@Test
1535
public void detect_goodbye() {
1636
String payload = "{\n" +
@@ -54,4 +74,49 @@ public void detect_escape() {
5474
assertThat(eventType, is("message"));
5575
}
5676

77+
@Test
78+
public void issue_489() {
79+
String payload = "{\n" +
80+
" \"authed_users\": [\n" +
81+
" \"RETRACTED\"\n" +
82+
" ],\n" +
83+
" \"event_id\": \"RETRACTED\",\n" +
84+
" \"api_app_id\": \"RETRACTED\",\n" +
85+
" \"team_id\": \"RETRACTED\",\n" +
86+
" \"event\": {\n" +
87+
" \"client_msg_id\": \"b5ac4998-4526-4d0b-8bb0-7bd614c1b774\",\n" +
88+
" \"blocks\": [\n" +
89+
" {\n" +
90+
" \"elements\": [\n" +
91+
" {\n" +
92+
" \"elements\": [\n" +
93+
" {\n" +
94+
" \"text\": \"test\",\n" +
95+
" \"type\": \"text\"\n" +
96+
" }\n" +
97+
" ],\n" +
98+
" \"type\": \"rich_text_section\"\n" +
99+
" }\n" +
100+
" ],\n" +
101+
" \"type\": \"rich_text\",\n" +
102+
" \"block_id\": \"Ws3Y\"\n" +
103+
" }\n" +
104+
" ],\n" +
105+
" \"event_ts\": \"1592257234.003200\",\n" +
106+
" \"channel\": \"RETRACTED\",\n" +
107+
" \"text\": \"test\",\n" +
108+
" \"team\": \"RETRACTED\",\n" +
109+
" \"type\": \"message\",\n" +
110+
" \"channel_type\": \"channel\",\n" +
111+
" \"user\": \"RETRACTED\",\n" +
112+
" \"ts\": \"1592257234.003200\"\n" +
113+
" },\n" +
114+
" \"type\": \"event_callback\",\n" +
115+
" \"event_time\": 1592257234,\n" +
116+
" \"token\": \"RETRACTED\"\n" +
117+
"}";
118+
String eventType = eventTypeExtractor.extractEventType(payload);
119+
assertThat(eventType, is("message"));
120+
}
121+
57122
}

0 commit comments

Comments
 (0)