Skip to content

Commit

Permalink
feat(activitylog): add missing events
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Jun 13, 2024
1 parent 555638b commit f90ee55
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
54 changes: 50 additions & 4 deletions services/activitylog/pkg/service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -70,24 +70,70 @@ 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 {
s.log.Error().Err(err).Msg("error getting response data")
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)
}

Expand Down
13 changes: 3 additions & 10 deletions services/activitylog/pkg/service/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{}{
Expand Down
18 changes: 9 additions & 9 deletions services/activitylog/pkg/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f90ee55

Please sign in to comment.