import: Fix pointer logic for zulip->zulip imports.

Previously, the pointer was almost guaranteed to be an invalid random
value, because we renumber message IDs unconditionally now.
This commit is contained in:
Tim Abbott 2019-01-02 14:31:53 -08:00
parent c03615b982
commit 8cfea958de

View File

@ -903,6 +903,29 @@ def do_import_realm(import_dir: Path, subdomain: str) -> Realm:
update_model_ids(Reaction, data, 'reaction')
bulk_import_model(data, Reaction)
for user_profile in UserProfile.objects.filter(is_bot=False, realm=realm):
# Since we now unconditionally renumbers message IDs, we need
# to reset the user's pointer to what will be a valid value.
#
# For zulip->zulip imports, we could do something clever, but
# it should always be safe to reset to first unread message.
#
# Longer-term, the plan is to eliminate pointer as a concept.
first_unread_message = UserMessage.objects.filter(user_profile=user_profile).extra(
where=[UserMessage.where_unread()]
).first()
if first_unread_message is not None:
user_profile.pointer = first_unread_message.message_id
else:
last_message = UserMessage.objects.filter(user_profile=user_profile).last()
if last_message is not None:
user_profile.pointer = last_message.message_id
else:
# -1 is the guard value for new user accounts with no messages.
user_profile.pointer = -1
user_profile.save(update_fields=["pointer"])
# Do attachments AFTER message data is loaded.
# TODO: de-dup how we read these json files.
fn = os.path.join(import_dir, "attachment.json")