diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.py index 57e4d774dc..4b1fdfcfca 100644 --- a/zerver/lib/import_realm.py +++ b/zerver/lib/import_realm.py @@ -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")