Skip to content

Commit

Permalink
GO-4459 Merge branch 'main' of ssh://github.com/anyproto/anytype-hear…
Browse files Browse the repository at this point in the history
…t into go-4459-implement-first-stage-of-anytype-rest-api

# Conflicts:
#	pb/commands.pb.go
  • Loading branch information
requilence committed Jan 22, 2025
2 parents 8e14863 + 1d3ee52 commit 3806f9b
Show file tree
Hide file tree
Showing 16 changed files with 1,449 additions and 1,310 deletions.
5 changes: 1 addition & 4 deletions core/block/editor/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ type State struct {

groupId string
noObjectType bool
originalCreatedTimestamp int64 // pass here from snapshots when importing objects
originalCreatedTimestamp int64 // pass here from snapshots when importing objects or used for derived objects such as relations, types and etc
}

func (s *State) MigrationVersion() uint32 {
Expand Down Expand Up @@ -1701,9 +1701,6 @@ func (s *State) AddRelationLinks(links ...*model.RelationLink) {
for _, l := range links {
if !relLinks.Has(l.Key) {
relLinks = append(relLinks, l)
if l.Format == model.RelationFormat_checkbox {
s.SetDetail(domain.RelationKey(l.Key), domain.Bool(false))
}
}
}
s.relationLinks = relLinks
Expand Down
39 changes: 0 additions & 39 deletions core/block/editor/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2976,43 +2976,4 @@ func TestState_AddRelationLinks(t *testing.T) {
assert.True(t, s.GetRelationLinks().Has("existingLink"))
assert.Len(t, s.GetRelationLinks(), 1)
})
t.Run("add checkbox link", func(t *testing.T) {
// given
s := &State{}
checkboxLink := &model.RelationLink{
Key: "checkboxLink",
Format: model.RelationFormat_checkbox,
}

// when
s.AddRelationLinks(checkboxLink)

// then
relLinks := s.GetRelationLinks()
assert.Equal(t, 1, len(relLinks))
assert.True(t, relLinks.Has("checkboxLink"))
detailValue := s.Details().Get("checkboxLink")
assert.Equal(t, domain.Bool(false), detailValue)
})
t.Run("multi links", func(t *testing.T) {
// given
s := &State{}
link1 := &model.RelationLink{
Key: "link1",
Format: model.RelationFormat_shorttext,
}
link2 := &model.RelationLink{
Key: "link2",
Format: model.RelationFormat_checkbox,
}

// when
s.AddRelationLinks(link1, link2)

// then
relLinks := s.GetRelationLinks()
assert.Equal(t, 2, len(relLinks))
assert.True(t, relLinks.Has("link1"))
assert.True(t, relLinks.Has("link2"))
})
}
6 changes: 6 additions & 0 deletions core/block/object/objectcreator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,9 @@ func buildDateObject(space clientspace.Space, details *domain.Details) (string,
details, err = detailsGetter.DetailsFromId()
return dateObject.Id(), details, err
}

func setOriginalCreatedTimestamp(state *state.State, details *domain.Details) {
if createDate := details.GetInt64(bundle.RelationKeyCreatedDate); createDate != 0 {
state.SetOriginalCreatedTimestamp(createDate)
}
}
6 changes: 3 additions & 3 deletions core/block/object/objectcreator/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"strings"
"time"

