diff --git a/protocol/errors.go b/protocol/errors.go index 00e02e2f8..76b7f5251 100644 --- a/protocol/errors.go +++ b/protocol/errors.go @@ -10,4 +10,5 @@ var ( ErrNotImplemented = errors.New("not implemented") ErrContactNotFound = errors.New("contact not found") ErrCommunityIDEmpty = errors.New("community ID is empty") + ErrUserNotMember = errors.New("user not a member") ) diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 54ff8a5d0..e34bda77b 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -3090,6 +3090,21 @@ func (m *Messenger) enableHistoryArchivesImportAfterDelay() { }() } +func (m *Messenger) checkIfIMemberOfCommunity(communityID types.HexBytes) error { + community, err := m.communitiesManager.GetByID(communityID) + if err != nil { + m.communitiesManager.LogStdout("couldn't get community to import archives", zap.Error(err)) + return err + } + + if !community.HasMember(&m.identity.PublicKey) { + m.communitiesManager.LogStdout("can't import archives when user not a member of community") + return ErrUserNotMember + } + + return nil +} + func (m *Messenger) resumeHistoryArchivesImport(communityID types.HexBytes) error { archiveIDsToImport, err := m.communitiesManager.GetMessageArchiveIDsToImport(communityID) if err != nil { @@ -3100,6 +3115,11 @@ func (m *Messenger) resumeHistoryArchivesImport(communityID types.HexBytes) erro return nil } + err = m.checkIfIMemberOfCommunity(communityID) + if err != nil { + return err + } + currentTask := m.communitiesManager.GetHistoryArchiveDownloadTask(communityID.String()) // no need to resume imports if there's already a task ongoing if currentTask != nil { @@ -3161,7 +3181,10 @@ importMessageArchivesLoop: m.communitiesManager.LogStdout("interrupted importing history archive messages") return nil case <-importTicker.C: - + err := m.checkIfIMemberOfCommunity(communityID) + if err != nil { + break importMessageArchivesLoop + } archiveIDsToImport, err := m.communitiesManager.GetMessageArchiveIDsToImport(communityID) if err != nil { m.communitiesManager.LogStdout("couldn't get message archive IDs to import", zap.Error(err)) diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index 433310c5b..eef652294 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -1306,6 +1306,11 @@ func (m *Messenger) downloadAndImportHistoryArchives(id types.HexBytes, magnetli m.communitiesManager.LogStdout("couldn't update last seen magnetlink", zap.Error(err)) } + err = m.checkIfIMemberOfCommunity(id) + if err != nil { + return + } + err = m.importHistoryArchives(id, cancel) if err != nil { m.communitiesManager.LogStdout("failed to import history archives", zap.Error(err))