acl sync handler [wip]
This commit is contained in:
parent
8d7eaf665c
commit
b0b4e5b721
31
common/commonspace/syncacl/syncaclhandler.go
Normal file
31
common/commonspace/syncacl/syncaclhandler.go
Normal file
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user