Skip to content

Commit

Permalink
Implement favourites for eos/grpc (#4863)
Browse files Browse the repository at this point in the history
* Implement favourites for eos/grpc

* Add changelog

* Add changelog

* Remove unused const

---------

Co-authored-by: Fabrizio Furano <[email protected]>
Co-authored-by: Giuseppe Lo Presti <[email protected]>
  • Loading branch information
3 people authored Oct 29, 2024
1 parent ce7d0f8 commit 1d84cf0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog/unreleased/fav-grpc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: Favourites for eos/grpc

https://github.com/cs3org/reva/pull/4863
68 changes: 67 additions & 1 deletion pkg/eosclient/eosgrpc/eosgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ import (
)

const (
versionPrefix = ".sys.v#."
versionPrefix = ".sys.v#."
favoritesKey = "http://owncloud.org/ns/favorite"
)

const (
Expand All @@ -57,6 +58,29 @@ const (
UserAttr
)

func serializeAttribute(a *eosclient.Attribute) string {
return fmt.Sprintf("%s.%s=%s", attrTypeToString(a.Type), a.Key, a.Val)
}

func attrTypeToString(at eosclient.AttrType) string {
switch at {
case eosclient.SystemAttr:
return "sys"
case eosclient.UserAttr:
return "user"
default:
return "invalid"
}
}

func isValidAttribute(a *eosclient.Attribute) bool {
// validate that an attribute is correct.
if (a.Type != eosclient.SystemAttr && a.Type != eosclient.UserAttr) || a.Key == "" {
return false
}
return true
}

// Options to configure the Client.
type Options struct {

Expand Down Expand Up @@ -483,6 +507,22 @@ func (c *Client) fixupACLs(ctx context.Context, auth eosclient.Authorization, in

// SetAttr sets an extended attributes on a path.
func (c *Client) SetAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, errorIfExists, recursive bool, path, app string) error {
if !isValidAttribute(attr) {
return errors.New("eos: attr is invalid: " + serializeAttribute(attr))
}

// Favorites need to be stored per user so handle these separately
if attr.Type == eosclient.UserAttr && attr.Key == favoritesKey {
info, err := c.GetFileInfoByPath(ctx, auth, path)
if err != nil {
return err
}
return c.handleFavAttr(ctx, auth, attr, recursive, path, info, true)
}
return c.setEOSAttr(ctx, auth, attr, errorIfExists, recursive, path, app)
}

func (c *Client) setEOSAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, errorIfExists, recursive bool, path, app string) error {
log := appctx.GetLogger(ctx)
log.Info().Str("func", "SetAttr").Str("uid,gid", auth.Role.UID+","+auth.Role.GID).Str("path", path).Msg("")

Expand Down Expand Up @@ -531,6 +571,32 @@ func (c *Client) SetAttr(ctx context.Context, auth eosclient.Authorization, attr
return err
}

func (c *Client) handleFavAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, recursive bool, path string, info *eosclient.FileInfo, set bool) error {
var err error
u := appctx.ContextMustGetUser(ctx)
if info == nil {
info, err = c.GetFileInfoByPath(ctx, auth, path)
if err != nil {
return err
}
}
favStr := info.Attrs[favoritesKey]
favs, err := acl.Parse(favStr, acl.ShortTextForm)
if err != nil {
return err
}
if set {
err = favs.SetEntry(acl.TypeUser, u.Id.OpaqueId, "1")
if err != nil {
return err
}
} else {
favs.DeleteEntry(acl.TypeUser, u.Id.OpaqueId)
}
attr.Val = favs.Serialize()
return c.setEOSAttr(ctx, auth, attr, false, recursive, path, "")
}

// UnsetAttr unsets an extended attribute on a path.
func (c *Client) UnsetAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, recursive bool, path, app string) error {
log := appctx.GetLogger(ctx)
Expand Down

0 comments on commit 1d84cf0

Please sign in to comment.