Zerver: Improve topic names for Slack threads

This commit is contained in:
CIC4DA 2024-01-14 20:25:57 +05:30
parent fb7d77545f
commit cc3b801abf
2 changed files with 23 additions and 14 deletions

View File

@ -57,6 +57,7 @@ from zerver.models import (
Recipient,
UserProfile,
)
from zerver.models.constants import MAX_TOPIC_NAME_LENGTH
SlackToZulipUserIDT: TypeAlias = Dict[str, int]
AddedChannelsT: TypeAlias = Dict[str, Tuple[str, int]]
@ -976,20 +977,23 @@ def channel_message_to_zerver_message(
# Slack's unthreaded messages go into a single topic, while
# threads each generate a unique topic labeled by the date and
# a counter among topics on that day.
# message content.
topic_name = "imported from Slack"
if convert_slack_threads and "thread_ts" in message:
thread_ts = datetime.fromtimestamp(float(message["thread_ts"]), tz=timezone.utc)
thread_ts_str = thread_ts.strftime(r"%Y/%m/%d %H:%M:%S")
# The topic name is "2015-08-18 Slack thread 2", where the counter at the end is to disambiguate
# threads with the same date.
thread_message = content[: min(MAX_TOPIC_NAME_LENGTH, len(content))]
# The topic name is "2015-08-18 content[:49]" or "2015-08-18 content[:48]…"
# using the unicode ellipsis character (…) -- it's only one character long
if thread_ts_str in thread_map:
topic_name = thread_map[thread_ts_str]
else:
thread_date = thread_ts.strftime(r"%Y-%m-%d")
thread_counter[thread_date] += 1
count = thread_counter[thread_date]
topic_name = f"{thread_date} Slack thread {count}"
topic_name = f"{thread_date} {thread_message}"
if len(topic_name) > MAX_TOPIC_NAME_LENGTH:
topic_name = topic_name[: (MAX_TOPIC_NAME_LENGTH - 1)] + ""
thread_map[thread_ts_str] = topic_name
zulip_message = build_message(

View File

@ -1131,7 +1131,7 @@ class SlackImporter(ZulipTestCase):
"channel_name": "random",
},
{
"text": "random",
"text": "A random text smaller than 60 characters",
"user": "U061A5N1G",
"ts": "1439868294.000008",
# A different Thread!
@ -1139,7 +1139,7 @@ class SlackImporter(ZulipTestCase):
"channel_name": "random",
},
{
"text": "random",
"text": "A random text which is greater than 60 characters is used",
"user": "U061A5N1G",
"ts": "1439868295.000008",
# Another different Thread!
@ -1190,13 +1190,18 @@ class SlackImporter(ZulipTestCase):
self.assertEqual(zerver_message[0]["content"], "@**Jane**: hey!")
self.assertEqual(zerver_message[0]["has_link"], False)
self.assertEqual(zerver_message[1]["content"], "random")
self.assertEqual(zerver_message[1][EXPORT_TOPIC_NAME], "2015-06-12 Slack thread 1")
self.assertEqual(zerver_message[2][EXPORT_TOPIC_NAME], "2015-06-12 Slack thread 1")
# A new thread with a different date from 2015-06-12, starts the counter from 1.
self.assertEqual(zerver_message[3][EXPORT_TOPIC_NAME], "2015-08-18 Slack thread 1")
# A new thread with a different timestamp, but the same date as 2015-08-18, starts the
# counter from 2.
self.assertEqual(zerver_message[4][EXPORT_TOPIC_NAME], "2015-08-18 Slack thread 2")
self.assertEqual(zerver_message[1][EXPORT_TOPIC_NAME], "2015-06-12 random")
self.assertEqual(zerver_message[2][EXPORT_TOPIC_NAME], "2015-06-12 random")
# A new thread with a different date from 2015-06-12, with content length smaller than 60 characters.
self.assertEqual(
zerver_message[3][EXPORT_TOPIC_NAME],
"2015-08-18 A random text smaller than 60 characters",
)
# A new thread with a different timestamp, but the same date as 2015-08-18, with content length greater than 60 characters.
self.assertEqual(
zerver_message[4][EXPORT_TOPIC_NAME],
"2015-08-18 A random text which is greater than 60 character…",
)
self.assertEqual(
zerver_message[1]["recipient"], slack_recipient_name_to_zulip_recipient_id["random"]
)