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.

import_realm: Fix third-party messages' has_image and has_link.

PieterCK09d0e3b8fd24
diff --git a/zerver/data_import/microsoft_teams.py b/zerver/data_import/microsoft_teams.py
index d2bea7e4c53..ba0be8fbec2 100644
--- a/zerver/data_import/microsoft_teams.py
+++ b/zerver/data_import/microsoft_teams.py
@@ -644,17 +644,6 @@ def process_messages(
content = attachment_result.updated_content
accumulated_batch_upload_records += attachment_result.upload_records
- if attachment_result.upload_records != []:
- # Currently process_hosted_content_attachments only supports processeing
- # images so has_image is always true in this case.
- has_image = True
- has_link = True
- has_attachments = True
- else:
- has_image = False
- has_link = False
- has_attachments = False
-
zulip_message = build_message(
topic_name=topic_name,
date_sent=get_timestamp_from_message(message),
@@ -665,9 +654,10 @@ def process_messages(
recipient_id=recipient_id,
realm_id=realm_id,
is_channel_message=not is_direct_message_type,
- has_image=has_image,
- has_link=has_link,
- has_attachment=has_attachments,
+ # Import will correct the message's has_image and has_link attributes.
+ has_image=False,
+ has_link=False,
+ has_attachment=attachment_result.upload_records != [],
is_direct_message_type=is_direct_message_type,
)
zerver_messages.append(zulip_message)
diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.py
index f070eb4425e..7c87b22cc1a 100644
--- a/zerver/lib/import_realm.py
+++ b/zerver/lib/import_realm.py
@@ -487,7 +487,7 @@ def fix_customprofilefield(data: ImportedTableData) -> None:
item["value"] = orjson.dumps(new_id_list).decode()
-def fix_message_rendered_content(
+def fix_message_content_attributes(
realm: Realm,
sender_map: dict[int, Record],
messages: list[Record],
@@ -496,6 +496,8 @@ def fix_message_rendered_content(
) -> None:
"""
This function sets the rendered_content of the messages we're importing.
+ For messages imported from a third-party export, it also corrects the
+ has_link and has_image attributes.
"""
for message in messages:
if content_key not in message:
@@ -576,13 +578,16 @@ def fix_message_rendered_content(
realm_alert_words_automaton = None
# This also enqueues thumbnailing for images that are referenced
- rendered_content = markdown_convert(
+ rendering_result = markdown_convert(
content=content,
realm_alert_words_automaton=realm_alert_words_automaton,
message_realm=realm,
sent_by_bot=sent_by_bot,
translate_emoticons=translate_emoticons,
- ).rendered_content
+ )
+ rendered_content = rendering_result.rendered_content
+ message["has_image"] = rendering_result.has_image
+ message["has_link"] = rendering_result.has_link
message[rendered_content_key] = rendered_content
if "scheduled_timestamp" not in message:
@@ -613,7 +618,7 @@ def fix_message_edit_history(
for edit_history_message_dict in edit_history:
edit_history_message_dict["user_id"] = user_id_map[edit_history_message_dict["user_id"]]
- fix_message_rendered_content(
+ fix_message_content_attributes(
realm,
sender_map,
messages=edit_history,
@@ -1985,7 +1990,7 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
fix_upload_links(data, "zerver_scheduledmessage")
- fix_message_rendered_content(
+ fix_message_content_attributes(
realm=realm,
sender_map=sender_map,
messages=data["zerver_scheduledmessage"],
@@ -2324,7 +2329,7 @@ def _process_message_file(
for row in data["zerver_usermessage"]:
assert row["message"] in message_id_map
- fix_message_rendered_content(
+ fix_message_content_attributes(
realm=realm,
sender_map=sender_map,
messages=data["zerver_message"],
diff --git a/zerver/tests/test_microsoft_teams_importer.py b/zerver/tests/test_microsoft_teams_importer.py
index 75006763f13..fb9ad5f5a22 100644
--- a/zerver/tests/test_microsoft_teams_importer.py
+++ b/zerver/tests/test_microsoft_teams_importer.py
@@ -477,20 +477,19 @@ class MicrosoftTeamsImporterIntegrationTest(MicrosoftTeamsImportTestCase):
message_with_attachment.has_image,
attachment.content_type in THUMBNAIL_ACCEPT_IMAGE_TYPES,
)
- self.assertTrue(message_with_attachment.has_link)
assert isinstance(message_with_attachment.rendered_content, str)
self.assertIn(
f"/user_uploads/{attachment.path_id}", message_with_attachment.rendered_content
)
message_ids_with_attachment.add(message_with_attachment.id)
- # Make sure no weird message conversion with wrong has_attachment, has_link
+ # Make sure no weird message conversion with wrong has_attachment
# and has_image property.
self.assertSetEqual(
message_ids_with_attachment,
set(
Message.objects.filter(
- realm=test_realm, has_link=True, has_attachment=True, has_image=True
+ realm=test_realm, has_attachment=True, has_image=True
).values_list("id", flat=True)
),
)