Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GO-3800: tag as object #1664

Open
wants to merge 52 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
c2adf08
GO-3942: add tag type
AnastasiaShemyakinskaya Aug 23, 2024
82c7e81
GO-3942: fix
AnastasiaShemyakinskaya Aug 23, 2024
9918986
GO-3942 Merge branch 'main' of github.com:anyproto/anytype-heart into…
AnastasiaShemyakinskaya Aug 23, 2024
6310c48
GO-3942: add support in import
AnastasiaShemyakinskaya Aug 23, 2024
f550bb6
GO-3942: support tag import
AnastasiaShemyakinskaya Aug 26, 2024
08039d8
GO-3942 Merge branch 'main' of github.com:anyproto/anytype-heart into…
AnastasiaShemyakinskaya Aug 26, 2024
8d81d6c
GO-3942: fix for pb import
AnastasiaShemyakinskaya Aug 26, 2024
bbd5175
GO-3942: fix relation of import collection
AnastasiaShemyakinskaya Aug 27, 2024
b32be06
GO-3974: support kanban
AnastasiaShemyakinskaya Sep 6, 2024
e51fa3e
GO-3968: support new tag objects in import
AnastasiaShemyakinskaya Sep 11, 2024
0a2ea0b
GO-3974: fix panic
AnastasiaShemyakinskaya Sep 12, 2024
306650a
GO-3974: add tests
AnastasiaShemyakinskaya Sep 13, 2024
d501d0a
GO-3942 Merge branch 'main' of github.com:anyproto/anytype-heart into…
AnastasiaShemyakinskaya Sep 16, 2024
74f1203
GO-3942: fix conflicts
AnastasiaShemyakinskaya Sep 16, 2024
b227b1a
GO-3974 Merge branch 'go-3942-add-layouts' of github.com:anyproto/any…
AnastasiaShemyakinskaya Sep 16, 2024
310a9bd
GO-3967: add migration
AnastasiaShemyakinskaya Sep 13, 2024
68c7140
GO-3967: fixes
AnastasiaShemyakinskaya Sep 13, 2024
1ef3972
GO-3967: add object type to state
AnastasiaShemyakinskaya Sep 13, 2024
b87d83a
GO-3967: move migration after installing of types
AnastasiaShemyakinskaya Sep 13, 2024
08d67e8
GO-3967: add tag migration
AnastasiaShemyakinskaya Sep 13, 2024
33470b4
GO-3967: fix
AnastasiaShemyakinskaya Sep 13, 2024
0dd4d7d
GO-3492: fix relation links
AnastasiaShemyakinskaya Sep 16, 2024
eba21db
GO-3974 Merge branch 'go-3942-add-layouts' of github.com:anyproto/any…
AnastasiaShemyakinskaya Sep 16, 2024
581fd6f
GO-3968: add layout
AnastasiaShemyakinskaya Sep 16, 2024
9a90ce5
GO-3968 Merge branch 'go-3942-add-layouts' of github.com:anyproto/any…
AnastasiaShemyakinskaya Sep 16, 2024
43213dc
GO-3967 Merge branch 'go-3974-support-kanban-view-based-on-relations-…
AnastasiaShemyakinskaya Sep 16, 2024
30eb88c
GO-3967: fix migration tag
AnastasiaShemyakinskaya Sep 17, 2024
844c062
GO-3967: refactoring
AnastasiaShemyakinskaya Sep 17, 2024
49593eb
GO-3967: add tests
AnastasiaShemyakinskaya Sep 17, 2024
28c1029
GO-3974: sort kanban groups by objects counts
AnastasiaShemyakinskaya Sep 17, 2024
708b65b
GO-3974: sort kanban groups by objects counts
AnastasiaShemyakinskaya Sep 18, 2024
d573175
GO-3942 Merge branch 'main' of github.com:anyproto/anytype-heart into…
AnastasiaShemyakinskaya Sep 18, 2024
89d43f7
GO-3942: merge main
AnastasiaShemyakinskaya Sep 18, 2024
099bfce
GO-3974: fix lint
AnastasiaShemyakinskaya Sep 18, 2024
69ce31c
GO-3967: fix lint
AnastasiaShemyakinskaya Sep 18, 2024
0018206
GO-3974: fix error
AnastasiaShemyakinskaya Sep 18, 2024
cc6f096
GO-3974 Merge branch 'go-3942-add-layouts' of github.com:anyproto/any…
AnastasiaShemyakinskaya Sep 18, 2024
a69a072
GO-3942 Merge branch 'go-3800-release-7-tag-as-object' of github.com:…
AnastasiaShemyakinskaya Oct 2, 2024
79ac452
GO-3942: fix conflicts
AnastasiaShemyakinskaya Oct 2, 2024
dde96f5
GO-3942: generate type
AnastasiaShemyakinskaya Oct 2, 2024
84a4190
Merge pull request #1511 from anyproto/go-3942-add-layouts
AnastasiaShemyakinskaya Oct 10, 2024
793360a
Merge pull request #1573 from anyproto/go-3968-support-import-from-no…
AnastasiaShemyakinskaya Oct 10, 2024
c65e349
Merge pull request #1578 from anyproto/go-3974-support-kanban-view-ba…
AnastasiaShemyakinskaya Oct 10, 2024
a786f93
Merge pull request #1588 from anyproto/go-3967-implement-migration-fr…
AnastasiaShemyakinskaya Oct 10, 2024
128f2d4
GO-3800 Merge branch 'go-3800-release-7-tag-as-object' of github.com:…
AnastasiaShemyakinskaya Oct 10, 2024
6764be6
Merge branch 'main' of github.com:anyproto/anytype-heart into go-3800…
AnastasiaShemyakinskaya Oct 14, 2024
9acb04f
GO-3800: refactoring
AnastasiaShemyakinskaya Oct 18, 2024
55403a6
GO-3800 Merge branch 'main' of github.com:anyproto/anytype-heart into…
AnastasiaShemyakinskaya Oct 22, 2024
a127d0a
GO-3800: merge main
AnastasiaShemyakinskaya Oct 22, 2024
9763185
GO-3800 Merge branch 'main' of github.com:anyproto/anytype-heart into…
AnastasiaShemyakinskaya Oct 29, 2024
825f9b2
GO-3800: add random color for tag objects
AnastasiaShemyakinskaya Oct 30, 2024
cd5e15d
GO-3800: use secure rand method
AnastasiaShemyakinskaya Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/block/editor/converter/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (c *layoutConverter) Convert(space smartblock.Space, st *state.State, fromL

// TODO We need more granular cases (not catch-all)

if toLayout == model.ObjectType_tag {
return c.fromAnyToTag(st)
}

return c.fromAnyToAny(st)
}

Expand Down Expand Up @@ -327,3 +331,12 @@ func (c *layoutConverter) appendTypesFilter(types []string, filters []*model.Blo
}
return filters
}

func (c *layoutConverter) fromAnyToTag(st *state.State) error {
template.InitTemplate(st,
template.WithTitle,
template.WithNoDescription,
template.WithRandomRelationOptionColor,
)
return nil
}
12 changes: 12 additions & 0 deletions core/block/editor/migrator/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package migrator

import (
"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/pkg/lib/logging"
)

var log = logging.Logger("migrator")

type Migrator interface {
Migrate(state *state.State)
}
27 changes: 27 additions & 0 deletions core/block/editor/migrator/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package migrator

import (
"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/pbtypes"
)

type MigrationTags struct{}

func NewMigrationTags() Migrator {
return &MigrationTags{}
}

func (m *MigrationTags) Migrate(s *state.State) {
if layout, _ := s.Layout(); layout == model.ObjectType_relationOption {
relationKey := pbtypes.GetString(s.CombinedDetails(), bundle.RelationKeyRelationKey.String())
if relationKey != bundle.RelationKeyTag.String() {
return
}
s.SetObjectTypeKey(bundle.TypeKeyTag)
s.RemoveRelation(bundle.RelationKeyRelationKey.String())
s.RemoveRelation(bundle.RelationKeyUniqueKey.String())
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Int64(int64(model.ObjectType_tag)))
}
}
57 changes: 57 additions & 0 deletions core/block/editor/migrator/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package migrator

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/pbtypes"
)

