2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2016-01-24 03:56:05 +01:00
|
|
|
from __future__ import division
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2013-06-27 20:03:51 +02:00
|
|
|
from django.utils.timezone import now
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-10-26 17:10:48 +02:00
|
|
|
from django.contrib.sites.models import Site
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.models import Message, UserProfile, Stream, Recipient, Client, \
|
2013-03-29 21:16:51 +01:00
|
|
|
Subscription, Huddle, get_huddle, Realm, UserMessage, \
|
2013-07-18 18:48:56 +02:00
|
|
|
get_huddle_hash, clear_database, get_client, get_user_profile_by_id, \
|
2013-11-22 23:48:00 +01:00
|
|
|
split_email_to_domain, email_to_username
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.actions import do_send_message, set_default_streams, \
|
2016-05-18 20:23:03 +02:00
|
|
|
do_activate_user, do_deactivate_user, do_change_password, do_change_is_admin,\
|
|
|
|
do_change_bot_type
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.parallel import run_parallel
|
2013-10-23 23:39:34 +02:00
|
|
|
from django.db.models import Count
|
2012-09-21 16:10:36 +02:00
|
|
|
from django.conf import settings
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.bulk_create import bulk_create_realms, \
|
2013-01-10 21:50:09 +01:00
|
|
|
bulk_create_streams, bulk_create_users, bulk_create_huddles, \
|
|
|
|
bulk_create_clients
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.timestamp import timestamp_to_datetime
|
|
|
|
from zerver.models import MAX_MESSAGE_LENGTH
|
2015-10-13 22:54:35 +02:00
|
|
|
from zerver.models import DefaultStream, get_stream, get_realm
|
2013-10-17 16:33:04 +02:00
|
|
|
from zilencer.models import Deployment
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2012-08-28 18:45:10 +02:00
|
|
|
import datetime
|
|
|
|
import random
|
2013-01-14 20:09:25 +01:00
|
|
|
import glob
|
2012-10-22 19:47:27 +02:00
|
|
|
import os
|
2012-09-04 20:26:45 +02:00
|
|
|
from optparse import make_option
|
2016-06-04 00:52:29 +02:00
|
|
|
from six import text_type
|
2015-11-01 17:15:05 +01:00
|
|
|
from six.moves import range
|
2016-06-27 16:50:44 +02:00
|
|
|
from typing import Any, Callable, Dict, List, Iterable, Mapping, Sequence, Set, Tuple
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-11-08 21:49:04 +01:00
|
|
|
settings.TORNADO_SERVER = None
|
2012-10-12 21:27:19 +02:00
|
|
|
|
2016-05-18 20:23:03 +02:00
|
|
|
def create_users(realms, name_list, bot_type=None):
|
2016-06-13 06:38:14 +02:00
|
|
|
# type: (Mapping[text_type, Realm], Iterable[Tuple[text_type, text_type]], int) -> None
|
2016-06-27 16:42:00 +02:00
|
|
|
user_set = set() # type: Set[Tuple[text_type, text_type, text_type, bool]]
|
2012-10-23 23:29:56 +02:00
|
|
|
for full_name, email in name_list:
|
2013-07-18 18:48:56 +02:00
|
|
|
short_name = email_to_username(email)
|
2012-10-23 23:29:56 +02:00
|
|
|
user_set.add((email, full_name, short_name, True))
|
2016-05-18 20:23:03 +02:00
|
|
|
bulk_create_users(realms, user_set, bot_type)
|
2012-10-23 23:29:56 +02:00
|
|
|
|
|
|
|
def create_streams(realms, realm, stream_list):
|
2016-06-13 06:38:14 +02:00
|
|
|
# type: (Mapping[text_type, Realm], Realm, Iterable[text_type]) -> None
|
2016-06-27 16:42:00 +02:00
|
|
|
stream_set = set() # type: Set[Tuple[text_type, text_type]]
|
2012-10-23 23:29:56 +02:00
|
|
|
for stream_name in stream_list:
|
|
|
|
stream_set.add((realm.domain, stream_name))
|
|
|
|
bulk_create_streams(realms, stream_set)
|
2012-09-07 17:54:00 +02:00
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
class Command(BaseCommand):
|
2012-08-28 18:45:10 +02:00
|
|
|
help = "Populate a test database"
|
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
option_list = BaseCommand.option_list + (
|
2012-10-03 21:07:40 +02:00
|
|
|
make_option('-n', '--num-messages',
|
|
|
|
dest='num_messages',
|
2012-09-04 20:26:45 +02:00
|
|
|
type='int',
|
2012-09-14 18:31:11 +02:00
|
|
|
default=600,
|
2012-10-10 16:29:48 +02:00
|
|
|
help='The number of messages to create.'),
|
2012-09-20 06:19:37 +02:00
|
|
|
make_option('--extra-users',
|
|
|
|
dest='extra_users',
|
|
|
|
type='int',
|
|
|
|
default=0,
|
|
|
|
help='The number of extra users to create'),
|
2012-09-05 18:38:35 +02:00
|
|
|
make_option('--huddles',
|
|
|
|
dest='num_huddles',
|
|
|
|
type='int',
|
|
|
|
default=3,
|
|
|
|
help='The number of huddles to create.'),
|
|
|
|
make_option('--personals',
|
|
|
|
dest='num_personals',
|
|
|
|
type='int',
|
|
|
|
default=6,
|
|
|
|
help='The number of personal pairs to create.'),
|
2012-09-14 22:52:09 +02:00
|
|
|
make_option('--threads',
|
|
|
|
dest='threads',
|
|
|
|
type='int',
|
|
|
|
default=10,
|
|
|
|
help='The number of threads to use.'),
|
2012-09-05 18:38:35 +02:00
|
|
|
make_option('--percent-huddles',
|
|
|
|
dest='percent_huddles',
|
|
|
|
type='float',
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be huddles.'),
|
|
|
|
make_option('--percent-personals',
|
|
|
|
dest='percent_personals',
|
|
|
|
type='float',
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be personals.'),
|
2012-09-05 19:38:48 +02:00
|
|
|
make_option('--stickyness',
|
|
|
|
dest='stickyness',
|
|
|
|
type='float',
|
|
|
|
default=20,
|
|
|
|
help='The percent of messages to repeat recent folks.'),
|
2012-09-10 20:38:29 +02:00
|
|
|
make_option('--nodelete',
|
|
|
|
action="store_false",
|
|
|
|
default=True,
|
|
|
|
dest='delete',
|
|
|
|
help='Whether to delete all the existing messages.'),
|
2013-01-08 21:59:52 +01:00
|
|
|
make_option('--test-suite',
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
help='Whether to delete all the existing messages.'),
|
2012-10-03 21:07:40 +02:00
|
|
|
make_option('--replay-old-messages',
|
2012-09-27 19:58:42 +02:00
|
|
|
action="store_true",
|
|
|
|
default=False,
|
2012-10-03 21:07:40 +02:00
|
|
|
dest='replay_old_messages',
|
2012-09-27 19:58:42 +02:00
|
|
|
help='Whether to replace the log of old messages.'),
|
2012-09-04 20:26:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (**Any) -> None
|
2012-09-05 18:38:35 +02:00
|
|
|
if options["percent_huddles"] + options["percent_personals"] > 100:
|
|
|
|
self.stderr.write("Error! More than 100% of messages allocated.\n")
|
|
|
|
return
|
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
2012-10-29 19:43:00 +01:00
|
|
|
# Start by clearing all the data in our database
|
|
|
|
clear_database()
|
2012-09-05 21:49:56 +02:00
|
|
|
|
2012-10-23 23:29:56 +02:00
|
|
|
# Create our two default realms
|
2015-08-22 00:43:11 +02:00
|
|
|
zulip_realm = Realm.objects.create(domain="zulip.com", name="Zulip Dev")
|
2015-08-20 03:54:07 +02:00
|
|
|
if options["test_suite"]:
|
|
|
|
Realm.objects.create(domain="mit.edu")
|
2016-06-27 16:42:00 +02:00
|
|
|
realms = {} # type: Dict[text_type, Realm]
|
2012-10-23 23:29:56 +02:00
|
|
|
for realm in Realm.objects.all():
|
|
|
|
realms[realm.domain] = realm
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
# Create test Users (UserProfiles are automatically created,
|
|
|
|
# as are subscriptions to the ability to receive personals).
|
2013-07-24 20:41:09 +02:00
|
|
|
names = [("Othello, the Moor of Venice", "othello@zulip.com"), ("Iago", "iago@zulip.com"),
|
|
|
|
("Prospero from The Tempest", "prospero@zulip.com"),
|
|
|
|
("Cordelia Lear", "cordelia@zulip.com"), ("King Hamlet", "hamlet@zulip.com")]
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(options["extra_users"]):
|
2013-07-24 20:56:42 +02:00
|
|
|
names.append(('Extra User %d' % (i,), 'extrauser%d@zulip.com' % (i,)))
|
2012-10-23 23:29:56 +02:00
|
|
|
create_users(realms, names)
|
2015-08-20 03:53:48 +02:00
|
|
|
iago = UserProfile.objects.get(email="iago@zulip.com")
|
|
|
|
do_change_is_admin(iago, True)
|
2012-10-10 23:17:43 +02:00
|
|
|
# Create public streams.
|
2012-10-23 23:29:56 +02:00
|
|
|
stream_list = ["Verona", "Denmark", "Scotland", "Venice", "Rome"]
|
2013-08-06 17:40:44 +02:00
|
|
|
create_streams(realms, zulip_realm, stream_list)
|
2016-06-27 16:42:00 +02:00
|
|
|
recipient_streams = [Stream.objects.get(name=name, realm=zulip_realm).id for name in stream_list] # type: List[int]
|
2013-07-24 22:08:15 +02:00
|
|
|
|
2016-04-13 04:49:13 +02:00
|
|
|
# Create subscriptions to streams. The following
|
|
|
|
# algorithm will give each of the users a different but
|
|
|
|
# deterministic subset of the streams (given a fixed list
|
|
|
|
# of users).
|
2016-06-27 17:09:43 +02:00
|
|
|
subscriptions_to_add = [] # type: List[Subscription]
|
2016-06-27 16:42:00 +02:00
|
|
|
profiles = UserProfile.objects.select_related().all().order_by("email") # type: Sequence[UserProfile]
|
2012-09-10 20:38:29 +02:00
|
|
|
for i, profile in enumerate(profiles):
|
2012-10-10 23:17:43 +02:00
|
|
|
# Subscribe to some streams.
|
2012-10-23 23:29:56 +02:00
|
|
|
for type_id in recipient_streams[:int(len(recipient_streams) *
|
|
|
|
float(i)/len(profiles)) + 1]:
|
|
|
|
r = Recipient.objects.get(type=Recipient.STREAM, type_id=type_id)
|
|
|
|
s = Subscription(recipient=r, user_profile=profile)
|
|
|
|
subscriptions_to_add.append(s)
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_add)
|
2012-09-10 20:38:29 +02:00
|
|
|
else:
|
2015-10-13 22:54:35 +02:00
|
|
|
zulip_realm = get_realm("zulip.com")
|
2012-10-10 23:17:43 +02:00
|
|
|
recipient_streams = [klass.type_id for klass in
|
2012-10-10 22:57:21 +02:00
|
|
|
Recipient.objects.filter(type=Recipient.STREAM)]
|
2012-09-10 20:38:29 +02:00
|
|
|
|
|
|
|
# Extract a list of all users
|
2016-06-27 16:42:00 +02:00
|
|
|
user_profiles = [user_profile.id for user_profile in UserProfile.objects.all()] # type: List[int]
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-05 18:38:35 +02:00
|
|
|
# Create several initial huddles
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(options["num_huddles"]):
|
2013-03-29 19:45:50 +01:00
|
|
|
get_huddle(random.sample(user_profiles, random.randint(3, 4)))
|
2012-09-05 18:38:35 +02:00
|
|
|
|
|
|
|
# Create several initial pairs for personals
|
2013-03-29 19:45:50 +01:00
|
|
|
personals_pairs = [random.sample(user_profiles, 2)
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(options["num_personals"])]
|
2012-09-14 17:52:38 +02:00
|
|
|
|
2012-09-14 22:52:09 +02:00
|
|
|
threads = options["threads"]
|
2016-06-27 16:42:00 +02:00
|
|
|
jobs = [] # type: List[Tuple[int, List[List[int]], Dict[str, Any], Callable[[str], int]]]
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(threads):
|
2016-01-24 03:56:05 +01:00
|
|
|
count = options["num_messages"] // threads
|
2012-10-03 21:07:40 +02:00
|
|
|
if i < options["num_messages"] % threads:
|
2012-09-14 18:31:11 +02:00
|
|
|
count += 1
|
|
|
|
jobs.append((count, personals_pairs, options, self.stdout.write))
|
2013-11-01 20:51:09 +01:00
|
|
|
|
|
|
|
for job in jobs:
|
|
|
|
send_messages(job)
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
2012-11-28 23:09:11 +01:00
|
|
|
# Create the "website" and "API" clients; if we don't, the
|
2013-07-29 23:03:31 +02:00
|
|
|
# default values in zerver/decorators.py will not work
|
2012-11-28 23:09:11 +01:00
|
|
|
# with the Django test suite.
|
|
|
|
get_client("website")
|
|
|
|
get_client("API")
|
|
|
|
|
2015-08-19 05:43:13 +02:00
|
|
|
if options["test_suite"]:
|
|
|
|
# Create test users; the MIT ones are needed to test
|
|
|
|
# the Zephyr mirroring codepaths.
|
|
|
|
testsuite_mit_users = [
|
|
|
|
("Fred Sipb (MIT)", "sipbtest@mit.edu"),
|
|
|
|
("Athena Consulting Exchange User (MIT)", "starnine@mit.edu"),
|
|
|
|
("Esp Classroom (MIT)", "espuser@mit.edu"),
|
|
|
|
]
|
|
|
|
create_users(realms, testsuite_mit_users)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
|
|
|
# These bots are directly referenced from code and thus
|
|
|
|
# are needed for the test suite.
|
2013-11-14 16:59:10 +01:00
|
|
|
all_realm_bots = [(bot['name'], bot['email_template'] % (settings.INTERNAL_BOT_DOMAIN,))
|
|
|
|
for bot in settings.INTERNAL_BOTS]
|
2013-10-31 18:33:19 +01:00
|
|
|
zulip_realm_bots = [
|
2013-07-24 20:23:35 +02:00
|
|
|
("Zulip New User Bot", "new-user-bot@zulip.com"),
|
2013-07-24 20:23:35 +02:00
|
|
|
("Zulip Error Bot", "error-bot@zulip.com"),
|
2016-05-22 18:46:49 +02:00
|
|
|
("Zulip Default Bot", "default-bot@zulip.com"),
|
2013-01-08 21:59:52 +01:00
|
|
|
]
|
2013-10-31 18:33:19 +01:00
|
|
|
zulip_realm_bots.extend(all_realm_bots)
|
2016-05-18 20:23:03 +02:00
|
|
|
create_users(realms, zulip_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
2016-05-19 23:11:13 +02:00
|
|
|
zulip_webhook_bots = [
|
|
|
|
("Zulip Webhook Bot", "webhook-bot@zulip.com"),
|
|
|
|
]
|
|
|
|
create_users(realms, zulip_webhook_bots, bot_type=UserProfile.INCOMING_WEBHOOK_BOT)
|
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
if not options["test_suite"]:
|
2016-02-08 03:59:38 +01:00
|
|
|
# Initialize the email gateway bot as an API Super User
|
|
|
|
email_gateway_bot = UserProfile.objects.get(email__iexact=settings.EMAIL_GATEWAY_BOT)
|
|
|
|
email_gateway_bot.is_api_super_user = True
|
|
|
|
email_gateway_bot.save()
|
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
# To keep the messages.json fixtures file for the test
|
|
|
|
# suite fast, don't add these users and subscriptions
|
|
|
|
# when running populate_db for the test suite
|
|
|
|
|
2016-05-26 17:11:18 +02:00
|
|
|
zulip_stream_list = ["devel", "all", "announce", "design", "support", "social", "test",
|
2013-07-08 23:24:26 +02:00
|
|
|
"errors", "sales"]
|
2013-08-06 22:19:25 +02:00
|
|
|
create_streams(realms, zulip_realm, zulip_stream_list)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
2013-07-08 23:24:26 +02:00
|
|
|
# Add a few default streams
|
|
|
|
for stream_name in ["design", "devel", "social", "support"]:
|
2013-08-06 17:40:44 +02:00
|
|
|
DefaultStream.objects.create(realm=zulip_realm, stream=get_stream(stream_name, zulip_realm))
|
2013-07-08 23:24:26 +02:00
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
# Now subscribe everyone to these streams
|
|
|
|
subscriptions_to_add = []
|
2013-08-06 17:40:44 +02:00
|
|
|
profiles = UserProfile.objects.select_related().filter(realm=zulip_realm)
|
2013-08-06 22:19:25 +02:00
|
|
|
for cls in zulip_stream_list:
|
2013-08-06 17:40:44 +02:00
|
|
|
stream = Stream.objects.get(name=cls, realm=zulip_realm)
|
2013-01-08 21:59:52 +01:00
|
|
|
recipient = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id)
|
|
|
|
for profile in profiles:
|
|
|
|
# Subscribe to some streams.
|
|
|
|
s = Subscription(recipient=recipient, user_profile=profile)
|
|
|
|
subscriptions_to_add.append(s)
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_add)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
|
|
|
# These bots are not needed by the test suite
|
2013-08-06 22:19:25 +02:00
|
|
|
internal_zulip_users_nosubs = [
|
2013-07-24 20:19:19 +02:00
|
|
|
("Zulip Commit Bot", "commit-bot@zulip.com"),
|
2013-07-24 20:05:33 +02:00
|
|
|
("Zulip Trac Bot", "trac-bot@zulip.com"),
|
2013-07-24 20:03:00 +02:00
|
|
|
("Zulip Nagios Bot", "nagios-bot@zulip.com"),
|
2013-07-24 20:23:35 +02:00
|
|
|
("Zulip Feedback Bot", "feedback@zulip.com"),
|
2013-01-08 21:59:52 +01:00
|
|
|
]
|
2016-05-18 20:23:03 +02:00
|
|
|
create_users(realms, internal_zulip_users_nosubs, bot_type=UserProfile.DEFAULT_BOT)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
2013-03-07 17:12:35 +01:00
|
|
|
# Mark all messages as read
|
2013-11-01 20:51:09 +01:00
|
|
|
UserMessage.objects.all().update(flags=UserMessage.flags.read)
|
2013-03-07 17:12:35 +01:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
self.stdout.write("Successfully populated test database.\n")
|
2012-10-03 21:07:40 +02:00
|
|
|
if options["replay_old_messages"]:
|
|
|
|
restore_saved_messages()
|
2012-10-02 22:58:13 +02:00
|
|
|
|
2016-01-23 01:55:47 +01:00
|
|
|
recipient_hash = {} # type: Dict[int, Recipient]
|
2012-09-14 22:43:54 +02:00
|
|
|
def get_recipient_by_id(rid):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (int) -> Recipient
|
2012-09-14 22:43:54 +02:00
|
|
|
if rid in recipient_hash:
|
|
|
|
return recipient_hash[rid]
|
|
|
|
return Recipient.objects.get(id=rid)
|
|
|
|
|
2012-10-03 21:07:40 +02:00
|
|
|
def restore_saved_messages():
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: () -> None
|
2016-06-27 16:42:00 +02:00
|
|
|
old_messages = [] # type: List[Dict[str, Any]]
|
2016-01-26 02:19:31 +01:00
|
|
|
duplicate_suppression_hash = {} # type: Dict[str, bool]
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2016-06-27 16:10:33 +02:00
|
|
|
stream_dict = {} # type: Dict[Tuple[text_type, text_type], Tuple[text_type, text_type]]
|
2016-06-27 16:42:00 +02:00
|
|
|
user_set = set() # type: Set[Tuple[text_type, text_type, text_type, bool]]
|
|
|
|
email_set = set([u.email for u in UserProfile.objects.all()]) # type: Set[text_type]
|
|
|
|
realm_set = set() # type: Set[text_type]
|
2012-10-19 21:30:42 +02:00
|
|
|
# Initial client_set is nonempty temporarily because we don't have
|
|
|
|
# clients in logs at all right now -- later we can start with nothing.
|
|
|
|
client_set = set(["populate_db", "website", "zephyr_mirror"])
|
2016-06-27 16:42:00 +02:00
|
|
|
huddle_user_set = set() # type: Set[Tuple[text_type, ...]]
|
2012-10-20 18:02:58 +02:00
|
|
|
# First, determine all the objects our messages will need.
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Creating realms/streams/etc...")
|
2012-12-13 15:14:59 +01:00
|
|
|
def process_line(line):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (str) -> None
|
2012-10-20 21:43:13 +02:00
|
|
|
old_message_json = line.strip()
|
|
|
|
|
|
|
|
# Due to populate_db's shakespeare mode, we have a lot of
|
|
|
|
# duplicate messages in our log that only differ in their
|
|
|
|
# logged ID numbers (same timestamp, content, etc.). With
|
|
|
|
# sqlite, bulk creating those messages won't work properly: in
|
|
|
|
# particular, the first 100 messages will actually only result
|
|
|
|
# in 20 rows ending up in the target table, which screws up
|
|
|
|
# the below accounting where for handling changing
|
|
|
|
# subscriptions, we assume that the Nth row populate_db
|
|
|
|
# created goes with the Nth non-subscription row of the input
|
|
|
|
# So suppress the duplicates when using sqlite.
|
|
|
|
if "sqlite" in settings.DATABASES["default"]["ENGINE"]:
|
2013-06-18 23:55:55 +02:00
|
|
|
tmp_message = ujson.loads(old_message_json)
|
2012-10-20 21:43:13 +02:00
|
|
|
tmp_message['id'] = '1'
|
2013-06-18 23:55:55 +02:00
|
|
|
duplicate_suppression_key = ujson.dumps(tmp_message)
|
2012-10-20 21:43:13 +02:00
|
|
|
if duplicate_suppression_key in duplicate_suppression_hash:
|
2012-12-13 15:14:59 +01:00
|
|
|
return
|
2012-10-20 21:43:13 +02:00
|
|
|
duplicate_suppression_hash[duplicate_suppression_key] = True
|
|
|
|
|
2013-06-18 23:55:55 +02:00
|
|
|
old_message = ujson.loads(old_message_json)
|
2012-10-25 20:19:34 +02:00
|
|
|
message_type = old_message["type"]
|
2012-10-21 01:42:56 +02:00
|
|
|
|
2012-12-03 17:46:48 +01:00
|
|
|
# Lower case emails and domains; it will screw up
|
|
|
|
# deduplication if we don't
|
2012-12-04 20:58:45 +01:00
|
|
|
def fix_email(email):
|
2016-06-27 16:10:33 +02:00
|
|
|
# type: (text_type) -> text_type
|
2012-12-04 20:58:45 +01:00
|
|
|
return email.strip().lower()
|
|
|
|
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type in ["stream", "huddle", "personal"]:
|
|
|
|
old_message["sender_email"] = fix_email(old_message["sender_email"])
|
2012-12-11 19:25:31 +01:00
|
|
|
# Fix the length on too-long messages before we start processing them
|
|
|
|
if len(old_message["content"]) > MAX_MESSAGE_LENGTH:
|
|
|
|
old_message["content"] = "[ This message was deleted because it was too long ]"
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type in ["subscription_added", "subscription_removed"]:
|
2012-10-21 01:42:56 +02:00
|
|
|
old_message["domain"] = old_message["domain"].lower()
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["user"] = fix_email(old_message["user"])
|
2012-12-13 02:50:34 +01:00
|
|
|
elif message_type == "subscription_property":
|
|
|
|
old_message["user"] = fix_email(old_message["user"])
|
2013-05-17 16:36:38 +02:00
|
|
|
elif message_type == "user_email_changed":
|
|
|
|
old_message["old_email"] = fix_email(old_message["old_email"])
|
|
|
|
old_message["new_email"] = fix_email(old_message["new_email"])
|
2012-10-25 21:39:34 +02:00
|
|
|
elif message_type.startswith("user_"):
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["user"] = fix_email(old_message["user"])
|
2012-11-23 21:23:41 +01:00
|
|
|
elif message_type.startswith("enable_"):
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["user"] = fix_email(old_message["user"])
|
2012-10-21 01:42:56 +02:00
|
|
|
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type == 'personal':
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["recipient"][0]["email"] = fix_email(old_message["recipient"][0]["email"])
|
2012-10-25 20:19:34 +02:00
|
|
|
elif message_type == "huddle":
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(len(old_message["recipient"])):
|
2012-12-04 20:58:45 +01:00
|
|
|
old_message["recipient"][i]["email"] = fix_email(old_message["recipient"][i]["email"])
|
2012-10-21 01:42:56 +02:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
old_messages.append(old_message)
|
2012-10-18 03:02:21 +02:00
|
|
|
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type in ["subscription_added", "subscription_removed"]:
|
2016-06-27 16:10:33 +02:00
|
|
|
stream_name = old_message["name"].strip() # type: text_type
|
2012-12-03 17:46:48 +01:00
|
|
|
canon_stream_name = stream_name.lower()
|
|
|
|
if canon_stream_name not in stream_dict:
|
|
|
|
stream_dict[(old_message["domain"], canon_stream_name)] = \
|
|
|
|
(old_message["domain"], stream_name)
|
2012-12-04 21:07:33 +01:00
|
|
|
elif message_type == "user_created":
|
|
|
|
user_set.add((old_message["user"], old_message["full_name"], old_message["short_name"], False))
|
2012-12-04 21:32:58 +01:00
|
|
|
elif message_type == "realm_created":
|
|
|
|
realm_set.add(old_message["domain"])
|
2012-12-05 20:00:59 +01:00
|
|
|
|
|
|
|
if message_type not in ["stream", "huddle", "personal"]:
|
2012-12-13 15:14:59 +01:00
|
|
|
return
|
2012-12-05 20:00:59 +01:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
sender_email = old_message["sender_email"]
|
2012-11-27 18:26:51 +01:00
|
|
|
|
2016-06-27 16:10:33 +02:00
|
|
|
domain = text_type(split_email_to_domain(sender_email))
|
2012-10-20 18:02:58 +02:00
|
|
|
realm_set.add(domain)
|
|
|
|
|
|
|
|
if old_message["sender_email"] not in email_set:
|
|
|
|
user_set.add((old_message["sender_email"],
|
|
|
|
old_message["sender_full_name"],
|
2012-10-23 22:39:40 +02:00
|
|
|
old_message["sender_short_name"],
|
|
|
|
False))
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2012-10-19 21:30:42 +02:00
|
|
|
if 'sending_client' in old_message:
|
|
|
|
client_set.add(old_message['sending_client'])
|
|
|
|
|
2012-10-25 20:19:34 +02:00
|
|
|
if message_type == 'stream':
|
2012-12-03 17:46:48 +01:00
|
|
|
stream_name = old_message["recipient"].strip()
|
|
|
|
canon_stream_name = stream_name.lower()
|
|
|
|
if canon_stream_name not in stream_dict:
|
|
|
|
stream_dict[(domain, canon_stream_name)] = (domain, stream_name)
|
2012-10-25 20:19:34 +02:00
|
|
|
elif message_type == 'personal':
|
2012-10-20 18:02:58 +02:00
|
|
|
u = old_message["recipient"][0]
|
|
|
|
if u["email"] not in email_set:
|
2012-10-23 22:39:40 +02:00
|
|
|
user_set.add((u["email"], u["full_name"], u["short_name"], False))
|
2012-10-20 18:02:58 +02:00
|
|
|
email_set.add(u["email"])
|
2012-10-25 20:19:34 +02:00
|
|
|
elif message_type == 'huddle':
|
2012-10-20 18:02:58 +02:00
|
|
|
for u in old_message["recipient"]:
|
2012-10-23 22:39:40 +02:00
|
|
|
user_set.add((u["email"], u["full_name"], u["short_name"], False))
|
2012-10-20 18:02:58 +02:00
|
|
|
if u["email"] not in email_set:
|
2012-10-23 22:39:40 +02:00
|
|
|
user_set.add((u["email"], u["full_name"], u["short_name"], False))
|
2012-10-20 18:02:58 +02:00
|
|
|
email_set.add(u["email"])
|
2012-11-08 23:13:09 +01:00
|
|
|
huddle_user_set.add(tuple(sorted(set(u["email"] for u in old_message["recipient"]))))
|
2012-10-21 01:48:05 +02:00
|
|
|
else:
|
2012-10-31 16:40:41 +01:00
|
|
|
raise ValueError('Bad message type')
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2013-10-28 15:54:32 +01:00
|
|
|
event_glob = os.path.join(settings.EVENT_LOG_DIR, 'events.*')
|
2013-01-14 20:09:25 +01:00
|
|
|
for filename in sorted(glob.glob(event_glob)):
|
2015-10-14 22:31:08 +02:00
|
|
|
with open(filename, "r") as message_log:
|
2013-01-14 20:09:25 +01:00
|
|
|
for line in message_log.readlines():
|
|
|
|
process_line(line)
|
2012-12-13 15:14:59 +01:00
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
stream_recipients = {} # type: Dict[Tuple[int, text_type], Recipient]
|
|
|
|
user_recipients = {} # type: Dict[text_type, Recipient]
|
|
|
|
huddle_recipients = {} # type: Dict[text_type, Recipient]
|
2012-10-20 18:02:58 +02:00
|
|
|
|
|
|
|
# Then, create the objects our messages need.
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Creating realms...")
|
2012-10-20 18:02:58 +02:00
|
|
|
bulk_create_realms(realm_set)
|
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
realms = {} # type: Dict[text_type, Realm]
|
2012-10-20 18:02:58 +02:00
|
|
|
for realm in Realm.objects.all():
|
|
|
|
realms[realm.domain] = realm
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Creating clients...")
|
2012-10-19 21:30:42 +02:00
|
|
|
bulk_create_clients(client_set)
|
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
clients = {} # type: Dict[text_type, Client]
|
2012-10-19 21:30:42 +02:00
|
|
|
for client in Client.objects.all():
|
|
|
|
clients[client.name] = client
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Creating streams...")
|
2016-01-25 01:27:18 +01:00
|
|
|
bulk_create_streams(realms, list(stream_dict.values()))
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
streams = {} # type: Dict[int, Stream]
|
2012-10-20 18:02:58 +02:00
|
|
|
for stream in Stream.objects.all():
|
|
|
|
streams[stream.id] = stream
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.STREAM):
|
|
|
|
stream_recipients[(streams[recipient.type_id].realm_id,
|
2012-10-21 01:54:29 +02:00
|
|
|
streams[recipient.type_id].name.lower())] = recipient
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Creating users...")
|
2012-10-20 18:02:58 +02:00
|
|
|
bulk_create_users(realms, user_set)
|
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
users = {} # type: Dict[text_type, UserProfile]
|
|
|
|
users_by_id = {} # type: Dict[int, UserProfile]
|
2012-10-20 18:02:58 +02:00
|
|
|
for user_profile in UserProfile.objects.select_related().all():
|
2013-03-28 20:43:34 +01:00
|
|
|
users[user_profile.email] = user_profile
|
2012-10-20 18:02:58 +02:00
|
|
|
users_by_id[user_profile.id] = user_profile
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.PERSONAL):
|
2013-03-28 20:43:34 +01:00
|
|
|
user_recipients[users_by_id[recipient.type_id].email] = recipient
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Creating huddles...")
|
2012-10-20 18:02:58 +02:00
|
|
|
bulk_create_huddles(users, huddle_user_set)
|
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
huddles_by_id = {} # type: Dict[int, Huddle]
|
2012-10-20 18:02:58 +02:00
|
|
|
for huddle in Huddle.objects.all():
|
|
|
|
huddles_by_id[huddle.id] = huddle
|
|
|
|
for recipient in Recipient.objects.filter(type=Recipient.HUDDLE):
|
|
|
|
huddle_recipients[huddles_by_id[recipient.type_id].huddle_hash] = recipient
|
|
|
|
|
|
|
|
# TODO: Add a special entry type in the log that is a subscription
|
|
|
|
# change and import those as we go to make subscription changes
|
|
|
|
# take effect!
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Importing subscriptions...")
|
2016-01-26 02:19:31 +01:00
|
|
|
subscribers = {} # type: Dict[int, Set[int]]
|
2012-10-20 18:02:58 +02:00
|
|
|
for s in Subscription.objects.select_related().all():
|
|
|
|
if s.active:
|
2012-10-22 20:15:25 +02:00
|
|
|
subscribers.setdefault(s.recipient.id, set()).add(s.user_profile.id)
|
2012-10-20 18:02:58 +02:00
|
|
|
|
|
|
|
# Then create all the messages, without talking to the DB!
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Importing messages, part 1...")
|
2012-10-20 21:43:13 +02:00
|
|
|
first_message_id = None
|
|
|
|
if Message.objects.exists():
|
|
|
|
first_message_id = Message.objects.all().order_by("-id")[0].id + 1
|
|
|
|
|
2016-06-27 17:09:43 +02:00
|
|
|
messages_to_create = [] # type: List[Message]
|
2012-10-20 18:02:58 +02:00
|
|
|
for idx, old_message in enumerate(old_messages):
|
2012-12-06 00:08:21 +01:00
|
|
|
message_type = old_message["type"]
|
|
|
|
if message_type not in ["stream", "huddle", "personal"]:
|
2012-10-20 21:43:13 +02:00
|
|
|
continue
|
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
message = Message()
|
2012-09-29 00:33:49 +02:00
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
sender_email = old_message["sender_email"]
|
2013-11-22 23:48:00 +01:00
|
|
|
domain = split_email_to_domain(sender_email)
|
2012-10-20 18:02:58 +02:00
|
|
|
realm = realms[domain]
|
2012-09-29 00:33:49 +02:00
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
message.sender = users[sender_email]
|
|
|
|
type_hash = {"stream": Recipient.STREAM,
|
|
|
|
"huddle": Recipient.HUDDLE,
|
|
|
|
"personal": Recipient.PERSONAL}
|
2012-10-19 21:30:42 +02:00
|
|
|
|
|
|
|
if 'sending_client' in old_message:
|
|
|
|
message.sending_client = clients[old_message['sending_client']]
|
2013-07-24 20:41:09 +02:00
|
|
|
elif sender_email in ["othello@zulip.com", "iago@zulip.com", "prospero@zulip.com",
|
|
|
|
"cordelia@zulip.com", "hamlet@zulip.com"]:
|
2012-10-19 21:30:42 +02:00
|
|
|
message.sending_client = clients['populate_db']
|
2013-07-24 20:56:42 +02:00
|
|
|
elif realm.domain == "zulip.com":
|
2012-10-19 21:30:42 +02:00
|
|
|
message.sending_client = clients["website"]
|
|
|
|
elif realm.domain == "mit.edu":
|
|
|
|
message.sending_client = clients['zephyr_mirror']
|
|
|
|
else:
|
|
|
|
message.sending_client = clients['populate_db']
|
|
|
|
|
2012-12-06 00:08:21 +01:00
|
|
|
message.type = type_hash[message_type]
|
2012-10-03 21:05:48 +02:00
|
|
|
message.content = old_message["content"]
|
2012-10-11 00:01:39 +02:00
|
|
|
message.subject = old_message["subject"]
|
2012-12-05 19:53:23 +01:00
|
|
|
message.pub_date = timestamp_to_datetime(old_message["timestamp"])
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
if message.type == Recipient.PERSONAL:
|
2012-10-23 03:11:39 +02:00
|
|
|
message.recipient = user_recipients[old_message["recipient"][0]["email"]]
|
2012-10-10 22:57:21 +02:00
|
|
|
elif message.type == Recipient.STREAM:
|
2012-10-20 18:02:58 +02:00
|
|
|
message.recipient = stream_recipients[(realm.id,
|
2012-12-03 17:46:48 +01:00
|
|
|
old_message["recipient"].lower())]
|
2012-10-03 21:05:48 +02:00
|
|
|
elif message.type == Recipient.HUDDLE:
|
2012-10-20 18:02:58 +02:00
|
|
|
huddle_hash = get_huddle_hash([users[u["email"]].id
|
|
|
|
for u in old_message["recipient"]])
|
|
|
|
message.recipient = huddle_recipients[huddle_hash]
|
2012-09-27 19:58:42 +02:00
|
|
|
else:
|
2012-10-31 16:40:41 +01:00
|
|
|
raise ValueError('Bad message type')
|
2012-10-20 18:02:58 +02:00
|
|
|
messages_to_create.append(message)
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Importing messages, part 2...")
|
2013-03-27 15:58:23 +01:00
|
|
|
Message.objects.bulk_create(messages_to_create)
|
2012-12-13 15:14:59 +01:00
|
|
|
messages_to_create = []
|
2012-10-20 18:02:58 +02:00
|
|
|
|
|
|
|
# Finally, create all the UserMessage objects
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Importing usermessages, part 1...")
|
2016-06-27 16:42:00 +02:00
|
|
|
personal_recipients = {} # type: Dict[int, bool]
|
2012-10-23 04:01:55 +02:00
|
|
|
for r in Recipient.objects.filter(type = Recipient.PERSONAL):
|
|
|
|
personal_recipients[r.id] = True
|
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
all_messages = Message.objects.all() # type: Sequence[Message]
|
2016-06-27 17:09:43 +02:00
|
|
|
user_messages_to_create = [] # type: List[UserMessage]
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2016-06-27 17:09:43 +02:00
|
|
|
messages_by_id = {} # type: Dict[int, Message]
|
2012-10-20 18:02:58 +02:00
|
|
|
for message in all_messages:
|
2012-10-20 21:43:13 +02:00
|
|
|
messages_by_id[message.id] = message
|
|
|
|
|
2013-07-02 23:55:21 +02:00
|
|
|
if len(messages_by_id) == 0:
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "No old messages to replay")
|
2013-07-02 23:55:21 +02:00
|
|
|
return
|
|
|
|
|
2012-10-20 21:43:13 +02:00
|
|
|
if first_message_id is None:
|
|
|
|
first_message_id = min(messages_by_id.keys())
|
|
|
|
|
2012-12-13 01:30:50 +01:00
|
|
|
tot_user_messages = 0
|
2016-06-27 16:42:00 +02:00
|
|
|
pending_subs = {} # type: Dict[Tuple[int, int], bool]
|
2012-10-20 21:43:13 +02:00
|
|
|
current_message_id = first_message_id
|
2016-06-27 16:42:00 +02:00
|
|
|
pending_colors = {} # type: Dict[Tuple[text_type, text_type], text_type]
|
2012-10-20 21:43:13 +02:00
|
|
|
for old_message in old_messages:
|
2012-12-06 00:08:21 +01:00
|
|
|
message_type = old_message["type"]
|
|
|
|
if message_type == 'subscription_added':
|
2012-12-03 17:46:48 +01:00
|
|
|
stream_key = (realms[old_message["domain"]].id, old_message["name"].strip().lower())
|
2012-10-20 21:43:13 +02:00
|
|
|
subscribers.setdefault(stream_recipients[stream_key].id,
|
|
|
|
set()).add(users[old_message["user"]].id)
|
2012-10-20 23:31:48 +02:00
|
|
|
pending_subs[(stream_recipients[stream_key].id,
|
|
|
|
users[old_message["user"]].id)] = True
|
2012-10-20 21:43:13 +02:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "subscription_removed":
|
2012-12-03 17:46:48 +01:00
|
|
|
stream_key = (realms[old_message["domain"]].id, old_message["name"].strip().lower())
|
2012-11-07 17:14:43 +01:00
|
|
|
user_id = users[old_message["user"]].id
|
|
|
|
subscribers.setdefault(stream_recipients[stream_key].id, set())
|
|
|
|
try:
|
|
|
|
subscribers[stream_recipients[stream_key].id].remove(user_id)
|
|
|
|
except KeyError:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Error unsubscribing %s from %s: not subscribed" % (
|
|
|
|
old_message["user"], old_message["name"]))
|
2012-10-20 23:31:48 +02:00
|
|
|
pending_subs[(stream_recipients[stream_key].id,
|
|
|
|
users[old_message["user"]].id)] = False
|
2012-10-20 21:43:13 +02:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "user_activated" or message_type == "user_created":
|
2012-10-25 21:39:34 +02:00
|
|
|
# These are rare, so just handle them the slow way
|
2013-03-08 19:58:18 +01:00
|
|
|
user_profile = users[old_message["user"]]
|
2012-12-05 19:53:23 +01:00
|
|
|
join_date = timestamp_to_datetime(old_message['timestamp'])
|
2013-03-08 19:58:18 +01:00
|
|
|
do_activate_user(user_profile, log=False, join_date=join_date)
|
2012-10-25 20:19:55 +02:00
|
|
|
# Update the cache of users to show this user as activated
|
2013-03-08 19:58:18 +01:00
|
|
|
users_by_id[user_profile.id] = user_profile
|
|
|
|
users[old_message["user"]] = user_profile
|
2012-10-25 20:19:55 +02:00
|
|
|
continue
|
2013-07-06 05:38:52 +02:00
|
|
|
elif message_type == "user_deactivated":
|
|
|
|
user_profile = users[old_message["user"]]
|
2013-11-16 17:11:15 +01:00
|
|
|
do_deactivate_user(user_profile, log=False)
|
2013-07-06 05:38:52 +02:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "user_change_password":
|
2012-10-25 21:39:34 +02:00
|
|
|
# Just handle these the slow way
|
2013-03-29 18:36:27 +01:00
|
|
|
user_profile = users[old_message["user"]]
|
2013-05-17 16:41:01 +02:00
|
|
|
do_change_password(user_profile, old_message["pwhash"], log=False,
|
2013-03-29 18:36:27 +01:00
|
|
|
hashed_password=True)
|
2012-10-25 21:39:34 +02:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "user_change_full_name":
|
2012-10-25 21:39:34 +02:00
|
|
|
# Just handle these the slow way
|
2013-03-28 20:20:31 +01:00
|
|
|
user_profile = users[old_message["user"]]
|
2012-10-25 21:39:34 +02:00
|
|
|
user_profile.full_name = old_message["full_name"]
|
2013-07-23 22:01:38 +02:00
|
|
|
user_profile.save(update_fields=["full_name"])
|
2012-10-25 21:39:34 +02:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "enable_desktop_notifications_changed":
|
2012-11-23 21:23:41 +01:00
|
|
|
# Just handle these the slow way
|
2013-03-28 20:20:31 +01:00
|
|
|
user_profile = users[old_message["user"]]
|
2012-12-05 23:58:21 +01:00
|
|
|
user_profile.enable_desktop_notifications = (old_message["enable_desktop_notifications"] != "false")
|
2013-07-23 22:01:38 +02:00
|
|
|
user_profile.save(update_fields=["enable_desktop_notifications"])
|
2012-11-23 21:23:41 +01:00
|
|
|
continue
|
2013-05-03 21:49:01 +02:00
|
|
|
elif message_type == "enable_sounds_changed":
|
|
|
|
user_profile = users[old_message["user"]]
|
|
|
|
user_profile.enable_sounds = (old_message["enable_sounds"] != "false")
|
2013-07-23 22:01:38 +02:00
|
|
|
user_profile.save(update_fields=["enable_sounds"])
|
2013-05-07 23:19:52 +02:00
|
|
|
elif message_type == "enable_offline_email_notifications_changed":
|
|
|
|
user_profile = users[old_message["user"]]
|
|
|
|
user_profile.enable_offline_email_notifications = (old_message["enable_offline_email_notifications"] != "false")
|
2013-07-23 22:01:38 +02:00
|
|
|
user_profile.save(update_fields=["enable_offline_email_notifications"])
|
2013-05-03 21:49:01 +02:00
|
|
|
continue
|
2013-10-16 17:24:52 +02:00
|
|
|
elif message_type == "enable_offline_push_notifications_changed":
|
|
|
|
user_profile = users[old_message["user"]]
|
|
|
|
user_profile.enable_offline_push_notifications = (old_message["enable_offline_push_notifications"] != "false")
|
|
|
|
user_profile.save(update_fields=["enable_offline_push_notifications"])
|
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "default_streams":
|
2015-10-13 22:54:35 +02:00
|
|
|
set_default_streams(get_realm(old_message["domain"]),
|
2012-11-27 18:26:51 +01:00
|
|
|
old_message["streams"])
|
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "subscription_property":
|
2012-12-13 02:50:34 +01:00
|
|
|
property_name = old_message.get("property")
|
2013-04-08 18:01:01 +02:00
|
|
|
if property_name == "stream_color" or property_name == "color":
|
2013-05-17 16:50:31 +02:00
|
|
|
color = old_message.get("color", old_message.get("value"))
|
2012-12-13 02:50:34 +01:00
|
|
|
pending_colors[(old_message["user"],
|
2013-04-08 18:01:01 +02:00
|
|
|
old_message["stream_name"].lower())] = color
|
2013-05-20 21:33:17 +02:00
|
|
|
elif property_name in ["in_home_view", "notifications"]:
|
2013-05-17 17:21:53 +02:00
|
|
|
# TODO: Handle this
|
|
|
|
continue
|
2012-12-13 02:50:34 +01:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown property %s" % (property_name,))
|
2012-12-03 00:19:00 +01:00
|
|
|
continue
|
2012-12-06 00:08:21 +01:00
|
|
|
elif message_type == "realm_created":
|
2013-05-17 17:21:53 +02:00
|
|
|
# No action required
|
|
|
|
continue
|
|
|
|
elif message_type in ["user_email_changed", "update_onboarding", "update_message"]:
|
|
|
|
# TODO: Handle these
|
2012-12-04 21:32:58 +01:00
|
|
|
continue
|
2012-12-05 20:00:59 +01:00
|
|
|
if message_type not in ["stream", "huddle", "personal"]:
|
|
|
|
raise RuntimeError("Unexpected message type %s" % (message_type,))
|
2012-10-20 21:43:13 +02:00
|
|
|
|
|
|
|
message = messages_by_id[current_message_id]
|
|
|
|
current_message_id += 1
|
|
|
|
|
2012-10-20 18:02:58 +02:00
|
|
|
if message.recipient_id not in subscribers:
|
|
|
|
# Nobody received this message -- probably due to our
|
|
|
|
# subscriptions being out-of-date.
|
|
|
|
continue
|
2012-11-08 23:16:26 +01:00
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
recipient_user_ids = set() # type: Set[int]
|
2012-10-20 18:02:58 +02:00
|
|
|
for user_profile_id in subscribers[message.recipient_id]:
|
2012-11-08 23:16:26 +01:00
|
|
|
recipient_user_ids.add(user_profile_id)
|
2012-10-23 04:01:55 +02:00
|
|
|
if message.recipient_id in personal_recipients:
|
2012-10-23 03:11:39 +02:00
|
|
|
# Include the sender in huddle recipients
|
2012-11-08 23:16:26 +01:00
|
|
|
recipient_user_ids.add(message.sender_id)
|
|
|
|
|
|
|
|
for user_profile_id in recipient_user_ids:
|
2013-03-28 20:47:22 +01:00
|
|
|
if users_by_id[user_profile_id].is_active:
|
2012-11-08 23:16:26 +01:00
|
|
|
um = UserMessage(user_profile_id=user_profile_id,
|
2012-10-23 22:39:40 +02:00
|
|
|
message=message)
|
|
|
|
user_messages_to_create.append(um)
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2012-12-13 01:30:50 +01:00
|
|
|
if len(user_messages_to_create) > 100000:
|
|
|
|
tot_user_messages += len(user_messages_to_create)
|
2013-03-27 15:58:23 +01:00
|
|
|
UserMessage.objects.bulk_create(user_messages_to_create)
|
2012-12-13 01:30:50 +01:00
|
|
|
user_messages_to_create = []
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Importing usermessages, part 2...")
|
2012-12-13 01:30:50 +01:00
|
|
|
tot_user_messages += len(user_messages_to_create)
|
2013-03-27 15:58:23 +01:00
|
|
|
UserMessage.objects.bulk_create(user_messages_to_create)
|
2012-10-20 23:31:48 +02:00
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Finalizing subscriptions...")
|
2016-06-27 16:42:00 +02:00
|
|
|
current_subs = {} # type: Dict[Tuple[int, int], bool]
|
|
|
|
current_subs_obj = {} # type: Dict[Tuple[int, int], Subscription]
|
2012-10-20 23:31:48 +02:00
|
|
|
for s in Subscription.objects.select_related().all():
|
2012-10-22 20:15:25 +02:00
|
|
|
current_subs[(s.recipient_id, s.user_profile_id)] = s.active
|
|
|
|
current_subs_obj[(s.recipient_id, s.user_profile_id)] = s
|
2012-10-20 23:31:48 +02:00
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
subscriptions_to_add = [] # type: List[Subscription]
|
|
|
|
subscriptions_to_change = [] # type: List[Tuple[Tuple[int, int], bool]]
|
2012-10-20 23:31:48 +02:00
|
|
|
for pending_sub in pending_subs.keys():
|
|
|
|
(recipient_id, user_profile_id) = pending_sub
|
|
|
|
current_state = current_subs.get(pending_sub)
|
|
|
|
if pending_subs[pending_sub] == current_state:
|
|
|
|
# Already correct in the database
|
|
|
|
continue
|
|
|
|
elif current_state is not None:
|
|
|
|
subscriptions_to_change.append((pending_sub, pending_subs[pending_sub]))
|
|
|
|
continue
|
|
|
|
|
|
|
|
s = Subscription(recipient_id=recipient_id,
|
2012-10-22 20:15:25 +02:00
|
|
|
user_profile_id=user_profile_id,
|
2012-10-20 23:31:48 +02:00
|
|
|
active=pending_subs[pending_sub])
|
|
|
|
subscriptions_to_add.append(s)
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_add)
|
2016-01-27 22:09:40 +01:00
|
|
|
for (sub_tuple, active) in subscriptions_to_change:
|
|
|
|
current_subs_obj[sub_tuple].active = active
|
|
|
|
current_subs_obj[sub_tuple].save(update_fields=["active"])
|
2012-10-20 18:02:58 +02:00
|
|
|
|
2016-01-26 02:19:31 +01:00
|
|
|
subs = {} # type: Dict[Tuple[int, int], Subscription]
|
2012-12-13 02:50:34 +01:00
|
|
|
for sub in Subscription.objects.all():
|
|
|
|
subs[(sub.user_profile_id, sub.recipient_id)] = sub
|
|
|
|
|
2013-03-29 21:16:51 +01:00
|
|
|
# TODO: do restore of subscription colors -- we're currently not
|
|
|
|
# logging changes so there's little point in having the code :(
|
2012-12-13 02:50:34 +01:00
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Finished importing %s messages (%s usermessages)" % \
|
|
|
|
(len(all_messages), tot_user_messages))
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-12-04 23:50:02 +01:00
|
|
|
site = Site.objects.get_current()
|
2013-07-24 20:56:42 +02:00
|
|
|
site.domain = 'zulip.com'
|
2012-12-04 23:50:02 +01:00
|
|
|
site.save()
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Filling in user pointers...")
|
2012-12-04 23:50:02 +01:00
|
|
|
|
|
|
|
# Set restored pointers to the very latest messages
|
2013-11-01 20:51:09 +01:00
|
|
|
for user_profile in UserProfile.objects.all():
|
|
|
|
try:
|
|
|
|
top = UserMessage.objects.filter(
|
|
|
|
user_profile_id=user_profile.id).order_by("-message")[0]
|
|
|
|
user_profile.pointer = top.message_id
|
|
|
|
except IndexError:
|
|
|
|
user_profile.pointer = -1
|
|
|
|
user_profile.save(update_fields=["pointer"])
|
2012-12-04 23:50:02 +01:00
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print(datetime.datetime.now(), "Done replaying old messages")
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2012-10-03 21:07:40 +02:00
|
|
|
# Create some test messages, including:
|
2012-10-10 23:17:43 +02:00
|
|
|
# - multiple streams
|
2012-10-11 00:01:39 +02:00
|
|
|
# - multiple subjects per stream
|
2012-09-14 18:31:11 +02:00
|
|
|
# - multiple huddles
|
|
|
|
# - multiple personals converastions
|
2012-10-11 00:01:39 +02:00
|
|
|
# - multiple messages per subject
|
2012-09-14 18:31:11 +02:00
|
|
|
# - both single and multi-line content
|
2012-10-03 21:07:40 +02:00
|
|
|
def send_messages(data):
|
2016-06-27 16:50:44 +02:00
|
|
|
# type: (Tuple[int, Sequence[Sequence[int]], Mapping[str, Any], Callable[[str], Any]]) -> int
|
2012-10-03 21:05:48 +02:00
|
|
|
(tot_messages, personals_pairs, options, output) = data
|
2012-10-22 19:47:27 +02:00
|
|
|
random.seed(os.getpid())
|
2015-10-14 22:31:08 +02:00
|
|
|
texts = open("zilencer/management/commands/test_messages.txt", "r").readlines()
|
2012-09-27 21:00:10 +02:00
|
|
|
offset = random.randint(0, len(texts))
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-10-10 23:17:43 +02:00
|
|
|
recipient_streams = [klass.id for klass in
|
2016-06-27 16:42:00 +02:00
|
|
|
Recipient.objects.filter(type=Recipient.STREAM)] # type: List[int]
|
|
|
|
recipient_huddles = [h.id for h in Recipient.objects.filter(type=Recipient.HUDDLE)] # type: List[int]
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2016-06-27 16:42:00 +02:00
|
|
|
huddle_members = {} # type: Dict[int, List[int]]
|
2012-09-14 18:31:11 +02:00
|
|
|
for h in recipient_huddles:
|
2012-10-22 20:15:25 +02:00
|
|
|
huddle_members[h] = [s.user_profile.id for s in
|
2012-09-14 22:22:23 +02:00
|
|
|
Subscription.objects.filter(recipient_id=h)]
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
num_messages = 0
|
2012-09-14 18:31:11 +02:00
|
|
|
random_max = 1000000
|
2016-01-26 02:19:31 +01:00
|
|
|
recipients = {} # type: Dict[int, Tuple[int, int, Dict[str, Any]]]
|
2012-10-03 21:05:48 +02:00
|
|
|
while num_messages < tot_messages:
|
2016-01-26 02:19:31 +01:00
|
|
|
saved_data = {} # type: Dict[str, Any]
|
2012-10-03 21:05:48 +02:00
|
|
|
message = Message()
|
2012-10-19 21:30:42 +02:00
|
|
|
message.sending_client = get_client('populate_db')
|
2012-09-14 18:31:11 +02:00
|
|
|
length = random.randint(1, 5)
|
2012-09-26 20:41:54 +02:00
|
|
|
lines = (t.strip() for t in texts[offset: offset + length])
|
2012-10-03 21:05:48 +02:00
|
|
|
message.content = '\n'.join(lines)
|
2012-09-14 18:31:11 +02:00
|
|
|
offset += length
|
|
|
|
offset = offset % len(texts)
|
|
|
|
|
|
|
|
randkey = random.randint(1, random_max)
|
2012-10-03 21:05:48 +02:00
|
|
|
if (num_messages > 0 and
|
2012-09-14 18:31:11 +02:00
|
|
|
random.randint(1, random_max) * 100. / random_max < options["stickyness"]):
|
|
|
|
# Use an old recipient
|
2012-10-03 21:05:48 +02:00
|
|
|
message_type, recipient_id, saved_data = recipients[num_messages - 1]
|
|
|
|
if message_type == Recipient.PERSONAL:
|
2016-01-27 22:14:21 +01:00
|
|
|
personals_pair = saved_data['personals_pair']
|
2012-09-14 18:31:11 +02:00
|
|
|
random.shuffle(personals_pair)
|
2012-10-10 22:57:21 +02:00
|
|
|
elif message_type == Recipient.STREAM:
|
2016-01-27 22:14:21 +01:00
|
|
|
message.subject = saved_data['subject']
|
2012-10-03 21:05:48 +02:00
|
|
|
message.recipient = get_recipient_by_id(recipient_id)
|
|
|
|
elif message_type == Recipient.HUDDLE:
|
|
|
|
message.recipient = get_recipient_by_id(recipient_id)
|
2012-09-14 18:31:11 +02:00
|
|
|
elif (randkey <= random_max * options["percent_huddles"] / 100.):
|
2012-10-03 21:05:48 +02:00
|
|
|
message_type = Recipient.HUDDLE
|
|
|
|
message.recipient = get_recipient_by_id(random.choice(recipient_huddles))
|
2012-09-14 18:31:11 +02:00
|
|
|
elif (randkey <= random_max * (options["percent_huddles"] + options["percent_personals"]) / 100.):
|
2012-10-03 21:05:48 +02:00
|
|
|
message_type = Recipient.PERSONAL
|
2012-09-14 18:31:11 +02:00
|
|
|
personals_pair = random.choice(personals_pairs)
|
|
|
|
random.shuffle(personals_pair)
|
|
|
|
elif (randkey <= random_max * 1.0):
|
2012-10-10 22:57:21 +02:00
|
|
|
message_type = Recipient.STREAM
|
2012-10-10 23:17:43 +02:00
|
|
|
message.recipient = get_recipient_by_id(random.choice(recipient_streams))
|
2012-10-03 21:05:48 +02:00
|
|
|
|
|
|
|
if message_type == Recipient.HUDDLE:
|
|
|
|
sender_id = random.choice(huddle_members[message.recipient.id])
|
|
|
|
message.sender = get_user_profile_by_id(sender_id)
|
|
|
|
elif message_type == Recipient.PERSONAL:
|
|
|
|
message.recipient = Recipient.objects.get(type=Recipient.PERSONAL,
|
2012-09-14 18:31:11 +02:00
|
|
|
type_id=personals_pair[0])
|
2012-10-03 21:05:48 +02:00
|
|
|
message.sender = get_user_profile_by_id(personals_pair[1])
|
2016-01-27 22:14:21 +01:00
|
|
|
saved_data['personals_pair'] = personals_pair
|
2012-10-10 22:57:21 +02:00
|
|
|
elif message_type == Recipient.STREAM:
|
2012-10-10 22:53:24 +02:00
|
|
|
stream = Stream.objects.get(id=message.recipient.type_id)
|
2012-10-10 23:17:43 +02:00
|
|
|
# Pick a random subscriber to the stream
|
2012-10-03 21:05:48 +02:00
|
|
|
message.sender = random.choice(Subscription.objects.filter(
|
2012-10-22 20:15:25 +02:00
|
|
|
recipient=message.recipient)).user_profile
|
2016-06-27 16:10:33 +02:00
|
|
|
message.subject = stream.name + text_type(random.randint(1, 3))
|
2016-01-27 22:14:21 +01:00
|
|
|
saved_data['subject'] = message.subject
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-11-08 00:48:43 +01:00
|
|
|
message.pub_date = now()
|
2012-10-03 21:05:48 +02:00
|
|
|
do_send_message(message)
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2016-01-26 02:19:31 +01:00
|
|
|
recipients[num_messages] = (message_type, message.recipient.id, saved_data)
|
2012-10-03 21:05:48 +02:00
|
|
|
num_messages += 1
|
|
|
|
return tot_messages
|