From 265f5694a890710f24a1f4ee9e79a7fb396191f0 Mon Sep 17 00:00:00 2001 From: Jesse Geens Date: Fri, 24 Jan 2025 13:38:08 +0100 Subject: [PATCH] Resolving more PR comments --- share/model.go | 22 ++++++++------- share/sql/sql.go | 72 ++++++++++++++++++++---------------------------- 2 files changed, 42 insertions(+), 52 deletions(-) diff --git a/share/model.go b/share/model.go index 2cc5933..1e35aff 100644 --- a/share/model.go +++ b/share/model.go @@ -38,17 +38,17 @@ type ProtoShare struct { ItemType ItemType // file | folder | reference | symlink InitialPath string Inode string - Permissions uint8 Instance string + Permissions uint8 Orphan bool - Description string - Expiration datatypes.Null[datatypes.Date] + Expiration datatypes.NullTime } type Share struct { ProtoShare ShareWith string SharedWithIsGroup bool + Description string } type PublicLink struct { @@ -60,7 +60,7 @@ type PublicLink struct { NotifyUploadsExtraRecipients string Password string // Users can give a name to a share - ShareName string + LinkName string } type ShareState struct { @@ -75,9 +75,12 @@ type ShareState struct { } func (s *Share) AsCS3Share(granteeType userpb.UserType) *collaboration.Share { - ts := &typespb.Timestamp{ + creationTs := &typespb.Timestamp{ Seconds: uint64(s.CreatedAt.Unix()), } + updateTs := &typespb.Timestamp{ + Seconds: uint64(s.UpdatedAt.Unix()), + } return &collaboration.Share{ Id: &collaboration.ShareId{ OpaqueId: strconv.FormatUint(uint64(s.ID), 10), @@ -91,8 +94,8 @@ func (s *Share) AsCS3Share(granteeType userpb.UserType) *collaboration.Share { Grantee: extractGrantee(s.SharedWithIsGroup, s.ShareWith, granteeType), Owner: conversions.MakeUserID(s.UIDOwner), Creator: conversions.MakeUserID(s.UIDInitiator), - Ctime: ts, - Mtime: ts, + Ctime: creationTs, + Mtime: updateTs, } } @@ -123,7 +126,7 @@ func (p *PublicLink) AsCS3PublicShare() *link.PublicShare { } var expires *typespb.Timestamp if p.Expiration.Valid { - exp, err := p.Expiration.V.Value() + exp, err := p.Expiration.Value() if err == nil { expiration := exp.(time.Time) expires = &typespb.Timestamp{ @@ -144,13 +147,12 @@ func (p *PublicLink) AsCS3PublicShare() *link.PublicShare { Owner: conversions.MakeUserID(p.UIDOwner), Creator: conversions.MakeUserID(p.UIDInitiator), Token: p.Token, - DisplayName: p.ShareName, + DisplayName: p.LinkName, PasswordProtected: pwd, Expiration: expires, Ctime: ts, Mtime: ts, Quicklink: p.Quicklink, - Description: p.Description, NotifyUploads: p.NotifyUploads, NotifyUploadsExtraRecipients: p.NotifyUploadsExtraRecipients, } diff --git a/share/sql/sql.go b/share/sql/sql.go index 40f686d..57c302f 100644 --- a/share/sql/sql.go +++ b/share/sql/sql.go @@ -79,8 +79,7 @@ type config struct { } type mgr struct { - c *config - //db *sql.DB + c *config db *gorm.DB } @@ -267,15 +266,7 @@ func (m *mgr) Unshare(ctx context.Context, ref *collaboration.ShareReference) er var share *model.Share var err error if id := ref.GetId(); id != nil { - var intId int - intId, err = strconv.Atoi(id.OpaqueId) - share = &model.Share{ - ProtoShare: model.ProtoShare{ - Model: gorm.Model{ - ID: uint(intId), - }, - }, - } + share, err = emptyShareWithId(id.OpaqueId) } else { share, err = m.getShare(ctx, ref) } @@ -290,15 +281,7 @@ func (m *mgr) UpdateShare(ctx context.Context, ref *collaboration.ShareReference var share *model.Share var err error if id := ref.GetId(); id != nil { - var intId int - intId, err = strconv.Atoi(id.OpaqueId) - share = &model.Share{ - ProtoShare: model.ProtoShare{ - Model: gorm.Model{ - ID: uint(intId), - }, - }, - } + share, err = emptyShareWithId(id.OpaqueId) } else { share, err = m.getShare(ctx, ref) } @@ -432,7 +415,6 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F return receivedShares, nil } -// shareId *collaboration.ShareId func (m *mgr) getShareState(ctx context.Context, share *model.Share, user *userpb.User) (*model.ShareState, error) { var shareState model.ShareState query := m.db.Model(&shareState). @@ -449,14 +431,26 @@ func (m *mgr) getShareState(ctx context.Context, share *model.Share, user *userp Synced: false, User: user.Username, } - // Does not really matter if it fails, next time the user - // lists his shares this will just be called again - m.db.Save(&shareState) } return &shareState, nil } +func emptyShareWithId(id string) (*model.Share, error) { + intId, err := strconv.Atoi(id) + if err != nil { + return nil, err + } + share := &model.Share{ + ProtoShare: model.ProtoShare{ + Model: gorm.Model{ + ID: uint(intId), + }, + }, + } + return share, nil +} + func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId, gtype userpb.UserType) (*collaboration.ReceivedShare, error) { user := appctx.ContextMustGetUser(ctx) share, err := m.getByID(ctx, id) @@ -513,27 +507,21 @@ func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareRefe return s, nil } -func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) { +func (m *mgr) UpdateReceivedShare(ctx context.Context, recvShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) { user := appctx.ContextMustGetUser(ctx) - rs, err := m.getReceivedByID(ctx, share.Share.Id, user.Id.Type) + rs, err := m.getReceivedByID(ctx, recvShare.Share.Id, user.Id.Type) if err != nil { return nil, err } - shareId, err := strconv.Atoi(share.Share.Id.OpaqueId) + share, err := emptyShareWithId(recvShare.Share.Id.OpaqueId) if err != nil { return nil, err } - shareState, err := m.getShareState(ctx, &model.Share{ - ProtoShare: model.ProtoShare{ - Model: gorm.Model{ - ID: uint(shareId), - }, - }, - }, user) + shareState, err := m.getShareState(ctx, share, user) if err != nil { return nil, err } @@ -542,21 +530,21 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.Rece for _, path := range fieldMask.Paths { switch path { case "state": - rs.State = share.State + rs.State = recvShare.State + switch rs.State { + case collaboration.ShareState_SHARE_STATE_ACCEPTED: + shareState.Hidden = false + case collaboration.ShareState_SHARE_STATE_REJECTED: + shareState.Hidden = true + } case "hidden": - rs.Hidden = share.Hidden + rs.Hidden = recvShare.Hidden default: return nil, errtypes.NotSupported("updating " + path + " is not supported") } } // Now we do the actual update to the db model - switch rs.State { - case collaboration.ShareState_SHARE_STATE_ACCEPTED: - shareState.Hidden = false - case collaboration.ShareState_SHARE_STATE_REJECTED: - shareState.Hidden = true - } res := m.db.Save(&shareState) if res.Error != nil {