@@ -595,7 +595,7 @@ func (am *DefaultAccountManager) SaveOrAddUsers(ctx context.Context, accountID,
595595}
596596
597597// prepareUserUpdateEvents prepares a list user update events based on the changes between the old and new user data.
598- func (am * DefaultAccountManager ) prepareUserUpdateEvents (ctx context.Context , accountID string , initiatorUserID string , oldUser , newUser * types.User , transferredOwnerRole bool ) []func () {
598+ func (am * DefaultAccountManager ) prepareUserUpdateEvents (ctx context.Context , accountID string , initiatorUserID string , oldUser , newUser * types.User , transferredOwnerRole bool , removedGroupIDs , addedGroupIDs [] string , tx store. Store ) []func () {
599599 var eventsToStore []func ()
600600
601601 if oldUser .IsBlocked () != newUser .IsBlocked () {
@@ -621,6 +621,35 @@ func (am *DefaultAccountManager) prepareUserUpdateEvents(ctx context.Context, ac
621621 })
622622 }
623623
624+ addedGroups , err := tx .GetGroupsByIDs (ctx , store .LockingStrengthNone , accountID , addedGroupIDs )
625+ if err != nil {
626+ log .WithContext (ctx ).Errorf ("failed to get added groups for user %s update event: %v" , oldUser .Id , err )
627+ }
628+
629+ for _ , group := range addedGroups {
630+ meta := map [string ]any {
631+ "group" : group .Name , "group_id" : group .ID ,
632+ "is_service_user" : oldUser .IsServiceUser , "user_name" : oldUser .ServiceUserName ,
633+ }
634+ eventsToStore = append (eventsToStore , func () {
635+ am .StoreEvent (ctx , oldUser .Id , oldUser .Id , accountID , activity .GroupAddedToUser , meta )
636+ })
637+ }
638+
639+ removedGroups , err := tx .GetGroupsByIDs (ctx , store .LockingStrengthNone , accountID , removedGroupIDs )
640+ if err != nil {
641+ log .WithContext (ctx ).Errorf ("failed to get removed groups for user %s update event: %v" , oldUser .Id , err )
642+ }
643+ for _ , group := range removedGroups {
644+ meta := map [string ]any {
645+ "group" : group .Name , "group_id" : group .ID ,
646+ "is_service_user" : oldUser .IsServiceUser , "user_name" : oldUser .ServiceUserName ,
647+ }
648+ eventsToStore = append (eventsToStore , func () {
649+ am .StoreEvent (ctx , oldUser .Id , oldUser .Id , accountID , activity .GroupRemovedFromUser , meta )
650+ })
651+ }
652+
624653 return eventsToStore
625654}
626655
@@ -667,9 +696,10 @@ func (am *DefaultAccountManager) processUserUpdate(ctx context.Context, transact
667696 peersToExpire = userPeers
668697 }
669698
699+ var removedGroups , addedGroups []string
670700 if update .AutoGroups != nil && settings .GroupsPropagationEnabled {
671- removedGroups : = util .Difference (oldUser .AutoGroups , update .AutoGroups )
672- addedGroups : = util .Difference (update .AutoGroups , oldUser .AutoGroups )
701+ removedGroups = util .Difference (oldUser .AutoGroups , update .AutoGroups )
702+ addedGroups = util .Difference (update .AutoGroups , oldUser .AutoGroups )
673703 for _ , peer := range userPeers {
674704 for _ , groupID := range removedGroups {
675705 if err := transaction .RemovePeerFromGroup (ctx , peer .ID , groupID ); err != nil {
@@ -685,7 +715,7 @@ func (am *DefaultAccountManager) processUserUpdate(ctx context.Context, transact
685715 }
686716
687717 updateAccountPeers := len (userPeers ) > 0
688- userEventsToAdd := am .prepareUserUpdateEvents (ctx , updatedUser .AccountID , initiatorUserId , oldUser , updatedUser , transferredOwnerRole )
718+ userEventsToAdd := am .prepareUserUpdateEvents (ctx , updatedUser .AccountID , initiatorUserId , oldUser , updatedUser , transferredOwnerRole , removedGroups , addedGroups , transaction )
689719
690720 return updateAccountPeers , updatedUser , peersToExpire , userEventsToAdd , nil
691721}
0 commit comments