Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
users: Add optimized subscribers-subscriptions lookup for a single user.
Bedo Khaled946dc6eb942f
diff --git a/zerver/actions/users.py b/zerver/actions/users.pyindex e92c4a1972a..4be8764a7dd 100644--- a/zerver/actions/users.py+++ b/zerver/actions/users.py@@ -384,8 +384,8 @@ def send_events_for_user_deactivation(user_profile: UserProfile) -> None:non_guest_user_ids = active_non_guest_user_ids(realm.id)- # This code path is parallel to- # bulk_get_subscribers_of_target_user_subscriptions, but can't reuse it+ # This code path is similar to+ # get_subscribers_of_target_user_subscriptions, but can't reuse it# because we need to process stream and direct_message_group# subscriptions separately.deactivated_user_subs = Subscription.objects.filter(diff --git a/zerver/lib/users.py b/zerver/lib/users.pyindex 55f328e5be2..724395d8b1d 100644--- a/zerver/lib/users.py+++ b/zerver/lib/users.py@@ -834,14 +834,10 @@ def get_user_ids_who_can_access_user(target_user: UserProfile) -> list[int]:active_non_guest_user_ids_in_realm = active_non_guest_user_ids(realm.id)- users_sharing_any_subscription = bulk_get_subscribers_of_target_user_subscriptions(- [target_user]- )+ users_sharing_any_subscription = get_subscribers_of_target_user_subscriptions(target_user)user_ids_who_can_access_target_user = (- {target_user.id}- | set(active_non_guest_user_ids_in_realm)- | users_sharing_any_subscription[target_user.id]+ {target_user.id} | set(active_non_guest_user_ids_in_realm) | users_sharing_any_subscription)return list(user_ids_who_can_access_target_user)@@ -849,7 +845,11 @@ def get_user_ids_who_can_access_user(target_user: UserProfile) -> list[int]:def bulk_get_subscribers_of_target_user_subscriptions(target_users: list[UserProfile], include_deactivated_users_for_dm_groups: bool = False) -> dict[int, set[int]]:- """Get all users involved in stream and direct message groups with target_users."""+ """Get all users involved in stream and direct message groups with target_users.++ For a single target_user, use the more efficient+ get_subscribers_of_target_user_subscriptions.+ """target_user_ids = [user.id for user in target_users]target_user_subscriptions = (Subscription.objects.filter(@@ -876,6 +876,8 @@ def bulk_get_subscribers_of_target_user_subscriptions(active=True,)+ # This flag is currently dead code, but we keep it to be consistent+ # with its equivalent get_subscribers_of_target_user_subscriptions.if include_deactivated_users_for_dm_groups:subs_in_target_user_subscriptions_query = subs_in_target_user_subscriptions_query.filter(Q(recipient__type=Recipient.STREAM, is_user_active=True)@@ -909,6 +911,33 @@ def bulk_get_subscribers_of_target_user_subscriptions(return users_subbed_to_target_user_subscriptions_dict+def get_subscribers_of_target_user_subscriptions(+ target_user: UserProfile, include_deactivated_users_for_dm_groups: bool = False+) -> set[int]:+ target_user_subbed_recipient_ids = Subscription.objects.filter(+ user_profile=target_user,+ active=True,+ recipient__type__in=[Recipient.STREAM, Recipient.DIRECT_MESSAGE_GROUP],+ ).values("recipient_id")++ subs_in_target_user_subscriptions_query = Subscription.objects.filter(+ recipient_id__in=target_user_subbed_recipient_ids,+ active=True,+ )++ if include_deactivated_users_for_dm_groups:+ subs_in_target_user_subscriptions_query = subs_in_target_user_subscriptions_query.filter(+ Q(recipient__type=Recipient.STREAM, is_user_active=True)+ | Q(recipient__type=Recipient.DIRECT_MESSAGE_GROUP)+ )+ else:+ subs_in_target_user_subscriptions_query = subs_in_target_user_subscriptions_query.filter(+ is_user_active=True,+ )++ return set(subs_in_target_user_subscriptions_query.values_list("user_profile_id", flat=True))++def user_profile_to_user_row(user_profile: UserProfile) -> RawUserDict:return RawUserDict(id=user_profile.id,@@ -989,17 +1018,13 @@ def get_data_for_inaccessible_user(realm: Realm, user_id: int) -> APIUserDict:def get_accessible_user_ids(user_profile: UserProfile, include_deactivated_users: bool = False) -> set[int]:- subscribers_dict_of_target_user_subscriptions = (- bulk_get_subscribers_of_target_user_subscriptions(- [user_profile], include_deactivated_users_for_dm_groups=include_deactivated_users- )+ users_sharing_any_subscription = get_subscribers_of_target_user_subscriptions(+ user_profile, include_deactivated_users_for_dm_groups=include_deactivated_users)# This does not include bots, because either the caller# wants only human users or it handles bots separately.- accessible_user_ids = {user_profile.id} | subscribers_dict_of_target_user_subscriptions[- user_profile.id- ]+ accessible_user_ids = {user_profile.id} | users_sharing_any_subscriptionreturn accessible_user_idsdiff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex e5f742eb761..cf14bb88c8a 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -64,6 +64,7 @@ from zerver.lib.users import (Account,access_user_by_id,access_user_by_id_including_cross_realm,+ bulk_get_subscribers_of_target_user_subscriptions,check_can_access_user,get_accessible_user_ids,get_accounts_for_email,@@ -3591,7 +3592,7 @@ class GetProfileTest(ZulipTestCase):self.set_up_db_for_testing_user_access()self.login("polonius")- with self.assert_database_query_count(7):+ with self.assert_database_query_count(6):result = orjson.loads(self.client_get("/json/users").content)accessible_users = result["members"]# The user can access 3 bot users and 7 human users.@@ -3684,7 +3685,7 @@ class GetProfileTest(ZulipTestCase):accessible_user_ids_subset = [hamlet.id, iago.id, aaron.id, zoe.id, webhook_bot.id]inaccessible_user_ids_subset = [cordelia.id, desdemona.id]user_ids_to_fetch = accessible_user_ids_subset + inaccessible_user_ids_subset- with self.assert_database_query_count(7):+ with self.assert_database_query_count(6):result = orjson.loads(self.client_get("/json/users", {"user_ids": orjson.dumps(user_ids_to_fetch).decode()}@@ -3753,7 +3754,7 @@ class GetProfileTest(ZulipTestCase):result = self.client_get(f"/json/users/{user.id}")self.assert_json_error(result, "Insufficient permission")- with self.settings(PARTIAL_USERS=True), self.assert_database_query_count(7):+ with self.settings(PARTIAL_USERS=True), self.assert_database_query_count(6):result = self.client_get("/json/users")self.assert_json_success(result)@@ -3849,6 +3850,81 @@ class GetProfileTest(ZulipTestCase):hamlet = self.example_user("hamlet")self.assertTrue(check_can_access_user(hamlet, None))+ def test_bulk_get_subscribers_of_target_user_subscriptions(self) -> None:+ hamlet = self.example_user("hamlet")+ aaron = self.example_user("aaron")+ desdemona = self.example_user("desdemona")+ iago = self.example_user("iago")+ prospero = self.example_user("prospero")+ shiva = self.example_user("shiva")+ zoe = self.example_user("ZOE")+ othello = self.example_user("othello")+ cordelia = self.example_user("cordelia")+ polonius = self.example_user("polonius")+ imported_user = self.example_user("imported_user")++ # Unsubscribe cordelia and polonius from their+ # ONLY stream subscription, so we can be sure+ # they are included/excluded only because of DM interactions.+ self.unsubscribe(cordelia, "Verona")+ self.unsubscribe(polonius, "Verona")++ self.send_group_direct_message(hamlet, [cordelia, polonius, othello])+ self.send_personal_message(aaron, cordelia)++ do_deactivate_user(cordelia, acting_user=None)+ do_deactivate_user(zoe, acting_user=None)++ subscribers_dict = bulk_get_subscribers_of_target_user_subscriptions([hamlet, aaron])++ # polonius shares a DirectMessage group with hamlet.+ # The rest share a stream with hamlet.+ hamlet_subscribers = {+ polonius.id,+ aaron.id,+ hamlet.id,+ othello.id,+ desdemona.id,+ iago.id,+ prospero.id,+ shiva.id,+ imported_user.id,+ }++ # polonius does NOT share any subscription with aaron.+ # The rest share a stream with aaron.+ aaron_subscribers = {+ aaron.id,+ hamlet.id,+ othello.id,+ desdemona.id,+ iago.id,+ prospero.id,+ shiva.id,+ }++ # By default, all deactivated subscribers, cordelia and zoe,+ # are excluded.++ self.assertEqual(hamlet_subscribers, subscribers_dict[hamlet.id])+ self.assertEqual(aaron_subscribers, subscribers_dict[aaron.id])++ # We use the flag to also include deactivated subscribers+ # who share a DirectMessage (1:1 or group) subscription.+ subscribers_dict = bulk_get_subscribers_of_target_user_subscriptions(+ [hamlet, aaron], include_deactivated_users_for_dm_groups=True+ )++ # Same subscribers as above BUT with cordelia,+ # who shares a DirectMessage with hamlet and aaron,+ # now included, since the flag is scoped to DM interaction.+ # Deactivated stream subscribers (zoe) are still excluded.+ hamlet_subscribers.add(cordelia.id)+ aaron_subscribers.add(cordelia.id)++ self.assertEqual(hamlet_subscribers, subscribers_dict[hamlet.id])+ self.assertEqual(aaron_subscribers, subscribers_dict[aaron.id])+def test_get_users_for_spectators(self) -> None:# Checks that spectators can fetch users data.hamlet = self.example_user("hamlet")