Skip to content

Commit

Permalink
feat(activitylog): adjust response format
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Jun 12, 2024
1 parent e618861 commit 555638b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
30 changes: 19 additions & 11 deletions services/activitylog/pkg/service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package service

import (
"encoding/json"
"fmt"
"net/http"
"net/url"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/go-chi/chi/v5"
ehmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/eventhistory/v0"
ehsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/eventhistory/v0"
Expand Down Expand Up @@ -43,34 +43,42 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
ids = append(ids, a.EventID)
}

fmt.Println("IDS:", ids)

evRes, err := s.evHistory.GetEvents(r.Context(), &ehsvc.GetEventsRequest{Ids: ids})
if err != nil {
s.log.Error().Err(err).Msg("error getting events")
w.WriteHeader(http.StatusInternalServerError)
return
}

// TODO: compare returned events with initial list and remove missing ones

fmt.Println("EVENTS:", evRes.GetEvents())

var acts []Activity
for _, e := range evRes.GetEvents() {
// TODO: compare returned events with initial list and remove missing ones

// FIXME: Should all users get all events? If not we can filter here

var (
message string
res Resource
act Actor
ts Times
)

switch ev := s.unwrapEvent(e).(type) {
case nil:
// error already logged in unwrapEvent
continue
case events.UploadReady:
act := UploadReady(e.Id, ev)
acts = append(acts, act)
message = "{user} created {resource}"
res, act, ts, err = s.ResponseData(ev.FileRef, ev.ExecutingUser.GetId(), ev.ExecutingUser.GetDisplayName(), utils.TSToTime(ev.Timestamp))
}
}

fmt.Println("ACTIVITIES:", acts)
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()))
}

b, err := json.Marshal(acts)
if err != nil {
Expand Down
75 changes: 57 additions & 18 deletions services/activitylog/pkg/service/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package service
import (
"time"

"github.com/cs3org/reva/v2/pkg/events"
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
)

// GetActivitiesResponse is the response on GET activities requests
Expand All @@ -14,15 +16,14 @@ type GetActivitiesResponse struct {

// Activity represents an activity as it is returned to the client
type Activity struct {
ID string `json:"id"`
ID string `json:"id"`
DriveItem Resource `json:"driveItem"`
Actor Actor `json:"actor"`
Times Times `json:"times"`
Template Template `json:"template"`

// TODO: Implement these
Action interface{} `json:"action"`
DriveItem Resource `json:"driveItem"`
Actor Actor `json:"actor"`
Times Times `json:"times"`

Template Template `json:"template"`
// TODO: Implement
Action interface{} `json:"action"`
}

// Resource represents an item such as a file or folder
Expand All @@ -48,20 +49,58 @@ type Template struct {
Variables map[string]interface{} `json:"variables"`
}

// UploadReady converts a UploadReady events to an Activity
func UploadReady(eid string, e events.UploadReady) Activity {
rid, _ := storagespace.FormatReference(e.FileRef)
res := Resource{
ID: rid,
Name: e.Filename,
}
// NewActivity creates a new activity
func NewActivity(message string, res Resource, user Actor, ts time.Time, eventID string) Activity {
return Activity{
ID: eid,
ID: eventID,
Times: Times{
RecordedTime: ts,
},
DriveItem: res,
Actor: user,
Template: Template{
Message: "file created",
Message: message,
Variables: map[string]interface{}{
"resource": res,
"user": user,
},
},
}
}

// ResponseData returns the relevant response data for the activity
func (s *ActivitylogService) ResponseData(ref *provider.Reference, uid *user.UserId, username string, ts time.Time) (Resource, Actor, Times, error) {
gwc, err := s.gws.Next()
if err != nil {
return Resource{}, Actor{}, Times{}, err
}

ctx, err := utils.GetServiceUserContext(s.cfg.ServiceAccount.ServiceAccountID, gwc, s.cfg.ServiceAccount.ServiceAccountSecret)
if err != nil {
return Resource{}, Actor{}, Times{}, err
}

info, err := utils.GetResource(ctx, ref, gwc)
if err != nil {
return Resource{}, Actor{}, Times{}, err
}

if username == "" {
u, err := utils.GetUser(uid, gwc)
if err != nil {
return Resource{}, Actor{}, Times{}, err
}
username = u.GetUsername()
}

return Resource{
ID: storagespace.FormatResourceID(*info.Id),
Name: info.Path,
}, Actor{
ID: uid.GetOpaqueId(),
DisplayName: username,
}, Times{
RecordedTime: ts,
}, nil

}
4 changes: 4 additions & 0 deletions services/eventhistory/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (eh *EventHistoryService) getEvent(id string) (*ehmsg.Event, error) {
return nil, err
}

if len(evs) == 0 {
return nil, store.ErrNotFound
}

var ev StoreEvent
if err := json.Unmarshal(evs[0].Value, &ev); err != nil {
eh.log.Error().Err(err).Str("eventid", id).Msg("could not unmarshal event")
Expand Down

0 comments on commit 555638b

Please sign in to comment.