chore(no-torrent)_: Made torrentClientReady() belong to TorrentManager

This commit is contained in:
Samuel Hawksby-Robinson 2024-05-31 15:43:44 +01:00
parent 9b458c63e0
commit 93256fc252
5 changed files with 21 additions and 21 deletions

View File

@ -208,7 +208,7 @@ func (m *TorrentManager) StartTorrentClient() error {
return nil
}
func (m *TorrentManager) StopTorrentClient() error {
func (m *TorrentManager) Stop() error {
if m.TorrentClientStarted() {
m.StopHistoryArchiveTasksIntervals()
m.logger.Info("Stopping torrent client")
@ -225,6 +225,15 @@ func (m *TorrentManager) TorrentClientStarted() bool {
return m.torrentClient != nil
}
func (m *TorrentManager) IsReady() bool {
// Simply checking for `torrentConfig.Enabled` isn't enough
// as there's a possibility that the torrent client couldn't
// be instantiated (for example in case of port conflicts)
return m.torrentConfig != nil &&
m.torrentConfig.Enabled &&
m.TorrentClientStarted()
}
func (m *TorrentManager) GetCommunityChatsFilters(communityID types.HexBytes) ([]*transport.Filter, error) {
chatIDs, err := m.persistence.GetCommunityChatIDs(communityID)
if err != nil {

View File

@ -579,7 +579,7 @@ func NewMessenger(
ensVerifier.Stop,
pushNotificationClient.Stop,
communitiesManager.Stop,
torrentManager.StopTorrentClient,
torrentManager.Stop,
encryptionProtocol.Stop,
func() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
@ -829,7 +829,7 @@ func (m *Messenger) Start() (*MessengerResponse, error) {
return nil, err
}
if m.torrentClientReady() {
if m.torrentManager.IsReady() {
available := m.SubscribeMailserverAvailable()
go func() {
<-available
@ -918,7 +918,7 @@ func (m *Messenger) handleConnectionChange(online bool) {
}
}
// Update Communities manager
// Update torrent manager
if m.torrentManager != nil {
m.torrentManager.SetOnline(online)
}

View File

@ -1895,7 +1895,7 @@ func (m *Messenger) acceptRequestToJoinCommunity(requestToJoin *communities.Requ
Shard: community.Shard().Protobuffer(),
}
if m.torrentClientReady() && m.torrentManager.TorrentFileExists(community.IDString()) {
if m.torrentManager.IsReady() && m.torrentManager.TorrentFileExists(community.IDString()) {
magnetlink, err := m.torrentManager.GetHistoryArchiveMagnetlink(community.ID())
if err != nil {
m.logger.Warn("couldn't get magnet link for community", zap.Error(err))
@ -2804,7 +2804,7 @@ func (m *Messenger) EditCommunity(request *requests.EditCommunity) (*MessengerRe
id := community.ID()
if m.torrentClientReady() {
if m.torrentManager.IsReady() {
if !communitySettings.HistoryArchiveSupportEnabled {
m.torrentManager.StopHistoryArchiveTasksInterval(id)
} else if !m.torrentManager.IsSeedingHistoryArchiveTorrent(id) {
@ -2889,7 +2889,7 @@ func (m *Messenger) ImportCommunity(ctx context.Context, key *ecdsa.PrivateKey)
return nil, err
}
if m.torrentClientReady() {
if m.torrentManager.IsReady() {
var communities []*communities.Community
communities = append(communities, community)
go m.InitHistoryArchiveTasks(communities)
@ -4167,7 +4167,7 @@ func (m *Messenger) DisableCommunityHistoryArchiveProtocol() error {
return nil
}
m.torrentManager.StopTorrentClient()
m.torrentManager.Stop()
nodeConfig.TorrentConfig.Enabled = false
err = m.settings.SaveSetting("node-config", nodeConfig)
@ -4278,15 +4278,6 @@ func (m *Messenger) pinMessagesToWakuMessages(pinMessages []*common.PinMessage,
return wakuMessages, nil
}
func (m *Messenger) torrentClientReady() bool {
// Simply checking for `torrentConfig.Enabled` isn't enough
// as there's a possiblity that the torrent client couldn't
// be instantiated (for example in case of port conflicts)
return m.config.torrentConfig != nil &&
m.config.torrentConfig.Enabled &&
m.torrentManager.TorrentClientStarted()
}
func (m *Messenger) chatMessagesToWakuMessages(chatMessages []*common.Message, c *communities.Community) ([]*types.Message, error) {
wakuMessages := make([]*types.Message, 0)
for _, msg := range chatMessages {

View File

@ -971,7 +971,7 @@ func (m *Messenger) RequestImportDiscordChannel(request *requests.ImportDiscordC
m.logger.Error("Failed to get community settings", zap.Error(err))
continue
}
if m.torrentClientReady() && communitySettings.HistoryArchiveSupportEnabled {
if m.torrentManager.IsReady() && communitySettings.HistoryArchiveSupportEnabled {
err = m.torrentManager.SeedHistoryArchiveTorrent(request.CommunityID)
if err != nil {
@ -1737,7 +1737,7 @@ func (m *Messenger) RequestImportDiscordCommunity(request *requests.ImportDiscor
continue
}
if m.torrentClientReady() && communitySettings.HistoryArchiveSupportEnabled {
if m.torrentManager.IsReady() && communitySettings.HistoryArchiveSupportEnabled {
err = m.torrentManager.SeedHistoryArchiveTorrent(discordCommunity.ID())
if err != nil {

View File

@ -1342,7 +1342,7 @@ func (m *Messenger) HandleHistoryArchiveMagnetlinkMessage(state *ReceivedMessage
return nil
}
if m.torrentClientReady() && settings.HistoryArchiveSupportEnabled {
if m.torrentManager.IsReady() && settings.HistoryArchiveSupportEnabled {
lastClock, err := m.communitiesManager.GetMagnetlinkMessageClock(id)
if err != nil {
return err
@ -1728,7 +1728,7 @@ func (m *Messenger) HandleCommunityRequestToJoinResponse(state *ReceivedMessageS
}
magnetlink := requestToJoinResponseProto.MagnetUri
if m.torrentClientReady() && communitySettings != nil && communitySettings.HistoryArchiveSupportEnabled && magnetlink != "" {
if m.torrentManager.IsReady() && communitySettings != nil && communitySettings.HistoryArchiveSupportEnabled && magnetlink != "" {
currentTask := m.torrentManager.GetHistoryArchiveDownloadTask(community.IDString())
go func(currentTask *communities.HistoryArchiveDownloadTask) {