@@ -1368,7 +1368,7 @@ func (am *DefaultAccountManager) SyncUserJWTGroups(ctx context.Context, userAuth
1368
1368
return nil
1369
1369
}
1370
1370
1371
- if err = transaction .SaveGroups (ctx , store .LockingStrengthUpdate , userAuth .AccountId , newGroupsToCreate ); err != nil {
1371
+ if err = transaction .CreateGroups (ctx , store .LockingStrengthUpdate , userAuth .AccountId , newGroupsToCreate ); err != nil {
1372
1372
return fmt .Errorf ("error saving groups: %w" , err )
1373
1373
}
1374
1374
@@ -1382,28 +1382,22 @@ func (am *DefaultAccountManager) SyncUserJWTGroups(ctx context.Context, userAuth
1382
1382
1383
1383
// Propagate changes to peers if group propagation is enabled
1384
1384
if settings .GroupsPropagationEnabled {
1385
- groups , err = transaction .GetAccountGroups (ctx , store .LockingStrengthShare , userAuth .AccountId )
1386
- if err != nil {
1387
- return fmt .Errorf ("error getting account groups: %w" , err )
1388
- }
1389
-
1390
- groupsMap := make (map [string ]* types.Group , len (groups ))
1391
- for _ , group := range groups {
1392
- groupsMap [group .ID ] = group
1393
- }
1394
-
1395
1385
peers , err := transaction .GetUserPeers (ctx , store .LockingStrengthShare , userAuth .AccountId , userAuth .UserId )
1396
1386
if err != nil {
1397
1387
return fmt .Errorf ("error getting user peers: %w" , err )
1398
1388
}
1399
1389
1400
- updatedGroups , err := updateUserPeersInGroups (groupsMap , peers , addNewGroups , removeOldGroups )
1401
- if err != nil {
1402
- return fmt .Errorf ("error modifying user peers in groups: %w" , err )
1403
- }
1404
-
1405
- if err = transaction .SaveGroups (ctx , store .LockingStrengthUpdate , userAuth .AccountId , updatedGroups ); err != nil {
1406
- return fmt .Errorf ("error saving groups: %w" , err )
1390
+ for _ , peer := range peers {
1391
+ for _ , g := range addNewGroups {
1392
+ if err := transaction .AddPeerToGroup (ctx , userAuth .AccountId , peer .ID , g ); err != nil {
1393
+ return fmt .Errorf ("error adding peer %s to group %s: %w" , peer .ID , g , err )
1394
+ }
1395
+ }
1396
+ for _ , g := range removeOldGroups {
1397
+ if err := transaction .RemovePeerFromGroup (ctx , peer .ID , g ); err != nil {
1398
+ return fmt .Errorf ("error removing peer %s from group %s: %w" , peer .ID , g , err )
1399
+ }
1400
+ }
1407
1401
}
1408
1402
1409
1403
if err = transaction .IncrementNetworkSerial (ctx , store .LockingStrengthUpdate , userAuth .AccountId ); err != nil {
@@ -1971,53 +1965,56 @@ func (am *DefaultAccountManager) UpdateToPrimaryAccount(ctx context.Context, acc
1971
1965
// propagateUserGroupMemberships propagates all account users' group memberships to their peers.
1972
1966
// Returns true if any groups were modified, true if those updates affect peers and an error.
1973
1967
func propagateUserGroupMemberships (ctx context.Context , transaction store.Store , accountID string ) (groupsUpdated bool , peersAffected bool , err error ) {
1974
- groups , err := transaction .GetAccountGroups (ctx , store .LockingStrengthShare , accountID )
1968
+ users , err := transaction .GetAccountUsers (ctx , store .LockingStrengthShare , accountID )
1975
1969
if err != nil {
1976
1970
return false , false , err
1977
1971
}
1978
1972
1979
- groupsMap := make ( map [ string ] * types. Group , len ( groups ) )
1980
- for _ , group := range groups {
1981
- groupsMap [ group . ID ] = group
1973
+ accountGroupPeers , err := transaction . GetAccountGroupPeers ( ctx , store . LockingStrengthShare , accountID )
1974
+ if err != nil {
1975
+ return false , false , fmt . Errorf ( "error getting account group peers: %w" , err )
1982
1976
}
1983
1977
1984
- users , err := transaction .GetAccountUsers (ctx , store .LockingStrengthShare , accountID )
1978
+ accountGroups , err := transaction .GetAccountGroups (ctx , store .LockingStrengthShare , accountID )
1985
1979
if err != nil {
1986
- return false , false , err
1980
+ return false , false , fmt . Errorf ( "error getting account groups: %w" , err )
1987
1981
}
1988
1982
1989
- groupsToUpdate := make (map [string ]* types.Group )
1983
+ for _ , group := range accountGroups {
1984
+ if _ , exists := accountGroupPeers [group .ID ]; ! exists {
1985
+ accountGroupPeers [group .ID ] = make (map [string ]struct {})
1986
+ }
1987
+ }
1990
1988
1989
+ updatedGroups := []string {}
1991
1990
for _ , user := range users {
1992
1991
userPeers , err := transaction .GetUserPeers (ctx , store .LockingStrengthShare , accountID , user .Id )
1993
1992
if err != nil {
1994
1993
return false , false , err
1995
1994
}
1996
1995
1997
- updatedGroups , err := updateUserPeersInGroups (groupsMap , userPeers , user .AutoGroups , nil )
1998
- if err != nil {
1999
- return false , false , err
2000
- }
2001
-
2002
- for _ , group := range updatedGroups {
2003
- groupsToUpdate [group .ID ] = group
2004
- groupsMap [group .ID ] = group
1996
+ for _ , peer := range userPeers {
1997
+ for _ , groupID := range user .AutoGroups {
1998
+ if _ , exists := accountGroupPeers [groupID ]; ! exists {
1999
+ // we do not wanna create the groups here
2000
+ log .WithContext (ctx ).Warnf ("group %s does not exist for user group propagation" , groupID )
2001
+ continue
2002
+ }
2003
+ if _ , exists := accountGroupPeers [groupID ][peer.ID ]; exists {
2004
+ continue
2005
+ }
2006
+ if err := transaction .AddPeerToGroup (ctx , accountID , peer .ID , groupID ); err != nil {
2007
+ return false , false , fmt .Errorf ("error adding peer %s to group %s: %w" , peer .ID , groupID , err )
2008
+ }
2009
+ updatedGroups = append (updatedGroups , groupID )
2010
+ }
2005
2011
}
2006
2012
}
2007
2013
2008
- if len (groupsToUpdate ) == 0 {
2009
- return false , false , nil
2010
- }
2011
-
2012
- peersAffected , err = areGroupChangesAffectPeers (ctx , transaction , accountID , maps .Keys (groupsToUpdate ))
2013
- if err != nil {
2014
- return false , false , err
2015
- }
2016
-
2017
- err = transaction .SaveGroups (ctx , store .LockingStrengthUpdate , accountID , maps .Values (groupsToUpdate ))
2014
+ peersAffected , err = areGroupChangesAffectPeers (ctx , transaction , accountID , updatedGroups )
2018
2015
if err != nil {
2019
- return false , false , err
2016
+ return false , false , fmt . Errorf ( "error checking if group changes affect peers: %w" , err )
2020
2017
}
2021
2018
2022
- return true , peersAffected , nil
2019
+ return len ( updatedGroups ) > 0 , peersAffected , nil
2023
2020
}
0 commit comments