Skip to content

Commit

Permalink
Merge pull request #2015 from anyproto/go-4840-send-amend-details-eve…
Browse files Browse the repository at this point in the history
…nt-sync

GO-4840 Send AmendDetails synchronously
  • Loading branch information
KirillSto authored Jan 16, 2025
2 parents aa990e0 + be1d0c8 commit f24fcea
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 123 deletions.
21 changes: 11 additions & 10 deletions core/block/detailservice/mock_detailservice/mock_Service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions core/block/detailservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Service interface {
SetDetails(ctx session.Context, objectId string, details []domain.Detail) error
SetDetailsAndUpdateLastUsed(ctx session.Context, objectId string, details []domain.Detail) error
SetDetailsList(ctx session.Context, objectIds []string, details []domain.Detail) error
ModifyDetails(objectId string, modifier func(current *domain.Details) (*domain.Details, error)) error
ModifyDetails(ctx session.Context, objectId string, modifier func(current *domain.Details) (*domain.Details, error)) error
ModifyDetailsList(req *pb.RpcObjectListModifyDetailValuesRequest) error

ObjectTypeAddRelations(ctx context.Context, objectTypeId string, relationKeys []domain.RelationKey) error
Expand Down Expand Up @@ -114,15 +114,15 @@ func (s *service) SetDetailsList(ctx session.Context, objectIds []string, detail
}

// ModifyDetails performs details get and update under the sb lock to make sure no modifications are done in the middle
func (s *service) ModifyDetails(objectId string, modifier func(current *domain.Details) (*domain.Details, error)) (err error) {
func (s *service) ModifyDetails(ctx session.Context, objectId string, modifier func(current *domain.Details) (*domain.Details, error)) (err error) {
return cache.Do(s.objectGetter, objectId, func(du basic.DetailsUpdatable) error {
return du.UpdateDetails(modifier)
return du.UpdateDetails(ctx, modifier)
})
}

func (s *service) ModifyDetailsAndUpdateLastUsed(objectId string, modifier func(current *domain.Details) (*domain.Details, error)) (err error) {
func (s *service) ModifyDetailsAndUpdateLastUsed(ctx session.Context, objectId string, modifier func(current *domain.Details) (*domain.Details, error)) (err error) {
return cache.Do(s.objectGetter, objectId, func(du basic.DetailsUpdatable) error {
return du.UpdateDetailsAndLastUsed(modifier)
return du.UpdateDetailsAndLastUsed(ctx, modifier)
})
}

Expand All @@ -133,7 +133,7 @@ func (s *service) ModifyDetailsList(req *pb.RpcObjectListModifyDetailValuesReque
if i == 0 {
modifyDetailsFunc = s.ModifyDetailsAndUpdateLastUsed
}
err := modifyDetailsFunc(objectId, func(current *domain.Details) (*domain.Details, error) {
err := modifyDetailsFunc(nil, objectId, func(current *domain.Details) (*domain.Details, error) {
for _, op := range req.Operations {
if !pbtypes.IsNullValue(op.Set) {
// Set operation has higher priority than Add and Remove, because it modifies full value
Expand Down
8 changes: 4 additions & 4 deletions core/block/editor/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ type DetailsSettable interface {
}

type DetailsUpdatable interface {
UpdateDetails(update func(current *domain.Details) (*domain.Details, error)) (err error)
UpdateDetailsAndLastUsed(update func(current *domain.Details) (*domain.Details, error)) (err error)
UpdateDetails(ctx session.Context, update func(current *domain.Details) (*domain.Details, error)) (err error)
UpdateDetailsAndLastUsed(ctx session.Context, update func(current *domain.Details) (*domain.Details, error)) (err error)
}

type Restrictionable interface {
Expand Down Expand Up @@ -119,8 +119,8 @@ func NewBasic(
type basic struct {
smartblock.SmartBlock

objectStore spaceindex.Store
layoutConverter converter.LayoutConverter
objectStore spaceindex.Store
layoutConverter converter.LayoutConverter
fileObjectService fileobject.Service
lastUsedUpdater lastused.ObjectUsageUpdater
}
Expand Down
12 changes: 6 additions & 6 deletions core/block/editor/basic/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func (bs *basic) setDetails(ctx session.Context, details []domain.Detail, showEv
return updatedKeys, nil
}

func (bs *basic) UpdateDetails(update func(current *domain.Details) (*domain.Details, error)) (err error) {
_, _, err = bs.updateDetails(update)
func (bs *basic) UpdateDetails(ctx session.Context, update func(current *domain.Details) (*domain.Details, error)) (err error) {
_, _, err = bs.updateDetails(ctx, update)
return err
}

func (bs *basic) UpdateDetailsAndLastUsed(update func(current *domain.Details) (*domain.Details, error)) error {
oldDetails, newDetails, err := bs.updateDetails(update)
func (bs *basic) UpdateDetailsAndLastUsed(ctx session.Context, update func(current *domain.Details) (*domain.Details, error)) error {
oldDetails, newDetails, err := bs.updateDetails(ctx, update)
if err != nil {
return err
}
Expand All @@ -84,11 +84,11 @@ func (bs *basic) UpdateDetailsAndLastUsed(update func(current *domain.Details) (
return nil
}

func (bs *basic) updateDetails(update func(current *domain.Details) (*domain.Details, error)) (oldDetails *domain.Details, newDetails *domain.Details, err error) {
func (bs *basic) updateDetails(ctx session.Context, update func(current *domain.Details) (*domain.Details, error)) (oldDetails *domain.Details, newDetails *domain.Details, err error) {
if update == nil {
return nil, nil, fmt.Errorf("update function is nil")
}
s := bs.NewState()
s := bs.NewStateCtx(ctx)

oldDetails = s.CombinedDetails()
oldDetailsCopy := oldDetails.Copy()
Expand Down
6 changes: 3 additions & 3 deletions core/block/editor/basic/details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestBasic_UpdateDetails(t *testing.T) {
}})

// when
err := f.basic.UpdateDetails(func(current *domain.Details) (*domain.Details, error) {
err := f.basic.UpdateDetails(nil, func(current *domain.Details) (*domain.Details, error) {
current.Set(bundle.RelationKeyAperture, domain.String("aperture"))
current.Set(bundle.RelationKeyRelationMaxCount, domain.Int64(5))
return current, nil
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestBasic_UpdateDetails(t *testing.T) {
}})

// when
err = f.basic.UpdateDetails(func(current *domain.Details) (*domain.Details, error) {
err = f.basic.UpdateDetails(nil, func(current *domain.Details) (*domain.Details, error) {
current.Set(bundle.RelationKeySpaceDashboardId, domain.String("new123"))
return current, nil
})
Expand All @@ -129,7 +129,7 @@ func TestBasic_UpdateDetails(t *testing.T) {
assert.NoError(t, err)

// when
err = f.basic.UpdateDetails(func(current *domain.Details) (*domain.Details, error) {
err = f.basic.UpdateDetails(nil, func(current *domain.Details) (*domain.Details, error) {
current.Delete(bundle.RelationKeyTargetObjectType)
return current, nil
})
Expand Down
2 changes: 1 addition & 1 deletion core/block/editor/participant.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (p *participant) TryClose(objectTTL time.Duration) (bool, error) {
}

func (p *participant) modifyDetails(newDetails *domain.Details) (err error) {
return p.DetailsUpdatable.UpdateDetails(func(current *domain.Details) (*domain.Details, error) {
return p.DetailsUpdatable.UpdateDetails(nil, func(current *domain.Details) (*domain.Details, error) {
return current.Merge(newDetails), nil
})
}
Expand Down
4 changes: 2 additions & 2 deletions core/block/editor/smartblock/smarttest/smarttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (st *SmartTest) SetDetailsAndUpdateLastUsed(ctx session.Context, details []
return st.SetDetails(ctx, details, showEvent)
}

func (st *SmartTest) UpdateDetails(update func(current *domain.Details) (*domain.Details, error)) (err error) {
func (st *SmartTest) UpdateDetails(ctx session.Context, update func(current *domain.Details) (*domain.Details, error)) (err error) {
details := st.Doc.(*state.State).CombinedDetails()
if details == nil {
details = domain.NewDetails()
Expand All @@ -282,7 +282,7 @@ func (st *SmartTest) UpdateDetails(update func(current *domain.Details) (*domain
return nil
}

func (st *SmartTest) UpdateDetailsAndLastUsed(update func(current *domain.Details) (*domain.Details, error)) (err error) {
func (st *SmartTest) UpdateDetailsAndLastUsed(ctx session.Context, update func(current *domain.Details) (*domain.Details, error)) (err error) {
details := st.Doc.(*state.State).CombinedDetails()
if details == nil {
details = domain.NewDetails()
Expand Down
35 changes: 34 additions & 1 deletion core/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"context"
"fmt"

"github.com/anyproto/anytype-heart/core/block/detailservice"
"github.com/anyproto/anytype-heart/core/domain"
Expand Down Expand Up @@ -60,7 +61,7 @@ func (mw *Middleware) ObjectSetInternalFlags(cctx context.Context, req *pb.RpcOb
return m
}
ds := mustService[detailservice.Service](mw)
err := ds.ModifyDetails(req.ContextId, func(current *domain.Details) (*domain.Details, error) {
err := ds.ModifyDetails(ctx, req.ContextId, func(current *domain.Details) (*domain.Details, error) {
d := current.Copy()
return internalflag.PutToDetails(d, req.InternalFlags), nil
})
Expand Down Expand Up @@ -165,3 +166,35 @@ func (mw *Middleware) ObjectListSetIsFavorite(_ context.Context, req *pb.RpcObje
}
return response(pb.RpcObjectListSetIsFavoriteResponseError_NULL, nil)
}

func (mw *Middleware) ObjectRelationAdd(cctx context.Context, req *pb.RpcObjectRelationAddRequest) *pb.RpcObjectRelationAddResponse {
ctx := mw.newContext(cctx)
if len(req.RelationKeys) == 0 {
return &pb.RpcObjectRelationAddResponse{Error: &pb.RpcObjectRelationAddResponseError{
Code: pb.RpcObjectRelationAddResponseError_BAD_INPUT,
Description: fmt.Errorf("relation keys list is empty").Error(),
}}
}

detailsService := mustService[detailservice.Service](mw)
err := detailsService.ModifyDetails(ctx, req.ContextId, func(current *domain.Details) (*domain.Details, error) {
for _, key := range req.RelationKeys {
if current.Has(domain.RelationKey(key)) {
continue
}
current.Set(domain.RelationKey(key), domain.Null())
}
return current, nil
})
if err != nil {
return &pb.RpcObjectRelationAddResponse{Error: &pb.RpcObjectRelationAddResponseError{
Code: pb.RpcObjectRelationAddResponseError_BAD_INPUT,
Description: getErrorDescription(err),
}}
}

return &pb.RpcObjectRelationAddResponse{
Error: &pb.RpcObjectRelationAddResponseError{},
Event: mw.getResponseEvent(ctx),
}
}
29 changes: 0 additions & 29 deletions core/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/samber/lo"

"github.com/anyproto/anytype-heart/core/block"
"github.com/anyproto/anytype-heart/core/block/detailservice"
importer "github.com/anyproto/anytype-heart/core/block/import"
"github.com/anyproto/anytype-heart/core/block/import/common"
"github.com/anyproto/anytype-heart/core/block/object/objectgraph"
Expand Down Expand Up @@ -408,34 +407,6 @@ func objectResponse(
return response
}

func (mw *Middleware) ObjectRelationAdd(_ context.Context, req *pb.RpcObjectRelationAddRequest) *pb.RpcObjectRelationAddResponse {
if len(req.RelationKeys) == 0 {
return &pb.RpcObjectRelationAddResponse{Error: &pb.RpcObjectRelationAddResponseError{
Code: pb.RpcObjectRelationAddResponseError_BAD_INPUT,
Description: fmt.Errorf("relation keys list is empty").Error(),
}}
}

detailsService := mustService[detailservice.Service](mw)
err := detailsService.ModifyDetails(req.ContextId, func(current *domain.Details) (*domain.Details, error) {
for _, key := range req.RelationKeys {
if current.Has(domain.RelationKey(key)) {
continue
}
current.Set(domain.RelationKey(key), domain.Null())
}
return current, nil
})
if err != nil {
return &pb.RpcObjectRelationAddResponse{Error: &pb.RpcObjectRelationAddResponseError{
Code: pb.RpcObjectRelationAddResponseError_BAD_INPUT,
Description: getErrorDescription(err),
}}
}

return &pb.RpcObjectRelationAddResponse{Error: &pb.RpcObjectRelationAddResponseError{}}
}

func (mw *Middleware) ObjectRelationDelete(cctx context.Context, req *pb.RpcObjectRelationDeleteRequest) *pb.RpcObjectRelationDeleteResponse {
ctx := mw.newContext(cctx)
response := func(code pb.RpcObjectRelationDeleteResponseErrorCode, err error) *pb.RpcObjectRelationDeleteResponse {
Expand Down
61 changes: 0 additions & 61 deletions space/techspace/mock_techspace/mock_AccountObject.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f24fcea

Please sign in to comment.