From d755864d0d280a16d8ae6d10f3e1aeb44dd8d06b Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Wed, 10 Jan 2024 19:45:50 +0100 Subject: [PATCH] Change request join method --- .../acl/aclclient/aclinvitingclient.go | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/commonspace/acl/aclclient/aclinvitingclient.go b/commonspace/acl/aclclient/aclinvitingclient.go index 30940d19..498109d0 100644 --- a/commonspace/acl/aclclient/aclinvitingclient.go +++ b/commonspace/acl/aclclient/aclinvitingclient.go @@ -2,11 +2,15 @@ package aclclient import ( "context" + "fmt" "storj.io/drpc" + "github.com/anyproto/any-sync/accountservice" "github.com/anyproto/any-sync/app" + "github.com/anyproto/any-sync/commonspace/object/accountdata" "github.com/anyproto/any-sync/commonspace/object/acl/list" + "github.com/anyproto/any-sync/commonspace/object/acl/liststorage" "github.com/anyproto/any-sync/commonspace/spacesyncproto" "github.com/anyproto/any-sync/consensus/consensusproto" "github.com/anyproto/any-sync/net/pool" @@ -18,12 +22,13 @@ const CName = "common.acl.aclclient" type AclInvitingClient interface { app.Component AclGetRecords(ctx context.Context, spaceId, aclHead string) ([]*consensusproto.RawRecordWithId, error) - RequestJoin(ctx context.Context, spaceId string, acl list.AclList, payload list.RequestJoinPayload) error + RequestJoin(ctx context.Context, spaceId string, payload list.RequestJoinPayload) error } type aclInvitingClient struct { nodeConf nodeconf.Service pool pool.Pool + keys *accountdata.AccountKeys } func NewAclInvitingClient() AclInvitingClient { @@ -37,6 +42,7 @@ func (c *aclInvitingClient) Name() (name string) { func (c *aclInvitingClient) Init(a *app.App) (err error) { c.pool = a.MustComponent(pool.CName).(pool.Pool) c.nodeConf = a.MustComponent(nodeconf.CName).(nodeconf.Service) + c.keys = a.MustComponent(accountservice.CName).(accountservice.Service).Account() return nil } @@ -64,15 +70,33 @@ func (c *aclInvitingClient) AclGetRecords(ctx context.Context, spaceId, aclHead return } -func (c *aclInvitingClient) RequestJoin(ctx context.Context, spaceId string, acl list.AclList, payload list.RequestJoinPayload) (err error) { - acl.RLock() - res, err := acl.RecordBuilder().BuildRequestJoin(payload) +func (c *aclInvitingClient) RequestJoin(ctx context.Context, spaceId string, payload list.RequestJoinPayload) (err error) { + res, err := c.AclGetRecords(ctx, spaceId, "") + if err != nil { + return err + } + if len(res) == 0 { + return fmt.Errorf("acl not found") + } + storage, err := liststorage.NewInMemoryAclListStorage(res[0].Id, res) + if err != nil { + return err + } + acl, err := list.BuildAclListWithIdentity(c.keys, storage, list.NoOpAcceptorVerifier{}) + if err != nil { + return err + } + pubIdentity := payload.InviteKey.GetPublic() + for _, rec := range acl.AclState().JoinRecords() { + if rec.RequestIdentity.Equals(pubIdentity) { + return fmt.Errorf("request already sent") + } + } + rec, err := acl.RecordBuilder().BuildRequestJoin(payload) if err != nil { - acl.RUnlock() return } - acl.RUnlock() - data, err := res.Marshal() + data, err := rec.Marshal() if err != nil { return }