Skip to content

Commit

Permalink
Merge pull request #1096 from hrkskov/feat/metadata_events
Browse files Browse the repository at this point in the history
Add metadata related event types
  • Loading branch information
kanata2 authored Aug 24, 2022
2 parents 4e3dfc2 + 95d0341 commit 870ec8a
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 29 deletions.
108 changes: 79 additions & 29 deletions slackevents/inner_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,47 @@ type WorkflowStepExecuteEvent struct {
EventTimestamp string `json:"event_ts"`
}

// MessageMetadataPostedEvent is sent, if a message with metadata is posted
type MessageMetadataPostedEvent struct {
Type string `json:"type"`
AppId string `json:"app_id"`
BotId string `json:"bot_id"`
UserId string `json:"user_id"`
TeamId string `json:"team_id"`
ChannelId string `json:"channel_id"`
Metadata *slack.SlackMetadata `json:"metadata"`
MessageTimestamp string `json:"message_ts"`
EventTimestamp string `json:"event_ts"`
}

// MessageMetadataUpdatedEvent is sent, if a message with metadata is deleted
type MessageMetadataUpdatedEvent struct {
Type string `json:"type"`
ChannelId string `json:"channel_id"`
EventTimestamp string `json:"event_ts"`
PreviousMetadata *slack.SlackMetadata `json:"previous_metadata"`
AppId string `json:"app_id"`
BotId string `json:"bot_id"`
UserId string `json:"user_id"`
TeamId string `json:"team_id"`
MessageTimestamp string `json:"message_ts"`
Metadata *slack.SlackMetadata `json:"metadata"`
}

// MessageMetadataDeletedEvent is sent, if a message with metadata is deleted
type MessageMetadataDeletedEvent struct {
Type string `json:"type"`
ChannelId string `json:"channel_id"`
EventTimestamp string `json:"event_ts"`
PreviousMetadata *slack.SlackMetadata `json:"previous_metadata"`
AppId string `json:"app_id"`
BotId string `json:"bot_id"`
UserId string `json:"user_id"`
TeamId string `json:"team_id"`
MessageTimestamp string `json:"message_ts"`
DeletedTimestamp string `json:"deleted_ts"`
}

type EventWorkflowStep struct {
WorkflowStepExecuteID string `json:"workflow_step_execute_id"`
WorkflowID string `json:"workflow_id"`
Expand Down Expand Up @@ -503,39 +544,48 @@ const (
EmojiChanged = EventsAPIType("emoji_changed")
// WorkflowStepExecute Happens, if a workflow step of your app is invoked
WorkflowStepExecute = EventsAPIType("workflow_step_execute")
// MessageMetadataPosted A message with metadata was posted
MessageMetadataPosted = EventsAPIType("message_metadata_posted")
// MessageMetadataPosted A message with metadata was updated
MessageMetadataUpdated = EventsAPIType("message_metadata_updated")
// MessageMetadataPosted A message with metadata was deleted
MessageMetadataDeleted = EventsAPIType("message_metadata_deleted")
)

// EventsAPIInnerEventMapping maps INNER Event API events to their corresponding struct
// implementations. The structs should be instances of the unmarshalling
// target for the matching event type.
var EventsAPIInnerEventMapping = map[EventsAPIType]interface{}{
AppMention: AppMentionEvent{},
AppHomeOpened: AppHomeOpenedEvent{},
AppUninstalled: AppUninstalledEvent{},
ChannelCreated: ChannelCreatedEvent{},
ChannelDeleted: ChannelDeletedEvent{},
ChannelArchive: ChannelArchiveEvent{},
ChannelUnarchive: ChannelUnarchiveEvent{},
ChannelLeft: ChannelLeftEvent{},
ChannelRename: ChannelRenameEvent{},
ChannelIDChanged: ChannelIDChangedEvent{},
GroupDeleted: GroupDeletedEvent{},
GroupArchive: GroupArchiveEvent{},
GroupUnarchive: GroupUnarchiveEvent{},
GroupLeft: GroupLeftEvent{},
GroupRename: GroupRenameEvent{},
GridMigrationFinished: GridMigrationFinishedEvent{},
GridMigrationStarted: GridMigrationStartedEvent{},
LinkShared: LinkSharedEvent{},
Message: MessageEvent{},
MemberJoinedChannel: MemberJoinedChannelEvent{},
MemberLeftChannel: MemberLeftChannelEvent{},
PinAdded: PinAddedEvent{},
PinRemoved: PinRemovedEvent{},
ReactionAdded: ReactionAddedEvent{},
ReactionRemoved: ReactionRemovedEvent{},
TeamJoin: TeamJoinEvent{},
TokensRevoked: TokensRevokedEvent{},
EmojiChanged: EmojiChangedEvent{},
WorkflowStepExecute: WorkflowStepExecuteEvent{},
AppMention: AppMentionEvent{},
AppHomeOpened: AppHomeOpenedEvent{},
AppUninstalled: AppUninstalledEvent{},
ChannelCreated: ChannelCreatedEvent{},
ChannelDeleted: ChannelDeletedEvent{},
ChannelArchive: ChannelArchiveEvent{},
ChannelUnarchive: ChannelUnarchiveEvent{},
ChannelLeft: ChannelLeftEvent{},
ChannelRename: ChannelRenameEvent{},
ChannelIDChanged: ChannelIDChangedEvent{},
GroupDeleted: GroupDeletedEvent{},
GroupArchive: GroupArchiveEvent{},
GroupUnarchive: GroupUnarchiveEvent{},
GroupLeft: GroupLeftEvent{},
GroupRename: GroupRenameEvent{},
GridMigrationFinished: GridMigrationFinishedEvent{},
GridMigrationStarted: GridMigrationStartedEvent{},
LinkShared: LinkSharedEvent{},
Message: MessageEvent{},
MemberJoinedChannel: MemberJoinedChannelEvent{},
MemberLeftChannel: MemberLeftChannelEvent{},
PinAdded: PinAddedEvent{},
PinRemoved: PinRemovedEvent{},
ReactionAdded: ReactionAddedEvent{},
ReactionRemoved: ReactionRemovedEvent{},
TeamJoin: TeamJoinEvent{},
TokensRevoked: TokensRevokedEvent{},
EmojiChanged: EmojiChangedEvent{},
WorkflowStepExecute: WorkflowStepExecuteEvent{},
MessageMetadataPosted: MessageMetadataPostedEvent{},
MessageMetadataUpdated: MessageMetadataUpdatedEvent{},
MessageMetadataDeleted: MessageMetadataDeletedEvent{},
}
186 changes: 186 additions & 0 deletions slackevents/inner_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,189 @@ func TestWorkflowStepExecute(t *testing.T) {
}
}
}

