From b0b4e5b721712f2d96ca3d4a4927d8e63c858470 Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Mon, 7 Nov 2022 11:22:48 +0300 Subject: [PATCH] acl sync handler [wip] --- common/commonspace/syncacl/syncaclhandler.go | 31 ++++++++++ common/pkg/acl/list/list.go | 64 ++++++++++++++++---- common/pkg/acl/list/list_test.go | 4 ++ 3 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 common/commonspace/syncacl/syncaclhandler.go diff --git a/common/commonspace/syncacl/syncaclhandler.go b/common/commonspace/syncacl/syncaclhandler.go new file mode 100644 index 00000000..feb78e07 --- /dev/null +++ b/common/commonspace/syncacl/syncaclhandler.go @@ -0,0 +1,31 @@ +package syncacl + +import ( + "context" + "fmt" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/list" +) + +type syncAclHandler struct { + acl list.ACLList +} + +func (s *syncAclHandler) HandleMessage(ctx context.Context, senderId string, req *spacesyncproto.ObjectSyncMessage) (err error) { + aclMsg := &aclrecordproto.ACLSyncMessage{} + if err = aclMsg.Unmarshal(req.Payload); err != nil { + return + } + content := aclMsg.GetContent() + switch { + case content.GetAddRecords() != nil: + return s.handleAddRecords(ctx, senderId, content.GetAddRecords()) + default: + return fmt.Errorf("unexpected aclSync message: %T", content.Value) + } +} + +func (s *syncAclHandler) handleAddRecords(ctx context.Context, senderId string, addRecord *aclrecordproto.ACLAddRecords) (err error) { + return +} diff --git a/common/pkg/acl/list/list.go b/common/pkg/acl/list/list.go index 3c91bd33..1078f974 100644 --- a/common/pkg/acl/list/list.go +++ b/common/pkg/acl/list/list.go @@ -14,7 +14,10 @@ import ( type IterFunc = func(record *ACLRecord) (IsContinue bool) -var ErrIncorrectCID = errors.New("incorrect CID") +var ( + ErrIncorrectCID = errors.New("incorrect CID") + ErrInconsistent = errors.New("inconsistent record") +) type RWLocker interface { sync.Locker @@ -33,6 +36,9 @@ type ACLList interface { Get(id string) (*ACLRecord, error) Iterate(iterFunc IterFunc) IterateFrom(startId string, iterFunc IterFunc) + + AddRawRecord(rawRec *aclrecordproto.RawACLRecordWithId) (added bool, err error) + Close() (err error) } @@ -42,9 +48,10 @@ type aclList struct { indexes map[string]int id string - builder *aclStateBuilder - aclState *ACLState - keychain *common.Keychain + recBuilder ACLRecordBuilder + builder *aclStateBuilder + aclState *ACLState + keychain *common.Keychain sync.RWMutex } @@ -118,13 +125,13 @@ func build(id string, stateBuilder *aclStateBuilder, recBuilder ACLRecordBuilder } list = &aclList{ - root: aclRecRoot.Model.(*aclrecordproto.ACLRoot), - records: records, - indexes: indexes, - builder: stateBuilder, - aclState: state, - id: id, - RWMutex: sync.RWMutex{}, + root: aclRecRoot.Model.(*aclrecordproto.ACLRoot), + records: records, + indexes: indexes, + builder: stateBuilder, + recBuilder: recBuilder, + aclState: state, + id: id, } return } @@ -133,6 +140,41 @@ func (a *aclList) Records() []*ACLRecord { return a.records } +func (a *aclList) AddRawRecord(rawRec *aclrecordproto.RawACLRecordWithId) (added bool, err error) { + if _, ok := a.indexes[rawRec.Id]; ok { + return + } + record, err := a.recBuilder.ConvertFromRaw(rawRec) + if err != nil { + return + } + var lastId string + if len(a.records) > 0 { + lastId = a.records[len(a.records)-1].Id + } + if record.PrevId != lastId { + return false, ErrInconsistent + } + a.records = append(a.records, record) + a.indexes[record.Id] = len(a.records) - 1 + return true, nil +} + +func (a *aclList) IsValidNext(rawRec *aclrecordproto.RawACLRecordWithId) (err error) { + rec, err := a.recBuilder.ConvertFromRaw(rawRec) + if err != nil { + return + } + var lastId string + if len(a.records) > 0 { + lastId = a.records[len(a.records)-1].Id + } + if rec.PrevId != lastId { + return ErrInconsistent + } + return +} + func (a *aclList) ID() string { return a.id } diff --git a/common/pkg/acl/list/list_test.go b/common/pkg/acl/list/list_test.go index c4effdae..965d6221 100644 --- a/common/pkg/acl/list/list_test.go +++ b/common/pkg/acl/list/list_test.go @@ -89,3 +89,7 @@ func TestAclList_ACLState_UserJoinAndRemove(t *testing.T) { _, err = aclList.ACLState().PermissionsAtRecord(records[3].Id, idB) assert.Error(t, err, "B should have no permissions at record 3, because user should be removed") } + +func TestAclList_AddRawRecord(t *testing.T) { + +}