1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-10 18:10:49 +09:00

GO-2875 Leave space when online

This commit is contained in:
mcrakhman 2024-02-14 18:50:30 +01:00
parent 5f3fc44ea4
commit 90f27c67d2
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
5 changed files with 33 additions and 67 deletions

View file

@ -119,6 +119,18 @@ func (t *treeSyncer) StartSync() {
}
}
func (t *treeSyncer) StopSync() {
t.Lock()
defer t.Unlock()
t.isRunning = false
}
func (t *treeSyncer) ShouldSync(peerId string) bool {
t.Lock()
defer t.Unlock()
return t.isRunning
}
func (t *treeSyncer) SyncAll(ctx context.Context, peerId string, existing, missing []string) error {
t.Lock()
defer t.Unlock()

View file

@ -2,8 +2,10 @@ package core
import (
"context"
"errors"
"fmt"
"github.com/anyproto/any-sync/commonspace/object/acl/list"
"github.com/anyproto/any-sync/util/crypto"
"github.com/ipfs/go-cid"
@ -14,8 +16,12 @@ import (
)
func (mw *Middleware) SpaceDelete(cctx context.Context, req *pb.RpcSpaceDeleteRequest) *pb.RpcSpaceDeleteResponse {
spaceService := mw.applicationService.GetApp().MustComponent(space.CName).(space.Service)
err := spaceService.Delete(cctx, req.SpaceId)
spaceService := getService[space.Service](mw)
aclService := getService[acl.AclService](mw)
err := aclService.Leave(cctx, req.SpaceId)
if err == nil || errors.Is(err, list.ErrIsOwner) || errors.Is(err, list.ErrPendingRequest) {
err = spaceService.Delete(cctx, req.SpaceId)
}
code := mapErrorCode(err,
errToCode(space.ErrSpaceDeleted, pb.RpcSpaceDeleteResponseError_SPACE_IS_DELETED),
errToCode(space.ErrSpaceNotExists, pb.RpcSpaceDeleteResponseError_NO_SUCH_SPACE),

2
go.mod
View file

@ -7,7 +7,7 @@ require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/VividCortex/ewma v1.2.0
github.com/adrium/goheif v0.0.0-20230113233934-ca402e77a786
github.com/anyproto/any-sync v0.3.21-0.20240213213140-87ceaec712b7
github.com/anyproto/any-sync v0.3.21-0.20240214174758-d90797a74119
github.com/anyproto/go-naturaldate/v2 v2.0.2-0.20230524105841-9829cfd13438
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/blevesearch/bleve/v2 v2.3.10

6
go.sum
View file

@ -113,6 +113,12 @@ github.com/anyproto/any-sync v0.3.21-0.20240213165311-92be6aaf6d82 h1:NzTI67hVfN
github.com/anyproto/any-sync v0.3.21-0.20240213165311-92be6aaf6d82/go.mod h1:exw/7+W3jfkuaVWsStQD8s8aLFT9L2NBiW/DVwTNi/E=
github.com/anyproto/any-sync v0.3.21-0.20240213213140-87ceaec712b7 h1:uMEYnMUB92bn78euUItt7OKRS/CGPQ9P9U1w/DYrB14=
github.com/anyproto/any-sync v0.3.21-0.20240213213140-87ceaec712b7/go.mod h1:exw/7+W3jfkuaVWsStQD8s8aLFT9L2NBiW/DVwTNi/E=
github.com/anyproto/any-sync v0.3.21-0.20240214171603-14f6a1983593 h1:/ty9PsUxOS5HWaGIFIexA59IcqVvcHp8YyVpv8ju3mA=
github.com/anyproto/any-sync v0.3.21-0.20240214171603-14f6a1983593/go.mod h1:exw/7+W3jfkuaVWsStQD8s8aLFT9L2NBiW/DVwTNi/E=
github.com/anyproto/any-sync v0.3.21-0.20240214173759-50ae342d44f3 h1:JxgMRbPMzc2U5Jj7m7NsuVuUr7Q222UuSlRjzprM4Hs=
github.com/anyproto/any-sync v0.3.21-0.20240214173759-50ae342d44f3/go.mod h1:exw/7+W3jfkuaVWsStQD8s8aLFT9L2NBiW/DVwTNi/E=
github.com/anyproto/any-sync v0.3.21-0.20240214174758-d90797a74119 h1:5pvzG74aWs3kZL6Gfa8l/rMyCDU2mPHdjNxMaNRoNsM=
github.com/anyproto/any-sync v0.3.21-0.20240214174758-d90797a74119/go.mod h1:exw/7+W3jfkuaVWsStQD8s8aLFT9L2NBiW/DVwTNi/E=
github.com/anyproto/badger/v4 v4.2.1-0.20240110160636-80743fa3d580 h1:Ba80IlCCxkZ9H1GF+7vFu/TSpPvbpDCxXJ5ogc4euYc=
github.com/anyproto/badger/v4 v4.2.1-0.20240110160636-80743fa3d580/go.mod h1:T/uWAYxrXdaXw64ihI++9RMbKTCpKd/yE9+saARew7k=
github.com/anyproto/go-chash v0.1.0 h1:I9meTPjXFRfXZHRJzjOHC/XF7Q5vzysKkiT/grsogXY=

View file

@ -2,7 +2,6 @@ package deletioncontroller
import (
"context"
"errors"
"sync"
"time"
@ -11,8 +10,6 @@ import (
"github.com/anyproto/any-sync/app/logger"
"github.com/anyproto/any-sync/commonspace/acl/aclclient"
"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/coordinator/coordinatorclient"
"github.com/anyproto/any-sync/coordinator/coordinatorproto"
"github.com/anyproto/any-sync/util/periodicsync"
@ -89,29 +86,20 @@ func (d *deletionController) Close(ctx context.Context) error {
}
func (d *deletionController) loopIterate(ctx context.Context) error {
ownedIds, otherIds := d.updateStatuses(ctx)
ownedIds := d.updateStatuses(ctx)
d.mx.Lock()
var (
toDeleteOwnedIds []string
toLeaveOtherIds []string
)
var toDeleteOwnedIds []string
for _, id := range ownedIds {
if _, exists := d.toDelete[id]; exists {
toDeleteOwnedIds = append(toDeleteOwnedIds, id)
}
}
for _, id := range otherIds {
if _, exists := d.toDelete[id]; exists {
toLeaveOtherIds = append(toLeaveOtherIds, id)
}
}
d.mx.Unlock()
d.deleteOwnedSpaces(ctx, toDeleteOwnedIds)
d.leaveOtherSpaces(ctx, toLeaveOtherIds)
return nil
}
func (d *deletionController) updateStatuses(ctx context.Context) (ownedIds, otherIds []string) {
func (d *deletionController) updateStatuses(ctx context.Context) (ownedIds []string) {
ids := d.spaceManager.AllSpaceIds()
remoteStatuses, err := d.client.StatusCheckMany(ctx, ids)
if err != nil {
@ -133,13 +121,9 @@ func (d *deletionController) updateStatuses(ctx context.Context) (ownedIds, othe
continue
}
isOwned := false
if nodeStatus.Status == coordinatorproto.SpaceStatus_SpaceStatusCreated {
if nodeStatus.Permissions == coordinatorproto.SpacePermissions_SpacePermissionsOwner {
isOwned = true
ownedIds = append(ownedIds, ids[idx])
} else {
otherIds = append(otherIds, ids[idx])
}
if nodeStatus.Status == coordinatorproto.SpaceStatus_SpaceStatusCreated && nodeStatus.Permissions == coordinatorproto.SpacePermissions_SpacePermissionsOwner {
isOwned = true
ownedIds = append(ownedIds, ids[idx])
}
remoteStatus := convStatus(nodeStatus.Status)
err := d.spaceManager.UpdateRemoteStatus(ctx, ids[idx], remoteStatus, isOwned)
@ -162,45 +146,3 @@ func (d *deletionController) deleteOwnedSpaces(ctx context.Context, spaceIds []s
d.mx.Unlock()
}
}
func (d *deletionController) leaveOtherSpaces(ctx context.Context, spaceIds []string) {
for _, spaceId := range spaceIds {
// TODO: optimize this to cache acls not to load them every time
res, err := d.joiningClient.AclGetRecords(ctx, spaceId, "")
if err != nil {
log.Warn("acl get records error", zap.Error(err), zap.String("spaceId", spaceId))
continue
}
storage, err := liststorage.NewInMemoryAclListStorage(res[0].Id, res)
if err != nil {
log.Warn("acl storage creation error", zap.Error(err), zap.String("spaceId", spaceId))
continue
}
acl, err := list.BuildAclListWithIdentity(d.keys, storage, list.NoOpAcceptorVerifier{})
if err != nil {
log.Warn("acl list build error", zap.Error(err), zap.String("spaceId", spaceId))
continue
}
identity := d.keys.SignKey.GetPublic()
_, err = acl.AclState().Record(identity)
if acl.AclState().Permissions(identity).NoPermissions() || !errors.Is(err, list.ErrNoSuchRecord) {
d.mx.Lock()
delete(d.toDelete, spaceId)
d.mx.Unlock()
continue
}
rec, err := acl.RecordBuilder().BuildRequestRemove()
if err != nil {
log.Warn("acl record build error", zap.Error(err), zap.String("spaceId", spaceId))
continue
}
_, err = d.joiningClient.SendRecord(ctx, spaceId, rec)
if err != nil {
log.Warn("acl record send error", zap.Error(err), zap.String("spaceId", spaceId))
continue
}
d.mx.Lock()
delete(d.toDelete, spaceId)
d.mx.Unlock()
}
}