"github.com/anyproto/any-sync/commonspace/object/tree/treestorage"
"go.uber.org/zap"
Expand Down Expand Up @@ -213,6 +214,7 @@ func (s *service) reinstallObject(
st.SetDetails(installingDetails)
st.SetDetailAndBundledRelation(bundle.RelationKeyIsUninstalled, domain.Bool(false))
st.SetDetailAndBundledRelation(bundle.RelationKeyIsDeleted, domain.Bool(false))
st.SetOriginalCreatedTimestamp(time.Now().Unix())

key = domain.TypeKey(st.UniqueKeyInternal())
details = st.CombinedDetails()
Expand Down Expand Up @@ -253,9 +255,7 @@ func (s *service) prepareDetailsForInstallingObject(
details.SetString(bundle.RelationKeySpaceId, spaceID)
details.SetString(bundle.RelationKeySourceObject, sourceId)
details.SetBool(bundle.RelationKeyIsReadonly, false)

// we should delete old createdDate as it belongs to source object from marketplace
details.Delete(bundle.RelationKeyCreatedDate)
details.SetInt64(bundle.RelationKeyCreatedDate, time.Now().Unix())

if isNewSpace {
lastused.SetLastUsedDateForInitialObjectType(sourceId, details)
Expand Down
5 changes: 5 additions & 0 deletions core/block/object/objectcreator/object_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package objectcreator
import (
"context"
"fmt"
"time"

"golang.org/x/exp/slices"

Expand Down Expand Up @@ -35,12 +36,16 @@ func (s *service) createObjectType(ctx context.Context, space clientspace.Space,
return "", nil, fmt.Errorf("fill recommended relations: %w", err)
}
}
if !object.Has(bundle.RelationKeyCreatedDate) {
object.SetInt64(bundle.RelationKeyCreatedDate, time.Now().Unix())
}

object.SetString(bundle.RelationKeyId, id)
object.SetInt64(bundle.RelationKeyLayout, int64(model.ObjectType_objectType))

createState := state.NewDocWithUniqueKey("", nil, uniqueKey).(*state.State)
createState.SetDetails(object)
setOriginalCreatedTimestamp(createState, details)
id, newDetails, err = s.CreateSmartBlockFromStateInSpace(ctx, space, []domain.TypeKey{bundle.TypeKeyObjectType}, createState)
if err != nil {
return "", nil, fmt.Errorf("create smartblock from state: %w", err)
Expand Down
5 changes: 5 additions & 0 deletions core/block/object/objectcreator/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package objectcreator
import (
"context"
"fmt"
"time"

"github.com/globalsign/mgo/bson"

Expand Down Expand Up @@ -31,6 +32,9 @@ func (s *service) createRelation(ctx context.Context, space clientspace.Space, d
if details.GetString(bundle.RelationKeyName) == "" {
return "", nil, fmt.Errorf("missing relation name")
}
if !details.Has(bundle.RelationKeyCreatedDate) {
details.SetInt64(bundle.RelationKeyCreatedDate, time.Now().Unix())
}

object = details.Copy()
key := domain.RelationKey(details.GetString(bundle.RelationKeyRelationKey))
Expand All @@ -55,5 +59,6 @@ func (s *service) createRelation(ctx context.Context, space clientspace.Space, d

createState := state.NewDocWithUniqueKey("", nil, uniqueKey).(*state.State)
createState.SetDetails(object)
setOriginalCreatedTimestamp(createState, details)
return s.CreateSmartBlockFromStateInSpace(ctx, space, []domain.TypeKey{bundle.TypeKeyRelation}, createState)
}
13 changes: 11 additions & 2 deletions core/block/object/objectcreator/relation_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package objectcreator
import (
"context"
"fmt"
"time"

"github.com/globalsign/mgo/bson"

Expand All @@ -25,7 +26,9 @@ func (s *service) createRelationOption(ctx context.Context, space clientspace.Sp
if details.GetString(bundle.RelationKeyRelationKey) == "" {
return "", nil, fmt.Errorf("relation key is empty")
}

if !details.Has(bundle.RelationKeyCreatedDate) {
details.SetInt64(bundle.RelationKeyCreatedDate, time.Now().Unix())
}
uniqueKey, err := getUniqueKeyOrGenerate(coresb.SmartBlockTypeRelationOption, details)
if err != nil {
return "", nil, fmt.Errorf("getUniqueKeyOrGenerate: %w", err)
Expand All @@ -37,13 +40,19 @@ func (s *service) createRelationOption(ctx context.Context, space clientspace.Sp

createState := state.NewDocWithUniqueKey("", nil, uniqueKey).(*state.State)
createState.SetDetails(object)
setOriginalCreatedTimestamp(createState, details)
return s.CreateSmartBlockFromStateInSpace(ctx, space, []domain.TypeKey{bundle.TypeKeyRelationOption}, createState)
}

func getUniqueKeyOrGenerate(sbType coresb.SmartBlockType, details *domain.Details) (domain.UniqueKey, error) {
uniqueKey := details.GetString(bundle.RelationKeyUniqueKey)
if uniqueKey == "" {
return domain.NewUniqueKey(sbType, bson.NewObjectId().Hex())
newUniqueKey, err := domain.NewUniqueKey(sbType, bson.NewObjectId().Hex())
if err != nil {
return nil, err
}
details.SetString(bundle.RelationKeyUniqueKey, newUniqueKey.Marshal())
return newUniqueKey, err
}
return domain.UnmarshalUniqueKey(uniqueKey)
}
28 changes: 27 additions & 1 deletion core/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/anyproto/anytype-heart/core/block/detailservice"
"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/localstore/objectstore"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/internalflag"
)

Expand Down Expand Up @@ -177,12 +180,22 @@ func (mw *Middleware) ObjectRelationAdd(cctx context.Context, req *pb.RpcObjectR
}

detailsService := mustService[detailservice.Service](mw)
objectStore := mustService[objectstore.ObjectStore](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())
format, err := mw.extractRelationFormat(current, objectStore, key)
if err != nil {
log.Errorf("failed to fetch relation from store to get format %s, falling back to basic", err)
}
switch format {
case model.RelationFormat_checkbox:
current.Set(domain.RelationKey(key), domain.Bool(false))
default:
current.Set(domain.RelationKey(key), domain.Null())
}
}
return current, nil
})
Expand All @@ -198,3 +211,16 @@ func (mw *Middleware) ObjectRelationAdd(cctx context.Context, req *pb.RpcObjectR
Event: mw.getResponseEvent(ctx),
}
}

func (mw *Middleware) extractRelationFormat(current *domain.Details, objectStore objectstore.ObjectStore, key string) (model.RelationFormat, error) {
spaceId := current.GetString(bundle.RelationKeySpaceId)
relation, err := objectStore.SpaceIndex(spaceId).FetchRelationByKeys(domain.RelationKey(key))
if err != nil {
return model.RelationFormat_longtext, err
}
var format model.RelationFormat
if len(relation) != 0 {
format = relation[0].Format
}
return format, nil
}
26 changes: 21 additions & 5 deletions core/publish/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ type PublishingUberSnapshotMeta struct {
InviteLink string `json:"inviteLink,omitempty"`
}

type Heads struct {
Heads []string `json:"heads"`
type Version struct {
Heads []string `json:"heads"`
JoinSpace bool `json:"joinSpace"`
}

// Contains all publishing .pb files
Expand Down Expand Up @@ -177,7 +178,7 @@ func (s *service) publishToPublishServer(ctx context.Context, spaceId, pageId, u
return err
}

version, err := s.evaluateDocumentVersion(spc, pageId)
version, err := s.evaluateDocumentVersion(spc, pageId, joinSpace)
if err != nil {
return err
}
Expand Down Expand Up @@ -356,7 +357,7 @@ func (s *service) extractInviteLink(ctx context.Context, spaceId string, joinSpa
return inviteLink, nil
}

func (s *service) evaluateDocumentVersion(spc clientspace.Space, pageId string) (string, error) {
func (s *service) evaluateDocumentVersion(spc clientspace.Space, pageId string, joinSpace bool) (string, error) {
treeStorage, err := spc.Storage().TreeStorage(pageId)
if err != nil {
return "", err
Expand All @@ -366,7 +367,7 @@ func (s *service) evaluateDocumentVersion(spc clientspace.Space, pageId string)
return "", err
}
slices.Sort(heads)
h := &Heads{Heads: heads}
h := &Version{Heads: heads, JoinSpace: joinSpace}
jsonData, err := json.Marshal(h)
if err != nil {
return "", err
Expand Down Expand Up @@ -423,6 +424,7 @@ func (s *service) PublishList(ctx context.Context, spaceId string) ([]*pb.RpcPub
}
pbPublishes := make([]*pb.RpcPublishingPublishState, 0, len(publishes))
for _, publish := range publishes {
version := s.retrieveVersion(publish)
pbPublishes = append(pbPublishes, &pb.RpcPublishingPublishState{
SpaceId: publish.SpaceId,
ObjectId: publish.ObjectId,
Expand All @@ -431,16 +433,27 @@ func (s *service) PublishList(ctx context.Context, spaceId string) ([]*pb.RpcPub
Version: publish.Version,
Timestamp: publish.Timestamp,
Size_: publish.Size_,
JoinSpace: version.JoinSpace,
})
}
return pbPublishes, nil
}

func (s *service) retrieveVersion(publish *publishapi.Publish) *Version {
version := &Version{}
err := json.Unmarshal([]byte(publish.Version), version)
if err != nil {
log.Error("failed to unmarshal publish version", zap.Error(err))
}
return version
}

func (s *service) ResolveUri(ctx context.Context, uri string) (*pb.RpcPublishingPublishState, error) {
publish, err := s.publishClientService.ResolveUri(ctx, uri)
if err != nil {
return nil, err
}
version := s.retrieveVersion(publish)
return &pb.RpcPublishingPublishState{
SpaceId: publish.SpaceId,
ObjectId: publish.ObjectId,
Expand All @@ -449,6 +462,7 @@ func (s *service) ResolveUri(ctx context.Context, uri string) (*pb.RpcPublishing
Version: publish.Version,
Timestamp: publish.Timestamp,
Size_: publish.Size_,
JoinSpace: version.JoinSpace,
}, nil
}

Expand All @@ -457,6 +471,7 @@ func (s *service) GetStatus(ctx context.Context, spaceId string, objectId string
if err != nil {
return nil, err
}
version := s.retrieveVersion(status)
return &pb.RpcPublishingPublishState{
SpaceId: status.SpaceId,
ObjectId: status.ObjectId,
Expand All @@ -465,5 +480,6 @@ func (s *service) GetStatus(ctx context.Context, spaceId string, objectId string
Version: status.Version,
Timestamp: status.Timestamp,
Size_: status.Size_,
JoinSpace: version.JoinSpace,
}, nil
}
Loading

0 comments on commit 3806f9b

Please sign in to comment.