feat(graph): expose the favorite state on drives and sharedWithMe

Spaces and received shares can be marked as favorites, but the graph
API did not report it, so the web app could not show them.

drive.root and the sharedWithMe driveItems now carry
@libre.graph.me.following. The space root info only holds the requested
quota metadata, so the per-user flag is also read from the favorites
list the storage driver adds to the opaque.

Fixes #2511
This commit is contained in:
Alexander Ackermann committed 2026-11-17 13:36:41 +01:00
1 parent 0ec0411e75
commit d2b1ab3c45
5 files changed
+114

No files matched your search

+4
View File
@@ -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
}
+31
View File
@@ -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()
@@ -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),
@@ -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"}
+2
View File
@@ -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 {