From 0733de1c542ea558ba8bd390eabf0d6fba00847c Mon Sep 17 00:00:00 2001 From: kirillston Date: Fri, 6 Jun 2025 15:58:54 +0200 Subject: [PATCH 1/4] Restrict template objects creation --- pkg/lib/bundle/types.gen.go | 25 +++++++++++++------------ pkg/lib/bundle/types.json | 1 + 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pkg/lib/bundle/types.gen.go b/pkg/lib/bundle/types.gen.go index eb50f06d7..5724649a2 100644 --- a/pkg/lib/bundle/types.gen.go +++ b/pkg/lib/bundle/types.gen.go @@ -9,7 +9,7 @@ import ( "github.com/anyproto/anytype-heart/pkg/lib/pb/model" ) -const TypeChecksum = "880c466315e22d0b573f6542b4ca1dcc4e2cb3395de3bad901f87460ff914191" +const TypeChecksum = "4887c4b1b59f6fa595cc67185d19aec9212680c2316aa452ec39b66d5eea1b83" const ( TypePrefix = "_ot" ) @@ -435,17 +435,18 @@ var ( }, TypeKeyTemplate: { - Description: "", - IconColor: 8, - IconName: "copy", - Layout: model.ObjectType_basic, - Name: "Template", - PluralName: "Templates", - Readonly: true, - RelationLinks: []*model.RelationLink{MustGetRelationLink(RelationKeyTargetObjectType), MustGetRelationLink(RelationKeyTemplateIsBundled)}, - Revision: 4, - Types: []model.SmartBlockType{model.SmartBlockType_Template}, - Url: TypePrefix + "template", + Description: "", + IconColor: 8, + IconName: "copy", + Layout: model.ObjectType_basic, + Name: "Template", + PluralName: "Templates", + Readonly: true, + RelationLinks: []*model.RelationLink{MustGetRelationLink(RelationKeyTargetObjectType), MustGetRelationLink(RelationKeyTemplateIsBundled)}, + RestrictObjectCreation: true, + Revision: 4, + Types: []model.SmartBlockType{model.SmartBlockType_Template}, + Url: TypePrefix + "template", }, TypeKeyVideo: { diff --git a/pkg/lib/bundle/types.json b/pkg/lib/bundle/types.json index 33f6a7df6..ed573dafa 100644 --- a/pkg/lib/bundle/types.json +++ b/pkg/lib/bundle/types.json @@ -298,6 +298,7 @@ "targetObjectType", "templateIsBundled" ], + "restrictObjectCreation": true, "revision": 4 }, { From 80a0f213ff733dd4660251afe14e79a6fb76d551 Mon Sep 17 00:00:00 2001 From: kirillston Date: Fri, 6 Jun 2025 16:39:02 +0200 Subject: [PATCH 2/4] Allow template creation via ObjectCreate --- core/block/object/objectcreator/creator.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/block/object/objectcreator/creator.go b/core/block/object/objectcreator/creator.go index eeb553acc..495fe0473 100644 --- a/core/block/object/objectcreator/creator.go +++ b/core/block/object/objectcreator/creator.go @@ -116,10 +116,8 @@ func (s *service) createObjectInSpace( } details = internalflag.PutToDetails(details, req.InternalFlags) - if bundle.HasObjectTypeByKey(req.ObjectTypeKey) { - if t := bundle.MustGetType(req.ObjectTypeKey); t.RestrictObjectCreation { - return "", nil, errors.Wrap(restriction.ErrRestricted, "creation of this object type is restricted") - } + if t, _ := bundle.GetType(req.ObjectTypeKey); t != nil && t.RestrictObjectCreation && req.ObjectTypeKey != bundle.TypeKeyTemplate { + return "", nil, errors.Wrap(restriction.ErrRestricted, "creation of this object type is restricted") } switch req.ObjectTypeKey { From 8cc1320f3ee56acc7eea1b27d2dde87d434c515e Mon Sep 17 00:00:00 2001 From: kirillston Date: Fri, 6 Jun 2025 19:02:06 +0200 Subject: [PATCH 3/4] Fix concurrent tests --- core/history/history_test.go | 11 +++++------ core/payments/emailcollector/emailcollector_test.go | 7 ++++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/history/history_test.go b/core/history/history_test.go index c1ce7d73e..470c95114 100644 --- a/core/history/history_test.go +++ b/core/history/history_test.go @@ -1134,14 +1134,13 @@ func TestHistory_Show(t *testing.T) { Model: &pb.Change{}, } - heads := []string{"id", "id1"} changesMap := map[string][]*objecttree.Change{ - "id1 id": {root, ch1}, - "id id1": {root, ch}, + "id id1": {root, ch1}, + "id1 id": {root, ch}, objectId: {root, ch, ch1}, } - h := newFixtureShow(t, changesMap, heads, objectId, spaceID) + h := newFixtureShow(t, changesMap, objectId, spaceID) // when fullId := domain.FullID{ObjectID: objectId, SpaceID: spaceID} @@ -1214,7 +1213,7 @@ func newFixtureDiffVersions(t *testing.T, } } -func newFixtureShow(t *testing.T, changes map[string][]*objecttree.Change, heads []string, objectId, spaceID string) *historyFixture { +func newFixtureShow(t *testing.T, changes map[string][]*objecttree.Change, objectId, spaceID string) *historyFixture { spaceService := mock_space.NewMockService(t) space := mock_clientspace.NewMockSpace(t) ctrl := gomock.NewController(t) @@ -1232,7 +1231,7 @@ func newFixtureShow(t *testing.T, changes map[string][]*objecttree.Change, heads return &historyStub{ objectId: objectId, changes: chs, - heads: heads, + heads: opts.Heads, }, nil }).AnyTimes() space.EXPECT().TreeBuilder().Return(treeBuilder) diff --git a/core/payments/emailcollector/emailcollector_test.go b/core/payments/emailcollector/emailcollector_test.go index e1726cffc..8c18b73a5 100644 --- a/core/payments/emailcollector/emailcollector_test.go +++ b/core/payments/emailcollector/emailcollector_test.go @@ -1,6 +1,7 @@ package emailcollector import ( + "sync/atomic" "testing" "github.com/stretchr/testify/assert" @@ -188,10 +189,10 @@ func TestEmailCollector(t *testing.T) { t.Run("should handle concurrent requests", func(t *testing.T) { // Given - var callCount int + var callCount atomic.Uint32 collector := &mockEmailCollector{ onSetRequest: func(req *pb.RpcMembershipGetVerificationEmailRequest) error { - callCount++ + callCount.Add(1) assert.Equal(t, "test@example.com", req.Email) return nil }, @@ -214,6 +215,6 @@ func TestEmailCollector(t *testing.T) { for i := 0; i < 10; i++ { <-done } - assert.Equal(t, 10, callCount) + assert.Equal(t, uint32(10), callCount.Load()) }) } From 0a4097290ada013292e337ac23580c2ee3f1d06d Mon Sep 17 00:00:00 2001 From: kirillston Date: Fri, 6 Jun 2025 19:35:57 +0200 Subject: [PATCH 4/4] Fix lint --- core/block/object/objectcreator/creator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/block/object/objectcreator/creator.go b/core/block/object/objectcreator/creator.go index 495fe0473..b485d4b57 100644 --- a/core/block/object/objectcreator/creator.go +++ b/core/block/object/objectcreator/creator.go @@ -116,7 +116,7 @@ func (s *service) createObjectInSpace( } details = internalflag.PutToDetails(details, req.InternalFlags) - if t, _ := bundle.GetType(req.ObjectTypeKey); t != nil && t.RestrictObjectCreation && req.ObjectTypeKey != bundle.TypeKeyTemplate { + if t, e := bundle.GetType(req.ObjectTypeKey); e == nil && t.RestrictObjectCreation && req.ObjectTypeKey != bundle.TypeKeyTemplate { return "", nil, errors.Wrap(restriction.ErrRestricted, "creation of this object type is restricted") }