mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-09 09:35:03 +09:00
Merge pull request #178 from anyproto/GO-2990-cancel-join
GO-2990: Cancel join
This commit is contained in:
commit
7108554910
2 changed files with 64 additions and 28 deletions
|
@ -18,7 +18,8 @@ const CName = "common.acl.aclclient"
|
|||
type AclJoiningClient interface {
|
||||
app.Component
|
||||
AclGetRecords(ctx context.Context, spaceId, aclHead string) ([]*consensusproto.RawRecordWithId, error)
|
||||
RequestJoin(ctx context.Context, spaceId string, payload list.RequestJoinPayload) error
|
||||
RequestJoin(ctx context.Context, spaceId string, payload list.RequestJoinPayload) (aclHeadId string, err error)
|
||||
CancelJoin(ctx context.Context, spaceId string) (err error)
|
||||
CancelRemoveSelf(ctx context.Context, spaceId string) (err error)
|
||||
}
|
||||
|
||||
|
@ -45,55 +46,70 @@ func (c *aclJoiningClient) AclGetRecords(ctx context.Context, spaceId, aclHead s
|
|||
return c.coordinatorClient.AclGetRecords(ctx, spaceId, aclHead)
|
||||
}
|
||||
|
||||
func (c *aclJoiningClient) RequestJoin(ctx context.Context, spaceId string, payload list.RequestJoinPayload) (err error) {
|
||||
func (c *aclJoiningClient) getAcl(ctx context.Context, spaceId string) (l list.AclList, err error) {
|
||||
res, err := c.AclGetRecords(ctx, spaceId, "")
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
if len(res) == 0 {
|
||||
return fmt.Errorf("acl not found")
|
||||
err = fmt.Errorf("acl not found")
|
||||
return
|
||||
}
|
||||
storage, err := liststorage.NewInMemoryAclListStorage(res[0].Id, res)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
acl, err := list.BuildAclListWithIdentity(c.keys, storage, list.NoOpAcceptorVerifier{})
|
||||
return list.BuildAclListWithIdentity(c.keys, storage, list.NoOpAcceptorVerifier{})
|
||||
}
|
||||
|
||||
func (c *aclJoiningClient) CancelJoin(ctx context.Context, spaceId string) (err error) {
|
||||
acl, err := c.getAcl(ctx, spaceId)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
pendingReq, err := acl.AclState().JoinRecord(acl.AclState().Identity(), false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res, err := acl.RecordBuilder().BuildRequestCancel(pendingReq.RecordId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.coordinatorClient.AclAddRecord(ctx, spaceId, res)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *aclJoiningClient) RequestJoin(ctx context.Context, spaceId string, payload list.RequestJoinPayload) (aclHeadId string, err error) {
|
||||
acl, err := c.getAcl(ctx, spaceId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
joinRecs, err := acl.AclState().JoinRecords(false)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
for _, rec := range joinRecs {
|
||||
if rec.RequestIdentity.Equals(c.keys.SignKey.GetPublic()) {
|
||||
// that means that we already requested to join
|
||||
return nil
|
||||
return
|
||||
}
|
||||
}
|
||||
rec, err := acl.RecordBuilder().BuildRequestJoin(payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.coordinatorClient.AclAddRecord(ctx, spaceId, rec)
|
||||
recWithId, err := c.coordinatorClient.AclAddRecord(ctx, spaceId, rec)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
aclHeadId = recWithId.Id
|
||||
return
|
||||
}
|
||||
|
||||
func (c *aclJoiningClient) CancelRemoveSelf(ctx context.Context, spaceId string) (err error) {
|
||||
res, err := c.AclGetRecords(ctx, spaceId, "")
|
||||
acl, err := c.getAcl(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
|
||||
return
|
||||
}
|
||||
pendingReq, err := acl.AclState().Record(acl.AclState().Identity())
|
||||
if err != nil {
|
||||
|
|
|
@ -39,16 +39,20 @@ type aclWaiter struct {
|
|||
|
||||
acl list.AclList
|
||||
spaceId string
|
||||
aclHeadId string
|
||||
prevHeadId string
|
||||
|
||||
onFinish func(acl list.AclList) error
|
||||
onReject func(acl list.AclList) error
|
||||
finished bool
|
||||
}
|
||||
|
||||
func New(spaceId string, onFinish func(acl list.AclList) error) AclWaiter {
|
||||
func New(spaceId string, aclHeadId string, onFinish, onReject func(acl list.AclList) error) AclWaiter {
|
||||
return &aclWaiter{
|
||||
spaceId: spaceId,
|
||||
onFinish: onFinish,
|
||||
spaceId: spaceId,
|
||||
aclHeadId: aclHeadId,
|
||||
onFinish: onFinish,
|
||||
onReject: onReject,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,6 +68,9 @@ func (a *aclWaiter) Name() (name string) {
|
|||
}
|
||||
|
||||
func (a *aclWaiter) loop(ctx context.Context) error {
|
||||
if a.finished {
|
||||
return nil
|
||||
}
|
||||
if a.acl == nil {
|
||||
res, err := a.client.AclGetRecords(ctx, a.spaceId, "")
|
||||
if err != nil {
|
||||
|
@ -100,8 +107,21 @@ func (a *aclWaiter) loop(ctx context.Context) error {
|
|||
}
|
||||
// if the user was added
|
||||
if !a.acl.AclState().Permissions(a.keys.SignKey.GetPublic()).NoPermissions() {
|
||||
if !a.finished {
|
||||
err := a.onFinish(a.acl)
|
||||
err := a.onFinish(a.acl)
|
||||
if err == nil {
|
||||
a.finished = true
|
||||
return nil
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// check the join record, if it exists
|
||||
_, err := a.acl.AclState().JoinRecord(a.acl.AclState().Identity(), false)
|
||||
if err != nil {
|
||||
_, err = a.acl.Get(a.aclHeadId)
|
||||
// that means the request was declined
|
||||
if err == nil {
|
||||
err := a.onReject(a.acl)
|
||||
if err == nil {
|
||||
a.finished = true
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue