Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

users: Skip redundant workplace users count entry on role change.

Sahil Batra371b2ff98409
diff --git a/zerver/actions/user_groups.py b/zerver/actions/user_groups.py
index e2d769f72ac..10e6f3df7b1 100644
--- a/zerver/actions/user_groups.py
+++ b/zerver/actions/user_groups.py
@@ -109,7 +109,11 @@ def create_user_group_in_database(
@transaction.atomic(savepoint=False)
def update_users_in_full_members_system_group(
- realm: Realm, affected_user_ids: Sequence[int] = [], *, acting_user: UserProfile | None
+ realm: Realm,
+ affected_user_ids: Sequence[int] = [],
+ *,
+ acting_user: UserProfile | None,
+ skip_workplace_users_count_audit_log: bool = False,
) -> None:
full_members_system_group = NamedUserGroup.objects.get(
realm_for_sharding=realm, name=SystemGroups.FULL_MEMBERS, is_system_group=True
@@ -166,12 +170,18 @@ def update_users_in_full_members_system_group(
if len(old_full_members) > 0:
bulk_remove_members_from_user_groups(
- [full_members_system_group], old_full_member_ids, acting_user=acting_user
+ [full_members_system_group],
+ old_full_member_ids,
+ acting_user=acting_user,
+ skip_workplace_users_count_audit_log=skip_workplace_users_count_audit_log,
)
if len(new_full_members) > 0:
bulk_add_members_to_user_groups(
- [full_members_system_group], new_full_member_ids, acting_user=acting_user
+ [full_members_system_group],
+ new_full_member_ids,
+ acting_user=acting_user,
+ skip_workplace_users_count_audit_log=skip_workplace_users_count_audit_log,
)
@@ -326,6 +336,7 @@ def bulk_add_members_to_user_groups(
user_profile_ids: list[int],
*,
acting_user: UserProfile | None,
+ skip_workplace_users_count_audit_log: bool = False,
) -> None:
# All intended callers of this function involve a single user
# being added to one or more groups, or many users being added to
@@ -363,7 +374,9 @@ def bulk_add_members_to_user_groups(
for user_group in user_groups
)
- if check_any_group_used_for_workplace_users_group(realm, user_groups):
+ if not skip_workplace_users_count_audit_log and check_any_group_used_for_workplace_users_group(
+ realm, user_groups
+ ):
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
@@ -417,6 +430,7 @@ def bulk_remove_members_from_user_groups(
user_profile_ids: list[int],
*,
acting_user: UserProfile | None,
+ skip_workplace_users_count_audit_log: bool = False,
) -> None:
# All intended callers of this function involve a single user
# being added to one or more groups, or many users being added to
@@ -452,7 +466,9 @@ def bulk_remove_members_from_user_groups(
for user_group in user_groups
)
- if check_any_group_used_for_workplace_users_group(realm, user_groups):
+ if not skip_workplace_users_count_audit_log and check_any_group_used_for_workplace_users_group(
+ realm, user_groups
+ ):
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
diff --git a/zerver/actions/users.py b/zerver/actions/users.py
index ce1021440b6..88555110f06 100644
--- a/zerver/actions/users.py
+++ b/zerver/actions/users.py
@@ -691,8 +691,15 @@ def do_change_user_role(
do_send_user_group_members_update_event("add_members", system_group, [user_profile.id])
if UserProfile.ROLE_MEMBER in [old_value, value]:
+ # The USER_ROLE_CHANGED entry above records the workplace users
+ # count once all the membership updates for this role change are
+ # done, so a WORKPLACE_USERS_COUNT_CHANGED entry recording the
+ # same count would be redundant.
update_users_in_full_members_system_group(
- user_profile.realm, [user_profile.id], acting_user=acting_user
+ user_profile.realm,
+ [user_profile.id],
+ acting_user=acting_user,
+ skip_workplace_users_count_audit_log=True,
)
# realm_user_count_by_role counts the workplace users from
diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py
index 518c2805416..dbd8bf0935f 100644
--- a/zerver/tests/test_audit_log.py
+++ b/zerver/tests/test_audit_log.py
@@ -1944,6 +1944,7 @@ class TestRealmAuditLog(ZulipTestCase):
realm, "workplace_users_group", moderators_group, acting_user=None
)
realm.refresh_from_db()
+ now = timezone_now()
do_change_user_role(
user_profile, UserProfile.ROLE_REALM_ADMINISTRATOR, acting_user=None, notify=False
)
@@ -1960,6 +1961,15 @@ class TestRealmAuditLog(ZulipTestCase):
.filter(is_bot=False, is_active=True)
.count(),
)
+ # We skip creating WORKPLACE_USERS_COUNT_CHAGNED entry since
+ # USER_ROLE_CHANGED entry already has the correct user count.
+ self.assertFalse(
+ RealmAuditLog.objects.filter(
+ realm=realm,
+ event_type=AuditLogEventType.WORKPLACE_USERS_COUNT_CHANGED,
+ event_time__gte=now,
+ ).exists()
+ )
# Check when workplace_users_group is set to a system group
# where just role counts are not enough and group membership
@@ -1968,6 +1978,7 @@ class TestRealmAuditLog(ZulipTestCase):
realm, "workplace_users_group", full_members_group, acting_user=None
)
realm.refresh_from_db()
+ now = timezone_now()
do_change_user_role(user_profile, UserProfile.ROLE_GUEST, acting_user=None, notify=False)
role_changed_entry = RealmAuditLog.objects.filter(
realm=realm,
@@ -1982,6 +1993,13 @@ class TestRealmAuditLog(ZulipTestCase):
.filter(is_bot=False, is_active=True)
.count(),
)
+ self.assertFalse(
+ RealmAuditLog.objects.filter(
+ realm=realm,
+ event_type=AuditLogEventType.WORKPLACE_USERS_COUNT_CHANGED,
+ event_time__gte=now,
+ ).exists()
+ )
# Check with workplace_users_group set to an anonymous group.
do_change_realm_permission_group_setting(
@@ -1991,6 +2009,7 @@ class TestRealmAuditLog(ZulipTestCase):
acting_user=None,
)
realm.refresh_from_db()
+ now = timezone_now()
do_change_user_role(user_profile, UserProfile.ROLE_MEMBER, acting_user=None, notify=False)
role_changed_entry = RealmAuditLog.objects.filter(
realm=realm,
@@ -2005,6 +2024,13 @@ class TestRealmAuditLog(ZulipTestCase):
.filter(is_bot=False, is_active=True)
.count(),
)
+ self.assertFalse(
+ RealmAuditLog.objects.filter(
+ realm=realm,
+ event_type=AuditLogEventType.WORKPLACE_USERS_COUNT_CHANGED,
+ event_time__gte=now,
+ ).exists()
+ )
def test_workplace_users_count_when_creating_user(self) -> None:
"""