populate_db Use message_type variable consistently.

We previously were only using it at the first loop through all
messages, which meant code accessing the message type copied from one
place to another would break (potentially subtly), because things
would work if and only if the very last message happened to have the
same type as what is expected in the relevant piece of code.

(imported from commit ad9ce5efdb200e0c0d5c3ffa6db33113fdad8c5a)
This commit is contained in:
Tim Abbott
2012-12-05 18:08:21 -05:00
parent 28aa976be3
commit b5c8fd73f5

View File

@@ -415,7 +415,8 @@ def restore_saved_messages():
messages_to_create = [] messages_to_create = []
for idx, old_message in enumerate(old_messages): for idx, old_message in enumerate(old_messages):
if old_message["type"] not in ["stream", "huddle", "personal"]: message_type = old_message["type"]
if message_type not in ["stream", "huddle", "personal"]:
continue continue
message = Message() message = Message()
@@ -441,7 +442,7 @@ def restore_saved_messages():
else: else:
message.sending_client = clients['populate_db'] message.sending_client = clients['populate_db']
message.type = type_hash[old_message["type"]] message.type = type_hash[message_type]
message.content = old_message["content"] message.content = old_message["content"]
message.subject = old_message["subject"] message.subject = old_message["subject"]
message.pub_date = timestamp_to_datetime(old_message["timestamp"]) message.pub_date = timestamp_to_datetime(old_message["timestamp"])
@@ -481,15 +482,15 @@ def restore_saved_messages():
pending_subs = {} pending_subs = {}
current_message_id = first_message_id current_message_id = first_message_id
for old_message in old_messages: for old_message in old_messages:
# Update our subscribers hashes as we see subscription events message_type = old_message["type"]
if old_message["type"] == 'subscription_added': if message_type == 'subscription_added':
stream_key = (realms[old_message["domain"]].id, old_message["name"].strip().lower()) stream_key = (realms[old_message["domain"]].id, old_message["name"].strip().lower())
subscribers.setdefault(stream_recipients[stream_key].id, subscribers.setdefault(stream_recipients[stream_key].id,
set()).add(users[old_message["user"]].id) set()).add(users[old_message["user"]].id)
pending_subs[(stream_recipients[stream_key].id, pending_subs[(stream_recipients[stream_key].id,
users[old_message["user"]].id)] = True users[old_message["user"]].id)] = True
continue continue
elif old_message["type"] == "subscription_removed": elif message_type == "subscription_removed":
stream_key = (realms[old_message["domain"]].id, old_message["name"].strip().lower()) stream_key = (realms[old_message["domain"]].id, old_message["name"].strip().lower())
user_id = users[old_message["user"]].id user_id = users[old_message["user"]].id
subscribers.setdefault(stream_recipients[stream_key].id, set()) subscribers.setdefault(stream_recipients[stream_key].id, set())
@@ -501,7 +502,7 @@ def restore_saved_messages():
pending_subs[(stream_recipients[stream_key].id, pending_subs[(stream_recipients[stream_key].id,
users[old_message["user"]].id)] = False users[old_message["user"]].id)] = False
continue continue
elif old_message["type"] == "user_activated" or old_message["type"] == "user_created": elif message_type == "user_activated" or message_type == "user_created":
# These are rare, so just handle them the slow way # These are rare, so just handle them the slow way
user = User.objects.get(email=old_message["user"]) user = User.objects.get(email=old_message["user"])
join_date = timestamp_to_datetime(old_message['timestamp']) join_date = timestamp_to_datetime(old_message['timestamp'])
@@ -510,32 +511,32 @@ def restore_saved_messages():
users_by_id[user.userprofile.id] = UserProfile.objects.get(user=user) users_by_id[user.userprofile.id] = UserProfile.objects.get(user=user)
users[user.email] = user.userprofile users[user.email] = user.userprofile
continue continue
elif old_message["type"] == "user_change_password": elif message_type == "user_change_password":
# Just handle these the slow way # Just handle these the slow way
user = User.objects.get(email=old_message["user"]) user = User.objects.get(email=old_message["user"])
user.password = old_message["pwhash"] user.password = old_message["pwhash"]
user.save() user.save()
continue continue
elif old_message["type"] == "user_change_full_name": elif message_type == "user_change_full_name":
# Just handle these the slow way # Just handle these the slow way
user_profile = UserProfile.objects.get(user__email=old_message["user"]) user_profile = UserProfile.objects.get(user__email=old_message["user"])
user_profile.full_name = old_message["full_name"] user_profile.full_name = old_message["full_name"]
user_profile.save() user_profile.save()
continue continue
elif old_message["type"] == "enable_desktop_notifications_changed": elif message_type == "enable_desktop_notifications_changed":
# Just handle these the slow way # Just handle these the slow way
user_profile = UserProfile.objects.get(user__email=old_message["user"]) user_profile = UserProfile.objects.get(user__email=old_message["user"])
user_profile.enable_desktop_notifications = (old_message["enable_desktop_notifications"] != "false") user_profile.enable_desktop_notifications = (old_message["enable_desktop_notifications"] != "false")
user_profile.save() user_profile.save()
continue continue
elif old_message["type"] == "default_streams": elif message_type == "default_streams":
set_default_streams(Realm.objects.get(domain=old_message["domain"]), set_default_streams(Realm.objects.get(domain=old_message["domain"]),
old_message["streams"]) old_message["streams"])
continue continue
elif old_message["type"] == "subscription_property": elif message_type == "subscription_property":
handle_subscription_property(old_message) handle_subscription_property(old_message)
continue continue
elif old_message["type"] == "realm_created": elif message_type == "realm_created":
continue continue
if message_type not in ["stream", "huddle", "personal"]: if message_type not in ["stream", "huddle", "personal"]:
raise RuntimeError("Unexpected message type %s" % (message_type,)) raise RuntimeError("Unexpected message type %s" % (message_type,))