Skip to content

Commit b4190b0

Browse files
authored
fix: sync shared-drive description on file renames (#4761)
## Context Shared-drive names shown in the Sharing tab were not updated in some rename flows. Root cause: `share-update` (description sync) was only triggered for directory-root renames. For file-root shared drives, renaming via `PATCH /files/:id` or `PATCH /sharings/drives/:sharingID/:fileID` did not always refresh `sharing.description`. ## Changes - Refactored `UpdateSharingDescriptionIfNeeded` to accept: - `referencedBy []couchdb.DocReference` - `newDescription string` - Triggered description update on **actual rename only** (`oldName != newName`) for: - `/files` patch flow (single patch + bulk patch) - `/sharings/drives/:sharingID/:fileID` patch flow - Kept existing behavior for directory roots and extended it to file roots.
2 parents 0dc241c + ad9ad4f commit b4190b0

4 files changed

Lines changed: 81 additions & 13 deletions

File tree

model/sharing/update_description.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package sharing
33
import (
44
"github.com/cozy/cozy-stack/model/instance"
55
"github.com/cozy/cozy-stack/model/job"
6-
"github.com/cozy/cozy-stack/model/vfs"
76
"github.com/cozy/cozy-stack/pkg/consts"
7+
"github.com/cozy/cozy-stack/pkg/couchdb"
88
)
99

10-
// UpdateSharingDescriptionIfNeeded checks if the given directory is a sharing
11-
// root and triggers a job to update the sharing description if needed.
12-
func UpdateSharingDescriptionIfNeeded(inst *instance.Instance, dir *vfs.DirDoc) {
13-
// Check if this directory is referenced by any sharings
14-
for _, ref := range dir.ReferencedBy {
10+
// UpdateSharingDescriptionIfNeeded checks if the given references contain
11+
// sharing references and triggers a job to update the sharing description.
12+
func UpdateSharingDescriptionIfNeeded(inst *instance.Instance, referencedBy []couchdb.DocReference, newDescription string) {
13+
// Check if this file/directory is referenced by any sharings
14+
for _, ref := range referencedBy {
1515
if ref.Type == consts.Sharings {
16-
// This directory is a sharing root, trigger an update job
16+
// This file/directory is a sharing root, trigger an update job
1717
msg, err := job.NewMessage(&UpdateMsg{
1818
SharingID: ref.ID,
19-
NewDescription: dir.DocName,
19+
NewDescription: newDescription,
2020
})
2121
if err != nil {
2222
inst.Logger().WithNamespace("sharing").

web/files/files.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,15 @@ func applyPatch(c echo.Context, fs vfs.VFS, patch *docPatch) (err error) {
743743
UpdateDirCozyMetadata(c, dir)
744744
dir, err = vfs.ModifyDirMetadata(fs, dir, &patch.DocPatch)
745745
if err == nil && patch.Name != nil && oldDirName != *patch.Name {
746-
sharing.UpdateSharingDescriptionIfNeeded(middlewares.GetInstance(c), dir)
746+
sharing.UpdateSharingDescriptionIfNeeded(middlewares.GetInstance(c), dir.ReferencedBy, dir.DocName)
747747
}
748748
} else {
749+
oldFileName := file.DocName
749750
UpdateFileCozyMetadata(c, file, false)
750751
file, err = vfs.ModifyFileMetadata(fs, file, &patch.DocPatch)
752+
if err == nil && patch.Name != nil && oldFileName != *patch.Name {
753+
sharing.UpdateSharingDescriptionIfNeeded(middlewares.GetInstance(c), file.ReferencedBy, file.DocName)
754+
}
751755
}
752756
}
753757
if err != nil {
@@ -794,11 +798,15 @@ func applyPatches(c echo.Context, fs vfs.VFS, patches []*docPatch) (errors []*js
794798
UpdateDirCozyMetadata(c, dir)
795799
dir, errp = vfs.ModifyDirMetadata(fs, dir, &patch.DocPatch)
796800
if errp == nil && patch.Name != nil && oldDirName != *patch.Name {
797-
sharing.UpdateSharingDescriptionIfNeeded(middlewares.GetInstance(c), dir)
801+
sharing.UpdateSharingDescriptionIfNeeded(middlewares.GetInstance(c), dir.ReferencedBy, dir.DocName)
798802
}
799803
} else if file != nil {
804+
oldFileName := file.DocName
800805
UpdateFileCozyMetadata(c, file, false)
801-
_, errp = vfs.ModifyFileMetadata(fs, file, &patch.DocPatch)
806+
file, errp = vfs.ModifyFileMetadata(fs, file, &patch.DocPatch)
807+
if errp == nil && patch.Name != nil && oldFileName != *patch.Name {
808+
sharing.UpdateSharingDescriptionIfNeeded(middlewares.GetInstance(c), file.ReferencedBy, file.DocName)
809+
}
802810
}
803811
if errp != nil {
804812
jsonapiError := wrapVfsErrorJSONAPI(errp)

web/sharings/drives.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,21 +395,27 @@ func applyPatch(c echo.Context, fs vfs.VFS, patch *docPatch) (err error) {
395395
}
396396

397397
if dir != nil {
398+
oldDirName := dir.DocName
398399
files.UpdateDirCozyMetadata(c, dir)
399400
dir, err = vfs.ModifyDirMetadata(fs, dir, &patch.DocPatch)
400401
if err != nil {
401402
return err
402403
}
403-
if patch.Name != nil {
404+
if patch.Name != nil && oldDirName != *patch.Name {
404405
// Update sharing description if this directory is a sharing root
405-
sharing.UpdateSharingDescriptionIfNeeded(middlewares.GetInstance(c), dir)
406+
sharing.UpdateSharingDescriptionIfNeeded(middlewares.GetInstance(c), dir.ReferencedBy, dir.DocName)
406407
}
407408
} else {
409+
oldFileName := file.DocName
408410
files.UpdateFileCozyMetadata(c, file, false)
409411
file, err = vfs.ModifyFileMetadata(fs, file, &patch.DocPatch)
410412
if err != nil {
411413
return err
412414
}
415+
if patch.Name != nil && oldFileName != *patch.Name {
416+
// Update sharing description if this file is a file-root sharing root
417+
sharing.UpdateSharingDescriptionIfNeeded(middlewares.GetInstance(c), file.ReferencedBy, file.DocName)
418+
}
413419
}
414420

415421
if dir != nil {

web/sharings/drives_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,6 +2901,17 @@ func TestFileRootSharedDriveMutationRoutes(t *testing.T) {
29012901

29022902
patched.Path("$.data.attributes.name").String().IsEqual("RenamedRoot.txt")
29032903
patched.Path("$.data.attributes.cozyMetadata.favorite").Boolean().True()
2904+
require.Eventually(t, func() bool {
2905+
ownerSharing, err := sharing.FindSharing(env.acme, sharingID)
2906+
if err != nil || ownerSharing.Description != "RenamedRoot.txt" {
2907+
return false
2908+
}
2909+
recipientSharing, err := sharing.FindSharing(env.betty, sharingID)
2910+
if err != nil {
2911+
return false
2912+
}
2913+
return recipientSharing.Description == "RenamedRoot.txt"
2914+
}, 5*time.Second, 50*time.Millisecond)
29042915

29052916
eB.PATCH("/sharings/drives/"+sharingID+"/"+rootFileID).
29062917
WithHeader("Authorization", "Bearer "+env.bettyToken).
@@ -2931,6 +2942,49 @@ func TestFileRootSharedDriveMutationRoutes(t *testing.T) {
29312942
Path("$.data.attributes.trashed").Boolean().False()
29322943
})
29332944

2945+
t.Run("OwnerCanRenameFileRootViaFilesPatch", func(t *testing.T) {
2946+
eA, _, _ := env.createClients(t)
2947+
2948+
rootFileID := createFile(t, eA, "", "OwnerRenamedRoot.txt", env.acmeToken)
2949+
sharingID, _ := createFileRootSharedDrive(
2950+
t,
2951+
env.acme,
2952+
env.acmeToken,
2953+
env.tsA.URL,
2954+
rootFileID,
2955+
"Owner file-root shared drive",
2956+
[]RecipientInfo{{Name: "Betty", Email: "betty@example.net", ReadOnly: false}},
2957+
)
2958+
acceptSharedDriveForBetty(t, env.acme, env.betty, env.tsA.URL, env.tsB.URL, sharingID)
2959+
2960+
newDriveName := testify(t, "Owner renamed file-root via files patch")
2961+
2962+
renamed := eA.PATCH("/files/"+rootFileID).
2963+
WithHeader("Authorization", "Bearer "+env.acmeToken).
2964+
WithHeader("Content-Type", "application/json").
2965+
WithBytes([]byte(`{
2966+
"data": {
2967+
"type": "io.cozy.files",
2968+
"id": "` + rootFileID + `",
2969+
"attributes": {
2970+
"name": "` + newDriveName + `"
2971+
}
2972+
}
2973+
}`)).
2974+
Expect().Status(200).
2975+
JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
2976+
Object()
2977+
renamed.Path("$.data.attributes.name").String().IsEqual(newDriveName)
2978+
2979+
require.Eventually(t, func() bool {
2980+
s, err := sharing.FindSharing(env.betty, sharingID)
2981+
if err != nil {
2982+
return false
2983+
}
2984+
return s.Description == newDriveName
2985+
}, 5*time.Second, 50*time.Millisecond)
2986+
})
2987+
29342988
t.Run("DestroyTrashedRootFile", func(t *testing.T) {
29352989
eA, eB, _ := env.createClients(t)
29362990

0 commit comments

Comments
 (0)