Update account peers if ns group has peers

This commit is contained in:
bcmmbaga
2024-07-18 21:04:58 +03:00
parent 36d4c21671
commit 3e76deaa87
2 changed files with 37 additions and 16 deletions
+10
View File
@@ -490,3 +490,13 @@ func isGroupLinkedToUser(users map[string]*User, groupID string) (bool, *User) {
} }
return false, nil return false, nil
} }
// anyGroupHasPeers checks if any of the given groups in the account have peers.
func anyGroupHasPeers(account *Account, groupIDs []string) bool {
for _, groupID := range groupIDs {
if group, exists := account.Groups[groupID]; exists && group.HasPeers() {
return true
}
}
return false
}
+27 -16
View File
@@ -79,14 +79,18 @@ func (am *DefaultAccountManager) CreateNameServerGroup(ctx context.Context, acco
account.NameServerGroups[newNSGroup.ID] = newNSGroup account.NameServerGroups[newNSGroup.ID] = newNSGroup
account.Network.IncSerial() updateAccountPeers := anyGroupHasPeers(account, newNSGroup.Groups)
err = am.Store.SaveAccount(ctx, account) if updateAccountPeers {
if err != nil { account.Network.IncSerial()
}
if err := am.Store.SaveAccount(ctx, account); err != nil {
return nil, err return nil, err
} }
am.updateAccountPeers(ctx, account) if updateAccountPeers {
am.updateAccountPeers(ctx, account)
}
am.StoreEvent(ctx, userID, newNSGroup.ID, accountID, activity.NameserverGroupCreated, newNSGroup.EventMeta()) am.StoreEvent(ctx, userID, newNSGroup.ID, accountID, activity.NameserverGroupCreated, newNSGroup.EventMeta())
return newNSGroup.Copy(), nil return newNSGroup.Copy(), nil
@@ -94,7 +98,6 @@ func (am *DefaultAccountManager) CreateNameServerGroup(ctx context.Context, acco
// SaveNameServerGroup saves nameserver group // SaveNameServerGroup saves nameserver group
func (am *DefaultAccountManager) SaveNameServerGroup(ctx context.Context, accountID, userID string, nsGroupToSave *nbdns.NameServerGroup) error { func (am *DefaultAccountManager) SaveNameServerGroup(ctx context.Context, accountID, userID string, nsGroupToSave *nbdns.NameServerGroup) error {
unlock := am.Store.AcquireAccountWriteLock(ctx, accountID) unlock := am.Store.AcquireAccountWriteLock(ctx, accountID)
defer unlock() defer unlock()
@@ -112,16 +115,20 @@ func (am *DefaultAccountManager) SaveNameServerGroup(ctx context.Context, accoun
return err return err
} }
oldNSGroup := account.NameServerGroups[nsGroupToSave.ID]
updateAccountPeers := anyGroupHasPeers(account, nsGroupToSave.Groups) || anyGroupHasPeers(account, oldNSGroup.Groups)
if updateAccountPeers {
account.Network.IncSerial()
}
account.NameServerGroups[nsGroupToSave.ID] = nsGroupToSave account.NameServerGroups[nsGroupToSave.ID] = nsGroupToSave
account.Network.IncSerial() if err = am.Store.SaveAccount(ctx, account); err != nil {
err = am.Store.SaveAccount(ctx, account)
if err != nil {
return err return err
} }
am.updateAccountPeers(ctx, account) if updateAccountPeers {
am.updateAccountPeers(ctx, account)
}
am.StoreEvent(ctx, userID, nsGroupToSave.ID, accountID, activity.NameserverGroupUpdated, nsGroupToSave.EventMeta()) am.StoreEvent(ctx, userID, nsGroupToSave.ID, accountID, activity.NameserverGroupUpdated, nsGroupToSave.EventMeta())
return nil return nil
@@ -144,14 +151,18 @@ func (am *DefaultAccountManager) DeleteNameServerGroup(ctx context.Context, acco
} }
delete(account.NameServerGroups, nsGroupID) delete(account.NameServerGroups, nsGroupID)
account.Network.IncSerial() updateAccountPeers := anyGroupHasPeers(account, nsGroup.Groups)
err = am.Store.SaveAccount(ctx, account) if updateAccountPeers {
if err != nil { account.Network.IncSerial()
}
if err := am.Store.SaveAccount(ctx, account); err != nil {
return err return err
} }
am.updateAccountPeers(ctx, account) if updateAccountPeers {
am.updateAccountPeers(ctx, account)
}
am.StoreEvent(ctx, userID, nsGroup.ID, accountID, activity.NameserverGroupDeleted, nsGroup.EventMeta()) am.StoreEvent(ctx, userID, nsGroup.ID, accountID, activity.NameserverGroupDeleted, nsGroup.EventMeta())
return nil return nil