func TestMigrationTags_Migrate(t *testing.T) {
t.Run("not relation option", func(t *testing.T) {
// given
migrator := NewMigrationTags()
s := &state.State{}
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Int64(int64(model.ObjectType_basic)))

// when
migrator.Migrate(s)

// then
assert.Equal(t, int64(model.ObjectType_basic), pbtypes.GetInt64(s.Details(), bundle.RelationKeyLayout.String()))
})
t.Run("not tag relation option", func(t *testing.T) {
// given
migrator := NewMigrationTags()
s := &state.State{}
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Int64(int64(model.ObjectType_relationOption)))
s.SetDetail(bundle.RelationKeyRelationKey.String(), pbtypes.String("key"))

// when
migrator.Migrate(s)

// then
assert.Equal(t, int64(model.ObjectType_relationOption), pbtypes.GetInt64(s.Details(), bundle.RelationKeyLayout.String()))
assert.Equal(t, "key", pbtypes.GetString(s.Details(), bundle.RelationKeyRelationKey.String()))
})
t.Run("migrate relation option", func(t *testing.T) {
// given
migrator := NewMigrationTags()
s := &state.State{}
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Int64(int64(model.ObjectType_relationOption)))
s.SetDetail(bundle.RelationKeyRelationKey.String(), pbtypes.String(bundle.RelationKeyTag.String()))
s.SetDetail(bundle.RelationKeyUniqueKey.String(), pbtypes.String("key"))

// when
migrator.Migrate(s)

// then
assert.Equal(t, int64(model.ObjectType_tag), pbtypes.GetInt64(s.CombinedDetails(), bundle.RelationKeyLayout.String()))
assert.Empty(t, pbtypes.GetString(s.CombinedDetails(), bundle.RelationKeyUniqueKey.String()))
assert.Empty(t, pbtypes.GetString(s.CombinedDetails(), bundle.RelationKeyRelationKey.String()))
})
}
42 changes: 42 additions & 0 deletions core/block/editor/migrator/ralations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package migrator

import (
"context"

"github.com/anyproto/anytype-heart/core/block/editor/smartblock"
"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
coresb "github.com/anyproto/anytype-heart/pkg/lib/core/smartblock"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/pbtypes"
)

type MigrationRelations struct {
space smartblock.Space
}

func NewMigrationRelations(space smartblock.Space) Migrator {
return &MigrationRelations{space: space}
}

func (m *MigrationRelations) Migrate(s *state.State) {
if layout, _ := s.Layout(); layout == model.ObjectType_relation {
relationKey := pbtypes.GetString(s.CombinedDetails(), bundle.RelationKeyUniqueKey.String())
relationFormat := pbtypes.GetInt64(s.CombinedDetails(), bundle.RelationKeyRelationFormat.String())
if relationKey == bundle.RelationKeyTag.URL() && relationFormat == int64(model.RelationFormat_tag) {
m.migrateTag(s)
}
}
}

func (m *MigrationRelations) migrateTag(s *state.State) {
uniqueKey := domain.MustUniqueKey(coresb.SmartBlockTypeObjectType, bundle.TypeKeyTag.String())
tagTypeId, err := m.space.DeriveObjectID(context.Background(), uniqueKey)
if err != nil {
log.Errorf("failed to migrate state: %v", err)
} else {
s.SetDetail(bundle.RelationKeyRelationFormatObjectTypes.String(), pbtypes.StringList([]string{tagTypeId}))
}
s.SetDetail(bundle.RelationKeyRelationFormat.String(), pbtypes.Int64(int64(model.RelationFormat_object)))
}
98 changes: 98 additions & 0 deletions core/block/editor/migrator/relations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package migrator

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
coresb "github.com/anyproto/anytype-heart/pkg/lib/core/smartblock"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/space/clientspace/mock_clientspace"
"github.com/anyproto/anytype-heart/util/pbtypes"
)

