From f90ee55aa810949154a96dc812724b445d6ab9dc Mon Sep 17 00:00:00 2001 From: jkoberg Date: Thu, 13 Jun 2024 16:02:05 +0200 Subject: [PATCH] feat(activitylog): add missing events Signed-off-by: jkoberg --- services/activitylog/pkg/service/http.go | 54 +++++++++++++++++-- services/activitylog/pkg/service/response.go | 13 ++--- .../activitylog/pkg/service/service_test.go | 18 +++---- 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/services/activitylog/pkg/service/http.go b/services/activitylog/pkg/service/http.go index 81548b3ff9a..a39b11645da 100644 --- a/services/activitylog/pkg/service/http.go +++ b/services/activitylog/pkg/service/http.go @@ -50,7 +50,7 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h return } - var acts []Activity + var resp GetActivitiesResponse for _, e := range evRes.GetEvents() { // TODO: compare returned events with initial list and remove missing ones @@ -70,6 +70,48 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h case events.UploadReady: message = "{user} created {resource}" res, act, ts, err = s.ResponseData(ev.FileRef, ev.ExecutingUser.GetId(), ev.ExecutingUser.GetDisplayName(), utils.TSToTime(ev.Timestamp)) + case events.FileTouched: + message = "{user} created {resource}" + res, act, ts, err = s.ResponseData(ev.Ref, ev.Executant, "", utils.TSToTime(ev.Timestamp)) + case events.ContainerCreated: + message = "{user} created {resource}" + res, act, ts, err = s.ResponseData(ev.Ref, ev.Executant, "", utils.TSToTime(ev.Timestamp)) + case events.ItemTrashed: + message = "{user} trashed {resource}" + res, act, ts, err = s.ResponseData(ev.Ref, ev.Executant, "", utils.TSToTime(ev.Timestamp)) + case events.ItemPurged: + message = "{user} purged {resource}" + res, act, ts, err = s.ResponseData(ev.Ref, ev.Executant, "", utils.TSToTime(ev.Timestamp)) + case events.ItemMoved: + message = "{user} moved {resource}" + res, act, ts, err = s.ResponseData(ev.Ref, ev.Executant, "", utils.TSToTime(ev.Timestamp)) + case events.ShareCreated: + message = "{user} shared {resource}" + res, act, ts, err = s.ResponseData(toRef(ev.ItemID), ev.Executant, "", utils.TSToTime(ev.CTime)) + case events.ShareUpdated: + message = "{user} updated share of {resource}" + res, act, ts, err = s.ResponseData(toRef(ev.ItemID), ev.Executant, "", utils.TSToTime(ev.MTime)) + case events.ShareRemoved: + message = "{user} removed share of {resource}" + res, act, ts, err = s.ResponseData(toRef(ev.ItemID), ev.Executant, "", ev.Timestamp) + case events.LinkCreated: + message = "{user} created link to {resource}" + res, act, ts, err = s.ResponseData(toRef(ev.ItemID), ev.Executant, "", utils.TSToTime(ev.CTime)) + case events.LinkUpdated: + message = "{user} updated link to {resource}" + res, act, ts, err = s.ResponseData(toRef(ev.ItemID), ev.Executant, "", utils.TSToTime(ev.CTime)) + case events.LinkRemoved: + message = "{user} removed link to {resource}" + res, act, ts, err = s.ResponseData(toRef(ev.ItemID), ev.Executant, "", utils.TSToTime(ev.Timestamp)) + case events.SpaceShared: + message = "{user} shared space {resource}" + res, act, ts, err = s.ResponseData(sToRef(ev.ID), ev.Executant, "", ev.Timestamp) + case events.SpaceShareUpdated: + message = "{user} updated share of space {resource}" + res, act, ts, err = s.ResponseData(sToRef(ev.ID), ev.Executant, "", ev.Timestamp) + case events.SpaceUnshared: + message = "{user} unshared space {resource}" + res, act, ts, err = s.ResponseData(sToRef(ev.ID), ev.Executant, "", ev.Timestamp) } if err != nil { @@ -77,17 +119,21 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h continue } - acts = append(acts, NewActivity(message, res, act, ts.RecordedTime, e.GetId())) + resp.Activities = append(resp.Activities, NewActivity(message, res, act, ts.RecordedTime, e.GetId())) } - b, err := json.Marshal(acts) + b, err := json.Marshal(resp) if err != nil { s.log.Error().Err(err).Msg("error marshalling activities") w.WriteHeader(http.StatusInternalServerError) return } - w.Write(b) + if _, err := w.Write(b); err != nil { + s.log.Error().Err(err).Msg("error writing response") + w.WriteHeader(http.StatusInternalServerError) + return + } w.WriteHeader(http.StatusOK) } diff --git a/services/activitylog/pkg/service/response.go b/services/activitylog/pkg/service/response.go index 9b24564ee4c..44273145e7d 100644 --- a/services/activitylog/pkg/service/response.go +++ b/services/activitylog/pkg/service/response.go @@ -16,14 +16,9 @@ type GetActivitiesResponse struct { // Activity represents an activity as it is returned to the client type Activity struct { - ID string `json:"id"` - DriveItem Resource `json:"driveItem"` - Actor Actor `json:"actor"` - Times Times `json:"times"` - Template Template `json:"template"` - - // TODO: Implement - Action interface{} `json:"action"` + ID string `json:"id"` + Times Times `json:"times"` + Template Template `json:"template"` } // Resource represents an item such as a file or folder @@ -56,8 +51,6 @@ func NewActivity(message string, res Resource, user Actor, ts time.Time, eventID Times: Times{ RecordedTime: ts, }, - DriveItem: res, - Actor: user, Template: Template{ Message: message, Variables: map[string]interface{}{ diff --git a/services/activitylog/pkg/service/service_test.go b/services/activitylog/pkg/service/service_test.go index 55b2f62716f..2aa9b92a211 100644 --- a/services/activitylog/pkg/service/service_test.go +++ b/services/activitylog/pkg/service/service_test.go @@ -14,7 +14,7 @@ func TestAddActivity(t *testing.T) { Name string Tree map[string]*provider.ResourceInfo Activities map[string]string - Expected map[string][]Activity + Expected map[string][]RawActivity }{ { Name: "simple", @@ -26,7 +26,7 @@ func TestAddActivity(t *testing.T) { Activities: map[string]string{ "activity": "base", }, - Expected: map[string][]Activity{ + Expected: map[string][]RawActivity{ "base": activitites("activity", 0), "parent": activitites("activity", 1), "spaceid": activitites("activity", 2), @@ -43,7 +43,7 @@ func TestAddActivity(t *testing.T) { "activity1": "base", "activity2": "base", }, - Expected: map[string][]Activity{ + Expected: map[string][]RawActivity{ "base": activitites("activity1", 0, "activity2", 0), "parent": activitites("activity1", 1, "activity2", 1), "spaceid": activitites("activity1", 2, "activity2", 2), @@ -61,7 +61,7 @@ func TestAddActivity(t *testing.T) { "activity1": "base1", "activity2": "base2", }, - Expected: map[string][]Activity{ + Expected: map[string][]RawActivity{ "base1": activitites("activity1", 0), "base2": activitites("activity2", 0), "parent": activitites("activity1", 1, "activity2", 1), @@ -83,7 +83,7 @@ func TestAddActivity(t *testing.T) { "activity2": "base2", "activity3": "base3", }, - Expected: map[string][]Activity{ + Expected: map[string][]RawActivity{ "base1": activitites("activity1", 0), "base2": activitites("activity2", 0), "base3": activitites("activity3", 0), @@ -109,7 +109,7 @@ func TestAddActivity(t *testing.T) { "activity3": "base3", "activity4": "parent2", }, - Expected: map[string][]Activity{ + Expected: map[string][]RawActivity{ "base1": activitites("activity1", 0), "base2": activitites("activity2", 0), "base3": activitites("activity3", 0), @@ -143,9 +143,9 @@ func TestAddActivity(t *testing.T) { } } -func activitites(acts ...interface{}) []Activity { - var activities []Activity - act := Activity{} +func activitites(acts ...interface{}) []RawActivity { + var activities []RawActivity + act := RawActivity{} for _, a := range acts { switch v := a.(type) { case string: