diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 26e7c6771..3c12c84c3 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -4243,34 +4243,43 @@ func (m *Messenger) startCommunityRekeyLoop() error { } for _, c := range cs { - if c.IsAdmin() && c.Encrypted() { - // if rekey time period exceeded, perform a rekey - if c.RekeyedAt().Add(rki).Before(time.Now()) { - err = m.RekeyCommunity(c.ID()) - if err != nil { - return err - } - } - - go func() { - logger.Debug("CommunityRekeyLoop started", zap.Binary("community ID", c.ID())) - for { - select { - case <-ticker.C: - err = m.RekeyCommunity(c.ID()) - if err != nil { - logger.Error("error sending rekey message", zap.Error(err), zap.Binary("community ID", c.ID())) - } - - case <-m.quit: - ticker.Stop() - logger.Debug("CommunityRekeyLoop stopped", zap.Binary("community ID", c.ID())) - return - } - } - }() + err = m.rekeyCommunity(c, rki, ticker, logger) + if err != nil { + return err } } return nil } + +func (m *Messenger) rekeyCommunity(c *communities.Community, rki time.Duration, ticker *time.Ticker, logger *zap.Logger) error { + if c.IsAdmin() && c.Encrypted() { + // if rekey time period exceeded, perform a rekey + if c.RekeyedAt().Add(rki).Before(time.Now()) { + err := m.RekeyCommunity(c.ID()) + if err != nil { + return err + } + } + + go func() { + logger.Debug("CommunityRekeyLoop started", zap.Binary("community ID", c.ID())) + for { + select { + case <-ticker.C: + err := m.RekeyCommunity(c.ID()) + if err != nil { + logger.Error("error sending rekey message", zap.Error(err), zap.Binary("community ID", c.ID())) + } + + case <-m.quit: + ticker.Stop() + logger.Debug("CommunityRekeyLoop stopped", zap.Binary("community ID", c.ID())) + return + } + } + }() + } + + return nil +}