mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 09:35:00 +09:00
Fix bugs and refactor
This commit is contained in:
parent
4f3fbc3e18
commit
012dce8d1a
4 changed files with 74 additions and 81 deletions
|
@ -19,7 +19,6 @@ import (
|
|||
"github.com/anytypeio/go-anytype-middleware/core/relation/relationutils"
|
||||
"github.com/anytypeio/go-anytype-middleware/pb"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/pbtypes"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/slice"
|
||||
)
|
||||
|
||||
|
@ -255,20 +254,7 @@ func (s *State) applyEvent(ev *pb.EventMessage) (err error) {
|
|||
event := o.BlockDataViewObjectOrderUpdate
|
||||
if err = apply(event.Id, func(b simple.Block) error {
|
||||
if dvBlock, ok := b.(dataview.Block); ok {
|
||||
var existOrder []string
|
||||
for _, order := range dvBlock.Model().GetDataview().ObjectOrders {
|
||||
if order.ViewId == event.ViewId && order.GroupId == event.GroupId {
|
||||
existOrder = order.ObjectIds
|
||||
}
|
||||
}
|
||||
|
||||
changes := o.BlockDataViewObjectOrderUpdate.GetSliceChanges()
|
||||
changedIds := slice.ApplyChanges(existOrder, pbtypes.EventsToSliceChange(changes), slice.Identity[string])
|
||||
|
||||
dvBlock.SetViewObjectOrder([]*model.BlockContentDataviewObjectOrder{
|
||||
{ViewId: event.ViewId, GroupId: event.GroupId, ObjectIds: changedIds},
|
||||
})
|
||||
|
||||
dvBlock.ApplyObjectOrderUpdate(event)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("not a dataview block")
|
||||
|
|
|
@ -69,6 +69,7 @@ type Block interface {
|
|||
DeleteRelationOld(relationKey string) error
|
||||
|
||||
ApplyViewUpdate(upd *pb.EventBlockDataviewViewUpdate)
|
||||
ApplyObjectOrderUpdate(upd *pb.EventBlockDataviewObjectOrderUpdate)
|
||||
|
||||
AddFilter(viewId string, filter *model.BlockContentDataviewFilter) error
|
||||
RemoveFilters(viewId string, filterIDs []string) error
|
||||
|
@ -137,11 +138,11 @@ func (d *Dataview) Diff(b simple.Block) (msgs []simple.EventMessage, err error)
|
|||
|
||||
for _, order2 := range dv.content.ObjectOrders {
|
||||
var found bool
|
||||
var changes []slice.Change[string]
|
||||
var changes []*pb.EventBlockDataviewSliceChange
|
||||
for _, order1 := range d.content.ObjectOrders {
|
||||
if order1.ViewId == order2.ViewId && order1.GroupId == order2.GroupId {
|
||||
found = true
|
||||
changes = slice.Diff(order1.ObjectIds, order2.ObjectIds, slice.Identity[string], slice.Equal[string])
|
||||
changes = diffViewObjectOrder(order1, order2)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -166,7 +167,7 @@ func (d *Dataview) Diff(b simple.Block) (msgs []simple.EventMessage, err error)
|
|||
Id: dv.Id,
|
||||
ViewId: order2.ViewId,
|
||||
GroupId: order2.GroupId,
|
||||
SliceChanges: pbtypes.SliceChangeToEvents(changes),
|
||||
SliceChanges: changes,
|
||||
}}}})
|
||||
}
|
||||
}
|
||||
|
@ -339,11 +340,7 @@ func (s *Dataview) SetView(viewID string, view model.BlockContentDataviewView) e
|
|||
v.Relations = view.Relations
|
||||
v.Sorts = view.Sorts
|
||||
v.Filters = view.Filters
|
||||
for _, f := range v.Filters {
|
||||
if f.Id == "" {
|
||||
f.Id = bson.NewObjectId().Hex()
|
||||
}
|
||||
}
|
||||
|
||||
v.Name = view.Name
|
||||
v.Type = view.Type
|
||||
v.CoverRelationKey = view.CoverRelationKey
|
||||
|
|
|
@ -298,3 +298,71 @@ func unwrapChanges[T, R any](
|
|||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func diffViewObjectOrder(a, b *model.BlockContentDataviewObjectOrder) []*pb.EventBlockDataviewSliceChange {
|
||||
diff := slice.Diff(a.ObjectIds, b.ObjectIds, slice.Identity[string], slice.Equal[string])
|
||||
if len(diff) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var res []*pb.EventBlockDataviewSliceChange
|
||||
for _, sliceCh := range diff {
|
||||
if add := sliceCh.Add(); add != nil {
|
||||
res = append(res, &pb.EventBlockDataviewSliceChange{
|
||||
Op: pb.EventBlockDataview_SliceOperationAdd,
|
||||
Ids: add.Items,
|
||||
AfterId: add.AfterId,
|
||||
})
|
||||
}
|
||||
if move := sliceCh.Move(); move != nil {
|
||||
res = append(res, &pb.EventBlockDataviewSliceChange{
|
||||
Op: pb.EventBlockDataview_SliceOperationMove,
|
||||
Ids: move.IDs,
|
||||
AfterId: move.AfterId,
|
||||
})
|
||||
}
|
||||
if rm := sliceCh.Remove(); rm != nil {
|
||||
res = append(res, &pb.EventBlockDataviewSliceChange{
|
||||
Op: pb.EventBlockDataview_SliceOperationRemove,
|
||||
Ids: rm.IDs,
|
||||
})
|
||||
}
|
||||
|
||||
// Replace change is not supported
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (l *Dataview) ApplyObjectOrderUpdate(upd *pb.EventBlockDataviewObjectOrderUpdate) {
|
||||
var existOrder []string
|
||||
for _, order := range l.Model().GetDataview().ObjectOrders {
|
||||
if order.ViewId == upd.ViewId && order.GroupId == upd.GroupId {
|
||||
existOrder = order.ObjectIds
|
||||
}
|
||||
}
|
||||
|
||||
rawChanges := upd.GetSliceChanges()
|
||||
|
||||
changes := make([]slice.Change[string], 0, len(rawChanges))
|
||||
for _, eventCh := range rawChanges {
|
||||
var ch slice.Change[string]
|
||||
switch eventCh.Op {
|
||||
case pb.EventBlockDataview_SliceOperationAdd:
|
||||
ch = slice.MakeChangeAdd(eventCh.Ids, eventCh.AfterId)
|
||||
case pb.EventBlockDataview_SliceOperationMove:
|
||||
ch = slice.MakeChangeMove[string](eventCh.Ids, eventCh.AfterId)
|
||||
case pb.EventBlockDataview_SliceOperationRemove:
|
||||
ch = slice.MakeChangeRemove[string](eventCh.Ids)
|
||||
case pb.EventBlockDataview_SliceOperationReplace:
|
||||
// Replace change is not supported
|
||||
}
|
||||
changes = append(changes, ch)
|
||||
}
|
||||
|
||||
changedIds := slice.ApplyChanges(existOrder, changes, slice.Identity[string])
|
||||
|
||||
l.SetViewObjectOrder([]*model.BlockContentDataviewObjectOrder{
|
||||
{ViewId: upd.ViewId, GroupId: upd.GroupId, ObjectIds: changedIds},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
|
||||
"github.com/gogo/protobuf/types"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/pb"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/slice"
|
||||
)
|
||||
|
@ -210,60 +209,3 @@ func StructNotNilKeys(st *types.Struct) (keys []string) {
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
func EventsToSliceChange(changes []*pb.EventBlockDataviewSliceChange) []slice.Change[string] {
|
||||
var res []slice.Change[string]
|
||||
for _, eventCh := range changes {
|
||||
var ch slice.Change[string]
|
||||
switch eventCh.Op {
|
||||
case pb.EventBlockDataview_SliceOperationAdd:
|
||||
ch = slice.MakeChangeAdd(eventCh.Ids, eventCh.AfterId)
|
||||
case pb.EventBlockDataview_SliceOperationMove:
|
||||
ch = slice.MakeChangeMove[string](eventCh.Ids, eventCh.AfterId)
|
||||
case pb.EventBlockDataview_SliceOperationRemove:
|
||||
ch = slice.MakeChangeRemove[string](eventCh.Ids)
|
||||
case pb.EventBlockDataview_SliceOperationReplace:
|
||||
// TODO check this out
|
||||
// ch = slice.MakeChangeReplace(slice.StringsToIDs(eventCh.Ids), eventCh.AfterId)
|
||||
}
|
||||
res = append(res, ch)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func SliceChangeToEvents(changes []slice.Change[string]) []*pb.EventBlockDataviewSliceChange {
|
||||
var res []*pb.EventBlockDataviewSliceChange
|
||||
for _, sliceCh := range changes {
|
||||
if add := sliceCh.Add(); add != nil {
|
||||
res = append(res, &pb.EventBlockDataviewSliceChange{
|
||||
Op: pb.EventBlockDataview_SliceOperationAdd,
|
||||
Ids: add.Items,
|
||||
AfterId: add.AfterId,
|
||||
})
|
||||
}
|
||||
if move := sliceCh.Move(); move != nil {
|
||||
res = append(res, &pb.EventBlockDataviewSliceChange{
|
||||
Op: pb.EventBlockDataview_SliceOperationMove,
|
||||
Ids: move.IDs,
|
||||
AfterId: move.AfterId,
|
||||
})
|
||||
}
|
||||
if rm := sliceCh.Remove(); rm != nil {
|
||||
res = append(res, &pb.EventBlockDataviewSliceChange{
|
||||
Op: pb.EventBlockDataview_SliceOperationRemove,
|
||||
Ids: rm.IDs,
|
||||
})
|
||||
}
|
||||
// TODO check this out
|
||||
// if replace := sliceCh.Replace(); replace != nil {
|
||||
// res = append(res, &pb.EventBlockDataviewSliceChange{
|
||||
// Op: pb.EventBlockDataview_SliceOperationReplace,
|
||||
// Ids: slice.IDsToStrings(replace.Items),
|
||||
// AfterId: replace.AfterId,
|
||||
// })
|
||||
// }
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue