Skip to content

Commit

Permalink
Implement favourites for eos/grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrizio Furano committed Sep 23, 2024
1 parent 122da0b commit 193e4b0
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion pkg/eosclient/eosgrpc/eosgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ import (
)

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

const (
Expand All @@ -57,6 +59,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 @@ -508,6 +533,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 @@ -556,6 +597,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 193e4b0

Please sign in to comment.