1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 17:44:59 +09:00

GO-5297: Refactor and fix tests

This commit is contained in:
Sergey 2025-04-25 18:00:26 +02:00
parent 4a922d253c
commit 775ecc005b
No known key found for this signature in database
GPG key ID: 3B6BEF79160221C6
8 changed files with 114 additions and 70 deletions

View file

@ -2,7 +2,6 @@ package aclobjectmanager
import (
"context"
"encoding/base64"
"fmt"
"slices"
"sync"
@ -11,7 +10,6 @@ import (
"github.com/anyproto/any-sync/app"
"github.com/anyproto/any-sync/app/debugstat"
"github.com/anyproto/any-sync/app/logger"
"github.com/anyproto/any-sync/commonspace"
"github.com/anyproto/any-sync/commonspace/object/acl/list"
"github.com/anyproto/any-sync/commonspace/object/acl/syncacl"
"github.com/anyproto/any-sync/util/crypto"
@ -19,15 +17,12 @@ import (
"go.uber.org/zap"
"github.com/anyproto/anytype-heart/core/block/chats/chatpush"
"github.com/anyproto/anytype-heart/core/event"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/space/clientspace"
"github.com/anyproto/anytype-heart/space/internal/components/aclnotifications"
"github.com/anyproto/anytype-heart/space/internal/components/invitemigrator"
"github.com/anyproto/anytype-heart/space/internal/components/participantwatcher"
"github.com/anyproto/anytype-heart/space/internal/components/spaceloader"
"github.com/anyproto/anytype-heart/space/internal/components/spacestatus"
"github.com/anyproto/anytype-heart/space/spacecore/spacekey"
"github.com/anyproto/anytype-heart/space/spaceinfo"
)
@ -48,6 +43,7 @@ func New(ownerMetadata []byte, guestKey crypto.PrivKey) AclObjectManager {
type pushNotificationService interface {
SubscribeToTopics(ctx context.Context, spaceId string, topics []string)
BroadcastKeyUpdate(spaceId string, aclState *list.AclState) error
}
type aclObjectManager struct {
@ -72,7 +68,6 @@ type aclObjectManager struct {
lastIndexed string
guestKey crypto.PrivKey
mx sync.Mutex
eventSender event.Sender
}
type SpaceLoaderListener interface {
@ -123,7 +118,6 @@ func (a *aclObjectManager) Init(ap *app.App) (err error) {
a.waitLoad = make(chan struct{})
a.wait = make(chan struct{})
a.pushNotificationService = app.MustComponent[pushNotificationService](ap)
a.eventSender = app.MustComponent[event.Sender](ap)
return nil
}
@ -271,43 +265,11 @@ func (a *aclObjectManager) processAcl() (err error) {
a.mx.Lock()
defer a.mx.Unlock()
a.lastIndexed = acl.Head().Id
return a.broadcastKeyUpdate(aclState, common)
}
func (a *aclObjectManager) broadcastKeyUpdate(aclState *list.AclState, common commonspace.Space) error {
firstMetadataKey, err := aclState.FirstMetadataKey()
err = a.pushNotificationService.BroadcastKeyUpdate(common.Id(), aclState)
if err != nil {
return err
return fmt.Errorf("broadcast key update: %w", err)
}
readKey, err := aclState.CurrentReadKey()
if err != nil {
return err
}
spaceKeyId, _, err := spacekey.DeriveSpaceKey(firstMetadataKey)
if err != nil {
return err
}
encryptionKey, err := spacekey.DeriveSymmetricKey(readKey)
if err != nil {
return err
}
raw, err := encryptionKey.Raw()
if err != nil {
return err
}
encodedKey := base64.StdEncoding.EncodeToString(raw)
a.eventSender.Broadcast(&pb.Event{
Messages: []*pb.EventMessage{
{
SpaceId: common.Id(),
Value: &pb.EventMessageValueOfKeyUpdate{KeyUpdate: &pb.EventKeyUpdate{
SpaceKeyId: spaceKeyId,
EncryptionKeyId: aclState.CurrentReadKeyId(),
EncryptionKey: encodedKey,
}},
},
},
})
return nil
}

View file

@ -33,6 +33,25 @@ import (
"github.com/anyproto/anytype-heart/tests/testutil"
)
type pushNotificationServiceDummy struct {
}
func (s *pushNotificationServiceDummy) Init(a *app.App) (err error) {
return nil
}
func (s *pushNotificationServiceDummy) Name() (name string) {
return "pushNotificationServiceDummy"
}
func (s *pushNotificationServiceDummy) SubscribeToTopics(ctx context.Context, spaceId string, topics []string) {
}
func (s pushNotificationServiceDummy) BroadcastKeyUpdate(spaceId string, aclState *list.AclState) error {
return nil
}
func TestAclObjectManager(t *testing.T) {
t.Run("owner", func(t *testing.T) {
a := list.NewAclExecutor("spaceId")
@ -205,7 +224,8 @@ func newFixture(t *testing.T) *fixture {
Register(testutil.PrepareMock(ctx, fx.a, fx.mockAclNotification)).
Register(testutil.PrepareMock(ctx, fx.a, fx.mockAccountService)).
Register(fx.spaceLoaderListener).
Register(fx)
Register(fx).
Register(&pushNotificationServiceDummy{})
return fx
}