Skip to content

Commit

Permalink
Resolving more PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Geens committed Jan 27, 2025
1 parent 9cc87e9 commit 265f569
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 52 deletions.
22 changes: 12 additions & 10 deletions share/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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),
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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{
Expand All @@ -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,
}
Expand Down
72 changes: 30 additions & 42 deletions share/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ type config struct {
}

type mgr struct {
c *config
//db *sql.DB
c *config
db *gorm.DB
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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).
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit 265f569

Please sign in to comment.