Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
importer/slack: Collapse whitespace in thread topic names.
PieterCK5679975cb080
diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.pyindex ea1ebf22273..1e06e61135b 100644--- a/zerver/data_import/slack.py+++ b/zerver/data_import/slack.py@@ -982,10 +982,15 @@ def get_zulip_thread_topic_name(e.g "2024-05-22 Hello this is a long message that will be c… (1)""""thread_date = thread_ts.date().isoformat()+ # Topic names must be a single line, so collapse each run of whitespace+ # before truncating; otherwise blank lines in the middle of the message+ # would consume most of the snippet.+ content_for_topic_name = " ".join(message_content.split())+ topic_name_without_counter = f"{thread_date} {content_for_topic_name}".strip()# Truncatetruncated_zulip_topic_name = truncate_content(- f"{thread_date} {message_content}".strip(), MAX_TOPIC_NAME_LENGTH, "…"+ topic_name_without_counter, MAX_TOPIC_NAME_LENGTH, "…")collision = thread_counter[truncated_zulip_topic_name]thread_counter[truncated_zulip_topic_name] += 1@@ -994,9 +999,7 @@ def get_zulip_thread_topic_name(# Important: The count is at the end, after …, so we need to# subtract its length when doing truncation.final_topic_name = (- truncate_content(- f"{thread_date} {message_content}".strip(), MAX_TOPIC_NAME_LENGTH - len(f"{count}"), "…"- )+ truncate_content(topic_name_without_counter, MAX_TOPIC_NAME_LENGTH - len(f"{count}"), "…")+ f"{count}")return final_topic_namediff --git a/zerver/tests/fixtures/slack_fixtures/exported_messages_fixtures/threads_with_whitespace_in_topic_name.json b/zerver/tests/fixtures/slack_fixtures/exported_messages_fixtures/threads_with_whitespace_in_topic_name.jsonnew file mode 100644index 00000000000..68a4a66533e--- /dev/null+++ b/zerver/tests/fixtures/slack_fixtures/exported_messages_fixtures/threads_with_whitespace_in_topic_name.json@@ -0,0 +1,32 @@+[+ {+ "text": "Hi\n\nthere\tfriend",+ "user": "U061A5N1G",+ "ts": "1439868294.000008",+ "thread_ts": "1439868294.000008",+ "channel_name": "random"+ },+ {+ "text": "hello",+ "user": "U061A1R2R",+ "ts": "1439869294.000008",+ "parent_user_id": "U061A5N1G",+ "thread_ts": "1439868294.000008",+ "channel_name": "random"+ },+ {+ "text": "Release notes:\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n- fixed the thing",+ "user": "U061A1R2R",+ "ts": "1547139200.000002",+ "thread_ts": "1547139200.000002",+ "channel_name": "random"+ },+ {+ "text": "nice",+ "user": "U061A5N1G",+ "ts": "1547149200.000002",+ "parent_user_id": "U061A1R2R",+ "thread_ts": "1547139200.000002",+ "channel_name": "random"+ }+]diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.pyindex b0b74f5122f..ea59d6ccbbc 100644--- a/zerver/tests/test_slack_importer.py+++ b/zerver/tests/test_slack_importer.py@@ -1845,6 +1845,49 @@ message body textself.assertEqual(zerver_message[0][EXPORT_TOPIC_NAME], MAIN_SLACK_IMPORT_TOPIC)self.assertEqual(zerver_message[1][EXPORT_TOPIC_NAME], expected_thread_1_topic_name)+ def test_convert_thread_topic_name_with_whitespace(self) -> None:+ slack_recipient_name_to_zulip_recipient_id = {+ "random": 2,+ "general": 1,+ }+ conversion_result = self.run_channel_message_to_zerver_message_with_fixtures(+ ["threads_with_whitespace_in_topic_name"],+ slack_recipient_name_to_zulip_recipient_id=slack_recipient_name_to_zulip_recipient_id,+ )++ zerver_message = conversion_result.zerver_message++ self.assert_length(zerver_message, 4)++ ### THREAD 1 CONVERSATION ###+ # Newlines and tabs are not valid topic name characters, so each run+ # of whitespace in the message snippet becomes a single space.+ expected_thread_1_topic_name = "2015-08-18 Hi there friend"+ thread_1_topic_link_syntax = get_stream_topic_link_syntax(+ slack_recipient_name_to_zulip_recipient_id["random"],+ "random",+ expected_thread_1_topic_name,+ )+ original_thread_1_message_1_content = "Hi\n\nthere\tfriend"+ expected_thread_1_message_1_content = f"""+{original_thread_1_message_1_content}++*1 reply in {thread_1_topic_link_syntax}*+""".strip()++ # The message itself keeps its original whitespace; only the topic+ # name derived from it is collapsed.+ self.assertEqual(zerver_message[0]["content"], expected_thread_1_message_1_content)+ self.assertEqual(zerver_message[0][EXPORT_TOPIC_NAME], MAIN_SLACK_IMPORT_TOPIC)+ self.assertEqual(zerver_message[1][EXPORT_TOPIC_NAME], expected_thread_1_topic_name)++ ### THREAD 2 CONVERSATION ###+ # Collapsing happens before truncation, so a long run of blank lines+ # in the middle of the message doesn't consume the whole snippet.+ expected_thread_2_topic_name = "2019-01-10 Release notes: - fixed the thing"+ self.assertEqual(zerver_message[2][EXPORT_TOPIC_NAME], MAIN_SLACK_IMPORT_TOPIC)+ self.assertEqual(zerver_message[3][EXPORT_TOPIC_NAME], expected_thread_2_topic_name)+@mock.patch("zerver.data_import.slack.build_usermessages", return_value=(2, 4))def test_channel_message_to_zerver_message_with_integration_bots(self, mock_build_usermessage: mock.Mock