enhancement(activitylog): enhance activitylog graph endpoint

- make use of libregraph artifacts
- add a basic activity kql ast parser
This commit is contained in:
Florian Schade
2024-06-18 12:13:13 +02:00
committed by jkoberg
parent 5249cbc138
commit 949c5d0848
4 changed files with 50 additions and 44 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/url"
"strings"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
revactx "github.com/cs3org/reva/v2/pkg/ctx"
@@ -12,10 +13,14 @@ import (
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/go-chi/chi/v5"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/ocis-pkg/l10n"
ehmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/eventhistory/v0"
ehsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/eventhistory/v0"
"github.com/owncloud/ocis/v2/services/graph/pkg/errorcode"
"github.com/owncloud/ocis/v2/services/search/pkg/query/ast"
"github.com/owncloud/ocis/v2/services/search/pkg/query/kql"
)
var (
@@ -42,8 +47,33 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
return
}
// TODO: Compare driveid with itemid to avoid bad requests
rid, err := parseIDParam(r, "item-id")
qraw := r.URL.Query().Get("kql")
if qraw == "" {
w.WriteHeader(http.StatusBadRequest)
}
qBuilder := kql.Builder{}
qast, err := qBuilder.Build(qraw)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
var itemID string
for _, n := range qast.Nodes {
v, ok := n.(*ast.StringNode)
if !ok {
continue
}
if strings.ToLower(v.Key) != "itemid" {
continue
}
itemID = v.Value
}
rid, err := storagespace.ParseID(itemID)
if err != nil {
s.log.Info().Err(err).Msg("invalid resource id")
w.WriteHeader(http.StatusBadRequest)
@@ -80,7 +110,7 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
message string
res Resource
act Actor
ts Times
ts libregraph.ActivityTimes
)
switch ev := s.unwrapEvent(e).(type) {

View File

@@ -7,6 +7,8 @@ import (
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/ocis-pkg/l10n"
)
@@ -29,14 +31,7 @@ var (
// GetActivitiesResponse is the response on GET activities requests
type GetActivitiesResponse struct {
Activities []Activity `json:"value"`
}
// Activity represents an activity as it is returned to the client
type Activity struct {
ID string `json:"id"`
Times Times `json:"times"`
Template Template `json:"template"`
Activities []libregraph.Activity `json:"value"`
}
// Resource represents an item such as a file or folder
@@ -51,23 +46,12 @@ type Actor struct {
DisplayName string `json:"displayName"`
}
// Times represents the timestamps of the Activity
type Times struct {
RecordedTime time.Time `json:"recordedTime"`
}
// Template contains activity details
type Template struct {
Message string `json:"message"`
Variables map[string]interface{} `json:"variables"`
}
// NewActivity creates a new activity
func NewActivity(message string, res Resource, user Actor, ts Times, eventID string) Activity {
return Activity{
ID: eventID,
func NewActivity(message string, res Resource, user Actor, ts libregraph.ActivityTimes, eventID string) libregraph.Activity {
return libregraph.Activity{
Id: eventID,
Times: ts,
Template: Template{
Template: libregraph.ActivityTemplate{
Message: message,
Variables: map[string]interface{}{
"resource": res,
@@ -78,26 +62,26 @@ func NewActivity(message string, res Resource, user Actor, ts Times, eventID str
}
// 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) {
func (s *ActivitylogService) ResponseData(ref *provider.Reference, uid *user.UserId, username string, ts time.Time) (Resource, Actor, libregraph.ActivityTimes, error) {
gwc, err := s.gws.Next()
if err != nil {
return Resource{}, Actor{}, Times{}, err
return Resource{}, Actor{}, libregraph.ActivityTimes{}, err
}
ctx, err := utils.GetServiceUserContext(s.cfg.ServiceAccount.ServiceAccountID, gwc, s.cfg.ServiceAccount.ServiceAccountSecret)
if err != nil {
return Resource{}, Actor{}, Times{}, err
return Resource{}, Actor{}, libregraph.ActivityTimes{}, err
}
info, err := utils.GetResource(ctx, ref, gwc)
if err != nil {
return Resource{}, Actor{}, Times{}, err
return Resource{}, Actor{}, libregraph.ActivityTimes{}, err
}
if username == "" {
u, err := utils.GetUser(uid, gwc)
if err != nil {
return Resource{}, Actor{}, Times{}, err
return Resource{}, Actor{}, libregraph.ActivityTimes{}, err
}
username = u.GetUsername()
}
@@ -108,7 +92,7 @@ func (s *ActivitylogService) ResponseData(ref *provider.Reference, uid *user.Use
}, Actor{
ID: uid.GetOpaqueId(),
DisplayName: username,
}, Times{
}, libregraph.ActivityTimes{
RecordedTime: ts,
}, nil

View File

@@ -15,11 +15,12 @@ import (
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/go-chi/chi/v5"
microstore "go-micro.dev/v4/store"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
ehsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/eventhistory/v0"
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
"github.com/owncloud/ocis/v2/services/activitylog/pkg/config"
microstore "go-micro.dev/v4/store"
)
// RawActivity represents an activity as it is stored in the activitylog store
@@ -75,9 +76,7 @@ func New(opts ...Option) (*ActivitylogService, error) {
registeredEvents: make(map[string]events.Unmarshaller),
}
s.mux.Route("/graph/v1.0/drives/{drive-id}", func(r chi.Router) {
r.Get("/items/{item-id}/activities", s.HandleGetItemActivities)
})
s.mux.Get("/graph/v1beta1/extensions/org.libregraph/activities", s.HandleGetItemActivities)
for _, e := range o.RegisteredEvents {
typ := reflect.TypeOf(e)