Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
import_realm: Don't flag messages whose attachments aren't imported.
PieterCK74b84689e883
diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.pyindex 78cdc54ab3b..f0ff114701f 100644--- a/zerver/lib/import_realm.py+++ b/zerver/lib/import_realm.py@@ -361,6 +361,18 @@ def clear_archived_moderation_or_announcement_channels(data: ImportedTableData)realm_dict[field] = None+def fix_message_has_attachment(data: ImportedTableData) -> None:+ """+ A message can be flagged as having an attachment that isn't in the+ export: third-party converters may flag one whose download failed,+ and Zulip keeps the flag when a user deletes an uploaded file. Only+ messages that some imported attachment belongs to have an+ attachment.+ """+ for message in data["zerver_message"]:+ message["has_attachment"] = message["id"] in message_id_to_attachments["zerver_message"]++def fix_upload_links(data: ImportedTableData, message_table: TableName) -> None:"""Because the URLs for uploaded files encode the realm ID of the@@ -2470,6 +2482,7 @@ def _process_message_file(re_map_foreign_keys(data, "zerver_message", "recipient", related_table="recipient")re_map_foreign_keys(data, "zerver_message", "sending_client", related_table="client")fix_datetime_fields(data, "zerver_message")+ fix_message_has_attachment(data)fix_upload_links(data, "zerver_message")# We already create mappings for zerver_message idsdiff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex 0c8aaa959b5..4cb6bf3e863 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -2690,6 +2690,35 @@ class RealmImportExportTest(ExportFile):self.assertTrue(os.path.isfile(os.path.join(full_upload_path, "night_logo.png")))self.assertEqual(imported_realm.night_logo_source, Realm.LOGO_UPLOADED)+ def test_import_message_with_deleted_attachment(self) -> None:+ hamlet = self.example_user("hamlet")+ self.login_user(hamlet)+ kept_url = upload_message_attachment("kept.txt", "text/plain", b"zulip!", hamlet)[0]+ deleted_url = upload_message_attachment("deleted.txt", "text/plain", b"zulip!", hamlet)[0]+ self.send_stream_message(hamlet, "Denmark", f"[kept.txt]({kept_url})", "kept upload")+ deleted_message_id = self.send_stream_message(+ hamlet, "Denmark", f"[deleted.txt]({deleted_url})", "deleted upload"+ )+ deleted_attachment = Attachment.objects.get(+ path_id=deleted_url.removeprefix("/user_uploads/")+ )+ result = self.client_delete(f"/json/attachments/{deleted_attachment.id}")+ self.assert_json_success(result)+ self.assertTrue(Message.objects.get(id=deleted_message_id).has_attachment)++ self.export_realm_and_create_auditlog(hamlet.realm)+ with self.settings(BILLING_ENABLED=False), self.assertLogs(level="INFO"):+ do_import_realm(get_output_dir(), "test-zulip")+ imported_realm = Realm.objects.get(string_id="test-zulip")++ imported_messages = Message.objects.filter(+ realm=imported_realm, **{f"{DB_TOPIC_NAME}__in": ["kept upload", "deleted upload"]}+ )+ self.assertEqual(+ {message.topic_name(): message.has_attachment for message in imported_messages},+ {"kept upload": True, "deleted upload": False},+ )+@use_s3_backenddef test_import_files_from_s3(self) -> None:uploads_bucket, avatar_bucket = create_s3_buckets(diff --git a/zerver/tests/test_microsoft_teams_importer.py b/zerver/tests/test_microsoft_teams_importer.pyindex 32c0870312a..2ee088ea423 100644--- a/zerver/tests/test_microsoft_teams_importer.py+++ b/zerver/tests/test_microsoft_teams_importer.py@@ -516,14 +516,14 @@ class MicrosoftTeamsImporterIntegrationTest(MicrosoftTeamsImportTestCase):)message_ids_with_attachment.add(message_with_attachment.id)- # Make sure no weird message conversion with wrong has_attachment- # and has_image property.+ # No message claims an attachment it doesn't have, e.g. because+ # its file failed to download.self.assertSetEqual(message_ids_with_attachment,set(- Message.objects.filter(- realm=test_realm, has_attachment=True, has_image=True- ).values_list("id", flat=True)+ Message.objects.filter(realm=test_realm, has_attachment=True).values_list(+ "id", flat=True+ )),)