Slack importer: Map Slack channel mentions to Zulip stream mentions.

This commit is contained in:
rht
2018-04-06 23:00:05 +01:00
committed by Tim Abbott
parent 035c440ff3
commit 630adb406b
2 changed files with 16 additions and 5 deletions

View File

@@ -28,7 +28,7 @@ from zerver.lib.emoji import NAME_TO_CODEPOINT_PATH
# stubs # stubs
ZerverFieldsT = Dict[str, Any] ZerverFieldsT = Dict[str, Any]
AddedUsersT = Dict[str, int] AddedUsersT = Dict[str, int]
AddedChannelsT = Dict[str, int] AddedChannelsT = Dict[str, Tuple[str, int]]
AddedRecipientsT = Dict[str, int] AddedRecipientsT = Dict[str, int]
def rm_tree(path: str) -> None: def rm_tree(path: str) -> None:
@@ -47,7 +47,7 @@ def slack_workspace_to_realm(domain_name: str, realm_id: int, user_list: List[Ze
1. realm, Converted Realm data 1. realm, Converted Realm data
2. added_users, which is a dictionary to map from slack user id to zulip user id 2. added_users, which is a dictionary to map from slack user id to zulip user id
3. added_recipient, which is a dictionary to map from channel name to zulip recipient_id 3. added_recipient, which is a dictionary to map from channel name to zulip recipient_id
4. added_channels, which is a dictionary to map from channel name to zulip stream_id 4. added_channels, which is a dictionary to map from channel name to channel id, zulip stream_id
5. avatars, which is list to map avatars to zulip avatar records.json 5. avatars, which is list to map avatars to zulip avatar records.json
6. emoji_url_map, which is maps emoji name to its slack url 6. emoji_url_map, which is maps emoji name to its slack url
""" """
@@ -301,7 +301,7 @@ def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: A
Returns: Returns:
1. zerver_defaultstream, which is a list of the default streams 1. zerver_defaultstream, which is a list of the default streams
2. zerver_stream, while is a list of all streams 2. zerver_stream, while is a list of all streams
3. added_channels, which is a dictionary to map from channel name to zulip stream_id 3. added_channels, which is a dictionary to map from channel name to channel id, zulip stream_id
4. zerver_subscription, which is a list of the subscriptions 4. zerver_subscription, which is a list of the subscriptions
5. zerver_recipient, which is a list of the recipients 5. zerver_recipient, which is a list of the recipients
6. added_recipient, which is a dictionary to map from channel name to zulip recipient_id 6. added_recipient, which is a dictionary to map from channel name to zulip recipient_id
@@ -352,7 +352,7 @@ def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: A
defaultstream_id += 1 defaultstream_id += 1
zerver_stream.append(stream) zerver_stream.append(stream)
added_channels[stream['name']] = stream_id added_channels[stream['name']] = (channel['id'], stream_id)
# construct the recipient object and append it to zerver_recipient # construct the recipient object and append it to zerver_recipient
# type 1: private # type 1: private
@@ -492,7 +492,9 @@ def convert_slack_workspace_messages(slack_data_dir: str, users: List[ZerverFiel
reactions = channel_message_to_zerver_message(realm_id, users, added_users, reactions = channel_message_to_zerver_message(realm_id, users, added_users,
added_recipient, all_messages, added_recipient, all_messages,
zerver_realmemoji, zerver_realmemoji,
realm['zerver_subscription'], domain_name) realm['zerver_subscription'],
added_channels,
domain_name)
logging.info('######### IMPORTING MESSAGES FINISHED #########\n') logging.info('######### IMPORTING MESSAGES FINISHED #########\n')
@@ -522,6 +524,7 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT],
all_messages: List[ZerverFieldsT], all_messages: List[ZerverFieldsT],
zerver_realmemoji: List[ZerverFieldsT], zerver_realmemoji: List[ZerverFieldsT],
zerver_subscription: List[ZerverFieldsT], zerver_subscription: List[ZerverFieldsT],
added_channels: AddedChannelsT,
domain_name: str) -> Tuple[List[ZerverFieldsT], domain_name: str) -> Tuple[List[ZerverFieldsT],
List[ZerverFieldsT], List[ZerverFieldsT],
List[ZerverFieldsT], List[ZerverFieldsT],
@@ -556,6 +559,7 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT],
has_attachment = has_image = False has_attachment = has_image = False
content, mentioned_users_id, has_link = convert_to_zulip_markdown(message['text'], content, mentioned_users_id, has_link = convert_to_zulip_markdown(message['text'],
users, users,
added_channels,
added_users) added_users)
rendered_content = None rendered_content = None

View File

@@ -4,6 +4,7 @@ from typing import Any, Dict, Tuple, List
# stubs # stubs
ZerverFieldsT = Dict[str, Any] ZerverFieldsT = Dict[str, Any]
AddedUsersT = Dict[str, int] AddedUsersT = Dict[str, int]
AddedChannelsT = Dict[str, Tuple[str, int]]
# Slack link can be in the format <http://www.foo.com|www.foo.com> and <http://foo.com/> # Slack link can be in the format <http://www.foo.com|www.foo.com> and <http://foo.com/>
LINK_REGEX = r""" LINK_REGEX = r"""
@@ -66,6 +67,7 @@ def get_user_full_name(user: ZerverFieldsT) -> str:
# Markdown mapping # Markdown mapping
def convert_to_zulip_markdown(text: str, users: List[ZerverFieldsT], def convert_to_zulip_markdown(text: str, users: List[ZerverFieldsT],
added_channels: AddedChannelsT,
added_users: AddedUsersT) -> Tuple[str, List[int], bool]: added_users: AddedUsersT) -> Tuple[str, List[int], bool]:
mentioned_users_id = [] mentioned_users_id = []
text = convert_markdown_syntax(text, SLACK_BOLD_REGEX, "**") text = convert_markdown_syntax(text, SLACK_BOLD_REGEX, "**")
@@ -80,6 +82,11 @@ def convert_to_zulip_markdown(text: str, users: List[ZerverFieldsT],
text = text.replace('<!channel>', '@**all**') text = text.replace('<!channel>', '@**all**')
text = text.replace('<!here>', '@**all**') text = text.replace('<!here>', '@**all**')
# Map Slack channel mention: '<#C5Z73A7RA|general>' to '#**general**'
for cname, ids in added_channels.items():
cid = ids[0]
text = text.replace('<#%s|%s>' % (cid, cname), '#**' + cname + '**')
tokens = text.split(' ') tokens = text.split(' ')
for iterator in range(len(tokens)): for iterator in range(len(tokens)):