func TestMessageMetadataPosted(t *testing.T) {
rawE := []byte(`
{
"type":"message_metadata_posted",
"app_id":"APPXXX",
"bot_id":"BOTXXX",
"user_id":"USERXXX",
"team_id":"TEAMXXX",
"channel_id":"CHANNELXXX",
"metadata":{
"event_type":"type",
"event_payload":{"key": "value"}
},
"message_ts":"1660398079.756349",
"event_ts":"1660398079.756349"
}
`)

mmp := MessageMetadataPostedEvent{}
err := json.Unmarshal(rawE, &mmp)
if err != nil {
t.Error(err)
}

if mmp.Type != "message_metadata_posted" {
t.Fail()
}
if mmp.AppId != "APPXXX" {
t.Fail()
}
if mmp.BotId != "BOTXXX" {
t.Fail()
}
if mmp.UserId != "USERXXX" {
t.Fail()
}
if mmp.TeamId != "TEAMXXX" {
t.Fail()
}
if mmp.ChannelId != "CHANNELXXX" {
t.Fail()
}
if mmp.Metadata.EventType != "type" {
t.Fail()
}
payload := mmp.Metadata.EventPayload
if len(payload) <= 0 {
t.Fail()
}
if mmp.EventTimestamp != "1660398079.756349" {
t.Fail()
}
if mmp.MessageTimestamp != "1660398079.756349" {
t.Fail()
}
}

func TestMessageMetadataUpdated(t *testing.T) {
rawE := []byte(`
{
"type":"message_metadata_updated",
"channel_id":"CHANNELXXX",
"event_ts":"1660398079.756349",
"previous_metadata":{
"event_type":"type1",
"event_payload":{"key1": "value1"}
},
"app_id":"APPXXX",
"bot_id":"BOTXXX",
"user_id":"USERXXX",
"team_id":"TEAMXXX",
"message_ts":"1660398079.756349",
"metadata":{
"event_type":"type2",
"event_payload":{"key2": "value2"}
}
}
`)

mmp := MessageMetadataUpdatedEvent{}
err := json.Unmarshal(rawE, &mmp)
if err != nil {
t.Error(err)
}

if mmp.Type != "message_metadata_updated" {
t.Fail()
}
if mmp.ChannelId != "CHANNELXXX" {
t.Fail()
}
if mmp.EventTimestamp != "1660398079.756349" {
t.Fail()
}
if mmp.PreviousMetadata.EventType != "type1" {
t.Fail()
}
payload := mmp.PreviousMetadata.EventPayload
if len(payload) <= 0 {
t.Fail()
}
if mmp.AppId != "APPXXX" {
t.Fail()
}
if mmp.BotId != "BOTXXX" {
t.Fail()
}
if mmp.UserId != "USERXXX" {
t.Fail()
}
if mmp.TeamId != "TEAMXXX" {
t.Fail()
}
if mmp.MessageTimestamp != "1660398079.756349" {
t.Fail()
}
if mmp.Metadata.EventType != "type2" {
t.Fail()
}
payload = mmp.Metadata.EventPayload
if len(payload) <= 0 {
t.Fail()
}
}

func TestMessageMetadataDeleted(t *testing.T) {
rawE := []byte(`
{
"type":"message_metadata_deleted",
"channel_id":"CHANNELXXX",
"event_ts":"1660398079.756349",
"previous_metadata":{
"event_type":"type",
"event_payload":{"key": "value"}
},
"app_id":"APPXXX",
"bot_id":"BOTXXX",
"user_id":"USERXXX",
"team_id":"TEAMXXX",
"message_ts":"1660398079.756349",
"deleted_ts":"1660398079.756349"
}
`)

mmp := MessageMetadataDeletedEvent{}
err := json.Unmarshal(rawE, &mmp)
if err != nil {
t.Error(err)
}

if mmp.Type != "message_metadata_deleted" {
t.Fail()
}
if mmp.ChannelId != "CHANNELXXX" {
t.Fail()
}
if mmp.EventTimestamp != "1660398079.756349" {
t.Fail()
}
if mmp.PreviousMetadata.EventType != "type" {
t.Fail()
}
payload := mmp.PreviousMetadata.EventPayload
if len(payload) <= 0 {
t.Fail()
}
if mmp.AppId != "APPXXX" {
t.Fail()
}
if mmp.BotId != "BOTXXX" {
t.Fail()
}
if mmp.UserId != "USERXXX" {
t.Fail()
}
if mmp.TeamId != "TEAMXXX" {
t.Fail()
}
if mmp.MessageTimestamp != "1660398079.756349" {
t.Fail()
}
if mmp.DeletedTimestamp != "1660398079.756349" {
t.Fail()
}
}

0 comments on commit 870ec8a

Please sign in to comment.