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.
This commit is contained in:
Tim Abbott 2018-12-16 18:45:19 -08:00
parent f47f263655
commit 37189e1f9d

View File

@ -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',