2017-12-08 11:27:42 +01:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import hashlib
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import re
|
2018-02-08 00:06:02 +01:00
|
|
|
import logging
|
2018-02-01 00:56:57 +01:00
|
|
|
import requests
|
2017-12-08 11:27:42 +01:00
|
|
|
|
2018-02-21 10:02:44 +01:00
|
|
|
from django.conf import settings
|
2018-02-09 16:03:18 +01:00
|
|
|
from django.db import connection
|
2017-12-08 11:27:42 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
from typing import Any, Dict, List, Tuple
|
2017-12-14 19:41:14 +01:00
|
|
|
from zerver.models import UserProfile, Realm, Stream, UserMessage, \
|
|
|
|
Subscription, Message, Recipient, DefaultStream
|
2018-01-09 11:46:56 +01:00
|
|
|
from zerver.forms import check_subdomain_available
|
2018-01-11 15:52:31 +01:00
|
|
|
from zerver.lib.slack_message_conversion import convert_to_zulip_markdown, \
|
|
|
|
get_user_full_name
|
2017-12-29 10:57:48 +01:00
|
|
|
|
2017-12-08 11:27:42 +01:00
|
|
|
# stubs
|
|
|
|
ZerverFieldsT = Dict[str, Any]
|
|
|
|
AddedUsersT = Dict[str, int]
|
|
|
|
AddedChannelsT = Dict[str, int]
|
2018-01-17 15:35:24 +01:00
|
|
|
AddedRecipientsT = Dict[str, int]
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
def rm_tree(path: str) -> None:
|
|
|
|
if os.path.exists(path):
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
2018-02-09 16:03:18 +01:00
|
|
|
def idseq(model_class: Any) -> str:
|
|
|
|
return '{}_id_seq'.format(model_class._meta.db_table)
|
|
|
|
|
2018-02-18 02:45:38 +01:00
|
|
|
def allocate_ids(model_class: Any, count: int) -> List[int]:
|
2018-01-20 10:01:17 +01:00
|
|
|
"""
|
|
|
|
Increases the sequence number for a given table by the amount of objects being
|
|
|
|
imported into that table. Hence, this gives a reserved range of ids to import the converted
|
|
|
|
slack objects into the tables.
|
|
|
|
"""
|
2018-02-18 02:45:38 +01:00
|
|
|
conn = connection.cursor()
|
|
|
|
sequence = idseq(model_class)
|
|
|
|
conn.execute("select nextval('%s') from generate_series(1,%s)" %
|
|
|
|
(sequence, str(count)))
|
|
|
|
query = conn.fetchall() # Each element in the result is a tuple like (5,)
|
|
|
|
conn.close()
|
|
|
|
# convert List[Tuple[int]] to List[int]
|
|
|
|
return [item[0] for item in query]
|
2017-12-14 19:41:14 +01:00
|
|
|
|
2018-02-01 00:56:57 +01:00
|
|
|
def slack_workspace_to_realm(REALM_ID: int, user_list: List[ZerverFieldsT],
|
|
|
|
realm_subdomain: str, fixtures_path: str,
|
2018-01-23 19:04:59 +01:00
|
|
|
slack_data_dir: str) -> Tuple[ZerverFieldsT, AddedUsersT,
|
2018-02-17 00:42:59 +01:00
|
|
|
AddedRecipientsT, AddedChannelsT,
|
|
|
|
List[ZerverFieldsT]]:
|
2018-01-23 19:04:59 +01:00
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
1. realm, Converted Realm data
|
|
|
|
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
|
|
|
|
4. added_channels, which is a dictionary to map from channel name to zulip stream_id
|
2018-02-17 00:42:59 +01:00
|
|
|
5. avatars, which is list to map avatars to zulip avatar records.json
|
2018-01-23 19:04:59 +01:00
|
|
|
"""
|
2018-02-21 10:02:44 +01:00
|
|
|
DOMAIN_NAME = settings.EXTERNAL_HOST
|
2018-01-23 19:04:59 +01:00
|
|
|
NOW = float(timezone_now().timestamp())
|
|
|
|
|
2018-01-26 15:33:22 +01:00
|
|
|
zerver_realm = build_zerver_realm(fixtures_path, REALM_ID, realm_subdomain, NOW)
|
2018-01-23 19:04:59 +01:00
|
|
|
|
|
|
|
realm = dict(zerver_client=[{"name": "populate_db", "id": 1},
|
|
|
|
{"name": "website", "id": 2},
|
|
|
|
{"name": "API", "id": 3}],
|
|
|
|
zerver_userpresence=[], # shows last logged in data, which is not available in slack
|
|
|
|
zerver_userprofile_mirrordummy=[],
|
|
|
|
zerver_realmdomain=[{"realm": REALM_ID,
|
|
|
|
"allow_subdomains": False,
|
|
|
|
"domain": DOMAIN_NAME,
|
|
|
|
"id": REALM_ID}],
|
|
|
|
zerver_useractivity=[],
|
|
|
|
zerver_realm=zerver_realm,
|
|
|
|
zerver_huddle=[],
|
|
|
|
zerver_userprofile_crossrealm=[],
|
|
|
|
zerver_useractivityinterval=[],
|
|
|
|
zerver_realmfilter=[],
|
|
|
|
zerver_realmemoji=[])
|
|
|
|
|
2018-02-17 00:42:59 +01:00
|
|
|
zerver_userprofile, avatars, added_users = users_to_zerver_userprofile(
|
|
|
|
slack_data_dir, user_list, REALM_ID, int(NOW), DOMAIN_NAME)
|
2018-01-23 19:04:59 +01:00
|
|
|
channels_to_zerver_stream_fields = channels_to_zerver_stream(slack_data_dir,
|
|
|
|
REALM_ID,
|
|
|
|
added_users,
|
|
|
|
zerver_userprofile)
|
|
|
|
# See https://zulipchat.com/help/set-default-streams-for-new-users
|
|
|
|
# for documentation on zerver_defaultstream
|
|
|
|
realm['zerver_userprofile'] = zerver_userprofile
|
|
|
|
|
|
|
|
realm['zerver_defaultstream'] = channels_to_zerver_stream_fields[0]
|
|
|
|
realm['zerver_stream'] = channels_to_zerver_stream_fields[1]
|
|
|
|
realm['zerver_subscription'] = channels_to_zerver_stream_fields[3]
|
|
|
|
realm['zerver_recipient'] = channels_to_zerver_stream_fields[4]
|
|
|
|
added_channels = channels_to_zerver_stream_fields[2]
|
|
|
|
added_recipient = channels_to_zerver_stream_fields[5]
|
|
|
|
|
2018-02-17 00:42:59 +01:00
|
|
|
return realm, added_users, added_recipient, added_channels, avatars
|
2018-01-23 19:04:59 +01:00
|
|
|
|
2018-01-26 15:33:22 +01:00
|
|
|
def build_zerver_realm(fixtures_path: str, REALM_ID: int, realm_subdomain: str,
|
|
|
|
time: float) -> List[ZerverFieldsT]:
|
2018-01-23 19:04:59 +01:00
|
|
|
|
|
|
|
zerver_realm_skeleton = get_data_file(fixtures_path + 'zerver_realm_skeleton.json')
|
|
|
|
|
|
|
|
zerver_realm_skeleton[0]['id'] = REALM_ID
|
|
|
|
zerver_realm_skeleton[0]['string_id'] = realm_subdomain # subdomain / short_name of realm
|
|
|
|
zerver_realm_skeleton[0]['name'] = realm_subdomain
|
|
|
|
zerver_realm_skeleton[0]['date_created'] = time
|
|
|
|
|
|
|
|
return zerver_realm_skeleton
|
|
|
|
|
2018-02-01 00:56:57 +01:00
|
|
|
def users_to_zerver_userprofile(slack_data_dir: str, users: List[ZerverFieldsT], realm_id: int,
|
|
|
|
timestamp: Any, domain_name: str) -> Tuple[List[ZerverFieldsT],
|
2018-02-17 00:42:59 +01:00
|
|
|
List[ZerverFieldsT],
|
2018-02-01 00:56:57 +01:00
|
|
|
AddedUsersT]:
|
2017-12-08 11:27:42 +01:00
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
1. zerver_userprofile, which is a list of user profile
|
2018-02-17 00:42:59 +01:00
|
|
|
2. avatar_list, which is list to map avatars to zulip avatard records.json
|
|
|
|
3. added_users, which is a dictionary to map from slack user id to zulip
|
2017-12-08 11:27:42 +01:00
|
|
|
user id
|
|
|
|
"""
|
2018-02-08 00:06:02 +01:00
|
|
|
logging.info('######### IMPORTING USERS STARTED #########\n')
|
2018-01-20 10:01:17 +01:00
|
|
|
total_users = len(users)
|
2017-12-08 11:27:42 +01:00
|
|
|
zerver_userprofile = []
|
2018-02-16 23:54:38 +01:00
|
|
|
avatar_list = [] # type: List[ZerverFieldsT]
|
2017-12-08 11:27:42 +01:00
|
|
|
added_users = {}
|
2018-01-20 10:01:17 +01:00
|
|
|
|
2018-02-12 23:26:52 +01:00
|
|
|
user_id_list = allocate_ids(UserProfile, total_users)
|
2018-02-06 22:19:47 +01:00
|
|
|
|
|
|
|
# We have only one primary owner in slack, see link
|
|
|
|
# https://get.slack.help/hc/en-us/articles/201912948-Owners-and-Administrators
|
|
|
|
# This is to import the primary owner first from all the users
|
2018-02-12 23:26:52 +01:00
|
|
|
user_id_count = 0
|
2018-02-06 22:19:47 +01:00
|
|
|
primary_owner_id = user_id_count
|
|
|
|
user_id_count += 1
|
|
|
|
|
2017-12-08 11:27:42 +01:00
|
|
|
for user in users:
|
|
|
|
slack_user_id = user['id']
|
|
|
|
DESKTOP_NOTIFICATION = True
|
|
|
|
|
2018-02-06 22:19:47 +01:00
|
|
|
if user.get('is_primary_owner', False):
|
2018-02-12 23:26:52 +01:00
|
|
|
user_id = user_id_list[primary_owner_id]
|
2018-02-06 22:19:47 +01:00
|
|
|
else:
|
2018-02-12 23:26:52 +01:00
|
|
|
user_id = user_id_list[user_id_count]
|
2018-02-06 22:19:47 +01:00
|
|
|
|
2017-12-08 11:27:42 +01:00
|
|
|
# email
|
2018-01-23 19:04:59 +01:00
|
|
|
email = get_user_email(user, domain_name)
|
2017-12-08 11:27:42 +01:00
|
|
|
|
2018-02-16 23:54:38 +01:00
|
|
|
# avatar
|
|
|
|
# ref: https://chat.zulip.org/help/change-your-avatar
|
|
|
|
avatar_url = build_avatar_url(slack_user_id, user['team_id'],
|
|
|
|
user['profile']['avatar_hash'])
|
|
|
|
avatar_list = build_avatar(user_id, realm_id, email, avatar_url, timestamp, avatar_list)
|
|
|
|
|
2018-02-06 21:02:23 +01:00
|
|
|
# check if user is the admin
|
|
|
|
realm_admin = get_admin(user)
|
|
|
|
|
2017-12-08 11:27:42 +01:00
|
|
|
# timezone
|
2018-01-23 19:04:59 +01:00
|
|
|
timezone = get_user_timezone(user)
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
userprofile = dict(
|
|
|
|
enable_desktop_notifications=DESKTOP_NOTIFICATION,
|
2018-02-06 19:24:05 +01:00
|
|
|
is_staff=False, # 'staff' is for server administrators, which don't exist in Slack.
|
2018-02-16 23:49:44 +01:00
|
|
|
avatar_source='U',
|
2017-12-08 11:27:42 +01:00
|
|
|
is_bot=user.get('is_bot', False),
|
|
|
|
avatar_version=1,
|
|
|
|
default_desktop_notifications=True,
|
|
|
|
timezone=timezone,
|
|
|
|
default_sending_stream=None,
|
|
|
|
enable_offline_email_notifications=True,
|
|
|
|
user_permissions=[], # This is Zulip-specific
|
|
|
|
is_mirror_dummy=False,
|
|
|
|
pointer=-1,
|
|
|
|
default_events_register_stream=None,
|
2018-02-06 21:02:23 +01:00
|
|
|
is_realm_admin=realm_admin,
|
2017-12-08 11:27:42 +01:00
|
|
|
# invites_granted=0, # TODO
|
|
|
|
enter_sends=True,
|
|
|
|
bot_type=1 if user.get('is_bot', False) else None,
|
|
|
|
enable_stream_sounds=False,
|
|
|
|
is_api_super_user=False,
|
|
|
|
rate_limits="",
|
|
|
|
last_login=timestamp,
|
|
|
|
tos_version=None,
|
|
|
|
default_all_public_streams=False,
|
2018-01-07 22:09:02 +01:00
|
|
|
full_name=get_user_full_name(user),
|
2017-12-08 11:27:42 +01:00
|
|
|
twenty_four_hour_time=False,
|
|
|
|
groups=[], # This is Zulip-specific
|
|
|
|
enable_online_push_notifications=False,
|
|
|
|
alert_words="[]",
|
|
|
|
bot_owner=None, # This is Zulip-specific
|
|
|
|
short_name=user['name'],
|
|
|
|
enable_offline_push_notifications=True,
|
|
|
|
left_side_userlist=False,
|
|
|
|
enable_stream_desktop_notifications=False,
|
|
|
|
enable_digest_emails=True,
|
|
|
|
last_pointer_updater="",
|
|
|
|
email=email,
|
2018-02-08 23:59:00 +01:00
|
|
|
realm_name_in_notifications=False,
|
2017-12-08 11:27:42 +01:00
|
|
|
date_joined=timestamp,
|
|
|
|
last_reminder=timestamp,
|
|
|
|
is_superuser=False,
|
|
|
|
tutorial_status="T",
|
|
|
|
default_language="en",
|
|
|
|
enable_sounds=True,
|
|
|
|
pm_content_in_desktop_notifications=True,
|
|
|
|
is_active=not user['deleted'],
|
|
|
|
onboarding_steps="[]",
|
|
|
|
emojiset="google",
|
|
|
|
realm=realm_id,
|
|
|
|
# invites_used=0, # TODO
|
2018-02-06 22:19:47 +01:00
|
|
|
id=user_id)
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
# TODO map the avatar
|
|
|
|
# zerver auto-infer the url from Gravatar instead of from a specified
|
|
|
|
# url; zerver.lib.avatar needs to be patched
|
|
|
|
# profile['image_32'], Slack has 24, 32, 48, 72, 192, 512 size range
|
|
|
|
|
|
|
|
zerver_userprofile.append(userprofile)
|
2018-02-06 22:19:47 +01:00
|
|
|
added_users[slack_user_id] = user_id
|
|
|
|
if not user.get('is_primary_owner', False):
|
|
|
|
user_id_count += 1
|
|
|
|
|
2018-02-08 00:06:02 +01:00
|
|
|
logging.info(u"{} -> {}".format(user['name'], userprofile['email']))
|
|
|
|
logging.info('######### IMPORTING USERS FINISHED #########\n')
|
2018-02-17 00:42:59 +01:00
|
|
|
return zerver_userprofile, avatar_list, added_users
|
2017-12-08 11:27:42 +01:00
|
|
|
|
2018-01-23 19:04:59 +01:00
|
|
|
def get_user_email(user: ZerverFieldsT, domain_name: str) -> str:
|
|
|
|
if 'email' not in user['profile']:
|
|
|
|
email = (hashlib.sha256(user['real_name'].encode()).hexdigest() +
|
|
|
|
"@%s" % (domain_name))
|
|
|
|
else:
|
|
|
|
email = user['profile']['email']
|
|
|
|
return email
|
|
|
|
|
2018-02-16 23:54:38 +01:00
|
|
|
def build_avatar_url(slack_user_id: str, team_id: str, avatar_hash: str) -> str:
|
|
|
|
avatar_url = "https://ca.slack-edge.com/{}-{}-{}".format(team_id, slack_user_id,
|
|
|
|
avatar_hash)
|
|
|
|
return avatar_url
|
|
|
|
|
|
|
|
def build_avatar(zulip_user_id: int, realm_id: int, email: str, avatar_url: str,
|
|
|
|
timestamp: Any, avatar_list: List[ZerverFieldsT]) -> List[ZerverFieldsT]:
|
|
|
|
avatar = dict(
|
|
|
|
path=avatar_url, # Save slack's url here, which is used later while processing
|
|
|
|
realm_id=realm_id,
|
|
|
|
content_type=None,
|
|
|
|
user_profile_id=zulip_user_id,
|
|
|
|
last_modified=timestamp,
|
|
|
|
user_profile_email=email,
|
|
|
|
s3_path="",
|
|
|
|
size="")
|
|
|
|
avatar_list.append(avatar)
|
|
|
|
return avatar_list
|
|
|
|
|
2018-02-06 21:02:23 +01:00
|
|
|
def get_admin(user: ZerverFieldsT) -> bool:
|
|
|
|
admin = user.get('is_admin', False)
|
|
|
|
owner = user.get('is_owner', False)
|
|
|
|
primary_owner = user.get('is_primary_owner', False)
|
|
|
|
|
|
|
|
if admin or owner or primary_owner:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-01-23 19:04:59 +01:00
|
|
|
def get_user_timezone(user: ZerverFieldsT) -> str:
|
|
|
|
_default_timezone = "America/New_York"
|
|
|
|
timezone = user.get("tz", _default_timezone)
|
|
|
|
if timezone is None or '/' not in timezone:
|
|
|
|
timezone = _default_timezone
|
|
|
|
return timezone
|
|
|
|
|
2018-01-06 19:42:18 +01:00
|
|
|
def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: AddedUsersT,
|
2017-12-08 11:27:42 +01:00
|
|
|
zerver_userprofile: List[ZerverFieldsT]) -> Tuple[List[ZerverFieldsT],
|
|
|
|
List[ZerverFieldsT],
|
|
|
|
AddedChannelsT,
|
|
|
|
List[ZerverFieldsT],
|
2018-01-17 15:35:24 +01:00
|
|
|
List[ZerverFieldsT],
|
|
|
|
AddedRecipientsT]:
|
2017-12-08 11:27:42 +01:00
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
1. zerver_defaultstream, which is a list of the default 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
|
|
|
|
4. zerver_subscription, which is a list of the subscriptions
|
|
|
|
5. zerver_recipient, which is a list of the recipients
|
2018-01-23 19:04:59 +01:00
|
|
|
6. added_recipient, which is a dictionary to map from channel name to zulip recipient_id
|
2017-12-08 11:27:42 +01:00
|
|
|
"""
|
2018-02-08 00:06:02 +01:00
|
|
|
logging.info('######### IMPORTING CHANNELS STARTED #########\n')
|
2018-01-23 19:04:59 +01:00
|
|
|
channels = get_data_file(slack_data_dir + '/channels.json')
|
2018-01-20 10:01:17 +01:00
|
|
|
|
2017-12-08 11:27:42 +01:00
|
|
|
added_channels = {}
|
2018-01-20 10:01:17 +01:00
|
|
|
added_recipient = {}
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
zerver_stream = []
|
2018-01-23 19:04:59 +01:00
|
|
|
zerver_subscription = [] # type: List[ZerverFieldsT]
|
2017-12-08 11:27:42 +01:00
|
|
|
zerver_recipient = []
|
|
|
|
zerver_defaultstream = []
|
|
|
|
|
2018-01-20 10:01:17 +01:00
|
|
|
# Pre-compute all the total number of ids to fastword the ids in active db
|
|
|
|
total_channels = len(channels)
|
|
|
|
total_users = len(zerver_userprofile)
|
|
|
|
total_recipients = total_channels + total_users
|
|
|
|
total_subscription = total_users
|
|
|
|
for channel in channels:
|
|
|
|
for member in channel['members']:
|
|
|
|
total_subscription += 1
|
2018-01-17 15:35:24 +01:00
|
|
|
|
2018-02-21 08:38:26 +01:00
|
|
|
stream_id_list = allocate_ids(Stream, total_channels)
|
2018-02-12 23:26:52 +01:00
|
|
|
subscription_id_list = allocate_ids(Subscription, total_subscription)
|
|
|
|
recipient_id_list = allocate_ids(Recipient, total_recipients)
|
2018-02-23 10:16:03 +01:00
|
|
|
# corresponding to channels 'general' and 'random' which are slack specific
|
2018-02-12 23:26:52 +01:00
|
|
|
defaultstream_id_list = allocate_ids(DefaultStream, 2)
|
|
|
|
|
|
|
|
stream_id_count = subscription_id_count = recipient_id_count = defaultstream_id = 0
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
for channel in channels:
|
|
|
|
# slack_channel_id = channel['id']
|
|
|
|
|
|
|
|
# map Slack's topic and purpose content into Zulip's stream description.
|
|
|
|
# WARN This mapping is lossy since the topic.creator, topic.last_set,
|
|
|
|
# purpose.creator, purpose.last_set fields are not preserved.
|
|
|
|
description = "topic: {}\npurpose: {}".format(channel["topic"]["value"],
|
|
|
|
channel["purpose"]["value"])
|
2018-02-12 23:26:52 +01:00
|
|
|
stream_id = stream_id_list[stream_id_count]
|
|
|
|
recipient_id = recipient_id_list[recipient_id_count]
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
# construct the stream object and append it to zerver_stream
|
|
|
|
stream = dict(
|
|
|
|
realm=realm_id,
|
|
|
|
name=channel["name"],
|
|
|
|
deactivated=channel["is_archived"],
|
|
|
|
description=description,
|
2018-02-25 03:49:14 +01:00
|
|
|
invite_only=False, # TODO: private channels are not
|
|
|
|
# exported with Slack's standard plan;
|
|
|
|
# so this field is always false
|
2017-12-08 11:27:42 +01:00
|
|
|
date_created=float(channel["created"]),
|
2018-02-12 23:26:52 +01:00
|
|
|
id=stream_id)
|
2017-12-08 11:27:42 +01:00
|
|
|
|
2018-01-23 19:04:59 +01:00
|
|
|
# construct defaultstream object
|
2018-02-23 10:16:03 +01:00
|
|
|
# slack has the default channel 'general' and 'random'
|
|
|
|
# where every user is subscribed
|
|
|
|
default_channels = ['general', 'random'] # Slack specific
|
|
|
|
if channel['name'] in default_channels:
|
|
|
|
defaultstream = build_defaultstream(channel['name'], realm_id, stream_id,
|
|
|
|
defaultstream_id_list[defaultstream_id])
|
2017-12-08 11:27:42 +01:00
|
|
|
zerver_defaultstream.append(defaultstream)
|
2018-01-23 19:04:59 +01:00
|
|
|
defaultstream_id += 1
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
zerver_stream.append(stream)
|
2018-02-12 23:26:52 +01:00
|
|
|
added_channels[stream['name']] = stream_id
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
# construct the recipient object and append it to zerver_recipient
|
|
|
|
# type 1: private
|
|
|
|
# type 2: stream
|
|
|
|
# type 3: huddle
|
|
|
|
recipient = dict(
|
2018-02-12 23:26:52 +01:00
|
|
|
type_id=stream_id,
|
|
|
|
id=recipient_id,
|
2017-12-08 11:27:42 +01:00
|
|
|
type=2)
|
|
|
|
zerver_recipient.append(recipient)
|
2018-02-12 23:26:52 +01:00
|
|
|
added_recipient[stream['name']] = recipient_id
|
2017-12-08 11:27:42 +01:00
|
|
|
# TOODO add recipients for private message and huddles
|
|
|
|
|
|
|
|
# construct the subscription object and append it to zerver_subscription
|
2018-01-23 19:04:59 +01:00
|
|
|
zerver_subscription, subscription_id_count = build_subscription(channel['members'],
|
|
|
|
zerver_subscription,
|
2018-02-12 23:26:52 +01:00
|
|
|
recipient_id,
|
2018-01-23 19:04:59 +01:00
|
|
|
added_users,
|
2018-02-12 23:26:52 +01:00
|
|
|
subscription_id_list,
|
2018-01-23 19:04:59 +01:00
|
|
|
subscription_id_count)
|
|
|
|
# TOODO add zerver_subscription which correspond to
|
|
|
|
# huddles type recipient
|
|
|
|
# For huddles:
|
|
|
|
# sub['recipient']=recipient['id'] where recipient['type_id']=added_users[member]
|
|
|
|
|
|
|
|
# TOODO do private message subscriptions between each users have to
|
|
|
|
# be generated from scratch?
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
stream_id_count += 1
|
2018-01-17 15:35:24 +01:00
|
|
|
recipient_id_count += 1
|
2018-02-08 00:06:02 +01:00
|
|
|
logging.info(u"{} -> created".format(channel['name']))
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
# TODO map Slack's pins to Zulip's stars
|
|
|
|
# There is the security model that Slack's pins are known to the team owner
|
|
|
|
# as evident from where it is stored at (channels)
|
|
|
|
# "pins": [
|
|
|
|
# {
|
|
|
|
# "id": "1444755381.000003",
|
|
|
|
# "type": "C",
|
|
|
|
# "user": "U061A5N1G",
|
|
|
|
# "owner": "U061A5N1G",
|
|
|
|
# "created": "1444755463"
|
|
|
|
# }
|
|
|
|
# ],
|
|
|
|
|
|
|
|
for user in zerver_userprofile:
|
|
|
|
zulip_user_id = user['id']
|
|
|
|
# this maps the recipients and subscriptions
|
|
|
|
# related to private messages
|
2018-02-12 23:26:52 +01:00
|
|
|
recipient_id = recipient_id_list[recipient_id_count]
|
|
|
|
subscription_id = subscription_id_list[subscription_id_count]
|
|
|
|
|
|
|
|
recipient, sub = build_pm_recipient_sub_from_user(zulip_user_id, recipient_id,
|
|
|
|
subscription_id)
|
2017-12-08 11:27:42 +01:00
|
|
|
zerver_recipient.append(recipient)
|
2018-01-23 19:04:59 +01:00
|
|
|
zerver_subscription.append(sub)
|
|
|
|
subscription_id_count += 1
|
|
|
|
recipient_id_count += 1
|
2017-12-08 11:27:42 +01:00
|
|
|
|
2018-02-08 00:06:02 +01:00
|
|
|
logging.info('######### IMPORTING STREAMS FINISHED #########\n')
|
2018-01-23 19:04:59 +01:00
|
|
|
return zerver_defaultstream, zerver_stream, added_channels, zerver_subscription, \
|
|
|
|
zerver_recipient, added_recipient
|
|
|
|
|
|
|
|
def build_defaultstream(channel_name: str, realm_id: int, stream_id: int,
|
|
|
|
defaultstream_id: int) -> ZerverFieldsT:
|
2018-02-23 10:16:03 +01:00
|
|
|
defaultstream = dict(
|
|
|
|
stream=stream_id,
|
|
|
|
realm=realm_id,
|
|
|
|
id=defaultstream_id)
|
|
|
|
return defaultstream
|
2018-01-23 19:04:59 +01:00
|
|
|
|
|
|
|
def build_pm_recipient_sub_from_user(zulip_user_id: int, recipient_id: int,
|
|
|
|
subscription_id: int) -> Tuple[ZerverFieldsT,
|
|
|
|
ZerverFieldsT]:
|
|
|
|
recipient = dict(
|
|
|
|
type_id=zulip_user_id,
|
|
|
|
id=recipient_id,
|
|
|
|
type=1)
|
|
|
|
|
|
|
|
sub = dict(
|
|
|
|
recipient=recipient_id,
|
|
|
|
notifications=False,
|
|
|
|
color="#c2c2c2",
|
|
|
|
desktop_notifications=True,
|
|
|
|
pin_to_top=False,
|
|
|
|
in_home_view=True,
|
|
|
|
active=True,
|
|
|
|
user_profile=zulip_user_id,
|
|
|
|
id=subscription_id)
|
|
|
|
|
|
|
|
return recipient, sub
|
|
|
|
|
|
|
|
def build_subscription(channel_members: List[str], zerver_subscription: List[ZerverFieldsT],
|
|
|
|
recipient_id: int, added_users: AddedUsersT,
|
2018-02-12 23:26:52 +01:00
|
|
|
subscription_id_list: List[int],
|
|
|
|
subscription_id_count: int) -> Tuple[List[ZerverFieldsT], int]:
|
2018-01-23 19:04:59 +01:00
|
|
|
for member in channel_members:
|
2018-02-12 23:26:52 +01:00
|
|
|
subscription_id = subscription_id_list[subscription_id_count]
|
2017-12-08 11:27:42 +01:00
|
|
|
sub = dict(
|
2018-01-23 19:04:59 +01:00
|
|
|
recipient=recipient_id,
|
2017-12-08 11:27:42 +01:00
|
|
|
notifications=False,
|
|
|
|
color="#c2c2c2",
|
|
|
|
desktop_notifications=True,
|
|
|
|
pin_to_top=False,
|
|
|
|
in_home_view=True,
|
|
|
|
active=True,
|
2018-01-23 19:04:59 +01:00
|
|
|
user_profile=added_users[member],
|
|
|
|
id=subscription_id)
|
|
|
|
# The recipient is a stream for stream-readable message.
|
|
|
|
# proof : https://github.com/zulip/zulip/blob/master/zerver/views/messages.py#L240 &
|
|
|
|
# https://github.com/zulip/zulip/blob/master/zerver/views/messages.py#L324
|
2017-12-08 11:27:42 +01:00
|
|
|
zerver_subscription.append(sub)
|
2018-02-12 23:26:52 +01:00
|
|
|
subscription_id_count += 1
|
|
|
|
return zerver_subscription, subscription_id_count
|
2017-12-08 11:27:42 +01:00
|
|
|
|
2018-02-01 00:56:57 +01:00
|
|
|
def convert_slack_workspace_messages(slack_data_dir: str, users: List[ZerverFieldsT], REALM_ID: int,
|
|
|
|
added_users: AddedUsersT, added_recipient: AddedRecipientsT,
|
|
|
|
added_channels: AddedChannelsT,
|
2018-01-26 15:33:22 +01:00
|
|
|
realm: ZerverFieldsT) -> ZerverFieldsT:
|
2018-01-23 19:04:59 +01:00
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
1. message.json, Converted messages
|
|
|
|
"""
|
|
|
|
# now for message.json
|
|
|
|
message_json = {}
|
|
|
|
zerver_message = [] # type: List[ZerverFieldsT]
|
|
|
|
zerver_usermessage = [] # type: List[ZerverFieldsT]
|
2017-12-08 11:27:42 +01:00
|
|
|
|
2018-02-08 00:06:02 +01:00
|
|
|
logging.info('######### IMPORTING MESSAGES STARTED #########\n')
|
2018-01-23 19:04:59 +01:00
|
|
|
# To pre-compute the total number of messages and usermessages
|
|
|
|
total_messages = 0
|
|
|
|
total_usermessages = 0
|
|
|
|
for channel in added_channels.keys():
|
|
|
|
tm, tum = get_total_messages_and_usermessages(slack_data_dir, channel,
|
|
|
|
realm['zerver_subscription'],
|
|
|
|
added_recipient)
|
|
|
|
total_messages += tm
|
|
|
|
total_usermessages += tum
|
2018-02-12 23:26:52 +01:00
|
|
|
message_id_list = allocate_ids(Message, total_messages)
|
|
|
|
usermessage_id_list = allocate_ids(UserMessage, total_usermessages)
|
|
|
|
|
|
|
|
message_id_count = usermessage_id_count = 0
|
2018-01-23 19:04:59 +01:00
|
|
|
|
|
|
|
constants = [slack_data_dir, REALM_ID]
|
|
|
|
for channel in added_channels.keys():
|
|
|
|
message_id = len(zerver_message) + message_id_count # For the id of the messages
|
|
|
|
usermessage_id = len(zerver_usermessage) + usermessage_id_count
|
2018-02-12 23:26:52 +01:00
|
|
|
id_list = [message_id, usermessage_id, message_id_list, usermessage_id_list]
|
2018-02-01 00:56:57 +01:00
|
|
|
zm, zum = channel_message_to_zerver_message(constants, channel, users,
|
2018-01-23 19:04:59 +01:00
|
|
|
added_users, added_recipient,
|
|
|
|
realm['zerver_subscription'],
|
|
|
|
id_list)
|
|
|
|
zerver_message += zm
|
|
|
|
zerver_usermessage += zum
|
2018-02-08 00:06:02 +01:00
|
|
|
logging.info('######### IMPORTING MESSAGES FINISHED #########\n')
|
2018-01-23 19:04:59 +01:00
|
|
|
|
|
|
|
message_json['zerver_message'] = zerver_message
|
|
|
|
message_json['zerver_usermessage'] = zerver_usermessage
|
|
|
|
|
|
|
|
return message_json
|
|
|
|
|
|
|
|
def get_total_messages_and_usermessages(slack_data_dir: str, channel_name: str,
|
2018-01-20 10:01:17 +01:00
|
|
|
zerver_subscription: List[ZerverFieldsT],
|
|
|
|
added_recipient: AddedRecipientsT) -> Tuple[int, int]:
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
1. message_id, which is total number of messages
|
|
|
|
2. usermessage_id, which is total number of usermessages
|
|
|
|
"""
|
2018-01-23 19:04:59 +01:00
|
|
|
json_names = os.listdir(slack_data_dir + '/' + channel_name)
|
2018-01-20 10:01:17 +01:00
|
|
|
total_messages = 0
|
|
|
|
total_usermessages = 0
|
|
|
|
|
|
|
|
for json_name in json_names:
|
2018-01-23 19:04:59 +01:00
|
|
|
messages = get_data_file(slack_data_dir + '/%s/%s' % (channel_name, json_name))
|
2018-01-20 10:01:17 +01:00
|
|
|
for message in messages:
|
|
|
|
if 'subtype' in message.keys():
|
|
|
|
subtype = message['subtype']
|
|
|
|
if subtype in ["channel_join", "channel_leave", "channel_name"]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for subscription in zerver_subscription:
|
2018-01-23 19:04:59 +01:00
|
|
|
if subscription['recipient'] == added_recipient[channel_name]:
|
2018-01-20 10:01:17 +01:00
|
|
|
total_usermessages += 1
|
|
|
|
total_messages += 1
|
|
|
|
|
|
|
|
return total_messages, total_usermessages
|
|
|
|
|
2017-12-29 10:57:48 +01:00
|
|
|
def channel_message_to_zerver_message(constants: List[Any], channel: str,
|
2018-02-01 00:56:57 +01:00
|
|
|
users: List[ZerverFieldsT], added_users: AddedUsersT,
|
|
|
|
added_recipient: AddedRecipientsT,
|
2017-12-29 10:57:48 +01:00
|
|
|
zerver_subscription: List[ZerverFieldsT],
|
2018-02-12 23:26:52 +01:00
|
|
|
ids: List[Any]) -> Tuple[List[ZerverFieldsT],
|
2017-12-29 10:57:48 +01:00
|
|
|
List[ZerverFieldsT]]:
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
1. zerver_message, which is a list of the messages
|
|
|
|
2. zerver_usermessage, which is a list of the usermessages
|
|
|
|
"""
|
|
|
|
slack_data_dir, REALM_ID = constants
|
2018-02-12 23:26:52 +01:00
|
|
|
message_id_count, usermessage_id_count, message_id_list, usermessage_id_list = ids
|
2017-12-29 10:57:48 +01:00
|
|
|
json_names = os.listdir(slack_data_dir + '/' + channel)
|
|
|
|
zerver_message = []
|
2018-01-23 19:04:59 +01:00
|
|
|
zerver_usermessage = [] # type: List[ZerverFieldsT]
|
2017-12-29 10:57:48 +01:00
|
|
|
|
|
|
|
for json_name in json_names:
|
2018-01-23 19:04:59 +01:00
|
|
|
messages = get_data_file(slack_data_dir + '/%s/%s' % (channel, json_name))
|
2017-12-29 10:57:48 +01:00
|
|
|
for message in messages:
|
2018-02-23 03:16:31 +01:00
|
|
|
|
|
|
|
user = get_message_sending_user(message)
|
|
|
|
if not user:
|
|
|
|
# Ignore messages without user names
|
|
|
|
# These are Sometimes produced by slack
|
|
|
|
continue
|
|
|
|
|
2017-12-29 10:57:48 +01:00
|
|
|
has_attachment = False
|
2018-01-11 15:52:31 +01:00
|
|
|
content, mentioned_users_id, has_link = convert_to_zulip_markdown(message['text'],
|
|
|
|
users,
|
|
|
|
added_users)
|
2017-12-29 10:57:48 +01:00
|
|
|
rendered_content = None
|
|
|
|
if 'subtype' in message.keys():
|
|
|
|
subtype = message['subtype']
|
|
|
|
if subtype in ["channel_join", "channel_leave", "channel_name"]:
|
|
|
|
continue
|
|
|
|
|
2018-01-17 15:35:24 +01:00
|
|
|
recipient_id = added_recipient[channel]
|
2018-02-12 23:26:52 +01:00
|
|
|
message_id = message_id_list[message_id_count]
|
2017-12-29 10:57:48 +01:00
|
|
|
# construct message
|
|
|
|
zulip_message = dict(
|
|
|
|
sending_client=1,
|
|
|
|
rendered_content_version=1, # This is Zulip-specific
|
|
|
|
has_image=message.get('has_image', False),
|
2018-01-23 19:04:59 +01:00
|
|
|
subject='from slack', # This is Zulip-specific
|
2017-12-29 10:57:48 +01:00
|
|
|
pub_date=float(message['ts']),
|
|
|
|
id=message_id,
|
|
|
|
has_attachment=has_attachment, # attachment will be posted in the subsequent message;
|
|
|
|
# this is how Slack does it, i.e. less like email
|
|
|
|
edit_history=None,
|
2018-02-23 03:16:31 +01:00
|
|
|
sender=added_users[user], # map slack id to zulip id
|
2017-12-29 10:57:48 +01:00
|
|
|
content=content,
|
|
|
|
rendered_content=rendered_content, # slack doesn't cache this
|
2018-01-17 15:35:24 +01:00
|
|
|
recipient=recipient_id,
|
2017-12-29 10:57:48 +01:00
|
|
|
last_edit_time=None,
|
|
|
|
has_link=has_link)
|
|
|
|
zerver_message.append(zulip_message)
|
|
|
|
|
|
|
|
# construct usermessages
|
2018-02-12 23:26:52 +01:00
|
|
|
zerver_usermessage, usermessage_id_count = build_zerver_usermessage(
|
|
|
|
zerver_usermessage, usermessage_id_count, usermessage_id_list,
|
|
|
|
zerver_subscription, recipient_id, mentioned_users_id, message_id)
|
|
|
|
|
|
|
|
message_id_count += 1
|
2017-12-29 10:57:48 +01:00
|
|
|
return zerver_message, zerver_usermessage
|
|
|
|
|
2018-01-23 19:04:59 +01:00
|
|
|
def get_message_sending_user(message: ZerverFieldsT) -> str:
|
|
|
|
try:
|
|
|
|
user = message.get('user', message['file']['user'])
|
|
|
|
except KeyError:
|
2018-02-23 03:16:31 +01:00
|
|
|
user = message.get('user')
|
2018-01-23 19:04:59 +01:00
|
|
|
return user
|
|
|
|
|
2018-02-12 23:26:52 +01:00
|
|
|
def build_zerver_usermessage(zerver_usermessage: List[ZerverFieldsT], usermessage_id_count: int,
|
|
|
|
usermessage_id_list: List[int],
|
2018-01-23 19:04:59 +01:00
|
|
|
zerver_subscription: List[ZerverFieldsT], recipient_id: int,
|
|
|
|
mentioned_users_id: List[int],
|
|
|
|
message_id: int) -> Tuple[List[ZerverFieldsT], int]:
|
|
|
|
for subscription in zerver_subscription:
|
|
|
|
if subscription['recipient'] == recipient_id:
|
|
|
|
flags_mask = 1 # For read
|
|
|
|
if subscription['user_profile'] in mentioned_users_id:
|
|
|
|
flags_mask = 9 # For read and mentioned
|
|
|
|
|
|
|
|
usermessage = dict(
|
|
|
|
user_profile=subscription['user_profile'],
|
2018-02-12 23:26:52 +01:00
|
|
|
id=usermessage_id_list[usermessage_id_count],
|
2018-01-23 19:04:59 +01:00
|
|
|
flags_mask=flags_mask,
|
|
|
|
message=message_id)
|
2018-02-12 23:26:52 +01:00
|
|
|
usermessage_id_count += 1
|
2018-01-23 19:04:59 +01:00
|
|
|
zerver_usermessage.append(usermessage)
|
2018-02-12 23:26:52 +01:00
|
|
|
return zerver_usermessage, usermessage_id_count
|
2018-01-23 19:04:59 +01:00
|
|
|
|
2018-02-01 00:56:57 +01:00
|
|
|
def do_convert_data(slack_zip_file: str, realm_subdomain: str, output_dir: str, token: str) -> None:
|
2018-01-09 11:46:56 +01:00
|
|
|
check_subdomain_available(realm_subdomain)
|
2018-01-06 19:42:18 +01:00
|
|
|
slack_data_dir = slack_zip_file.replace('.zip', '')
|
2018-01-20 14:49:40 +01:00
|
|
|
if not os.path.exists(slack_data_dir):
|
|
|
|
os.makedirs(slack_data_dir)
|
2018-02-08 21:38:14 +01:00
|
|
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
# output directory should be empty initially
|
|
|
|
if os.listdir(output_dir):
|
|
|
|
raise Exception('Output directory should be empty!')
|
|
|
|
|
2018-01-20 14:49:40 +01:00
|
|
|
subprocess.check_call(['unzip', '-q', slack_zip_file, '-d', slack_data_dir])
|
2017-12-08 11:27:42 +01:00
|
|
|
# with zipfile.ZipFile(slack_zip_file, 'r') as zip_ref:
|
2018-01-06 19:42:18 +01:00
|
|
|
# zip_ref.extractall(slack_data_dir)
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
script_path = os.path.dirname(os.path.abspath(__file__)) + '/'
|
|
|
|
fixtures_path = script_path + '../fixtures/'
|
|
|
|
|
2018-02-12 23:26:52 +01:00
|
|
|
REALM_ID = allocate_ids(Realm, 1)[0]
|
2018-02-01 00:56:57 +01:00
|
|
|
|
|
|
|
user_list = get_user_data(token)
|
2018-02-17 00:42:59 +01:00
|
|
|
realm, added_users, added_recipient, added_channels, avatar_list = slack_workspace_to_realm(
|
|
|
|
REALM_ID, user_list, realm_subdomain, fixtures_path, slack_data_dir)
|
|
|
|
|
2018-02-01 00:56:57 +01:00
|
|
|
message_json = convert_slack_workspace_messages(slack_data_dir, user_list, REALM_ID,
|
|
|
|
added_users, added_recipient, added_channels,
|
|
|
|
realm)
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
zerver_attachment = [] # type: List[ZerverFieldsT]
|
2018-01-23 19:04:59 +01:00
|
|
|
attachment = {"zerver_attachment": zerver_attachment}
|
2017-12-08 11:27:42 +01:00
|
|
|
|
2018-01-23 19:04:59 +01:00
|
|
|
# IO realm.json
|
|
|
|
create_converted_data_files(realm, output_dir, '/realm.json', False)
|
2017-12-08 11:27:42 +01:00
|
|
|
# IO message.json
|
2018-01-23 19:04:59 +01:00
|
|
|
create_converted_data_files(message_json, output_dir, '/messages-000001.json', False)
|
2017-12-08 11:27:42 +01:00
|
|
|
# IO avatar records
|
2018-01-23 19:04:59 +01:00
|
|
|
create_converted_data_files([], output_dir, '/avatars/records.json', True)
|
2017-12-08 11:27:42 +01:00
|
|
|
# IO uploads TODO
|
2018-01-23 19:04:59 +01:00
|
|
|
create_converted_data_files([], output_dir, '/uploads/records.json', True)
|
2017-12-08 11:27:42 +01:00
|
|
|
# IO attachments
|
2018-01-23 19:04:59 +01:00
|
|
|
create_converted_data_files(attachment, output_dir, '/attachment.json', False)
|
2017-12-08 11:27:42 +01:00
|
|
|
|
|
|
|
# remove slack dir
|
2018-01-06 19:42:18 +01:00
|
|
|
rm_tree(slack_data_dir)
|
2017-12-08 11:27:42 +01:00
|
|
|
subprocess.check_call(["tar", "-czf", output_dir + '.tar.gz', output_dir, '-P'])
|
|
|
|
|
2018-02-08 00:06:02 +01:00
|
|
|
logging.info('######### DATA CONVERSION FINISHED #########\n')
|
|
|
|
logging.info("Zulip data dump created at %s" % (output_dir))
|
2018-01-23 19:04:59 +01:00
|
|
|
|
|
|
|
def get_data_file(path: str) -> Any:
|
|
|
|
data = json.load(open(path))
|
|
|
|
return data
|
|
|
|
|
2018-02-01 00:56:57 +01:00
|
|
|
def get_user_data(token: str) -> List[ZerverFieldsT]:
|
|
|
|
slack_user_list_url = "https://slack.com/api/users.list"
|
|
|
|
user_list = requests.get('%s?token=%s' % (slack_user_list_url, token))
|
|
|
|
if user_list.status_code == requests.codes.ok:
|
|
|
|
user_list_json = user_list.json()['members']
|
|
|
|
return user_list_json
|
|
|
|
else:
|
|
|
|
raise Exception('Enter a valid token!')
|
|
|
|
|
2018-01-23 19:04:59 +01:00
|
|
|
def create_converted_data_files(data: Any, output_dir: str, file_path: str,
|
|
|
|
make_new_dir: bool) -> None:
|
|
|
|
output_file = output_dir + file_path
|
|
|
|
if make_new_dir:
|
|
|
|
new_directory = os.path.dirname(file_path)
|
|
|
|
os.makedirs(output_dir + new_directory, exist_ok=True)
|
|
|
|
json.dump(data, open(output_file, 'w'))
|