From fff03a2685590c3e3ac534b5f94c4cf9fc33cdef Mon Sep 17 00:00:00 2001 From: Sergey Date: Fri, 13 Dec 2024 12:16:07 +0100 Subject: [PATCH] GO-4702: Fix using spaceId in state --- core/block/editor/clipboard/clipboard_test.go | 8 +- core/block/editor/smartblock/smartblock.go | 6 +- .../editor/smartblock/smarttest/smarttest.go | 4 +- core/block/editor/state/change_test.go | 86 +++++++++---------- core/block/editor/state/normalize_test.go | 26 +++--- core/block/editor/state/position_test.go | 26 +++--- core/block/editor/state/state.go | 29 +++---- core/block/editor/state/state_test.go | 10 +-- core/block/source/source.go | 4 +- core/debug/treearchive/treeimporter.go | 2 +- core/history/history.go | 4 +- tests/testutil/state.go | 2 +- 12 files changed, 102 insertions(+), 105 deletions(-) diff --git a/core/block/editor/clipboard/clipboard_test.go b/core/block/editor/clipboard/clipboard_test.go index dcdca3a0a..a21f8aaf2 100644 --- a/core/block/editor/clipboard/clipboard_test.go +++ b/core/block/editor/clipboard/clipboard_test.go @@ -547,7 +547,7 @@ func TestClipboard_TitleOps(t *testing.T) { s.Add(tb) s.InsertTo("", 0, tb.Model().Id) } - _, _, err := state.ApplyState(s, false) + _, _, err := state.ApplyState("", s, false) require.NoError(t, err) return sb } @@ -572,7 +572,7 @@ func TestClipboard_TitleOps(t *testing.T) { bm.Model().Id = "bookmarkId" s.Add(bm) s.InsertTo("", 0, bm.Model().Id) - _, _, err := state.ApplyState(s, false) + _, _, err := state.ApplyState("", s, false) require.NoError(t, err) return sb } @@ -1103,7 +1103,7 @@ func addDescription(st *smarttest.SmartTest, description string) { newState := st.Doc.NewState() template.InitTemplate(newState, template.WithForcedDescription) newState.Get(template.DescriptionBlockId).(text.Block).SetText(description, nil) - state.ApplyState(newState, false) + state.ApplyState("", newState, false) } func addRelations(st *smarttest.SmartTest) { @@ -1111,7 +1111,7 @@ func addRelations(st *smarttest.SmartTest) { template.InitTemplate(newState, template.RequireHeader) template.InitTemplate(newState, template.WithFeaturedRelations) template.InitTemplate(newState, template.WithForcedDescription) - state.ApplyState(newState, false) + state.ApplyState("", newState, false) } func TestClipboard_PasteToCodeBlock(t *testing.T) { diff --git a/core/block/editor/smartblock/smartblock.go b/core/block/editor/smartblock/smartblock.go index 9f40e4bdb..71aa78177 100644 --- a/core/block/editor/smartblock/smartblock.go +++ b/core/block/editor/smartblock/smartblock.go @@ -709,7 +709,7 @@ func (sb *smartBlock) Apply(s *state.State, flags ...ApplyFlag) (err error) { migrationVersionUpdated = s.MigrationVersion() != parent.MigrationVersion() } - msgs, act, err := state.ApplyState(s, !sb.disableLayouts) + msgs, act, err := state.ApplyState(sb.SpaceID(), s, !sb.disableLayouts) if err != nil { return } @@ -1071,7 +1071,7 @@ func (sb *smartBlock) StateAppend(f func(d state.Doc) (s *state.State, changes [ sb.updateRestrictions() sb.injectDerivedDetails(s, sb.SpaceID(), sb.Type()) sb.execHooks(HookBeforeApply, ApplyInfo{State: s}) - msgs, act, err := state.ApplyState(s, !sb.disableLayouts) + msgs, act, err := state.ApplyState(sb.SpaceID(), s, !sb.disableLayouts) if err != nil { return err } @@ -1106,7 +1106,7 @@ func (sb *smartBlock) StateRebuild(d state.Doc) (err error) { d.(*state.State).SetParent(sb.Doc.(*state.State)) // todo: make store diff sb.execHooks(HookBeforeApply, ApplyInfo{State: d.(*state.State)}) - msgs, _, err := state.ApplyState(d.(*state.State), !sb.disableLayouts) + msgs, _, err := state.ApplyState(sb.SpaceID(), d.(*state.State), !sb.disableLayouts) log.Infof("changes: stateRebuild: %d events", len(msgs)) if err != nil { // can't make diff - reopen doc diff --git a/core/block/editor/smartblock/smarttest/smarttest.go b/core/block/editor/smartblock/smarttest/smarttest.go index 1d67ee06e..0bffc1e0a 100644 --- a/core/block/editor/smartblock/smarttest/smarttest.go +++ b/core/block/editor/smartblock/smarttest/smarttest.go @@ -363,7 +363,7 @@ func (st *SmartTest) Apply(s *state.State, flags ...smartblock.ApplyFlag) (err e s.RemoveDetail(bundle.RelationKeyInternalFlags.String()) } - msgs, act, err := state.ApplyState(s, true) + msgs, act, err := state.ApplyState(st.SpaceID(), s, true) if err != nil { return } @@ -392,7 +392,7 @@ func (st *SmartTest) History() undo.History { func (st *SmartTest) StateRebuild(d state.Doc) (err error) { d.(*state.State).SetParent(st.Doc.(*state.State)) - _, _, err = state.ApplyState(d.(*state.State), false) + _, _, err = state.ApplyState(st.SpaceID(), d.(*state.State), false) return err } diff --git a/core/block/editor/state/change_test.go b/core/block/editor/state/change_test.go index cb64a9181..eb2b50cac 100644 --- a/core/block/editor/state/change_test.go +++ b/core/block/editor/state/change_test.go @@ -46,7 +46,7 @@ func TestState_ChangesCreate_MoveAdd(t *testing.T) { require.NoError(t, s.InsertTo("b", model.Block_Inner, "1", "2", "4", "5")) s.Add(simple.New(&model.Block{Id: "3.1"})) require.NoError(t, s.InsertTo("2", model.Block_Bottom, "3.1")) - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) changes := d.(*State).GetChanges() require.Len(t, changes, 4) @@ -62,7 +62,7 @@ func TestState_ChangesCreate_Collection_Set(t *testing.T) { d := NewDoc("root", nil) s := d.NewState() s.SetInStore([]string{"coll1", "key1"}, pbtypes.String("1")) - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) changes := d.(*State).GetChanges() require.Len(t, changes, 1) @@ -81,7 +81,7 @@ func TestState_ChangesCreate_Collection_Unset(t *testing.T) { d.(*State).store = makeStoreWithTwoKeysAndValue("coll1", "key1", "1") s := d.NewState() s.RemoveFromStore([]string{"coll1", "key1"}) - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) changes := d.(*State).GetChanges() require.Len(t, changes, 1) @@ -197,7 +197,7 @@ func TestState_ChangesCreate_StoreSlice(t *testing.T) { newState := doc.NewState() newState.UpdateStoreSlice(key, tc.after) - _, _, err := ApplyState(newState, false) + _, _, err := ApplyState("", newState, false) require.NoError(t, err) got := doc.(*State).GetChanges() @@ -232,12 +232,12 @@ func TestState_ChangesCreate_MoveAdd_Wrap(t *testing.T) { s.Add(simple.New(&model.Block{Id: "div", ChildrenIds: []string{"a", "b"}})) s.Get("root").Model().ChildrenIds = []string{"div"} - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) changes := d.(*State).GetChanges() s2 := dc.NewState() require.NoError(t, s2.ApplyChange(changes...)) - _, _, err = ApplyState(s2, true) + _, _, err = ApplyState("", s2, true) require.NoError(t, err) assert.Equal(t, d.(*State).String(), dc.(*State).String()) } @@ -264,12 +264,12 @@ func TestState_ChangesCreate_MoveAdd_Side(t *testing.T) { s.Unlink("5") s.InsertTo("1", model.Block_Left, "4", "5") - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) changes := d.(*State).GetChanges() s2 := dc.NewState() require.NoError(t, s2.ApplyChange(changes...)) - _, _, err = ApplyState(s2, true) + _, _, err = ApplyState("", s2, true) require.NoError(t, err) assert.Equal(t, d.(*State).String(), dc.(*State).String()) } @@ -290,7 +290,7 @@ func TestState_ChangesCreate_MoveAdd_Side_NewBlock(t *testing.T) { assertApplyingChanges := func(t *testing.T, state *State, wantState *State, originalState *State) { AssertTreesEqual(t, wantState.Blocks(), state.Blocks()) - _, _, err := ApplyState(state, true) + _, _, err := ApplyState("", state, true) require.NoError(t, err) changes := state.GetChanges() err = originalState.ApplyChange(changes...) @@ -368,7 +368,7 @@ func TestState_SetParent(t *testing.T) { ns.SetRootId(st.RootId()) ns.SetParent(st) - _, _, err := ApplyState(ns, false) + _, _, err := ApplyState("", ns, false) require.NoError(t, err) st2 := orig.Copy() @@ -391,7 +391,7 @@ func TestStateNormalizeMerge(t *testing.T) { s.InsertTo(fmt.Sprintf("common%d", i-1), model.Block_Bottom, id) } } - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) t.Run("parallel normalize", func(t *testing.T) { @@ -402,7 +402,7 @@ func TestStateNormalizeMerge(t *testing.T) { stateA.Add(simple.New(&model.Block{Id: "a1"})) stateA.Add(simple.New(&model.Block{Id: "a2"})) stateA.InsertTo("common39", model.Block_Bottom, "a1", "a2") - _, _, err = ApplyState(stateA, true) + _, _, err = ApplyState("", stateA, true) require.NoError(t, err) // t.Log(docA.String()) changesA := stateA.GetChanges() @@ -411,18 +411,18 @@ func TestStateNormalizeMerge(t *testing.T) { stateB := docB.NewState() stateB.Add(simple.New(&model.Block{Id: "b1"})) stateB.InsertTo("common39", model.Block_Bottom, "b1") - _, _, err = ApplyState(stateB, true) + _, _, err = ApplyState("", stateB, true) require.NoError(t, err) // t.Log(docB.String()) changesB := stateB.GetChanges() s = d.NewState() require.NoError(t, s.ApplyChange(changesB...)) - _, _, err = ApplyStateFastOne(s) + _, _, err = ApplyStateFastOne("", s) require.NoError(t, err) s = d.NewState() require.NoError(t, s.ApplyChange(changesA...)) - _, _, err = ApplyState(s, true) + _, _, err = ApplyState("", s, true) require.NoError(t, err) s = d.NewState() @@ -438,7 +438,7 @@ func TestStateNormalizeMerge(t *testing.T) { s.Add(simple.New(&model.Block{Id: "common40"})) s.InsertTo("common39", model.Block_Bottom, "common40") ids = append(ids, "common40") - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) aIds := []string{} @@ -450,7 +450,7 @@ func TestStateNormalizeMerge(t *testing.T) { stateA.Add(simple.New(&model.Block{Id: id})) } stateA.InsertTo("common0", model.Block_Top, aIds...) - _, _, err = ApplyState(stateA, true) + _, _, err = ApplyState("", stateA, true) require.NoError(t, err) changesA := stateA.GetChanges() @@ -463,17 +463,17 @@ func TestStateNormalizeMerge(t *testing.T) { stateB.Add(simple.New(&model.Block{Id: id})) } stateB.InsertTo("common40", model.Block_Bottom, bIds...) - _, _, err = ApplyState(stateB, true) + _, _, err = ApplyState("", stateB, true) require.NoError(t, err) changesB := stateB.GetChanges() s = d.NewState() require.NoError(t, s.ApplyChange(changesB...)) - _, _, err = ApplyStateFastOne(s) + _, _, err = ApplyStateFastOne("", s) require.NoError(t, err) s = d.NewState() require.NoError(t, s.ApplyChange(changesA...)) - _, _, err = ApplyState(s, true) + _, _, err = ApplyState("", s, true) require.NoError(t, err) s = d.NewState() @@ -505,12 +505,12 @@ func TestState_ChangeDataviewOrder(t *testing.T) { s := d.NewState() s.Get("dv").(dataview.Block).SetViewOrder([]string{"3", "1", "2"}) - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) changes := d.(*State).GetChanges() s2 := dc.NewState() require.NoError(t, s2.ApplyChange(changes...)) - _, _, err = ApplyState(s2, true) + _, _, err = ApplyState("", s2, true) require.NoError(t, err) assert.Equal(t, d.(*State).Pick("dv").Model().String(), dc.(*State).Pick("dv").Model().String()) } @@ -533,11 +533,11 @@ func TestState_ChangeDataviewUnlink(t *testing.T) { }, }})) s.InsertTo("root", model.Block_Inner, "dv") - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) changes := d.(*State).GetChanges() s = d.NewState() s.Unlink("dv") - _, _, err = ApplyState(s, true) + _, _, err = ApplyState("", s, true) require.NoError(t, err) changes = append(changes, d.(*State).GetChanges()...) @@ -546,7 +546,7 @@ func TestState_ChangeDataviewUnlink(t *testing.T) { require.Nil(t, s2.Get("dv")) require.Nil(t, s2.Pick("dv")) - _, _, err = ApplyState(s2, true) + _, _, err = ApplyState("", s2, true) require.NoError(t, err) require.Nil(t, dc.(*State).Get("dv")) require.Nil(t, dc.(*State).Pick("dv")) @@ -570,11 +570,11 @@ func TestState_ChangeDataviewRemoveAdd(t *testing.T) { }, }})) s.InsertTo("root", model.Block_Inner, "dv") - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) changes := d.(*State).GetChanges() s = d.NewState() s.Unlink("dv") - _, _, err = ApplyState(s, true) + _, _, err = ApplyState("", s, true) require.NoError(t, err) changes = append(changes, d.(*State).GetChanges()...) s = d.NewState() @@ -586,7 +586,7 @@ func TestState_ChangeDataviewRemoveAdd(t *testing.T) { }, }})) s.InsertTo("root", model.Block_Inner, "dv") - _, _, err = ApplyState(s, true) + _, _, err = ApplyState("", s, true) require.NoError(t, err) changes = append(changes, d.(*State).GetChanges()...) @@ -596,7 +596,7 @@ func TestState_ChangeDataviewRemoveAdd(t *testing.T) { require.Len(t, s2.Get("dv").Model().GetDataview().Views, 1) require.Equal(t, "2", s2.Get("dv").Model().GetDataview().Views[0].Id) - _, _, err = ApplyState(s2, true) + _, _, err = ApplyState("", s2, true) require.NoError(t, err) require.NotNil(t, dc.(*State).Get("dv")) require.Len(t, dc.(*State).Get("dv").Model().GetDataview().Views, 1) @@ -623,7 +623,7 @@ func TestState_ChangeDataviewRemoveMove(t *testing.T) { s := d1.NewState() require.NoError(t, s.ApplyChange(changes...)) - ApplyState(s, true) + ApplyState("", s, true) require.Nil(t, d1.(*State).blocks["b"]) require.NotContains(t, d1.(*State).Pick("s").Model().ChildrenIds, "b") require.NotContains(t, d1.(*State).Pick("root").Model().ChildrenIds, "b") @@ -692,7 +692,7 @@ func TestRelationChanges(t *testing.T) { ac := a.Copy() b := a.NewState() b.relationLinks = []*model.RelationLink{{Key: "3"}, {Key: "4"}, {Key: "5"}} - _, _, err := ApplyState(b, false) + _, _, err := ApplyState("", b, false) require.NoError(t, err) chs := a.GetChanges() require.NoError(t, ac.ApplyChange(chs...)) @@ -708,7 +708,7 @@ func TestLocalRelationChanges(t *testing.T) { b.relationLinks = []*model.RelationLink{{Key: bundle.RelationKeySyncStatus.String(), Format: model.RelationFormat_number}} // when - _, _, err := ApplyState(b, false) + _, _, err := ApplyState("", b, false) require.NoError(t, err) chs := a.GetChanges() @@ -723,7 +723,7 @@ func TestLocalRelationChanges(t *testing.T) { b.relationLinks = []*model.RelationLink{} // when - _, _, err := ApplyState(b, false) + _, _, err := ApplyState("", b, false) require.NoError(t, err) chs := a.GetChanges() @@ -738,7 +738,7 @@ func TestLocalRelationChanges(t *testing.T) { b.relationLinks = []*model.RelationLink{{Key: bundle.RelationKeySpaceId.String(), Format: model.RelationFormat_longtext}} // when - _, _, err := ApplyState(b, false) + _, _, err := ApplyState("", b, false) require.NoError(t, err) chs := a.GetChanges() @@ -753,7 +753,7 @@ func TestLocalRelationChanges(t *testing.T) { b.relationLinks = []*model.RelationLink{} // when - _, _, err := ApplyState(b, false) + _, _, err := ApplyState("", b, false) require.NoError(t, err) chs := a.GetChanges() @@ -769,7 +769,7 @@ func TestRootBlockChanges(t *testing.T) { s.Add(simple.New(&model.Block{Id: "new"})) require.NoError(t, s.InsertTo("root", model.Block_Inner, "new")) - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) changes := s.GetChanges() @@ -825,7 +825,7 @@ func buildStateFromAST(root *Block) *State { Blocks: root.Build(), }, }).(*State) - ApplyState(st, true) + ApplyState("", st, true) return st.NewState() } @@ -879,7 +879,7 @@ func TestRootDeviceChanges(t *testing.T) { s := a.NewState() // when - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) // then assert.Nil(t, err) @@ -897,7 +897,7 @@ func TestRootDeviceChanges(t *testing.T) { s.AddDevice(device) // when - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) // then assert.Nil(t, err) @@ -916,7 +916,7 @@ func TestRootDeviceChanges(t *testing.T) { s := a.NewState() s.SetDeviceName("id", "test1") // when - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) // then assert.Nil(t, err) @@ -935,7 +935,7 @@ func TestRootDeviceChanges(t *testing.T) { s.AddDevice(device) s.parent = nil // when - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) // then assert.Nil(t, err) @@ -980,7 +980,7 @@ func TestTableChanges(t *testing.T) { }, }})) - msgs, _, err := ApplyState(s, true) + msgs, _, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 1) @@ -988,7 +988,7 @@ func TestTableChanges(t *testing.T) { rows := s.Get("tableRow1") require.NotNil(t, rows) rows.Model().GetTableRow().IsHeader = true - msgs, _, err = ApplyState(s, true) + msgs, _, err = ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 1) diff --git a/core/block/editor/state/normalize_test.go b/core/block/editor/state/normalize_test.go index a9c3d28ae..6b663c4ff 100644 --- a/core/block/editor/state/normalize_test.go +++ b/core/block/editor/state/normalize_test.go @@ -50,7 +50,7 @@ func TestState_Normalize(t *testing.T) { r := NewDoc("1", nil) r.(*State).Add(simple.New(&model.Block{Id: "1"})) s := r.NewState() - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 0) assert.Empty(t, hist) @@ -63,7 +63,7 @@ func TestState_Normalize(t *testing.T) { }).(*State) s := r.NewState() s.Get("one") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 1) assert.Len(t, hist.Change, 1) @@ -97,7 +97,7 @@ func TestState_Normalize(t *testing.T) { s.Get("tableRows") s.Get("tableColumns") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) // 1 remove + 1 change assert.Len(t, hist.Change, 1) @@ -122,7 +122,7 @@ func TestState_Normalize(t *testing.T) { s := r.NewState() s.Get("c1") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) // 1 remove + 1 change assert.Len(t, hist.Change, 1) @@ -146,7 +146,7 @@ func TestState_Normalize(t *testing.T) { s := r.NewState() s.Unlink("c2") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 4) // 1 row change + 1 remove + 2 width reset assert.Len(t, hist.Remove, 2) @@ -168,7 +168,7 @@ func TestState_Normalize(t *testing.T) { s := r.NewState() s.normalizeTree() - ApplyState(s, true) + ApplyState("", s, true) blocks := r.Blocks() result := []string{} divs := []string{} @@ -207,7 +207,7 @@ func TestState_Normalize(t *testing.T) { r.Add(simple.New(&model.Block{Id: "root", ChildrenIds: rootIds})) s := r.NewState() - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) require.NoError(t, err) }) @@ -221,7 +221,7 @@ func TestState_Normalize(t *testing.T) { id := fmt.Sprint(i) s.Add(simple.New(&model.Block{Id: id})) s.InsertTo(targetId, targetPos, id) - msgs, _, err := ApplyState(s, true) + msgs, _, err := ApplyState("", s, true) require.NoError(t, err) for _, msg := range msgs { if add := msg.Msg.GetBlockAdd(); add != nil { @@ -262,7 +262,7 @@ func TestState_Normalize(t *testing.T) { for _, b := range ev.Blocks { r.Add(simple.New(b)) } - _, _, err = ApplyState(r.NewState(), true) + _, _, err = ApplyState("", r.NewState(), true) require.NoError(t, err) // t.Log(r.String()) @@ -283,7 +283,7 @@ func TestState_Normalize(t *testing.T) { r.Add(simple.New(&model.Block{Id: "e"})) s := r.NewState() require.NoError(t, s.Normalize(false)) - _, _, err := ApplyState(s, false) + _, _, err := ApplyState("", s, false) require.NoError(t, err) assert.Equal(t, []string{"a", "b", "c"}, r.Pick("root").Model().ChildrenIds) assert.Equal(t, []string{"d"}, r.Pick("a").Model().ChildrenIds) @@ -305,7 +305,7 @@ func TestState_Normalize(t *testing.T) { s := r.NewState() s.normalizeTree() - ApplyState(s, true) + ApplyState("", s, true) assert.Equal(t, "header", s.Pick(s.RootId()).Model().ChildrenIds[0]) }) @@ -336,7 +336,7 @@ func TestState_Normalize(t *testing.T) { // when require.NoError(t, s.Normalize(false)) - _, msg, err := ApplyState(s, false) + _, msg, err := ApplyState("", s, false) // then require.NoError(t, err) @@ -463,7 +463,7 @@ func BenchmarkNormalize(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - ApplyState(r.NewState(), true) + ApplyState("", r.NewState(), true) } } diff --git a/core/block/editor/state/position_test.go b/core/block/editor/state/position_test.go index 2b0f3ac37..3de3a0108 100644 --- a/core/block/editor/state/position_test.go +++ b/core/block/editor/state/position_test.go @@ -23,7 +23,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "second"})) s.InsertTo("", 0, "first", "second") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) assert.Len(t, hist.Add, 2) @@ -42,7 +42,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "second"})) s.InsertTo("target", model.Block_Bottom, "first", "second") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) assert.Len(t, hist.Add, 2) @@ -59,7 +59,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "second"})) s.InsertTo("target", model.Block_Top, "first", "second") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) assert.Len(t, hist.Add, 2) @@ -76,7 +76,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "second"})) s.InsertTo("target", model.Block_Inner, "first", "second") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) assert.Len(t, hist.Add, 2) @@ -95,7 +95,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "second"})) s.InsertTo("target", model.Block_Replace, "first", "second") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 3) assert.Len(t, hist.Remove, 1) @@ -106,7 +106,7 @@ func TestState_InsertTo(t *testing.T) { s = r.NewState() require.NoError(t, r.InsertTo("first", model.Block_Replace, "child")) - _, _, err = ApplyState(s, true) + _, _, err = ApplyState("", s, true) require.NoError(t, err) assert.Equal(t, []string{"child", "second"}, r.Pick("root").Model().ChildrenIds) }) @@ -123,7 +123,7 @@ func TestState_InsertTo(t *testing.T) { // when err := s.InsertTo("target", model.Block_Replace, "link") - _, _, err2 := ApplyState(s, true) + _, _, err2 := ApplyState("", s, true) // then assert.NoError(t, err) @@ -142,7 +142,7 @@ func TestState_InsertTo(t *testing.T) { // when err := s.InsertTo("target", model.Block_Replace, "link") - _, _, err2 := ApplyState(s, true) + _, _, err2 := ApplyState("", s, true) // then assert.NoError(t, err) @@ -174,7 +174,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "second"})) s.InsertTo("target", pos, "first", "second") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.NotEmpty(t, msgs) assert.Len(t, hist.Remove, 0) @@ -207,7 +207,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "third"})) s.InsertTo(c1.Model().Id, model.Block_Left, "third") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) assert.Len(t, hist.Remove, 0) @@ -220,7 +220,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "third"})) s.InsertTo(c2.Model().Id, model.Block_Left, "third") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) assert.Len(t, hist.Remove, 0) @@ -233,7 +233,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "third"})) s.InsertTo(c2.Model().Id, model.Block_Right, "third") - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) assert.Len(t, hist.Remove, 0) @@ -250,7 +250,7 @@ func TestState_InsertTo(t *testing.T) { s.Add(simple.New(&model.Block{Id: "2", ChildrenIds: []string{"1"}})) s.Get("root").Model().ChildrenIds = []string{"1"} - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) assert.Error(t, err) }) diff --git a/core/block/editor/state/state.go b/core/block/editor/state/state.go index 14af1de66..88b9e7d92 100644 --- a/core/block/editor/state/state.go +++ b/core/block/editor/state/state.go @@ -412,25 +412,25 @@ func (s *State) SearchText() string { return builder.String() } -func ApplyState(s *State, withLayouts bool) (msgs []simple.EventMessage, action undo.Action, err error) { - return s.apply(false, false, withLayouts) +func ApplyState(spaceId string, s *State, withLayouts bool) (msgs []simple.EventMessage, action undo.Action, err error) { + return s.apply(spaceId, false, false, withLayouts) } -func ApplyStateFast(s *State) (msgs []simple.EventMessage, action undo.Action, err error) { - return s.apply(true, false, false) +func ApplyStateFast(spaceId string, s *State) (msgs []simple.EventMessage, action undo.Action, err error) { + return s.apply(spaceId, true, false, false) } -func ApplyStateFastOne(s *State) (msgs []simple.EventMessage, action undo.Action, err error) { - return s.apply(true, true, false) +func ApplyStateFastOne(spaceId string, s *State) (msgs []simple.EventMessage, action undo.Action, err error) { + return s.apply(spaceId, true, true, false) } -func (s *State) apply(fast, one, withLayouts bool) (msgs []simple.EventMessage, action undo.Action, err error) { +func (s *State) apply(spaceId string, fast, one, withLayouts bool) (msgs []simple.EventMessage, action undo.Action, err error) { if s.parent != nil && (s.parent.parent != nil || fast) { s.intermediateApply() if one { return } - return s.parent.apply(fast, one, withLayouts) + return s.parent.apply("", fast, one, withLayouts) } if fast { return @@ -486,8 +486,7 @@ func (s *State) apply(fast, one, withLayouts bool) (msgs []simple.EventMessage, } flushNewBlocks := func() { if len(newBlocks) > 0 { - // TODO: Check that the SpaceID is available here - msgs = append(msgs, simple.EventMessage{Msg: event.NewMessage(s.SpaceID(), + msgs = append(msgs, simple.EventMessage{Msg: event.NewMessage(spaceId, &pb.EventMessageValueOfBlockAdd{ BlockAdd: &pb.EventBlockAdd{ Blocks: newBlocks, @@ -517,8 +516,7 @@ func (s *State) apply(fast, one, withLayouts bool) (msgs []simple.EventMessage, db.DetailsInit(s) } } - // TODO: Check that the SpaceID is available here - diff, err := orig.Diff(s.SpaceID(), b) + diff, err := orig.Diff(spaceId, b) if err != nil { return nil, undo.Action{}, err } @@ -652,8 +650,7 @@ func (s *State) apply(fast, one, withLayouts bool) (msgs []simple.EventMessage, if s.parent != nil && s.localDetails != nil { prev := s.parent.LocalDetails() if diff := pbtypes.StructDiff(prev, s.localDetails); diff != nil { - // TODO: Check that the SpaceID is available here - msgs = append(msgs, WrapEventMessages(true, StructDiffIntoEvents(s.SpaceID(), s.RootId(), diff))...) + msgs = append(msgs, WrapEventMessages(true, StructDiffIntoEvents(spaceId, s.RootId(), diff))...) s.parent.localDetails = s.localDetails } else if !s.localDetails.Equal(s.parent.localDetails) { s.parent.localDetails = s.localDetails @@ -1211,8 +1208,8 @@ func (s *State) CheckRestrictions() (err error) { } if rest.Edit { if ob := s.parent.Pick(id); ob != nil { - // TODO: Check that the SpaceID is available here - if msgs, _ := ob.Diff(s.SpaceID(), b); len(msgs) > 0 { + // SpaceId is empty because only the fact that there is any diff matters here + if msgs, _ := ob.Diff("", b); len(msgs) > 0 { return ErrRestricted } } diff --git a/core/block/editor/state/state_test.go b/core/block/editor/state/state_test.go index 919683fad..c0053a2a6 100644 --- a/core/block/editor/state/state_test.go +++ b/core/block/editor/state/state_test.go @@ -192,7 +192,7 @@ func TestApplyState(t *testing.T) { s.InsertTo("4", model.Block_Bottom, "5") s.changeId = "4" - msgs, hist, err := ApplyState(s, true) + msgs, hist, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, hist.Add, 2) assert.Len(t, hist.Change, 1) @@ -225,7 +225,7 @@ func TestApplyState(t *testing.T) { Text: "name", Checked: "done", })) - _, _, err := ApplyState(s, true) + _, _, err := ApplyState("", s, true) assert.Equal(t, "new name", s.Pick("title").Model().GetText().Text) require.NoError(t, err) }) @@ -253,7 +253,7 @@ func TestApplyState(t *testing.T) { "name": pbtypes.String("new name"), }, }) - msgs, _, err := ApplyState(s, true) + msgs, _, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) }) @@ -282,7 +282,7 @@ func TestApplyState(t *testing.T) { "done": pbtypes.Bool(false), }, }) - msgs, _, err := ApplyState(s, true) + msgs, _, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) }) @@ -307,7 +307,7 @@ func TestApplyState(t *testing.T) { d.BlocksInit(d.(simple.DetailsService)) s := d.NewState() s.Get("2").(text.Block).SetChecked(false) - msgs, _, err := ApplyState(s, true) + msgs, _, err := ApplyState("", s, true) require.NoError(t, err) assert.Len(t, msgs, 2) }) diff --git a/core/block/source/source.go b/core/block/source/source.go index fbf2ad815..5af5fdc76 100644 --- a/core/block/source/source.go +++ b/core/block/source/source.go @@ -349,7 +349,7 @@ func (s *source) buildState() (doc state.Doc, err error) { } // TODO: check if we can use apply fast one - if _, _, err = state.ApplyState(st, false); err != nil { + if _, _, err = state.ApplyState(s.spaceID, st, false); err != nil { return } return st, nil @@ -623,7 +623,7 @@ func BuildState(spaceId string, initState *state.State, ot objecttree.ReadableOb return } if applyState { - _, _, err = state.ApplyStateFastOne(st) + _, _, err = state.ApplyStateFastOne(spaceId, st) if err != nil { return } diff --git a/core/debug/treearchive/treeimporter.go b/core/debug/treearchive/treeimporter.go index 82728b162..bc4171e1d 100644 --- a/core/debug/treearchive/treeimporter.go +++ b/core/debug/treearchive/treeimporter.go @@ -77,7 +77,7 @@ func (t *treeImporter) State() (*state.State, error) { return nil, err } - if _, _, err = state.ApplyStateFast(st); err != nil { + if _, _, err = state.ApplyStateFast("", st); err != nil { return nil, err } return st, nil diff --git a/core/history/history.go b/core/history/history.go index c1d48ced2..d2fb0cf00 100644 --- a/core/history/history.go +++ b/core/history/history.go @@ -241,7 +241,7 @@ func (h *history) DiffVersions(req *pb.RpcHistoryDiffVersionsRequest) ([]*pb.Eve } currState.SetParent(previousState) - msg, _, err := state.ApplyState(currState, false) + msg, _, err := state.ApplyState(req.SpaceId, currState, false) if err != nil { return nil, nil, fmt.Errorf("failed to get history events for versions %s, %s: %w", req.CurrentVersion, req.PreviousVersion, err) } @@ -560,7 +560,7 @@ func (h *history) buildState(id domain.FullID, versionId string) (st *state.Stat if err != nil { return } - if _, _, err = state.ApplyStateFast(st); err != nil { + if _, _, err = state.ApplyStateFast(id.SpaceID, st); err != nil { return } diff --git a/tests/testutil/state.go b/tests/testutil/state.go index b624fce3c..9fb1a4df3 100644 --- a/tests/testutil/state.go +++ b/tests/testutil/state.go @@ -13,6 +13,6 @@ func BuildStateFromAST(root *blockbuilder.Block) *state.State { Blocks: root.Build(), }, }).(*state.State) - state.ApplyState(st, true) + state.ApplyState("", st, true) return st.NewState() }