diff --git a/services/graph/pkg/service/v0/drives.go b/services/graph/pkg/service/v0/drives.go index 99d643526c..634642cb9f 100644 --- a/services/graph/pkg/service/v0/drives.go +++ b/services/graph/pkg/service/v0/drives.go @@ -916,6 +916,10 @@ func (g Graph) cs3StorageSpaceToDrive(ctx context.Context, baseURL *url.URL, spa drive.Root.Permissions, _ = g.cs3SpacePermissionsToLibreGraph(ctx, space, false, apiVersion) } + if rootInfo := space.GetRootInfo(); rootInfo != nil { + drive.Root.SetLibreGraphMeFollowing(isFollowing(ctx, rootInfo)) + } + if _, ok := fieldMask["@libre.graph.hasTrashedItems"]; ok { drive.LibreGraphHasTrashedItems = &space.HasTrashedItems } diff --git a/services/graph/pkg/service/v0/follow.go b/services/graph/pkg/service/v0/follow.go index 637ea54ad2..10f3b9e3ab 100644 --- a/services/graph/pkg/service/v0/follow.go +++ b/services/graph/pkg/service/v0/follow.go @@ -1,7 +1,10 @@ package svc import ( + "context" + "encoding/json" "net/http" + "slices" rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -16,8 +19,36 @@ const ( _favoriteLabel = "favorite" // deliberately not using the existing node.FavoriteKey constant to avoid importing a specific storage driver package _favoriteMetadataKey = "http://owncloud.org/ns/favorite" + // the storage driver lists the ids of all users who marked a resource as favorite in this opaque entry + _favoritesOpaqueKey = "favorites" ) +// isFollowing reports whether the user in the context marked the resource as favorite. +// The per-user flag is only part of the arbitrary metadata when it was requested, +// which is not the case for the root info of a space, so the favorites list in the +// opaque is checked as well. +func isFollowing(ctx context.Context, info *provider.ResourceInfo) bool { + if info.GetArbitraryMetadata().GetMetadata()[_favoriteMetadataKey] == "1" { + return true + } + + entry := info.GetOpaque().GetMap()[_favoritesOpaqueKey] + if entry.GetDecoder() != "json" { + return false + } + + u, ok := revactx.ContextGetUser(ctx) + if !ok || u.GetId().GetOpaqueId() == "" { + return false + } + + var favorites []string + if err := json.Unmarshal(entry.GetValue(), &favorites); err != nil { + return false + } + return slices.Contains(favorites, u.GetId().GetOpaqueId()) +} + // FollowDriveItem marks a drive item as favorite. func (g Graph) FollowDriveItem(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/services/graph/pkg/service/v0/graph_test.go b/services/graph/pkg/service/v0/graph_test.go index 77b98a2f61..18df9336b7 100644 --- a/services/graph/pkg/service/v0/graph_test.go +++ b/services/graph/pkg/service/v0/graph_test.go @@ -407,6 +407,60 @@ var _ = Describe("Graph", func() { Expect(*value.Root.RemoteItem.Size).To(Equal(int64(1234))) Expect(*value.Root.RemoteItem.WebDavUrl).To(Equal("https://localhost:9200/dav/spaces/ownerStorageID$spaceID%21opaqueID/Folder/New%20Folder")) }) + It("reports whether the current user follows a space", func() { + favoritesOf := func(userIDs ...string) *typesv1beta1.Opaque { + value, err := json.Marshal(userIDs) + Expect(err).ToNot(HaveOccurred()) + return &typesv1beta1.Opaque{Map: map[string]*typesv1beta1.OpaqueEntry{ + "favorites": {Decoder: "json", Value: value}, + }} + } + space := func(id string, rootInfo *provider.ResourceInfo) *provider.StorageSpace { + return &provider.StorageSpace{ + Id: &provider.StorageSpaceId{OpaqueId: id}, + SpaceType: "project", + Root: &provider.ResourceId{StorageId: "pro-1", SpaceId: id, OpaqueId: id}, + Name: id, + RootInfo: rootInfo, + } + } + gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{ + Status: status.NewOK(ctx), + StorageSpaces: []*provider.StorageSpace{ + space("followed", &provider.ResourceInfo{Opaque: favoritesOf("someoneelse", "testuser")}), + space("followedbyothers", &provider.ResourceInfo{Opaque: favoritesOf("someoneelse")}), + space("followedmetadata", &provider.ResourceInfo{ArbitraryMetadata: &provider.ArbitraryMetadata{ + Metadata: map[string]string{"http://owncloud.org/ns/favorite": "1"}, + }}), + space("norootinfo", nil), + }, + }, nil) + gatewayClient.On("GetQuota", mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{ + Status: status.NewUnimplemented(ctx, fmt.Errorf("not supported"), "not supported"), + }, nil) + gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&provider.StatResponse{ + Status: status.NewNotFound(ctx, "no special files found"), + }, nil) + + r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/me/drives", nil) + r = r.WithContext(ctx) + rr := httptest.NewRecorder() + svc.GetDrivesV1(rr, r) + + Expect(rr.Code).To(Equal(http.StatusOK)) + + var response map[string][]libregraph.Drive + Expect(json.Unmarshal(rr.Body.Bytes(), &response)).To(Succeed()) + following := map[string]*bool{} + for _, drive := range response["value"] { + following[drive.GetName()] = drive.Root.LibreGraphMeFollowing + } + Expect(following).To(HaveLen(4)) + Expect(following["followed"]).To(HaveValue(BeTrue())) + Expect(following["followedbyothers"]).To(HaveValue(BeFalse())) + Expect(following["followedmetadata"]).To(HaveValue(BeTrue())) + Expect(following["norootinfo"]).To(BeNil()) + }) It("can not list spaces with wrong sort parameter", func() { gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{ Status: status.NewOK(ctx), diff --git a/services/graph/pkg/service/v0/sharedwithme_test.go b/services/graph/pkg/service/v0/sharedwithme_test.go index 8eb9779141..3f37781cdb 100644 --- a/services/graph/pkg/service/v0/sharedwithme_test.go +++ b/services/graph/pkg/service/v0/sharedwithme_test.go @@ -275,6 +275,29 @@ var _ = Describe("SharedWithMe", func() { }) + It("reports whether the current user follows the shared resource", func() { + svc.ListSharedWithMe( + tape, + httptest.NewRequest(http.MethodGet, "/graph/v1beta1/me/drive/sharedWithMe", nil), + ) + + jsonData := gjson.Get(tape.Body.String(), "value.0") + Expect(jsonData.Get("@libre\\.graph\\.me\\.following").Exists()).To(BeTrue()) + Expect(jsonData.Get("@libre\\.graph\\.me\\.following").Bool()).To(BeFalse()) + + statResponse.Info.ArbitraryMetadata = &providerv1beta1.ArbitraryMetadata{ + Metadata: map[string]string{"http://owncloud.org/ns/favorite": "1"}, + } + tape = httptest.NewRecorder() + svc.ListSharedWithMe( + tape, + httptest.NewRequest(http.MethodGet, "/graph/v1beta1/me/drive/sharedWithMe", nil), + ) + + jsonData = gjson.Get(tape.Body.String(), "value.0") + Expect(jsonData.Get("@libre\\.graph\\.me\\.following").Bool()).To(BeTrue()) + }) + It("populates the driveItem parentReference properties", func() { share := listReceivedSharesResponse.Shares[0].Share share.Id = &collaborationv1beta1.ShareId{OpaqueId: "1:2:3"} diff --git a/services/graph/pkg/service/v0/utils.go b/services/graph/pkg/service/v0/utils.go index 7553f98a47..d826838263 100644 --- a/services/graph/pkg/service/v0/utils.go +++ b/services/graph/pkg/service/v0/utils.go @@ -286,6 +286,8 @@ func cs3ReceivedSharesToDriveItems(ctx context.Context, driveItem.SetETag(etag) } + driveItem.SetLibreGraphMeFollowing(isFollowing(ctx, shareStat.GetInfo())) + // connect the dots { if mTime := shareStat.GetInfo().GetMtime(); mTime != nil {