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.

message: Pass args to select_related call for Message objects.

Sahil Batra36f8aba7db5c
diff --git a/zerver/lib/message.py b/zerver/lib/message.py
index 2e0a3bfe284..ecb29e252c5 100644
--- a/zerver/lib/message.py
+++ b/zerver/lib/message.py
@@ -596,7 +596,7 @@ class MessageDict:
# of going to the DB here should be overshadowed by the cost of rendering
# and updating the row.
# TODO: see #1379 to eliminate Markdown dependencies
- message = Message.objects.select_related().get(id=message_id)
+ message = Message.objects.select_related("sender", "sender__realm").get(id=message_id)
assert message is not None # Hint for mypy.
# It's unfortunate that we need to have side effects on the message
@@ -776,7 +776,7 @@ def access_message(
pass lock_message when inside a @transaction.atomic block.
"""
try:
- base_query = Message.objects.select_related()
+ base_query = Message.objects.select_related(*Message.DEFAULT_SELECT_RELATED)
if lock_message:
# We want to lock only the `Message` row, and not the related fields
# because the `Message` row only has a possibility of races.
@@ -808,7 +808,7 @@ def access_web_public_message(
raise MissingAuthenticationError
try:
- message = Message.objects.select_related().get(id=message_id)
+ message = Message.objects.select_related(*Message.DEFAULT_SELECT_RELATED).get(id=message_id)
except Message.DoesNotExist:
raise MissingAuthenticationError
diff --git a/zerver/models.py b/zerver/models.py
index 69ec1c4476d..ffadeca90ca 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -3015,6 +3015,8 @@ class Message(AbstractMessage):
search_tsvector = SearchVectorField(null=True)
+ DEFAULT_SELECT_RELATED = ["sender", "sender__realm", "realm", "recipient", "sending_client"]
+
def topic_name(self) -> str:
"""
Please start using this helper to facilitate an