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
No known key found for this signature in database
GPG Key ID: 7249A19D20613553
2 changed files with 37 additions and 16 deletions

View File

@ -490,3 +490,13 @@ func isGroupLinkedToUser(users map[string]*User, groupID string) (bool, *User) {
}
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
}

View File

@ -79,14 +79,18 @@ func (am *DefaultAccountManager) CreateNameServerGroup(ctx context.Context, acco
account.NameServerGroups[newNSGroup.ID] = newNSGroup
account.Network.IncSerial()
err = am.Store.SaveAccount(ctx, account)
if err != nil {
updateAccountPeers := anyGroupHasPeers(account, newNSGroup.Groups)
if updateAccountPeers {
account.Network.IncSerial()
}
if err := am.Store.SaveAccount(ctx, account); err != nil {
return nil, err
}
am.updateAccountPeers(ctx, account)
if updateAccountPeers {
am.updateAccountPeers(ctx, account)
}
am.StoreEvent(ctx, userID, newNSGroup.ID, accountID, activity.NameserverGroupCreated, newNSGroup.EventMeta())
return newNSGroup.Copy(), nil
@ -94,7 +98,6 @@ func (am *DefaultAccountManager) CreateNameServerGroup(ctx context.Context, acco
// SaveNameServerGroup saves nameserver group
func (am *DefaultAccountManager) SaveNameServerGroup(ctx context.Context, accountID, userID string, nsGroupToSave *nbdns.NameServerGroup) error {
unlock := am.Store.AcquireAccountWriteLock(ctx, accountID)
defer unlock()
@ -112,16 +115,20 @@ func (am *DefaultAccountManager) SaveNameServerGroup(ctx context.Context, accoun
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.Network.IncSerial()
err = am.Store.SaveAccount(ctx, account)
if err != nil {
if err = am.Store.SaveAccount(ctx, account); err != nil {
return err
}
am.updateAccountPeers(ctx, account)
if updateAccountPeers {
am.updateAccountPeers(ctx, account)
}
am.StoreEvent(ctx, userID, nsGroupToSave.ID, accountID, activity.NameserverGroupUpdated, nsGroupToSave.EventMeta())
return nil
@ -144,14 +151,18 @@ func (am *DefaultAccountManager) DeleteNameServerGroup(ctx context.Context, acco
}
delete(account.NameServerGroups, nsGroupID)
account.Network.IncSerial()
err = am.Store.SaveAccount(ctx, account)
if err != nil {
updateAccountPeers := anyGroupHasPeers(account, nsGroup.Groups)
if updateAccountPeers {
account.Network.IncSerial()
}
if err := am.Store.SaveAccount(ctx, account); err != nil {
return err
}
am.updateAccountPeers(ctx, account)
if updateAccountPeers {
am.updateAccountPeers(ctx, account)
}
am.StoreEvent(ctx, userID, nsGroup.ID, accountID, activity.NameserverGroupDeleted, nsGroup.EventMeta())
return nil