Skip to content

Commit

Permalink
add deserializations for file change, shared, deleted events
Browse files Browse the repository at this point in the history
  • Loading branch information
andyhaskell committed Oct 12, 2022
1 parent 78e593a commit 413153e
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
42 changes: 42 additions & 0 deletions slackevents/inner_events.go
Expand Up @@ -155,6 +155,39 @@ type GroupRenameInfo struct {
Created int `json:"created"`
}

// FileChangeEvent represents the information associated with the File change
// event.
type FileChangeEvent struct {
Type string `json:"type"`
FileID string `json:"file_id"`
File FileEventFile `json:"file"`
}

// FileDeletedEvent represents the information associated with the File deleted
// event.
type FileDeletedEvent struct {
Type string `json:"type"`
FileID string `json:"file_id"`
EventTimestamp string `json:"event_ts"`
}

// FileSharedEvent represents the information associated with the File shared
// event.
type FileSharedEvent struct {
Type string `json:"type"`
ChannelID string `json:"channel_id"`
FileID string `json:"file_id"`
UserID string `json:"user_id"`
File FileEventFile `json:"file"`
EventTimestamp string `json:"event_ts"`
}

// FileEventFile represents information on the specific file being shared in a
// file-related Slack event.
type FileEventFile struct {
ID string `json:"id"`
}

// GridMigrationFinishedEvent An enterprise grid migration has finished on this workspace.
type GridMigrationFinishedEvent struct {
Type string `json:"type"`
Expand Down Expand Up @@ -516,6 +549,12 @@ const (
GroupLeft = EventsAPIType("group_left")
// GroupRename is sent when a group is renamed.
GroupRename = EventsAPIType("group_rename")
// FileChange is sent when a file is changed.
FileChange = EventsAPIType("file_change")
// FileDeleted is sent when a file is deleted.
FileDeleted = EventsAPIType("file_deleted")
// FileShared is sent when a file is shared.
FileShared = EventsAPIType("file_shared")
// GridMigrationFinished An enterprise grid migration has finished on this workspace.
GridMigrationFinished = EventsAPIType("grid_migration_finished")
// GridMigrationStarted An enterprise grid migration has started on this workspace.
Expand Down Expand Up @@ -566,6 +605,9 @@ var EventsAPIInnerEventMapping = map[EventsAPIType]interface{}{
ChannelLeft: ChannelLeftEvent{},
ChannelRename: ChannelRenameEvent{},
ChannelIDChanged: ChannelIDChangedEvent{},
FileChange: FileChangeEvent{},
FileDeleted: FileDeletedEvent{},
FileShared: FileSharedEvent{},
GroupDeleted: GroupDeletedEvent{},
GroupArchive: GroupArchiveEvent{},
GroupUnarchive: GroupUnarchiveEvent{},
Expand Down
88 changes: 88 additions & 0 deletions slackevents/inner_events_test.go
Expand Up @@ -38,6 +38,94 @@ func TestAppUninstalled(t *testing.T) {
}
}

func TestFileChangeEvent(t *testing.T) {
rawE := []byte(`
{
"type": "file_change",
"file_id": "F1234567890",
"file": {
"id": "F1234567890"
}
}
`)

var e FileChangeEvent
if err := json.Unmarshal(rawE, &e); err != nil {
t.Fatal(err)
}
if e.Type != "file_change" {
t.Errorf("type should be file_change, was %s", e.Type)
}
if e.FileID != "F1234567890" {
t.Errorf("file ID should be F1234567890, was %s", e.FileID)
}
if e.File.ID != "F1234567890" {
t.Errorf("file.id should be F1234567890, was %s", e.File.ID)
}
}

func TestFileDeletedEvent(t *testing.T) {
rawE := []byte(`
{
"type": "file_deleted",
"file_id": "F1234567890",
"event_ts": "1234567890.123456"
}
`)

var e FileDeletedEvent
if err := json.Unmarshal(rawE, &e); err != nil {
t.Fatal(err)
}
if e.Type != "file_deleted" {
t.Errorf("type should be file_deleted, was %s", e.Type)
}
if e.FileID != "F1234567890" {
t.Errorf("file ID should be F1234567890, was %s", e.FileID)
}
if e.EventTimestamp != "1234567890.123456" {
t.Errorf("event timestamp should be 1234567890.123456, was %s", e.EventTimestamp)
}
}

func TestFileSharedEvent(t *testing.T) {
rawE := []byte(`
{
"type": "file_shared",
"channel_id": "C1234567890",
"file_id": "F1234567890",
"user_id": "U11235813",
"file": {
"id": "F1234567890"
},
"event_ts": "1234567890.123456"
}
`)

var e FileSharedEvent
if err := json.Unmarshal(rawE, &e); err != nil {
t.Fatal(err)
}
if e.Type != "file_shared" {
t.Errorf("type should be file_shared, was %s", e.Type)
}
if e.ChannelID != "C1234567890" {
t.Errorf("channel ID should be C1234567890, was %s", e.ChannelID)
}
if e.FileID != "F1234567890" {
t.Errorf("file ID should be F1234567890, was %s", e.FileID)
}
if e.UserID != "U11235813" {
t.Errorf("user ID should be U11235813, was %s", e.UserID)
}
if e.File.ID != "F1234567890" {
t.Errorf("file.id should be F1234567890, was %s", e.File.ID)
}
if e.EventTimestamp != "1234567890.123456" {
t.Errorf("event timestamp should be 1234567890.123456, was %s", e.EventTimestamp)
}
}

func TestGridMigrationFinishedEvent(t *testing.T) {
rawE := []byte(`
{
Expand Down

0 comments on commit 413153e

Please sign in to comment.