Repository navigation
feat: expose the author of file versions in the versions PROPFIND #816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package propfind_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
|
|
||
| provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/owncloud/reva/v2/internal/http/services/owncloud/ocdav/net" | ||
| "github.com/owncloud/reva/v2/internal/http/services/owncloud/ocdav/propfind" | ||
| "github.com/owncloud/reva/v2/pkg/utils" | ||
| ) | ||
|
|
||
| var _ = Describe("version author properties", func() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better move these tests inside the "propfind_test.go" file. |
||
| body := `<?xml version="1.0"?> | ||
| <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns"><d:prop> | ||
| <oc:meta-version-edited-by /><oc:meta-version-edited-by-name /> | ||
| </d:prop></d:propfind>` | ||
|
|
||
|
Comment on lines
+16
to
+20
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be moved to a This way we can ensure that the tests will receive the same data. |
||
| respond := func(md *provider.ResourceInfo) string { | ||
| pf, _, err := propfind.ReadPropfind(strings.NewReader(body)) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| ctx := context.WithValue(context.Background(), net.CtxKeyBaseURI, "/remote.php/dav/meta/id/versions") | ||
| b, err := propfind.MultistatusResponse(ctx, &pf, []*provider.ResourceInfo{md}, "", "", nil, false) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| return string(b) | ||
| } | ||
| version := func(o interface{}) *provider.ResourceInfo { | ||
| md := &provider.ResourceInfo{Type: provider.ResourceType_RESOURCE_TYPE_FILE, Path: "v/key"} | ||
| if o != nil { | ||
| md.Opaque = utils.AppendPlainToOpaque(nil, propfind.OpaqueVersionAuthorUsername, "carol") | ||
| md.Opaque = utils.AppendPlainToOpaque(md.Opaque, propfind.OpaqueVersionAuthorDisplayName, "Carol King") | ||
| } | ||
| return md | ||
| } | ||
|
Comment on lines
+21
to
+36
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to move these functions inside the tests. Yes, there will be some code duplication, but the test should be easier and more straightforward to read. |
||
|
|
||
| It("reports username and display name of the author", func() { | ||
| out := respond(version(true)) | ||
| Expect(out).To(ContainSubstring("<oc:meta-version-edited-by>carol</oc:meta-version-edited-by>")) | ||
| Expect(out).To(ContainSubstring("<oc:meta-version-edited-by-name>Carol King</oc:meta-version-edited-by-name>")) | ||
| }) | ||
|
|
||
| It("answers 404 for the properties of a version without a recorded author", func() { | ||
| out := respond(version(nil)) | ||
| Expect(out).To(ContainSubstring("HTTP/1.1 404 Not Found")) | ||
| Expect(out).ToNot(ContainSubstring("carol")) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ import ( | |
| "github.com/owncloud/reva/v2/pkg/storagespace" | ||
| "github.com/owncloud/reva/v2/pkg/utils" | ||
|
|
||
| userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
| rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" | ||
| provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" | ||
| types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" | ||
|
|
@@ -173,8 +174,10 @@ func (h *VersionsHandler) doListVersions(w http.ResponseWriter, r *http.Request, | |
| Type: provider.ResourceType_RESOURCE_TYPE_CONTAINER, | ||
| }) | ||
|
|
||
| authors := &versionAuthors{client: client, users: map[string]*userpb.User{}} | ||
| for i := range versions { | ||
| vi := &provider.ResourceInfo{ | ||
| Opaque: authors.opaqueFor(ctx, versions[i].GetOpaque()), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to just copy the information here. The opaque should contain just the user ID of the author; we only want to resolve his displayname if needed. This seems a wrong place to get a displayname when we don't know if it has been requested.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition, I'm not sure if we need a cache. |
||
| // TODO(jfd) we cannot access version content, this will be a problem when trying to fetch version thumbnails | ||
| // Opaque | ||
| Type: provider.ResourceType_RESOURCE_TYPE_FILE, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package ocdav | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" | ||
| userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
| rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" | ||
| typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" | ||
|
|
||
| "github.com/owncloud/reva/v2/internal/http/services/owncloud/ocdav/propfind" | ||
| "github.com/owncloud/reva/v2/pkg/appctx" | ||
| "github.com/owncloud/reva/v2/pkg/storage" | ||
| "github.com/owncloud/reva/v2/pkg/utils" | ||
| ) | ||
|
|
||
| // versionAuthors resolves the author of file versions to a name for the | ||
| // duration of one request, asking the user provider once per distinct user. | ||
| type versionAuthors struct { | ||
| client gateway.GatewayAPIClient | ||
| users map[string]*userpb.User | ||
| } | ||
|
|
||
| // opaqueFor returns the opaque to put on a version's ResourceInfo, or nil when | ||
| // the version has no recorded author. A user that can no longer be resolved | ||
| // (e.g. deleted) leaves the properties unset rather than failing the listing. | ||
| func (a *versionAuthors) opaqueFor(ctx context.Context, v *typespb.Opaque) *typespb.Opaque { | ||
| id := &userpb.UserId{} | ||
| if !utils.ExistsInOpaque(v, storage.RevisionAuthorOpaqueKey) || | ||
| utils.ReadJSONFromOpaque(v, storage.RevisionAuthorOpaqueKey, id) != nil { | ||
| return nil | ||
| } | ||
| key := id.GetIdp() + "!" + id.GetOpaqueId() | ||
| u, ok := a.users[key] | ||
| if !ok { | ||
| res, err := a.client.GetUser(ctx, &userpb.GetUserRequest{UserId: id}) | ||
| switch { | ||
| case err != nil: | ||
| appctx.GetLogger(ctx).Error().Err(err).Msg("could not resolve version author") | ||
| case res.GetStatus().GetCode() == rpc.Code_CODE_OK: | ||
| u = res.GetUser() | ||
| } | ||
| a.users[key] = u // also caches a miss | ||
| } | ||
| if u == nil { | ||
| return nil | ||
| } | ||
| o := utils.AppendPlainToOpaque(nil, propfind.OpaqueVersionAuthorUsername, u.GetUsername()) | ||
| return utils.AppendPlainToOpaque(o, propfind.OpaqueVersionAuthorDisplayName, u.GetDisplayName()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package ocdav | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
| rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" | ||
| typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/owncloud/reva/v2/internal/http/services/owncloud/ocdav/propfind" | ||
| "github.com/owncloud/reva/v2/pkg/rgrpc/status" | ||
| "github.com/owncloud/reva/v2/pkg/storage" | ||
| "github.com/owncloud/reva/v2/pkg/utils" | ||
| "github.com/owncloud/reva/v2/tests/cs3mocks/mocks" | ||
| ) | ||
|
|
||
| func TestVersionAuthorsResolveOncePerUserAndTolerateMisses(t *testing.T) { | ||
| ctx := context.Background() | ||
| gw := mocks.NewGatewayAPIClient(t) | ||
| gw.On("GetUser", mock.Anything, mock.MatchedBy(func(r *userpb.GetUserRequest) bool { return r.UserId.OpaqueId == "carol" })). | ||
| Return(&userpb.GetUserResponse{Status: status.NewOK(ctx), User: &userpb.User{Username: "carol", DisplayName: "Carol King"}}, nil).Once() | ||
| gw.On("GetUser", mock.Anything, mock.MatchedBy(func(r *userpb.GetUserRequest) bool { return r.UserId.OpaqueId == "gone" })). | ||
| Return(&userpb.GetUserResponse{Status: &rpc.Status{Code: rpc.Code_CODE_NOT_FOUND}}, nil).Once() | ||
|
|
||
| a := &versionAuthors{client: gw, users: map[string]*userpb.User{}} | ||
| mk := func(id string) *typespb.Opaque { | ||
| return utils.AppendJSONToOpaque(nil, storage.RevisionAuthorOpaqueKey, &userpb.UserId{OpaqueId: id, Idp: "idp"}) | ||
| } | ||
|
|
||
| for i := 0; i < 2; i++ { // second round must be served from the per-request cache | ||
| o := a.opaqueFor(ctx, mk("carol")) | ||
| require.Equal(t, "carol", utils.ReadPlainFromOpaque(o, propfind.OpaqueVersionAuthorUsername)) | ||
| require.Equal(t, "Carol King", utils.ReadPlainFromOpaque(o, propfind.OpaqueVersionAuthorDisplayName)) | ||
| require.Nil(t, a.opaqueFor(ctx, mk("gone")), "an unresolvable author must not fail the listing") | ||
| } | ||
| require.Nil(t, a.opaqueFor(ctx, nil), "a version without an author has no opaque") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package decomposedfs | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
| typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" | ||
|
|
||
| "github.com/owncloud/reva/v2/pkg/appctx" | ||
| ctxpkg "github.com/owncloud/reva/v2/pkg/ctx" | ||
| "github.com/owncloud/reva/v2/pkg/storage" | ||
| "github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/metadata/prefixes" | ||
| "github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/node" | ||
| "github.com/owncloud/reva/v2/pkg/utils" | ||
| ) | ||
|
|
||
| var authorAttrs = []string{prefixes.AuthorIDAttr, prefixes.AuthorIDPAttr, prefixes.AuthorTypeAttr} | ||
|
|
||
| func isAuthorAttr(name string) bool { | ||
| for _, a := range authorAttrs { | ||
| if a == name { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // setAuthor records the user in the context as author of the content being written. | ||
| // Without a user nothing is written, so no author is claimed. | ||
| func setAuthor(ctx context.Context, attrs node.Attributes) { | ||
| u, ok := ctxpkg.ContextGetUser(ctx) | ||
| if !ok || u.GetId().GetOpaqueId() == "" { | ||
| return | ||
| } | ||
| attrs.SetString(prefixes.AuthorIDAttr, u.GetId().GetOpaqueId()) | ||
| attrs.SetString(prefixes.AuthorIDPAttr, u.GetId().GetIdp()) | ||
| attrs.SetString(prefixes.AuthorTypeAttr, utils.UserTypeToString(u.GetId().GetType())) | ||
| } | ||
|
|
||
| // revisionAuthorOpaque reads the author of a revision. It returns nil for a | ||
| // revision without one, e.g. one created before authors were recorded. | ||
| func (fs *Decomposedfs) revisionAuthorOpaque(ctx context.Context, revisionPath string) *typespb.Opaque { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not against this function, but there are some things that itches...
|
||
| attrs, err := fs.lu.MetadataBackend().All(ctx, revisionPath) | ||
| if err != nil { | ||
| appctx.GetLogger(ctx).Debug().Err(err).Str("revision", revisionPath).Msg("could not read revision author") | ||
| return nil | ||
| } | ||
| id := string(attrs[prefixes.AuthorIDAttr]) | ||
| if id == "" { | ||
| return nil | ||
| } | ||
| return utils.AppendJSONToOpaque(nil, storage.RevisionAuthorOpaqueKey, &userpb.UserId{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| OpaqueId: id, | ||
| Idp: string(attrs[prefixes.AuthorIDPAttr]), | ||
| Type: utils.UserTypeMap(string(attrs[prefixes.AuthorTypeAttr])), | ||
| }) | ||
| } | ||
|
|
||
| // dropStaleAuthor removes the author attributes from target that the source does not have. | ||
| func (fs *Decomposedfs) dropStaleAuthor(ctx context.Context, sourcePath, targetPath string) error { | ||
| src, err := fs.lu.MetadataBackend().All(ctx, sourcePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| dst, err := fs.lu.MetadataBackend().All(ctx, targetPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, a := range authorAttrs { | ||
| if _, ok := src[a]; ok { | ||
| continue | ||
| } | ||
| // removing an attribute that is not there fails on the xattr backend | ||
| if _, ok := dst[a]; !ok { | ||
| continue | ||
| } | ||
| if err := fs.lu.MetadataBackend().Remove(ctx, targetPath, a, false); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes more sense for these constants to be part of the "pkg/utils" package.
From my point of view, either follow what is already done and use the string directly, or move all those opaque strings (including "owner-display-name", "downloadURL", etc) as constants in the "pkg/utils" package.
I'm fine to keep using the string directly and leave it as a technical debt for a followup PR if the amount of changes needed is too big.