chore(no-torrent)_: Resolved TorrentManager dep injection

I'm not particularly proud of this work. I've just passed in all the deps as vars, there are way too many concerns handled by the TorrentManager that I don't know what is the best approach to removing them. I've even resorted to declaring an 'Publisher' interface to handle all the 'publish()' calls the TorrentManager makes. Next up I need to resolved the communities Manager API, because I've removed all of its end points. The TorrentManager needs to be a lot more simple than it is.
This commit is contained in:
Samuel Hawksby-Robinson 2024-05-30 16:04:47 +01:00
parent c084ca4230
commit 6c52d0b120
2 changed files with 34 additions and 13 deletions

View File

@ -45,6 +45,10 @@ import (
"github.com/status-im/status-go/transactions"
)
type Publisher interface {
publish(subscription *Subscription)
}
var defaultAnnounceList = [][]string{
{"udp://tracker.opentrackr.org:1337/announce"},
{"udp://tracker.openbittorrent.com:6969/announce"},
@ -350,15 +354,15 @@ func NewManager(identity *ecdsa.PrivateKey, installationID string, db *sql.DB, e
quit: make(chan struct{}),
transport: transport,
timesource: timesource,
torrentManager: NewTorrentManager(torrentConfig, logger, stdoutLogger),
keyDistributor: keyDistributor,
communityLock: NewCommunityLock(logger),
}
manager.persistence = &Persistence{
persistence := &Persistence{
db: db,
recordBundleToCommunity: manager.dbRecordBundleToCommunity,
}
manager.persistence = persistence
if managerConfig.accountsManager != nil {
manager.accountsManager = managerConfig.accountsManager
@ -403,6 +407,8 @@ func NewManager(identity *ecdsa.PrivateKey, installationID string, db *sql.DB, e
manager.forceMembersReevaluation = make(map[string]chan struct{}, 10)
}
manager.torrentManager = NewTorrentManager(torrentConfig, logger, stdoutLogger, persistence, transport, identity, encryptor, manager)
return manager, nil
}

View File

@ -67,15 +67,30 @@ type TorrentManager struct {
logger *zap.Logger
stdoutLogger *zap.Logger
persistence *Persistence
transport *transport.Transport
identity *ecdsa.PrivateKey
encryptor *encryption.Protocol
publisher Publisher
}
func NewTorrentManager(torrentConfig *params.TorrentConfig, logger, stdoutLogger *zap.Logger) *TorrentManager {
func NewTorrentManager(torrentConfig *params.TorrentConfig, logger, stdoutLogger *zap.Logger, persistence *Persistence, transport *transport.Transport, identity *ecdsa.PrivateKey, encryptor *encryption.Protocol, publisher Publisher) *TorrentManager {
return &TorrentManager{
torrentConfig: torrentConfig,
torrentTasks: make(map[string]metainfo.Hash),
historyArchiveDownloadTasks: make(map[string]*HistoryArchiveDownloadTask),
logger: logger,
stdoutLogger: stdoutLogger,
logger: logger,
stdoutLogger: stdoutLogger,
persistence: persistence,
transport: transport,
identity: identity,
encryptor: encryptor,
publisher: publisher,
}
}
@ -429,7 +444,7 @@ func (m *TorrentManager) CreateHistoryArchiveTorrent(communityID types.HexBytes,
var encodedArchives []*EncodedArchiveData
topicsAsByteArrays := topicsAsByteArrays(topics)
m.publish(&Subscription{CreatingHistoryArchivesSignal: &signal.CreatingHistoryArchivesSignal{
m.publisher.publish(&Subscription{CreatingHistoryArchivesSignal: &signal.CreatingHistoryArchivesSignal{
CommunityID: communityID.String(),
}})
@ -629,7 +644,7 @@ func (m *TorrentManager) CreateHistoryArchiveTorrent(communityID types.HexBytes,
m.LogStdout("torrent created", zap.Any("from", startDate.Unix()), zap.Any("to", endDate.Unix()))
m.publish(&Subscription{
m.publisher.publish(&Subscription{
HistoryArchivesCreatedSignal: &signal.HistoryArchivesCreatedSignal{
CommunityID: communityID.String(),
From: int(startDate.Unix()),
@ -638,7 +653,7 @@ func (m *TorrentManager) CreateHistoryArchiveTorrent(communityID types.HexBytes,
})
} else {
m.LogStdout("no archives created")
m.publish(&Subscription{
m.publisher.publish(&Subscription{
NoHistoryArchivesCreatedSignal: &signal.NoHistoryArchivesCreatedSignal{
CommunityID: communityID.String(),
From: int(startDate.Unix()),
@ -693,7 +708,7 @@ func (m *TorrentManager) SeedHistoryArchiveTorrent(communityID types.HexBytes) e
torrent.DownloadAll()
m.publish(&Subscription{
m.publisher.publish(&Subscription{
HistoryArchivesSeedingSignal: &signal.HistoryArchivesSeedingSignal{
CommunityID: communityID.String(),
},
@ -717,7 +732,7 @@ func (m *TorrentManager) UnseedHistoryArchiveTorrent(communityID types.HexBytes)
torrent.Drop()
delete(m.torrentTasks, id)
m.publish(&Subscription{
m.publisher.publish(&Subscription{
HistoryArchivesUnseededSignal: &signal.HistoryArchivesUnseededSignal{
CommunityID: id,
},
@ -829,7 +844,7 @@ func (m *TorrentManager) DownloadHistoryArchivesByMagnetlink(communityID types.H
sort.Sort(sort.Reverse(archiveHashes))
m.publish(&Subscription{
m.publisher.publish(&Subscription{
DownloadingHistoryArchivesStartedSignal: &signal.DownloadingHistoryArchivesStartedSignal{
CommunityID: communityID.String(),
},
@ -895,7 +910,7 @@ func (m *TorrentManager) DownloadHistoryArchivesByMagnetlink(communityID types.H
m.LogStdout("couldn't save message archive ID", zap.Error(err))
continue
}
m.publish(&Subscription{
m.publisher.publish(&Subscription{
HistoryArchiveDownloadedSignal: &signal.HistoryArchiveDownloadedSignal{
CommunityID: communityID.String(),
From: int(metadata.Metadata.From),
@ -903,7 +918,7 @@ func (m *TorrentManager) DownloadHistoryArchivesByMagnetlink(communityID types.H
},
})
}
m.publish(&Subscription{
m.publisher.publish(&Subscription{
HistoryArchivesSeedingSignal: &signal.HistoryArchivesSeedingSignal{
CommunityID: communityID.String(),
},