From 1be465d23c1d486dea26076b01e8d17ec4a08cc5 Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Fri, 7 Jun 2024 10:27:32 +0100 Subject: [PATCH] chore(no-torrent)_: Refactored NewArchiveManager to use config pattern --- protocol/communities/manager.go | 10 ++++++++++ protocol/communities/manager_archive.go | 19 +++++++++---------- protocol/communities/manager_archive_file.go | 14 +++++++------- protocol/communities/manager_archive_nop.go | 6 +----- protocol/communities/manager_test.go | 17 ++++++++++++----- protocol/messenger.go | 16 +++++++++++++--- 6 files changed, 52 insertions(+), 30 deletions(-) diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 4ad6f39c9..1f22784df 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -198,6 +198,16 @@ type ArchiveService interface { TorrentFileExists(communityID string) bool } +type ArchiveManagerConfig struct { + TorrentConfig *params.TorrentConfig + Logger *zap.Logger + Persistence *Persistence + Transport *transport.Transport + Identity *ecdsa.PrivateKey + Encryptor *encryption.Protocol + Publisher Publisher +} + func (t *HistoryArchiveDownloadTask) IsCancelled() bool { t.m.RLock() defer t.m.RUnlock() diff --git a/protocol/communities/manager_archive.go b/protocol/communities/manager_archive.go index 13e074d53..5d35ad2b9 100644 --- a/protocol/communities/manager_archive.go +++ b/protocol/communities/manager_archive.go @@ -77,21 +77,20 @@ type ArchiveManager struct { // In this case this version of NewArchiveManager will return the full Desktop ArchiveManager ensuring that the // build command will import and build the torrent deps for the Desktop OSes. // NOTE: It is intentional that this file contains the identical function name as in "manager_archive_nop.go" -func NewArchiveManager(torrentConfig *params.TorrentConfig, logger *zap.Logger, persistence *Persistence, transport *transport.Transport, identity *ecdsa.PrivateKey, encryptor *encryption.Protocol, publisher Publisher) *ArchiveManager { +func NewArchiveManager(amc *ArchiveManagerConfig) *ArchiveManager { return &ArchiveManager{ - torrentConfig: torrentConfig, + torrentConfig: amc.TorrentConfig, torrentTasks: make(map[string]metainfo.Hash), historyArchiveDownloadTasks: make(map[string]*HistoryArchiveDownloadTask), - logger: logger, + logger: amc.Logger, + persistence: amc.Persistence, + transport: amc.Transport, + identity: amc.Identity, + encryptor: amc.Encryptor, - persistence: persistence, - transport: transport, - identity: identity, - encryptor: encryptor, - - publisher: publisher, - ArchiveFileManager: NewArchiveFileManager(torrentConfig, logger, persistence, identity, encryptor, publisher), + publisher: amc.Publisher, + ArchiveFileManager: NewArchiveFileManager(amc), } } diff --git a/protocol/communities/manager_archive_file.go b/protocol/communities/manager_archive_file.go index 07348108f..802d44d49 100644 --- a/protocol/communities/manager_archive_file.go +++ b/protocol/communities/manager_archive_file.go @@ -40,14 +40,14 @@ type ArchiveFileManager struct { publisher Publisher } -func NewArchiveFileManager(torrentConfig *params.TorrentConfig, logger *zap.Logger, persistence *Persistence, identity *ecdsa.PrivateKey, encryptor *encryption.Protocol, publisher Publisher) *ArchiveFileManager { +func NewArchiveFileManager(amc *ArchiveManagerConfig) *ArchiveFileManager { return &ArchiveFileManager{ - torrentConfig: torrentConfig, - logger: logger, - persistence: persistence, - identity: identity, - encryptor: encryptor, - publisher: publisher, + torrentConfig: amc.TorrentConfig, + logger: amc.Logger, + persistence: amc.Persistence, + identity: amc.Identity, + encryptor: amc.Encryptor, + publisher: amc.Publisher, } } diff --git a/protocol/communities/manager_archive_nop.go b/protocol/communities/manager_archive_nop.go index 30875530f..c9ee80bcb 100644 --- a/protocol/communities/manager_archive_nop.go +++ b/protocol/communities/manager_archive_nop.go @@ -4,15 +4,11 @@ package communities import ( - "crypto/ecdsa" "time" "github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/params" - "github.com/status-im/status-go/protocol/encryption" "github.com/status-im/status-go/protocol/transport" - - "go.uber.org/zap" ) type ArchiveManagerNop struct { @@ -23,7 +19,7 @@ type ArchiveManagerNop struct { // In this case this version of NewArchiveManager will return the mobile "nil" ArchiveManagerNop ensuring that the // build command will not import or build the torrent deps for the mobile OS. // NOTE: It is intentional that this file contains the identical function name as in "manager_archive.go" -func NewArchiveManager(torrentConfig *params.TorrentConfig, logger *zap.Logger, persistence *Persistence, transport *transport.Transport, identity *ecdsa.PrivateKey, encryptor *encryption.Protocol, publisher Publisher) *ArchiveManagerNop { +func NewArchiveManager(amc *ArchiveManagerConfig) *ArchiveManagerNop { return &ArchiveManagerNop{ &ArchiveFileManagerNop{}, } diff --git a/protocol/communities/manager_test.go b/protocol/communities/manager_test.go index a77b0f476..95076d6b2 100644 --- a/protocol/communities/manager_test.go +++ b/protocol/communities/manager_test.go @@ -64,8 +64,16 @@ func (s *ManagerSuite) buildManagers(ownerVerifier OwnerVerifier) (*Manager, *Ar s.Require().NoError(err) s.Require().NoError(m.Start()) - tc := buildTorrentConfig() - t := NewArchiveManager(&tc, logger, m.GetPersistence(), nil, key, nil, m) + amc := &ArchiveManagerConfig{ + TorrentConfig: buildTorrentConfig(), + Logger: logger, + Persistence: m.GetPersistence(), + Transport: nil, + Identity: key, + Encryptor: nil, + Publisher: m, + } + t := NewArchiveManager(amc) s.Require().NoError(err) return m, t @@ -1567,14 +1575,13 @@ func (s *ManagerSuite) TestCheckAllChannelsPermissions() { s.Require().Len(response.Channels[chatID2].ViewOnlyPermissions.Permissions, 0) } -func buildTorrentConfig() params.TorrentConfig { - torrentConfig := params.TorrentConfig{ +func buildTorrentConfig() *params.TorrentConfig { + return ¶ms.TorrentConfig{ Enabled: true, DataDir: os.TempDir() + "/archivedata", TorrentDir: os.TempDir() + "/torrents", Port: 0, } - return torrentConfig } func buildMessage(timestamp time.Time, topic types.TopicType, hash []byte) types.Message { diff --git a/protocol/messenger.go b/protocol/messenger.go index 9f4fac2dd..232610ef0 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -498,10 +498,20 @@ func NewMessenger( return nil, err } + amc := &communities.ArchiveManagerConfig{ + TorrentConfig: c.torrentConfig, + Logger: logger, + Persistence: communitiesManager.GetPersistence(), + Transport: transp, + Identity: identity, + Encryptor: encryptionProtocol, + Publisher: communitiesManager, + } + // Depending on the OS go will choose whether to use the "communities/manager_archive_nop.go" or // "communities/manager_archive.go" version of this function based on the build instructions for those files. // See those file for more details. - torrentManager := communities.NewArchiveManager(c.torrentConfig, logger, communitiesManager.GetPersistence(), transp, identity, encryptionProtocol, communitiesManager) + archiveManager := communities.NewArchiveManager(amc) if err != nil { return nil, err } @@ -536,7 +546,7 @@ func NewMessenger( pushNotificationServer: pushNotificationServer, communitiesManager: communitiesManager, communitiesKeyDistributor: communitiesKeyDistributor, - archiveManager: torrentManager, + archiveManager: archiveManager, accountsManager: c.accountsManager, ensVerifier: ensVerifier, featureFlags: c.featureFlags, @@ -582,7 +592,7 @@ func NewMessenger( ensVerifier.Stop, pushNotificationClient.Stop, communitiesManager.Stop, - torrentManager.Stop, + archiveManager.Stop, encryptionProtocol.Stop, func() error { ctx, cancel := context.WithTimeout(context.Background(), time.Second)