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.

handle_push_notification: Avoid extra DB query to fetch realm.

Prakhar Pratyushe99935de5f3f
diff --git a/zerver/lib/avatar.py b/zerver/lib/avatar.py
index d6217754b58..91d78b8b4e3 100644
--- a/zerver/lib/avatar.py
+++ b/zerver/lib/avatar.py
@@ -142,7 +142,12 @@ def _get_unversioned_gravatar_url(email: str, medium: bool, realm_id: int) -> st
return staticfiles_storage.url("images/default-avatar.png")
-def absolute_avatar_url(user_profile: UserProfile) -> str:
+def absolute_avatar_url(
+ user_profile: UserProfile,
+ # Pass `realm_url` to avoid a DB query when `user_profile.realm` isn't
+ # already loaded but the caller has realm available from another source.
+ realm_url: str | None = None,
+) -> str:
"""
Absolute URLs are used to simplify logic for applications that
won't be served by browsers, such as rendering GCM notifications.
@@ -150,7 +155,9 @@ def absolute_avatar_url(user_profile: UserProfile) -> str:
avatar = avatar_url(user_profile)
# avatar_url can return None if client_gravatar=True, however here we use the default value of False
assert avatar is not None
- return urljoin(user_profile.realm.url, avatar)
+ if realm_url is None:
+ realm_url = user_profile.realm.url
+ return urljoin(realm_url, avatar)
def is_avatar_new(ldap_avatar: bytes, user_profile: UserProfile) -> bool:
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py
index c8d9c0582c4..c191fc95f49 100644
--- a/zerver/lib/push_notifications.py
+++ b/zerver/lib/push_notifications.py
@@ -1205,7 +1205,7 @@ def get_message_payload_gcm(
sender_avatar_url = get_avatar_for_inaccessible_user()
sender_name = str(UserProfile.INACCESSIBLE_USER_NAME)
else:
- sender_avatar_url = absolute_avatar_url(message.sender)
+ sender_avatar_url = absolute_avatar_url(message.sender, message.realm.url)
sender_name = message.sender.full_name
message_payload = dict(copy.deepcopy(message_payload))
diff --git a/zerver/tests/test_handle_push_notification.py b/zerver/tests/test_handle_push_notification.py
index ba404016dc2..d3ef534570a 100644
--- a/zerver/tests/test_handle_push_notification.py
+++ b/zerver/tests/test_handle_push_notification.py
@@ -612,7 +612,6 @@ class HandlePushNotificationTest(PushNotificationTestCase):
# * 1 : `get_user_profile_by_id`
# * 2 : `access_message_and_usermessage` (Fetch Message + UserMessage)
# * 1 : update fetched user_message flag
- # * 1 : fetch `realm` in `get_message_payload_gcm` (TODO: avoid this)
# * 3 : fetch PushDeviceToken, update RealmCount, fetch PushDevice
message = self.get_message(
Recipient.PERSONAL,
@@ -621,7 +620,7 @@ class HandlePushNotificationTest(PushNotificationTestCase):
)
UserMessage.objects.create(user_profile=self.user_profile, message=message)
missed_message = {"message_id": message.id, "trigger": NotificationTriggers.DIRECT_MESSAGE}
- test_end_to_end(missed_message, 8)
+ test_end_to_end(missed_message, 7)
# Group DM
# 1 extra query than prev for `get_display_recipient` in `get_message_payload`.
@@ -634,7 +633,7 @@ class HandlePushNotificationTest(PushNotificationTestCase):
)
UserMessage.objects.create(user_profile=self.user_profile, message=message)
missed_message = {"message_id": message.id, "trigger": NotificationTriggers.DIRECT_MESSAGE}
- test_end_to_end(missed_message, 9)
+ test_end_to_end(missed_message, 8)
# Channel message
# 3 extra queries than 1:1 DM
@@ -645,7 +644,7 @@ class HandlePushNotificationTest(PushNotificationTestCase):
message = self.get_message(Recipient.STREAM, channel.id, realm.id)
UserMessage.objects.create(user_profile=self.user_profile, message=message)
missed_message = {"message_id": message.id, "trigger": NotificationTriggers.STREAM_PUSH}
- test_end_to_end(missed_message, 11)
+ test_end_to_end(missed_message, 10)
# Channel message: private channel + user-group mention
# 3 extra queries than prev:
@@ -661,7 +660,7 @@ class HandlePushNotificationTest(PushNotificationTestCase):
"trigger": NotificationTriggers.MENTION,
"mentioned_user_group_id": user_group.id,
}
- test_end_to_end(missed_message, 14)
+ test_end_to_end(missed_message, 13)
def test_send_remove_notifications_to_bouncer(self) -> None:
self.setup_apns_tokens()