From c6b3d0212d06a671f61333bf353cbc5094492e1d Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Thu, 15 Aug 2019 00:44:33 +0200 Subject: [PATCH] models: Move some display_recipient code to display_recipient.py. --- zerver/lib/actions.py | 3 ++- zerver/lib/display_recipient.py | 34 ++++++++++++++++++++++++++++++++ zerver/models.py | 35 ++++----------------------------- 3 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 zerver/lib/display_recipient.py diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 8265b9b90a..cc59d50535 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -29,6 +29,7 @@ from zerver.lib.bot_config import ( ) from zerver.lib.cache import ( bot_dict_fields, + display_recipient_cache_key, delete_user_profile_caches, to_dict_cache_key_id, user_profile_by_api_key_cache_key, @@ -96,7 +97,7 @@ from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, MAX_MESSAGE_LENGTH, get_client, get_stream, get_personal_recipient, \ get_user_profile_by_id, PreregistrationUser, \ bulk_get_recipients, get_stream_recipient, get_stream_recipients, \ - email_allowed_for_realm, email_to_username, display_recipient_cache_key, \ + email_allowed_for_realm, email_to_username, \ get_user_by_delivery_email, get_stream_cache_key, active_non_guest_user_ids, \ UserActivityInterval, active_user_ids, get_active_streams, \ realm_filters_for_realm, RealmFilter, stream_name_in_use, \ diff --git a/zerver/lib/display_recipient.py b/zerver/lib/display_recipient.py new file mode 100644 index 0000000000..94bd89bb5f --- /dev/null +++ b/zerver/lib/display_recipient.py @@ -0,0 +1,34 @@ +from typing import Any, Dict, List, Optional, Union + +from zerver.lib.cache import cache_with_key, display_recipient_cache_key +from zerver.models import Recipient, Stream, UserProfile + +DisplayRecipientCacheT = Union[str, List[Dict[str, Any]]] +@cache_with_key(lambda *args: display_recipient_cache_key(args[0]), + timeout=3600*24*7) +def get_display_recipient_remote_cache(recipient_id: int, recipient_type: int, + recipient_type_id: Optional[int]) -> DisplayRecipientCacheT: + """ + returns: an appropriate object describing the recipient. For a + stream this will be the stream name as a string. For a huddle or + personal, it will be an array of dicts about each recipient. + """ + if recipient_type == Recipient.STREAM: + assert recipient_type_id is not None + stream = Stream.objects.get(id=recipient_type_id) + return stream.name + + # The main priority for ordering here is being deterministic. + # Right now, we order by ID, which matches the ordering of user + # names in the left sidebar. + user_profile_list = (UserProfile.objects.filter(subscription__recipient_id=recipient_id) + .select_related() + .order_by('id')) + return [user_profile_to_display_recipient_dict(user_profile) for user_profile in user_profile_list] + +def user_profile_to_display_recipient_dict(user_profile: 'UserProfile') -> Dict[str, Any]: + return {'email': user_profile.email, + 'full_name': user_profile.full_name, + 'short_name': user_profile.short_name, + 'id': user_profile.id, + 'is_mirror_dummy': user_profile.is_mirror_dummy} diff --git a/zerver/models.py b/zerver/models.py index 7a6ae5bce5..7b10806b80 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -17,7 +17,7 @@ from zerver.lib.cache import cache_with_key, flush_user_profile, flush_realm, \ user_profile_by_api_key_cache_key, active_non_guest_user_ids_cache_key, \ user_profile_by_id_cache_key, user_profile_by_email_cache_key, \ user_profile_cache_key, generic_bulk_cached_fetch, cache_set, flush_stream, \ - display_recipient_cache_key, cache_delete, active_user_ids_cache_key, \ + cache_delete, active_user_ids_cache_key, \ get_stream_cache_key, realm_user_dicts_cache_key, \ bot_dicts_in_realm_cache_key, realm_user_dict_fields, \ bot_dict_fields, flush_message, flush_submessage, bot_profile_cache_key, \ @@ -88,6 +88,9 @@ def get_display_recipient_by_id(recipient_id: int, recipient_type: int, If the type is a stream, the type_id must be an int; a string is returned. Otherwise, type_id may be None; an array of recipient dicts is returned. """ + # Have to import here, to avoid circular dependency. + from zerver.lib.display_recipient import get_display_recipient_remote_cache + if recipient_id not in per_request_display_recipient_cache: result = get_display_recipient_remote_cache(recipient_id, recipient_type, recipient_type_id) per_request_display_recipient_cache[recipient_id] = result @@ -106,36 +109,6 @@ def flush_per_request_caches() -> None: global per_request_realm_filters_cache per_request_realm_filters_cache = {} -DisplayRecipientCacheT = Union[str, List[Dict[str, Any]]] -@cache_with_key(lambda *args: display_recipient_cache_key(args[0]), - timeout=3600*24*7) -def get_display_recipient_remote_cache(recipient_id: int, recipient_type: int, - recipient_type_id: Optional[int]) -> DisplayRecipientCacheT: - """ - returns: an appropriate object describing the recipient. For a - stream this will be the stream name as a string. For a huddle or - personal, it will be an array of dicts about each recipient. - """ - if recipient_type == Recipient.STREAM: - assert recipient_type_id is not None - stream = Stream.objects.get(id=recipient_type_id) - return stream.name - - # The main priority for ordering here is being deterministic. - # Right now, we order by ID, which matches the ordering of user - # names in the left sidebar. - user_profile_list = (UserProfile.objects.filter(subscription__recipient_id=recipient_id) - .select_related() - .order_by('id')) - return [user_profile_to_display_recipient_dict(user_profile) for user_profile in user_profile_list] - -def user_profile_to_display_recipient_dict(user_profile: 'UserProfile') -> Dict[str, Any]: - return {'email': user_profile.email, - 'full_name': user_profile.full_name, - 'short_name': user_profile.short_name, - 'id': user_profile.id, - 'is_mirror_dummy': user_profile.is_mirror_dummy} - def get_realm_emoji_cache_key(realm: 'Realm') -> str: return u'realm_emoji:%s' % (realm.id,)