Skip to content

Commit 3747ee2

Browse files
authored
Fix message event struct (#1391)
This has been the source of many bugs. Slack's docs say that a 'message event' contains a 'message' which is the same type as other representations of a message in slack. However, the library has previously represented this as a nested MessageEvent which is recursively nested. That creates lots of bugs, as you can never know if you're looking at the outer MessageEvent (which contains the event information) or the inner MessageEvent (which contains the message itself). Instead, we can use our canonical Message instead. Note that we also have to add a `root` onto Message, as this is populated for thread_broadcast events (for legacy reasons).
2 parents 523d99d + d7986b7 commit 3747ee2

File tree

3 files changed

+8
-22
lines changed

3 files changed

+8
-22
lines changed

messages.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ type Message struct {
1717
Msg
1818
SubMessage *Msg `json:"message,omitempty"`
1919
PreviousMessage *Msg `json:"previous_message,omitempty"`
20+
// Root is the message that was broadcast to the channel when the SubType is
21+
// thread_broadcast. If this is not a thread_broadcast message event, this
22+
// value is nil.
23+
Root *Msg `json:"root,omitempty"`
2024
}
2125

2226
// Msg SubTypes (https://api.slack.com/events/message)

slackevents/inner_events.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,8 @@ type MessageEvent struct {
284284
SourceTeam string `json:"source_team,omitempty"`
285285

286286
// Edited Message
287-
Message *MessageEvent `json:"message,omitempty"`
288-
PreviousMessage *MessageEvent `json:"previous_message,omitempty"`
289-
Edited *Edited `json:"edited,omitempty"`
287+
Message *slack.Message `json:"message,omitempty"`
288+
PreviousMessage *slack.Message `json:"previous_message,omitempty"`
290289

291290
// Deleted Message
292291
DeletedTimeStamp string `json:"deleted_ts,omitempty"`
@@ -298,19 +297,6 @@ type MessageEvent struct {
298297
BotID string `json:"bot_id,omitempty"`
299298
Username string `json:"username,omitempty"`
300299
Icons *Icon `json:"icons,omitempty"`
301-
302-
Upload bool `json:"upload"`
303-
Files []File `json:"files"`
304-
305-
Blocks slack.Blocks `json:"blocks,omitempty"`
306-
Attachments []slack.Attachment `json:"attachments,omitempty"`
307-
308-
Metadata slack.SlackMetadata `json:"metadata,omitempty"`
309-
310-
// Root is the message that was broadcast to the channel when the SubType is
311-
// thread_broadcast. If this is not a thread_broadcast message event, this
312-
// value is nil.
313-
Root *MessageEvent `json:"root"`
314300
}
315301

316302
// MemberJoinedChannelEvent A member joined a public or private channel

slackevents/inner_events_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,12 @@ func TestThreadBroadcastEvent(t *testing.T) {
423423
t.Error(err)
424424
}
425425

426-
if me.Root != nil {
427-
t.Error("me.Root should be nil")
428-
}
429-
430426
if me.Message.Root == nil {
431427
t.Fatal("me.Message.Root is nil")
432428
}
433429

434-
if me.Message.Root.TimeStamp != "1355517523.000005" {
435-
t.Errorf("me.Message.Root.TimeStamp = %q, want %q", me.Root.TimeStamp, "1355517523.000005")
430+
if me.Message.Root.Timestamp != "1355517523.000005" {
431+
t.Errorf("me.Message.Root.Timestamp = %q, want %q", me.Message.Root.Timestamp, "1355517523.000005")
436432
}
437433
}
438434

0 commit comments

Comments
 (0)