From 37189e1f9db828599e98a2d342e2bbdadf8dd9de Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Sun, 16 Dec 2018 18:45:19 -0800 Subject: [PATCH] soft deactivation: Handle case where a user has no message history. I'm aware of at least one case where this happened with some imported data history; better to not have that crash. --- zerver/lib/soft_deactivation.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/zerver/lib/soft_deactivation.py b/zerver/lib/soft_deactivation.py index cd4d63909e..8eb9a34905 100644 --- a/zerver/lib/soft_deactivation.py +++ b/zerver/lib/soft_deactivation.py @@ -161,9 +161,14 @@ def add_missing_messages(user_profile: UserProfile) -> None: UserMessage.objects.bulk_create(user_messages_to_insert) def do_soft_deactivate_user(user_profile: UserProfile) -> None: - user_profile.last_active_message_id = UserMessage.objects.filter( - user_profile=user_profile).order_by( - '-message__id')[0].message_id + try: + user_profile.last_active_message_id = UserMessage.objects.filter( + user_profile=user_profile).order_by( + '-message__id')[0].message_id + except IndexError: # nocoverage + # In the unlikely event that a user somehow has never received + # a message, we just use the overall max message ID. + user_profile.last_active_message_id = Message.objects.max().id user_profile.long_term_idle = True user_profile.save(update_fields=[ 'long_term_idle',