Skip to content

Commit

Permalink
Merge pull request #5 from mashiike/feature/eventbreidge-mesasge-noto…
Browse files Browse the repository at this point in the history
…mitempty

Feature/eventbreidge event detail
  • Loading branch information
mashiike authored Jun 15, 2022
2 parents 9eff67f + 4bfa18d commit 8218965
Show file tree
Hide file tree
Showing 14 changed files with 583 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ var driveFields = fmt.Sprintf("drive(%s)", strings.Join(
",",
))
var fileFields = fmt.Sprintf("file(%s)", strings.Join(
[]string{"id", "name", "kind", "mimeType", "modifiedTime", "lastModifyingUser", "trashed", "trashedTime", "trashingUser"},
[]string{"id", "name", "driveId", "kind", "mimeType", "modifiedTime", "lastModifyingUser", "trashed", "trashedTime", "trashingUser", "version", "size", "md5Checksum", "createdTime"},
",",
))
var changesFields = fmt.Sprintf("changes(%s)", strings.Join(
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/najeira/randstr v0.1.1
github.com/olekukonko/tablewriter v0.0.5
github.com/samber/lo v1.21.0
github.com/sebdah/goldie/v2 v2.5.3
github.com/shogo82148/go-retry v1.1.1
github.com/stretchr/testify v1.7.2
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
Expand Down Expand Up @@ -63,6 +64,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d // indirect
golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6
github.com/pires/go-proxyproto v0.6.0/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY=
github.com/pires/go-proxyproto v0.6.2 h1:KAZ7UteSOt6urjme6ZldyFm4wDe/z0ZUP0Yv0Dos0d8=
github.com/pires/go-proxyproto v0.6.2/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -292,11 +293,16 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.21.0 h1:FSby8pJQtX4KmyddTCCGhc3JvnnIVrDA+NW37rG+7G8=
github.com/samber/lo v1.21.0/go.mod h1:2I7tgIv8Q1SG2xEIkRq0F2i2zgxVpnyPOP0d3Gj2r+A=
github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y=
github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shogo82148/go-retry v1.1.1 h1:BfUEVHTNDSjYxoRPC+c/ht5Sy6qdwl+0kFhhubeh4Fo=
github.com/shogo82148/go-retry v1.1.1/go.mod h1:TPSFDcc2rlx2D/yfhi8BBOlsHhVBjjJoMvxG7iFHUbI=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
169 changes: 144 additions & 25 deletions notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,46 +48,165 @@ func NewEventBridgeNotification(ctx context.Context, cfg *NotificationConfig, aw
return n, nil, nil
}

func (n *EventBridgeNotification) SendChanges(ctx context.Context, item *ChannelItem, changes []*drive.Change) error {
sourcePrefix := fmt.Sprintf("oss.gdnotify/%s", item.DriveID)
entriesChunk := lo.Chunk(lo.Map(changes, func(c *drive.Change, _ int) types.PutEventsRequestEntry {
type TargetEntity struct {
Id string `json:"id"`
Kind string `json:"kind"`
Name string `json:"name"`
CreatedTime string `json:"createdTime"`
}
type ChangeEventDetail struct {
Subject string `json:"subject"`
Entity *TargetEntity `json:"entity"`
Actor *drive.User `json:"actor"`
Change *drive.Change `json:"change"`
}

const (
DetailTypeFileRemoved = "File Removed"
DetailTypeFileTrashed = "File Move to trash"
DetailTypeFileChanged = "File Changed"
DetailTypeDriveRemoved = "Shared Drive Removed"
DetailTypeDriveChanged = "Drive Status Changed"
)

var source, detailType string
switch c.ChangeType {
case "file":
source = fmt.Sprintf("%s/file/%s", sourcePrefix, c.FileId)
switch {
case c.Removed:
detailType = "File Removed"
case c.File != nil && c.File.Trashed:
detailType = "File Move to trash"
default:
detailType = "File Changed"
func (e *ChangeEventDetail) MarshalJSON() ([]byte, error) {
switch e.DetailType() {
case DetailTypeFileRemoved:
e.Subject = fmt.Sprintf("FileID %s was removed at %s", e.Change.FileId, e.Change.Time)
case DetailTypeFileTrashed:
if e.Change.File != nil {
if e.Change.File.TrashingUser != nil {
var user string
if e.Change.File.TrashingUser.EmailAddress == "" {
user = e.Change.File.TrashingUser.DisplayName
} else {
user = fmt.Sprintf("%s [%s]", e.Change.File.TrashingUser.DisplayName, e.Change.File.TrashingUser.EmailAddress)
}
e.Subject = fmt.Sprintf("File %s (%s) moved to trash by %s at %s", e.Change.File.Name, e.Change.FileId, user, e.Change.File.TrashedTime)
e.Actor = e.Change.File.TrashingUser
} else {
e.Subject = fmt.Sprintf("File %s (%s) moved to trash at %s", e.Change.File.Name, e.Change.FileId, e.Change.Time)
}
case "drive":
source = fmt.Sprintf("%s/drive/%s", sourcePrefix, c.DriveId)
switch {
case c.Removed:
detailType = "Shared Drive Removed"
default:
detailType = "Drive Status Changed"
} else {
e.Subject = fmt.Sprintf("FileID %s moved to trash at %s", e.Change.FileId, e.Change.Time)
}
case DetailTypeFileChanged:
if e.Change.File != nil {
if e.Change.File.LastModifyingUser != nil {
var user string
if e.Change.File.LastModifyingUser.EmailAddress == "" {
user = e.Change.File.LastModifyingUser.DisplayName
} else {
user = fmt.Sprintf("%s [%s]", e.Change.File.LastModifyingUser.DisplayName, e.Change.File.LastModifyingUser.EmailAddress)
}
e.Subject = fmt.Sprintf("File %s (%s) changed by %s at %s", e.Change.File.Name, e.Change.FileId, user, e.Change.File.ModifiedTime)
e.Actor = e.Change.File.LastModifyingUser
} else {
e.Subject = fmt.Sprintf("File %s (%s) changed at %s", e.Change.File.Name, e.Change.FileId, e.Change.Time)
}
} else {
e.Subject = fmt.Sprintf("FileID %s changed at %s", e.Change.FileId, e.Change.Time)
}
case DetailTypeDriveRemoved:
e.Subject = fmt.Sprintf("DriveId %s was removed at %s", e.Change.DriveId, e.Change.Time)
case DetailTypeDriveChanged:
if e.Change.Drive != nil {
e.Subject = fmt.Sprintf("Drive %s (%s) changed at %s", e.Change.Drive.Name, e.Change.DriveId, e.Change.Time)
} else {
e.Subject = fmt.Sprintf("DriveId %s changed at %s", e.Change.DriveId, e.Change.Time)
}
}
if e.Actor == nil {
e.Actor = &drive.User{
Kind: "drive#user",
DisplayName: "Unknown User",
}
}
e.Actor.ForceSendFields = []string{"EmailAddress", "DisplayName", "Kind"}
switch {
case e.Change.Drive != nil:
e.Entity = &TargetEntity{
Id: e.Change.Drive.Id,
Kind: e.Change.Drive.Kind,
Name: e.Change.Drive.Name,
CreatedTime: e.Change.Drive.CreatedTime,
}
case e.Change.File != nil:
e.Entity = &TargetEntity{
Id: e.Change.File.Id,
Kind: e.Change.File.Kind,
Name: e.Change.File.Name,
CreatedTime: e.Change.File.CreatedTime,
}
case e.Change.DriveId != "":
e.Entity = &TargetEntity{
Id: e.Change.DriveId,
Kind: "drive#drive",
}
case e.Change.FileId != "":
e.Entity = &TargetEntity{
Id: e.Change.FileId,
Kind: "drive#file",
}
}
type NoMethod ChangeEventDetail
data := NoMethod(*e)
return json.Marshal(data)
}

func (e *ChangeEventDetail) DetailType() string {
switch e.Change.ChangeType {
case "file":
switch {
case e.Change.Removed:
return DetailTypeFileRemoved
case e.Change.File != nil && e.Change.File.Trashed:
return DetailTypeFileTrashed
default:
source = fmt.Sprintf("%s/%s", sourcePrefix, c.ChangeType)
detailType = "Unexpected Changed"
logx.Printf(ctx, "[warn] unexpected change type `%s`, check Drive API Document", c.ChangeType)
return DetailTypeFileChanged
}
case "drive":
switch {
case e.Change.Removed:
return DetailTypeDriveRemoved
default:
return DetailTypeDriveChanged
}
default:
return "Unexpected Changed"
}
}
func (e *ChangeEventDetail) Source(sourcePrefix string) string {
switch e.Change.ChangeType {
case "file":
return fmt.Sprintf("%s/file/%s", sourcePrefix, e.Change.FileId)
case "drive":
return fmt.Sprintf("%s/drive/%s", sourcePrefix, e.Change.DriveId)
default:
return fmt.Sprintf("%s/%s", sourcePrefix, e.Change.ChangeType)
}
}

func (n *EventBridgeNotification) SendChanges(ctx context.Context, item *ChannelItem, changes []*drive.Change) error {
sourcePrefix := fmt.Sprintf("oss.gdnotify/%s", item.DriveID)
entriesChunk := lo.Chunk(lo.Map(changes, func(c *drive.Change, _ int) types.PutEventsRequestEntry {

t, err := time.Parse(time.RFC3339Nano, c.Time)
if err != nil {
logx.Printf(ctx, "[warn] time Parse failed `%s`: %s", c.Time, err.Error())
t = flextime.Now()
}
bs, err := json.Marshal(c)
ced := &ChangeEventDetail{
Change: c,
}
bs, err := json.Marshal(ced)
if err != nil {
logx.Printf(ctx, "[warn] change marshal failed: %s", err.Error())
bs = []byte("{}")
}
detail := string(bs)
source := ced.Source(sourcePrefix)
detailType := ced.DetailType()
logx.Printf(ctx, "[debug] event source=%s, detail-type=%s detail: %s", source, detailType, detail)
return types.PutEventsRequestEntry{
EventBusName: aws.String(n.eventBus),
Expand Down
Loading

0 comments on commit 8218965

Please sign in to comment.