Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
uploads: Avoid redundant queries in do_claim_attachments.
Ahmed Tabishfa2e408cba49
diff --git a/zerver/actions/uploads.py b/zerver/actions/uploads.pyindex fa7facd719b..bef2bbb1201 100644--- a/zerver/actions/uploads.py+++ b/zerver/actions/uploads.py@@ -29,25 +29,29 @@ class AttachmentChangeResult:def do_claim_attachments(message: Message | ScheduledMessage, potential_path_ids: list[str]) -> bool:+ if len(potential_path_ids) == 0:+ return False+claimed = False- for path_id in potential_path_ids:- user_profile = message.sender- is_message_realm_public = False- is_message_web_public = False- if isinstance(message, Message):- is_channel_message = message.is_channel_message- else:- assert isinstance(message, ScheduledMessage)- is_channel_message = message.is_channel_message()-- if is_channel_message:- stream = Stream.objects.get(id=message.recipient.type_id)- is_message_realm_public = stream.is_public()- is_message_web_public = (- user_profile.realm.web_public_streams_enabled() and stream.is_web_public- )+ user_profile = message.sender+ is_message_realm_public = False+ is_message_web_public = False+ if isinstance(message, Message):+ is_channel_message = message.is_channel_message+ else:+ assert isinstance(message, ScheduledMessage)+ is_channel_message = message.is_channel_message()++ if is_channel_message:+ stream = Stream.objects.get(id=message.recipient.type_id)+ is_message_realm_public = stream.is_public()+ is_message_web_public = (+ user_profile.realm.web_public_streams_enabled() and stream.is_web_public+ )- if not validate_attachment_request(user_profile, path_id)[0]:+ for path_id in potential_path_ids:+ is_valid, attachment = validate_attachment_request(user_profile, path_id)+ if not is_valid:# Technically, there are 2 cases here:# * The user put something in their message that has the form# of an upload URL, but does not actually correspond to a previously@@ -65,9 +69,10 @@ def do_claim_attachments()continue+ assert attachment is not Noneclaimed = Trueattachment = claim_attachment(- path_id, message, is_message_realm_public, is_message_web_public+ attachment, message, is_message_realm_public, is_message_web_public)if not isinstance(message, ScheduledMessage):# attachment update events don't say anything about scheduled messages,diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.pyindex 692fa0cf56a..6cef606b7b9 100644--- a/zerver/lib/upload/__init__.py+++ b/zerver/lib/upload/__init__.py@@ -333,12 +333,11 @@ def upload_message_attachment(def claim_attachment(- path_id: str,+ attachment: Attachment,message: Message | ScheduledMessage,is_message_realm_public: bool,is_message_web_public: bool = False,) -> Attachment:- attachment = Attachment.objects.get(path_id=path_id)if isinstance(message, ScheduledMessage):attachment.scheduled_messages.add(message)# Setting the is_web_public and is_realm_public flags would be incorrect@@ -350,7 +349,7 @@ def claim_attachment(attachment.messages.add(message)attachment.is_web_public = attachment.is_web_public or is_message_web_publicattachment.is_realm_public = attachment.is_realm_public or is_message_realm_public- attachment.save()+ attachment.save(update_fields=["is_web_public", "is_realm_public"])return attachmentdiff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex 4cb6bf3e863..e67336d622b 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -205,7 +205,7 @@ class ExportFile(ZulipTestCase):url = upload_message_attachment("dummy.txt", "text/plain", b"zulip!", user_profile)[0]attachment_path_id = url.replace("/user_uploads/", "")claim_attachment(- path_id=attachment_path_id,+ attachment=Attachment.objects.get(path_id=attachment_path_id),message=message,is_message_realm_public=True,)@@ -484,7 +484,7 @@ class RealmImportExportTest(ExportFile):url = upload_message_attachment("dummy.txt", "text/plain", b"zulip!", user_profile)[0]attachment_path_id = url.replace("/user_uploads/", "")attachment = claim_attachment(- path_id=attachment_path_id,+ attachment=Attachment.objects.get(path_id=attachment_path_id),message=Message.objects.get(id=personal_message_id),is_message_realm_public=True,)