enhancement: make use of body driveItem for graph share accept

This commit is contained in:
Florian Schade
2024-02-28 17:29:04 +01:00
committed by Ralf Haferkamp
parent 1178491710
commit 5f705c6263
@@ -2,12 +2,15 @@ package svc
import ( import (
"context" "context"
"errors"
"net/http" "net/http"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1" collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/go-chi/render" "github.com/go-chi/render"
libregraph "github.com/owncloud/libre-graph-api-go" libregraph "github.com/owncloud/libre-graph-api-go"
"google.golang.org/protobuf/types/known/fieldmaskpb" "google.golang.org/protobuf/types/known/fieldmaskpb"
@@ -18,7 +21,7 @@ import (
// DrivesDriveItemProvider is the interface that needs to be implemented by the individual space service // DrivesDriveItemProvider is the interface that needs to be implemented by the individual space service
type DrivesDriveItemProvider interface { type DrivesDriveItemProvider interface {
CreateChildren(ctx context.Context, driveId, itemId storageprovider.ResourceId, driveItem libregraph.DriveItem) (libregraph.DriveItem, error) AcceptShare(ctx context.Context, driveId, itemId storageprovider.ResourceId, driveItem libregraph.DriveItem) (libregraph.DriveItem, error)
} }
// DrivesDriveItemService contains the production business logic for everything that relates to drives // DrivesDriveItemService contains the production business logic for everything that relates to drives
@@ -35,14 +38,35 @@ func NewDrivesDriveItemService(logger log.Logger, gatewaySelector pool.Selectabl
}, nil }, nil
} }
// CreateChildren is currently only used for accepting pending//dangling shares. // AcceptShare is currently only used for accepting pending//dangling shares.
// fixMe: currently the driveItem is not used, why is it needed? // fixMe: currently the driveItem is not used, why is it needed?
func (s DrivesDriveItemService) CreateChildren(ctx context.Context, driveId, itemId storageprovider.ResourceId, _ libregraph.DriveItem) (libregraph.DriveItem, error) { func (s DrivesDriveItemService) AcceptShare(ctx context.Context, driveId, itemId storageprovider.ResourceId, driveItem libregraph.DriveItem) (libregraph.DriveItem, error) {
remoteItem := driveItem.GetRemoteItem()
switch {
case driveId.GetStorageId() != utils.ShareStorageProviderID:
fallthrough
case driveId.GetSpaceId() != utils.ShareStorageSpaceID:
fallthrough
case itemId.GetStorageId() != utils.ShareStorageProviderID:
fallthrough
case itemId.GetSpaceId() != utils.ShareStorageSpaceID:
fallthrough
case itemId.GetOpaqueId() != utils.ShareStorageSpaceID:
return libregraph.DriveItem{}, errors.New("invalid item or drive id, ids do not match any known share jail")
case remoteItem.GetId() == "":
return libregraph.DriveItem{}, errors.New("empty remote item id")
}
gatewayClient, err := s.gatewaySelector.Next() gatewayClient, err := s.gatewaySelector.Next()
if err != nil { if err != nil {
return libregraph.DriveItem{}, err return libregraph.DriveItem{}, err
} }
resourceId, err := storagespace.ParseID(remoteItem.GetId())
if err != nil {
return libregraph.DriveItem{}, err
}
receivedSharesResponse, err := gatewayClient.ListReceivedShares(ctx, &collaboration.ListReceivedSharesRequest{ receivedSharesResponse, err := gatewayClient.ListReceivedShares(ctx, &collaboration.ListReceivedSharesRequest{
Filters: []*collaboration.Filter{ Filters: []*collaboration.Filter{
{ {
@@ -60,35 +84,40 @@ func (s DrivesDriveItemService) CreateChildren(ctx context.Context, driveId, ite
{ {
Type: collaboration.Filter_TYPE_RESOURCE_ID, Type: collaboration.Filter_TYPE_RESOURCE_ID,
Term: &collaboration.Filter_ResourceId{ Term: &collaboration.Filter_ResourceId{
ResourceId: &storageprovider.ResourceId{ ResourceId: &resourceId,
StorageId: driveId.GetStorageId(),
SpaceId: driveId.GetSpaceId(),
OpaqueId: itemId.GetOpaqueId(),
},
}, },
}, },
}, },
}) })
if err != nil {
return libregraph.DriveItem{}, err
}
for _, receivedShare := range receivedSharesResponse.GetShares() { for _, receivedShare := range receivedSharesResponse.GetShares() {
mountPoint := receivedShare.GetMountPoint() updateMask := &fieldmaskpb.FieldMask{Paths: []string{"state"}}
if mountPoint == nil {
// fixMe: should not happen, add exception handling
continue
}
receivedShare.State = collaboration.ShareState_SHARE_STATE_ACCEPTED receivedShare.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
receivedShare.MountPoint = &storageprovider.Reference{
// keep the original mount point path, // handle mount point related changes
// custom path handling should come here later {
Path: mountPoint.GetPath(), mountPoint := receivedShare.GetMountPoint()
if mountPoint == nil {
mountPoint = &storageprovider.Reference{}
}
name := driveItem.GetName()
newPath := utils.MakeRelativePath(name)
// only update if mountPoint name is not empty and the path has changed
if name != "" && mountPoint.GetPath() != newPath {
mountPoint.Path = newPath
receivedShare.MountPoint = mountPoint
updateMask.Paths = append(updateMask.Paths, "mount_point")
}
} }
updateReceivedShareRequest := &collaboration.UpdateReceivedShareRequest{ updateReceivedShareRequest := &collaboration.UpdateReceivedShareRequest{
Share: receivedShare, Share: receivedShare,
// mount_point currently contain no changes, this is for future use UpdateMask: updateMask,
// state changes from pending or rejected to accept
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}},
} }
//fixMe: should be processed in parallel //fixMe: should be processed in parallel
@@ -137,8 +166,14 @@ func (api DrivesDriveItemApi) CreateChildren(w http.ResponseWriter, r *http.Requ
return return
} }
driveItem, err := api.drivesDriveItemService. driveItem := libregraph.DriveItem{}
CreateChildren(ctx, driveID, itemID, libregraph.DriveItem{}) if err := StrictJSONUnmarshal(r.Body, &driveItem); err != nil {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "invalid request body")
return
}
_, err = api.drivesDriveItemService.
AcceptShare(ctx, driveID, itemID, driveItem)
render.Status(r, http.StatusOK) render.Status(r, http.StatusOK)
render.JSON(w, r, driveItem) render.JSON(w, r, driveItem)