diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 95c86f16f..4c662c041 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -444,15 +444,18 @@ func (m *Manager) Start() error { if m.ownerVerifier != nil { m.runOwnerVerificationLoop() } + return nil +} - if m.torrentConfig != nil && m.torrentConfig.Enabled { - err := m.StartTorrentClient() - if err != nil { - m.LogStdout("couldn't start torrent client", zap.Error(err)) +func (m *Manager) SetOnline(online bool) { + if online { + if m.torrentConfig != nil && m.torrentConfig.Enabled && !m.TorrentClientStarted() { + err := m.StartTorrentClient() + if err != nil { + m.LogStdout("couldn't start torrent client", zap.Error(err)) + } } } - - return nil } func (m *Manager) runENSVerificationLoop() { diff --git a/protocol/communities/manager_test.go b/protocol/communities/manager_test.go index 47ad8a38b..9625ddd91 100644 --- a/protocol/communities/manager_test.go +++ b/protocol/communities/manager_test.go @@ -501,6 +501,16 @@ func (s *ManagerSuite) TestStopTorrentClient_ShouldStopHistoryArchiveTasks() { s.Require().Equal(count, 0) } +func (s *ManagerSuite) TestStartTorrentClient_DelayedUntilOnline() { + torrentConfig := buildTorrentConfig() + s.manager.SetTorrentConfig(&torrentConfig) + + s.Require().False(s.manager.TorrentClientStarted()) + + s.manager.SetOnline(true) + s.Require().True(s.manager.TorrentClientStarted()) +} + func (s *ManagerSuite) TestCreateHistoryArchiveTorrent_WithoutMessages() { torrentConfig := buildTorrentConfig() diff --git a/protocol/messenger.go b/protocol/messenger.go index cd63be97c..f3f0b8e7c 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -899,6 +899,11 @@ func (m *Messenger) handleConnectionChange(online bool) { } } + // Update Communities manager + if m.communitiesManager != nil { + m.communitiesManager.SetOnline(online) + } + // Publish contact code if online && m.shouldPublishContactCode { if err := m.publishContactCode(); err != nil {