Skip to content

Commit

Permalink
Merge pull request #1115 from diffeo/diffeo/deserialize-file-events
Browse files Browse the repository at this point in the history
Deserialize file events
  • Loading branch information
kanata2 committed Nov 4, 2022
2 parents 78e593a + 8bc6dae commit f1c8f0a
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
53 changes: 53 additions & 0 deletions slackevents/inner_events.go
Expand Up @@ -155,6 +155,47 @@ 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"`
}

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

// 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 +557,14 @@ 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")
// FileUnshared is sent when a file is unshared.
FileUnshared = EventsAPIType("file_unshared")
// 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 +615,10 @@ var EventsAPIInnerEventMapping = map[EventsAPIType]interface{}{
ChannelLeft: ChannelLeftEvent{},
ChannelRename: ChannelRenameEvent{},
ChannelIDChanged: ChannelIDChangedEvent{},
FileChange: FileChangeEvent{},
FileDeleted: FileDeletedEvent{},
FileShared: FileSharedEvent{},
FileUnshared: FileUnsharedEvent{},
GroupDeleted: GroupDeletedEvent{},
GroupArchive: GroupArchiveEvent{},
GroupUnarchive: GroupUnarchiveEvent{},
Expand Down
114 changes: 114 additions & 0 deletions slackevents/inner_events_test.go
Expand Up @@ -38,6 +38,120 @@ 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 TestFileUnsharedEvent(t *testing.T) {
rawE := []byte(`
{
"type": "file_unshared",
"file_id": "F1234567890",
"file": {
"id": "F1234567890"
}
}
`)

var e FileUnsharedEvent
if err := json.Unmarshal(rawE, &e); err != nil {
t.Fatal(err)
}
if e.Type != "file_unshared" {
t.Errorf("type should be file_shared, 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 TestGridMigrationFinishedEvent(t *testing.T) {
rawE := []byte(`
{
Expand Down

0 comments on commit f1c8f0a

Please sign in to comment.