func Test_migrateRelation(t *testing.T) {
t.Run("not relation", func(t *testing.T) {
// given
migrator := NewMigrationRelations(nil)
s := &state.State{}
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Int64(int64(model.ObjectType_basic)))

// when
migrator.Migrate(s)

// then
assert.Equal(t, int64(model.ObjectType_basic), pbtypes.GetInt64(s.Details(), bundle.RelationKeyLayout.String()))
})
t.Run("not tag relation", func(t *testing.T) {
// given
migrator := NewMigrationRelations(nil)
s := &state.State{}
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Int64(int64(model.ObjectType_relation)))
s.SetDetail(bundle.RelationKeyUniqueKey.String(), pbtypes.String("key"))

// when
migrator.Migrate(s)

// then
assert.Equal(t, int64(model.ObjectType_relation), pbtypes.GetInt64(s.CombinedDetails(), bundle.RelationKeyLayout.String()))
assert.Equal(t, "key", pbtypes.GetString(s.CombinedDetails(), bundle.RelationKeyUniqueKey.String()))
})
t.Run("not tag format relation", func(t *testing.T) {
// given
migrator := NewMigrationRelations(nil)
s := &state.State{}
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Int64(int64(model.ObjectType_relation)))
s.SetDetail(bundle.RelationKeyUniqueKey.String(), pbtypes.String(bundle.RelationKeyTag.URL()))
s.SetDetail(bundle.RelationKeyRelationFormat.String(), pbtypes.Int64(int64(model.RelationFormat_object)))

// when
migrator.Migrate(s)

// then
assert.Equal(t, int64(model.ObjectType_relation), pbtypes.GetInt64(s.CombinedDetails(), bundle.RelationKeyLayout.String()))
assert.Equal(t, bundle.RelationKeyTag.URL(), pbtypes.GetString(s.CombinedDetails(), bundle.RelationKeyUniqueKey.String()))
assert.Equal(t, int64(model.RelationFormat_object), pbtypes.GetInt64(s.CombinedDetails(), bundle.RelationKeyRelationFormat.String()))
})
t.Run("migrate relation", func(t *testing.T) {
// given
space := mock_clientspace.NewMockSpace(t)
space.EXPECT().DeriveObjectID(context.Background(), domain.MustUniqueKey(coresb.SmartBlockTypeObjectType, bundle.TypeKeyTag.String())).Return("id", nil)
migrator := NewMigrationRelations(space)
s := &state.State{}
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Int64(int64(model.ObjectType_relation)))
s.SetDetail(bundle.RelationKeyUniqueKey.String(), pbtypes.String(bundle.RelationKeyTag.URL()))
s.SetDetail(bundle.RelationKeyRelationFormat.String(), pbtypes.Int64(int64(model.RelationFormat_tag)))

// when
migrator.Migrate(s)

// then
assert.Equal(t, int64(model.RelationFormat_object), pbtypes.GetInt64(s.CombinedDetails(), bundle.RelationKeyRelationFormat.String()))
assert.Equal(t, []string{"id"}, pbtypes.GetStringList(s.CombinedDetails(), bundle.RelationKeyRelationFormatObjectTypes.String()))
})
t.Run("type tage not exist", func(t *testing.T) {
// given
space := mock_clientspace.NewMockSpace(t)
space.EXPECT().DeriveObjectID(context.Background(), domain.MustUniqueKey(coresb.SmartBlockTypeObjectType, bundle.TypeKeyTag.String())).Return("", fmt.Errorf("error"))

migrator := NewMigrationRelations(space)

s := &state.State{}
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Int64(int64(model.ObjectType_relation)))
s.SetDetail(bundle.RelationKeyUniqueKey.String(), pbtypes.String(bundle.RelationKeyTag.URL()))
s.SetDetail(bundle.RelationKeyRelationFormat.String(), pbtypes.Int64(int64(model.RelationFormat_tag)))

// when
migrator.Migrate(s)

// then
assert.Equal(t, int64(model.RelationFormat_object), pbtypes.GetInt64(s.CombinedDetails(), bundle.RelationKeyRelationFormat.String()))
assert.Len(t, pbtypes.GetStringList(s.CombinedDetails(), bundle.RelationKeyRelationFormatObjectTypes.String()), 0)
})
}
18 changes: 16 additions & 2 deletions core/block/editor/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/anyproto/anytype-heart/core/block/editor/clipboard"
"github.com/anyproto/anytype-heart/core/block/editor/dataview"
"github.com/anyproto/anytype-heart/core/block/editor/file"
"github.com/anyproto/anytype-heart/core/block/editor/migrator"
"github.com/anyproto/anytype-heart/core/block/editor/smartblock"
"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/core/block/editor/stext"
Expand Down Expand Up @@ -220,11 +221,12 @@ func (p *Page) CreationStateMigration(ctx *smartblock.InitContext) migration.Mig
template.WithTitle,
template.WithBlockChat,
)
// TODO case for relationOption?
case model.ObjectType_tag:
templates = append(templates,
template.WithTitle,
template.WithNoDescription)
template.WithNoDescription,
template.WithRandomRelationOptionColor,
)
default:
templates = append(templates,
template.WithTitle,
Expand All @@ -242,5 +244,17 @@ func (p *Page) StateMigrations() migration.Migrations {
Version: 2,
Proc: template.WithAddedFeaturedRelation(bundle.RelationKeyBacklinks),
},
{
Version: 3,
Proc: func(s *state.State) {
p.runStateMigrations(s)
},
},
})
}

