fix(archives): Skip importing the archives for community when user not a member (#4006)

This commit is contained in:
Boris Melnik 2023-09-15 10:42:28 +03:00 committed by GitHub
parent 3bacb84a02
commit 5e8300d6a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 1 deletions

View File

@ -10,4 +10,5 @@ var (
ErrNotImplemented = errors.New("not implemented") ErrNotImplemented = errors.New("not implemented")
ErrContactNotFound = errors.New("contact not found") ErrContactNotFound = errors.New("contact not found")
ErrCommunityIDEmpty = errors.New("community ID is empty") ErrCommunityIDEmpty = errors.New("community ID is empty")
ErrUserNotMember = errors.New("user not a member")
) )

View File

@ -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 { func (m *Messenger) resumeHistoryArchivesImport(communityID types.HexBytes) error {
archiveIDsToImport, err := m.communitiesManager.GetMessageArchiveIDsToImport(communityID) archiveIDsToImport, err := m.communitiesManager.GetMessageArchiveIDsToImport(communityID)
if err != nil { if err != nil {
@ -3100,6 +3115,11 @@ func (m *Messenger) resumeHistoryArchivesImport(communityID types.HexBytes) erro
return nil return nil
} }
err = m.checkIfIMemberOfCommunity(communityID)
if err != nil {
return err
}
currentTask := m.communitiesManager.GetHistoryArchiveDownloadTask(communityID.String()) currentTask := m.communitiesManager.GetHistoryArchiveDownloadTask(communityID.String())
// no need to resume imports if there's already a task ongoing // no need to resume imports if there's already a task ongoing
if currentTask != nil { if currentTask != nil {
@ -3161,7 +3181,10 @@ importMessageArchivesLoop:
m.communitiesManager.LogStdout("interrupted importing history archive messages") m.communitiesManager.LogStdout("interrupted importing history archive messages")
return nil return nil
case <-importTicker.C: case <-importTicker.C:
err := m.checkIfIMemberOfCommunity(communityID)
if err != nil {
break importMessageArchivesLoop
}
archiveIDsToImport, err := m.communitiesManager.GetMessageArchiveIDsToImport(communityID) archiveIDsToImport, err := m.communitiesManager.GetMessageArchiveIDsToImport(communityID)
if err != nil { if err != nil {
m.communitiesManager.LogStdout("couldn't get message archive IDs to import", zap.Error(err)) m.communitiesManager.LogStdout("couldn't get message archive IDs to import", zap.Error(err))

View File

@ -1306,6 +1306,11 @@ func (m *Messenger) downloadAndImportHistoryArchives(id types.HexBytes, magnetli
m.communitiesManager.LogStdout("couldn't update last seen magnetlink", zap.Error(err)) 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) err = m.importHistoryArchives(id, cancel)
if err != nil { if err != nil {
m.communitiesManager.LogStdout("failed to import history archives", zap.Error(err)) m.communitiesManager.LogStdout("failed to import history archives", zap.Error(err))