refactor: unify `Owned()` and `ControlledCommunities()`

This commit is contained in:
Patryk Osmaczko 2023-10-20 00:06:09 +02:00 committed by osmaczko
parent 345851c396
commit bd245ab45b
3 changed files with 17 additions and 29 deletions

View File

@ -723,37 +723,25 @@ func (m *Manager) DeletedCommunities() ([]*Community, error) {
return communities, nil
}
func (m *Manager) ControlledCommunities() ([]*Community, error) {
func (m *Manager) Controlled() ([]*Community, error) {
communities, err := m.persistence.CommunitiesWithPrivateKey(&m.identity.PublicKey, m.installationID)
if err != nil {
return nil, err
}
controlled := make([]*Community, 0, len(communities))
for _, c := range communities {
err = initializeCommunity(c)
if err != nil {
return nil, err
if c.IsControlNode() {
err = initializeCommunity(c)
if err != nil {
return nil, err
}
controlled = append(controlled, c)
}
}
return communities, nil
}
func (m *Manager) Owned() ([]*Community, error) {
communities, err := m.persistence.CommunitiesWithPrivateKey(&m.identity.PublicKey, m.installationID)
if err != nil {
return nil, err
}
var ownedCommunities []*Community
for _, community := range communities {
if community.IsControlNode() {
ownedCommunities = append(ownedCommunities, community)
}
}
return ownedCommunities, nil
return controlled, nil
}
// CreateCommunity takes a description, generates an ID for it, saves it and return it
@ -3676,7 +3664,7 @@ func (m *Manager) UpdateCommunitySettings(settings CommunitySettings) error {
}
func (m *Manager) GetOwnedCommunitiesChatIDs() (map[string]bool, error) {
ownedCommunities, err := m.Owned()
ownedCommunities, err := m.Controlled()
if err != nil {
return nil, err
}

View File

@ -836,7 +836,7 @@ func (m *Messenger) Start() (*MessengerResponse, error) {
}
if m.torrentClientReady() {
controlledCommunities, err := m.communitiesManager.ControlledCommunities()
controlledCommunities, err := m.communitiesManager.Controlled()
if err == nil && len(controlledCommunities) > 0 {
available := m.SubscribeMailserverAvailable()
go func() {
@ -1691,7 +1691,7 @@ func (m *Messenger) Init() error {
// Init filters for the communities we control
var communityFiltersToInitialize []transport.CommunityFilterToInitialize
controlledCommunities, err := m.communitiesManager.ControlledCommunities()
controlledCommunities, err := m.communitiesManager.Controlled()
if err != nil {
return err
}

View File

@ -251,7 +251,7 @@ func (m *Messenger) handleCommunitiesSubscription(c chan *communities.Subscripti
recentlyPublishedOrgs := func() map[string]*communities.Community {
result := make(map[string]*communities.Community)
controlledCommunities, err := m.communitiesManager.ControlledCommunities()
controlledCommunities, err := m.communitiesManager.Controlled()
if err != nil {
m.logger.Warn("failed to retrieve orgs", zap.Error(err))
return result
@ -379,7 +379,7 @@ func (m *Messenger) handleCommunitiesSubscription(c chan *communities.Subscripti
continue
}
controlledCommunities, err := m.communitiesManager.Owned()
controlledCommunities, err := m.communitiesManager.Controlled()
if err != nil {
m.logger.Warn("failed to retrieve orgs", zap.Error(err))
}
@ -413,7 +413,7 @@ func (m *Messenger) updateCommunitiesActiveMembersPeriodically() {
for {
select {
case <-ticker.C:
controlledCommunities, err := m.communitiesManager.Owned()
controlledCommunities, err := m.communitiesManager.Controlled()
if err != nil {
m.logger.Error("failed to update community active members count", zap.Error(err))
}
@ -3589,7 +3589,7 @@ func (m *Messenger) EnableCommunityHistoryArchiveProtocol() error {
return err
}
controlledCommunities, err := m.communitiesManager.Owned()
controlledCommunities, err := m.communitiesManager.Controlled()
if err != nil {
return err
}