func (p *Page) runStateMigrations(s *state.State) {
for _, m := range []migrator.Migrator{migrator.NewMigrationTags(), migrator.NewMigrationRelations(p.Space())} {
m.Migrate(s)
}
}
14 changes: 14 additions & 0 deletions core/block/editor/template/template.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package template

import (
"crypto/rand"
"math/big"

"github.com/gogo/protobuf/types"
"golang.org/x/exp/slices"

Expand Down Expand Up @@ -29,6 +32,8 @@ const (
ChatId = "chat"
)

var relationOptionsColors = []string{"grey", "yellow", "orange", "red", "pink", "purple", "blue", "ice", "teal", "lime"}

var log = logging.Logger("anytype-state-template")

type StateTransformer func(s *state.State)
Expand Down Expand Up @@ -86,6 +91,15 @@ var WithRelations = func(rels []domain.RelationKey) StateTransformer {
}
}

var WithRandomRelationOptionColor = StateTransformer(func(s *state.State) {
randomIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(relationOptionsColors))))
if err != nil {
return
}
s.SetDetail(bundle.RelationKeyRelationOptionColor.String(), pbtypes.String(relationOptionsColors[randomIndex.Int64()]))
s.AddBundledRelationLinks([]domain.RelationKey{bundle.RelationKeyRelationOptionColor}...)
})

var WithRequiredRelations = func(s *state.State) {
WithRelations(bundle.RequiredInternalRelations)(s)
}
Expand Down
2 changes: 1 addition & 1 deletion core/block/import/common/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (r *ImportCollection) addRelations(st *state.State) error {
for _, relation := range []*model.RelationLink{
{
Key: bundle.RelationKeyTag.String(),
Format: model.RelationFormat_tag,
Format: model.RelationFormat_object,
},
{
Key: bundle.RelationKeyCreatedDate.String(),
Expand Down
Loading
Loading