2018-05-29 13:55:52 +02:00
|
|
|
import os
|
2018-06-07 12:25:59 +02:00
|
|
|
import ujson
|
2018-05-29 13:55:52 +02:00
|
|
|
import dateutil.parser
|
|
|
|
import random
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.forms.models import model_to_dict
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
from typing import Any, Dict, List, Tuple
|
|
|
|
|
2018-08-17 18:56:00 +02:00
|
|
|
from zerver.models import Realm, UserProfile, Recipient, Message
|
2018-06-07 12:25:59 +02:00
|
|
|
from zerver.lib.export import MESSAGE_BATCH_CHUNK_SIZE
|
2018-08-01 01:10:55 +02:00
|
|
|
from zerver.data_import.import_util import ZerverFieldsT, build_zerver_realm, \
|
2018-08-03 21:11:47 +02:00
|
|
|
build_avatar, build_subscription, build_recipient, build_usermessages, \
|
2018-08-17 20:17:28 +02:00
|
|
|
build_defaultstream, process_avatars, build_realm, build_stream
|
2018-05-29 13:55:52 +02:00
|
|
|
|
|
|
|
# stubs
|
|
|
|
GitterDataT = List[Dict[str, Any]]
|
|
|
|
|
|
|
|
realm_id = 0
|
|
|
|
|
|
|
|
def gitter_workspace_to_realm(domain_name: str, gitter_data: GitterDataT,
|
|
|
|
realm_subdomain: str) -> Tuple[ZerverFieldsT,
|
|
|
|
List[ZerverFieldsT],
|
|
|
|
Dict[str, int]]:
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
1. realm, Converted Realm data
|
|
|
|
2. avatars, which is list to map avatars to zulip avatar records.json
|
|
|
|
3. user_map, which is a dictionary to map from gitter user id to zulip user id
|
|
|
|
"""
|
|
|
|
NOW = float(timezone_now().timestamp())
|
2018-08-01 01:01:55 +02:00
|
|
|
zerver_realm = build_zerver_realm(realm_id, realm_subdomain, NOW, 'Gitter') # type: List[ZerverFieldsT]
|
2018-08-10 02:27:17 +02:00
|
|
|
realm = build_realm(zerver_realm, realm_id, domain_name)
|
2018-05-29 13:55:52 +02:00
|
|
|
|
|
|
|
zerver_userprofile, avatars, user_map = build_userprofile(int(NOW), domain_name, gitter_data)
|
2018-08-17 20:17:28 +02:00
|
|
|
zerver_stream, zerver_defaultstream = build_stream_and_defaultstream(int(NOW))
|
2018-05-29 13:55:52 +02:00
|
|
|
zerver_recipient, zerver_subscription = build_recipient_and_subscription(
|
|
|
|
zerver_userprofile, zerver_stream)
|
|
|
|
|
|
|
|
realm['zerver_userprofile'] = zerver_userprofile
|
|
|
|
realm['zerver_stream'] = zerver_stream
|
|
|
|
realm['zerver_defaultstream'] = zerver_defaultstream
|
|
|
|
realm['zerver_recipient'] = zerver_recipient
|
|
|
|
realm['zerver_subscription'] = zerver_subscription
|
|
|
|
|
|
|
|
return realm, avatars, user_map
|
|
|
|
|
|
|
|
def build_userprofile(timestamp: Any, domain_name: str,
|
|
|
|
gitter_data: GitterDataT) -> Tuple[List[ZerverFieldsT],
|
|
|
|
List[ZerverFieldsT],
|
|
|
|
Dict[str, int]]:
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
1. zerver_userprofile, which is a list of user profile
|
|
|
|
2. avatar_list, which is list to map avatars to zulip avatard records.json
|
|
|
|
3. added_users, which is a dictionary to map from gitter user id to zulip id
|
|
|
|
"""
|
|
|
|
logging.info('######### IMPORTING USERS STARTED #########\n')
|
|
|
|
zerver_userprofile = []
|
|
|
|
avatar_list = [] # type: List[ZerverFieldsT]
|
|
|
|
user_map = {} # type: Dict[str, int]
|
|
|
|
user_id = 0
|
|
|
|
|
|
|
|
for data in gitter_data:
|
|
|
|
if data['fromUser']['id'] not in user_map:
|
|
|
|
user_data = data['fromUser']
|
|
|
|
user_map[user_data['id']] = user_id
|
|
|
|
|
|
|
|
email = get_user_email(user_data, domain_name)
|
2018-08-01 01:10:55 +02:00
|
|
|
build_avatar(user_id, realm_id, email, user_data['avatarUrl'],
|
|
|
|
timestamp, avatar_list)
|
2018-05-29 13:55:52 +02:00
|
|
|
|
|
|
|
# Build userprofile object
|
|
|
|
userprofile = UserProfile(
|
|
|
|
full_name=user_data['displayName'],
|
|
|
|
short_name=user_data['username'],
|
|
|
|
id=user_id,
|
|
|
|
email=email,
|
2018-08-17 16:16:49 +02:00
|
|
|
delivery_email=email,
|
2018-05-29 13:55:52 +02:00
|
|
|
avatar_source='U',
|
|
|
|
pointer=-1,
|
|
|
|
date_joined=timestamp,
|
|
|
|
last_login=timestamp)
|
|
|
|
userprofile_dict = model_to_dict(userprofile)
|
|
|
|
# Set realm id separately as the corresponding realm is not yet a Realm model
|
|
|
|
# instance
|
|
|
|
userprofile_dict['realm'] = realm_id
|
|
|
|
zerver_userprofile.append(userprofile_dict)
|
|
|
|
user_id += 1
|
|
|
|
logging.info('######### IMPORTING USERS FINISHED #########\n')
|
|
|
|
return zerver_userprofile, avatar_list, user_map
|
|
|
|
|
|
|
|
def get_user_email(user_data: ZerverFieldsT, domain_name: str) -> str:
|
|
|
|
# TODO Get user email from github
|
|
|
|
email = ("%s@users.noreply.github.com" % user_data['username'])
|
|
|
|
return email
|
|
|
|
|
2018-08-17 20:17:28 +02:00
|
|
|
def build_stream_and_defaultstream(timestamp: Any) -> Tuple[List[ZerverFieldsT],
|
|
|
|
List[ZerverFieldsT]]:
|
2018-05-29 13:55:52 +02:00
|
|
|
logging.info('######### IMPORTING STREAM STARTED #########\n')
|
|
|
|
# We have only one stream for gitter export
|
2018-08-17 20:17:28 +02:00
|
|
|
stream_name = 'from gitter'
|
|
|
|
stream_description = "Imported from gitter"
|
|
|
|
stream_id = 0
|
|
|
|
stream = build_stream(timestamp, realm_id, stream_name, stream_description,
|
|
|
|
stream_id)
|
|
|
|
|
|
|
|
defaultstream = build_defaultstream(realm_id=realm_id, stream_id=stream_id,
|
|
|
|
defaultstream_id=0)
|
2018-05-29 13:55:52 +02:00
|
|
|
logging.info('######### IMPORTING STREAMS FINISHED #########\n')
|
|
|
|
return [stream], [defaultstream]
|
|
|
|
|
|
|
|
def build_recipient_and_subscription(
|
|
|
|
zerver_userprofile: List[ZerverFieldsT],
|
|
|
|
zerver_stream: List[ZerverFieldsT]) -> Tuple[List[ZerverFieldsT],
|
|
|
|
List[ZerverFieldsT]]:
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
1. zerver_recipient, which is a list of mapped recipient
|
|
|
|
2. zerver_subscription, which is a list of mapped subscription
|
|
|
|
"""
|
|
|
|
zerver_recipient = []
|
|
|
|
zerver_subscription = []
|
|
|
|
recipient_id = subscription_id = 0
|
|
|
|
|
|
|
|
# For stream
|
|
|
|
|
|
|
|
# We have only one recipient, because we have only one stream
|
|
|
|
# Hence 'recipient_id'=0 corresponds to 'stream_id'=0
|
2018-08-02 00:35:02 +02:00
|
|
|
recipient = build_recipient(0, recipient_id, Recipient.STREAM)
|
2018-05-29 13:55:52 +02:00
|
|
|
zerver_recipient.append(recipient)
|
|
|
|
|
|
|
|
for user in zerver_userprofile:
|
|
|
|
subscription = build_subscription(recipient_id, user['id'], subscription_id)
|
|
|
|
zerver_subscription.append(subscription)
|
|
|
|
subscription_id += 1
|
|
|
|
recipient_id += 1
|
|
|
|
|
|
|
|
# For users
|
|
|
|
for user in zerver_userprofile:
|
2018-08-02 00:35:02 +02:00
|
|
|
recipient = build_recipient(user['id'], recipient_id, Recipient.PERSONAL)
|
2018-05-29 13:55:52 +02:00
|
|
|
subscription = build_subscription(recipient_id, user['id'], subscription_id)
|
|
|
|
zerver_recipient.append(recipient)
|
|
|
|
zerver_subscription.append(subscription)
|
|
|
|
recipient_id += 1
|
|
|
|
subscription_id += 1
|
|
|
|
|
|
|
|
return zerver_recipient, zerver_subscription
|
|
|
|
|
2018-06-07 12:25:59 +02:00
|
|
|
def convert_gitter_workspace_messages(gitter_data: GitterDataT, output_dir: str,
|
2018-05-29 13:55:52 +02:00
|
|
|
zerver_subscription: List[ZerverFieldsT],
|
2018-06-07 12:25:59 +02:00
|
|
|
user_map: Dict[str, int],
|
2018-07-04 22:59:40 +02:00
|
|
|
user_short_name_to_full_name: Dict[str, str],
|
2018-06-07 12:25:59 +02:00
|
|
|
chunk_size: int=MESSAGE_BATCH_CHUNK_SIZE) -> None:
|
2018-05-29 13:55:52 +02:00
|
|
|
"""
|
2018-06-07 12:25:59 +02:00
|
|
|
Messages are stored in batches
|
2018-05-29 13:55:52 +02:00
|
|
|
"""
|
|
|
|
logging.info('######### IMPORTING MESSAGES STARTED #########\n')
|
|
|
|
message_id = usermessage_id = 0
|
|
|
|
recipient_id = 0 # Corresponding to stream "gitter"
|
|
|
|
|
2018-06-07 12:25:59 +02:00
|
|
|
low_index = 0
|
|
|
|
upper_index = low_index + chunk_size
|
|
|
|
dump_file_id = 1
|
|
|
|
|
|
|
|
while True:
|
|
|
|
message_json = {}
|
|
|
|
zerver_message = []
|
2018-08-03 21:11:47 +02:00
|
|
|
zerver_usermessage = [] # type: List[ZerverFieldsT]
|
2018-06-07 12:25:59 +02:00
|
|
|
message_data = gitter_data[low_index: upper_index]
|
|
|
|
if len(message_data) == 0:
|
|
|
|
break
|
|
|
|
for message in message_data:
|
|
|
|
message_time = dateutil.parser.parse(message['sent']).timestamp()
|
2018-07-04 22:59:40 +02:00
|
|
|
mentioned_user_ids = get_usermentions(message, user_map,
|
|
|
|
user_short_name_to_full_name)
|
2018-06-07 12:25:59 +02:00
|
|
|
rendered_content = None
|
|
|
|
|
2018-08-17 18:56:00 +02:00
|
|
|
zulip_message = Message(
|
2018-06-07 12:25:59 +02:00
|
|
|
rendered_content_version=1, # This is Zulip-specific
|
|
|
|
subject='imported from gitter',
|
|
|
|
pub_date=float(message_time),
|
|
|
|
id=message_id,
|
|
|
|
content=message['text'],
|
2018-08-17 18:56:00 +02:00
|
|
|
rendered_content=rendered_content)
|
|
|
|
zulip_message_dict = model_to_dict(zulip_message,
|
|
|
|
exclude=['recipient', 'sender', 'sending_client'])
|
|
|
|
zulip_message_dict['recipient'] = recipient_id
|
|
|
|
zulip_message_dict['sender'] = user_map[message['fromUser']['id']]
|
|
|
|
zulip_message_dict['sending_client'] = 1
|
|
|
|
zerver_message.append(zulip_message_dict)
|
2018-06-07 12:25:59 +02:00
|
|
|
|
2018-08-03 21:11:47 +02:00
|
|
|
usermessage_id = build_usermessages(
|
|
|
|
zerver_usermessage, usermessage_id, zerver_subscription,
|
|
|
|
recipient_id, mentioned_user_ids, message_id)
|
2018-06-07 12:25:59 +02:00
|
|
|
message_id += 1
|
|
|
|
|
|
|
|
message_json['zerver_message'] = zerver_message
|
|
|
|
message_json['zerver_usermessage'] = zerver_usermessage
|
|
|
|
message_filename = os.path.join(output_dir, "messages-%06d.json" % (dump_file_id,))
|
|
|
|
logging.info("Writing Messages to %s\n" % (message_filename,))
|
|
|
|
write_data_to_file(os.path.join(message_filename), message_json)
|
|
|
|
|
|
|
|
low_index = upper_index
|
|
|
|
upper_index = chunk_size + low_index
|
|
|
|
dump_file_id += 1
|
2018-05-29 13:55:52 +02:00
|
|
|
|
|
|
|
logging.info('######### IMPORTING MESSAGES FINISHED #########\n')
|
|
|
|
|
2018-07-04 22:59:40 +02:00
|
|
|
def get_usermentions(message: Dict[str, Any], user_map: Dict[str, int],
|
|
|
|
user_short_name_to_full_name: Dict[str, str]) -> List[int]:
|
|
|
|
mentioned_user_ids = []
|
|
|
|
if 'mentions' in message:
|
|
|
|
for mention in message['mentions']:
|
|
|
|
if mention.get('userId') in user_map:
|
|
|
|
gitter_mention = '@%s' % (mention['screenName'])
|
|
|
|
zulip_mention = ('@**%s**' %
|
|
|
|
(user_short_name_to_full_name[mention['screenName']]))
|
|
|
|
message['text'] = message['text'].replace(gitter_mention, zulip_mention)
|
|
|
|
|
|
|
|
mentioned_user_ids.append(user_map[mention['userId']])
|
|
|
|
return mentioned_user_ids
|
|
|
|
|
2018-05-29 13:55:52 +02:00
|
|
|
def do_convert_data(gitter_data_file: str, output_dir: str, threads: int=6) -> None:
|
|
|
|
# Subdomain is set by the user while running the import commands
|
|
|
|
realm_subdomain = ""
|
|
|
|
domain_name = settings.EXTERNAL_HOST
|
|
|
|
|
|
|
|
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!")
|
|
|
|
|
|
|
|
# Read data from the gitter file
|
|
|
|
gitter_data = json.load(open(gitter_data_file))
|
|
|
|
|
|
|
|
realm, avatar_list, user_map = gitter_workspace_to_realm(
|
|
|
|
domain_name, gitter_data, realm_subdomain)
|
2018-07-04 22:59:40 +02:00
|
|
|
|
|
|
|
# For user mentions
|
|
|
|
user_short_name_to_full_name = {}
|
|
|
|
for userprofile in realm['zerver_userprofile']:
|
|
|
|
user_short_name_to_full_name[userprofile['short_name']] = userprofile['full_name']
|
|
|
|
|
2018-06-07 12:25:59 +02:00
|
|
|
convert_gitter_workspace_messages(
|
2018-07-04 22:59:40 +02:00
|
|
|
gitter_data, output_dir, realm['zerver_subscription'], user_map,
|
|
|
|
user_short_name_to_full_name)
|
2018-05-29 13:55:52 +02:00
|
|
|
|
|
|
|
avatar_folder = os.path.join(output_dir, 'avatars')
|
|
|
|
avatar_realm_folder = os.path.join(avatar_folder, str(realm_id))
|
|
|
|
os.makedirs(avatar_realm_folder, exist_ok=True)
|
2018-08-02 15:24:34 +02:00
|
|
|
avatar_records = process_avatars(avatar_list, avatar_folder, realm_id, threads)
|
2018-05-29 13:55:52 +02:00
|
|
|
|
|
|
|
attachment = {"zerver_attachment": []} # type: Dict[str, List[Any]]
|
|
|
|
|
|
|
|
# IO realm.json
|
|
|
|
create_converted_data_files(realm, output_dir, '/realm.json')
|
|
|
|
# IO emoji records
|
|
|
|
create_converted_data_files([], output_dir, '/emoji/records.json')
|
|
|
|
# IO avatar records
|
|
|
|
create_converted_data_files(avatar_records, output_dir, '/avatars/records.json')
|
|
|
|
# IO uploads records
|
|
|
|
create_converted_data_files([], output_dir, '/uploads/records.json')
|
|
|
|
# IO attachments records
|
|
|
|
create_converted_data_files(attachment, output_dir, '/attachment.json')
|
|
|
|
|
|
|
|
subprocess.check_call(["tar", "-czf", output_dir + '.tar.gz', output_dir, '-P'])
|
|
|
|
|
|
|
|
logging.info('######### DATA CONVERSION FINISHED #########\n')
|
|
|
|
logging.info("Zulip data dump created at %s" % (output_dir))
|
|
|
|
|
|
|
|
def create_converted_data_files(data: Any, output_dir: str, file_path: str) -> None:
|
|
|
|
output_file = output_dir + file_path
|
|
|
|
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
|
|
|
json.dump(data, open(output_file, 'w'), indent=4)
|
2018-06-07 12:25:59 +02:00
|
|
|
|
|
|
|
def write_data_to_file(output_file: str, data: Any) -> None:
|
|
|
|
with open(output_file, "w") as f:
|
|
|
|
f.write(ujson.dumps(data, indent=4))
|