Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
cross realm bots: Eliminate bulk_get_users confusion.
Steve Howell0c92879f2a6a
diff --git a/zerver/lib/users.py b/zerver/lib/users.pyindex 2604f457c64..c752c091f74 100644--- a/zerver/lib/users.py+++ b/zerver/lib/users.py@@ -17,7 +17,6 @@ from zerver.lib.cache import (bulk_cached_fetch,realm_user_dict_fields,user_profile_by_id_cache_key,- user_profile_cache_key_id,)from zerver.lib.exceptions import (JsonableError,@@ -173,22 +172,9 @@ def is_administrator_role(role: int) -> bool:return role in {UserProfile.ROLE_REALM_ADMINISTRATOR, UserProfile.ROLE_REALM_OWNER}-def bulk_get_users(- emails: List[str], realm: Optional[Realm], base_query: Optional[QuerySet[UserProfile]] = None-) -> Dict[str, UserProfile]:- if base_query is None:- assert realm is not None- query = UserProfile.objects.filter(realm=realm, is_active=True)- realm_id = realm.id- else:- # WARNING: Currently, this code path only really supports one- # version of `base_query` being used (because otherwise,- # they'll share the cache, which can screw up the filtering).- # If you're using this flow, you'll need to re-do any filters- # in base_query in the code itself; base_query is just a perf- # optimization.- query = base_query- realm_id = 0+def bulk_get_cross_realm_bots() -> Dict[str, UserProfile]:+ emails = list(settings.CROSS_REALM_BOT_EMAILS)+ query = UserProfile.objects.filter(realm__string_id=settings.SYSTEM_BOT_REALM)def fetch_users_by_email(emails: List[str]) -> QuerySet[UserProfile]:# This should be just@@ -205,9 +191,7 @@ def bulk_get_users(return user_profile.email.lower()return bulk_cached_fetch(- # Use a separate cache key to protect us from conflicts with- # the get_user cache.- lambda email: "bulk_get_users:" + user_profile_cache_key_id(email, realm_id),+ lambda email: f"bulk_get_cross_realm_bots:{email}",fetch_users_by_email,[email.lower() for email in emails],id_fetcher=user_to_email,@@ -539,18 +523,10 @@ def user_profile_to_user_row(user_profile: UserProfile) -> Dict[str, Any]:def get_cross_realm_dicts() -> List[Dict[str, Any]]:- users = bulk_get_users(- list(settings.CROSS_REALM_BOT_EMAILS),- None,- base_query=UserProfile.objects.filter(realm__string_id=settings.SYSTEM_BOT_REALM),- ).values()+ user_dict = bulk_get_cross_realm_bots()+ users = sorted(user_dict.values(), key=lambda user: user.full_name)result = []for user in users:- # Important: We filter here, is addition to in- # `base_query`, because of how bulk_get_users shares its- # cache with other UserProfile caches.- if user.realm.string_id != settings.SYSTEM_BOT_REALM: # nocoverage- continueuser_row = user_profile_to_user_row(user)# Because we want to avoid clients being exposed to the# implementation detail that these bots are self-owned, wediff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex fa05467c31f..fd2866d99e1 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -27,7 +27,7 @@ from zerver.actions.users import (do_delete_user,do_delete_user_preserving_messages,)-from zerver.lib.avatar import avatar_url, get_gravatar_url+from zerver.lib.avatar import avatar_url, get_avatar_field, get_gravatar_urlfrom zerver.lib.bulk_create import create_usersfrom zerver.lib.create_user import copy_default_settingsfrom zerver.lib.events import do_events_register@@ -48,7 +48,13 @@ from zerver.lib.test_helpers import ()from zerver.lib.upload import upload_avatar_imagefrom zerver.lib.user_groups import get_system_user_group_for_user-from zerver.lib.users import Accounts, access_user_by_id, get_accounts_for_email, user_ids_to_users+from zerver.lib.users import (+ Accounts,+ access_user_by_id,+ get_accounts_for_email,+ get_cross_realm_dicts,+ user_ids_to_users,+)from zerver.lib.utils import assert_is_not_nonefrom zerver.models import (CustomProfileField,@@ -1101,28 +1107,6 @@ class UserProfileTest(ZulipTestCase):with self.assertRaises(JsonableError):user_ids_to_users(real_user_ids, get_realm("zephyr"))- def test_bulk_get_users(self) -> None:- from zerver.lib.users import bulk_get_users-- hamlet = self.example_user("hamlet")- cordelia = self.example_user("cordelia")- webhook_bot = self.example_user("webhook_bot")- result = bulk_get_users(- [hamlet.email, cordelia.email],- get_realm("zulip"),- )- self.assertEqual(result[hamlet.email].email, hamlet.email)- self.assertEqual(result[cordelia.email].email, cordelia.email)-- result = bulk_get_users(- [hamlet.email, cordelia.email, webhook_bot.email],- None,- base_query=UserProfile.objects.all(),- )- self.assertEqual(result[hamlet.email].email, hamlet.email)- self.assertEqual(result[cordelia.email].email, cordelia.email)- self.assertEqual(result[webhook_bot.email].email, webhook_bot.email)-def test_get_accounts_for_email(self) -> None:reset_email_visibility_to_everyone_in_zulip_realm()@@ -1313,6 +1297,60 @@ class UserProfileTest(ZulipTestCase):with self.assertRaises(UserProfile.DoesNotExist):get_user_by_id_in_realm_including_cross_realm(hamlet.id, None)+ def test_cross_realm_dicts(self) -> None:+ def user_row(email: str) -> Dict[str, object]:+ user = UserProfile.objects.get(email=email)+ avatar_url = get_avatar_field(+ user_id=user.id,+ realm_id=user.realm_id,+ email=user.delivery_email,+ avatar_source=user.avatar_source,+ avatar_version=1,+ medium=False,+ client_gravatar=False,+ )+ return dict(+ # bot-specific fields+ avatar_url=avatar_url,+ date_joined=user.date_joined.isoformat(),+ delivery_email=email,+ email=email,+ full_name=user.full_name,+ user_id=user.id,+ # common fields+ avatar_version=1,+ bot_owner_id=None,+ bot_type=1,+ is_active=True,+ is_admin=False,+ is_billing_admin=False,+ is_bot=True,+ is_guest=False,+ is_owner=False,+ is_system_bot=True,+ role=400,+ timezone="",+ )++ expected_emails = [+ "[email protected]",+ "[email protected]",+ "[email protected]",+ ]++ expected_dicts = [user_row(email) for email in expected_emails]++ with self.assert_database_query_count(1):+ actual_dicts = get_cross_realm_dicts()++ self.assertEqual(actual_dicts, expected_dicts)++ # Now it should be cached.+ with self.assert_database_query_count(0, keep_cache_warm=True):+ actual_dicts = get_cross_realm_dicts()++ self.assertEqual(actual_dicts, expected_dicts)+def test_get_user_subscription_status(self) -> None:self.login("hamlet")iago = self.example_user("iago")