From 356e6e50185cadb12c5a7e10a5ff8936da4b632b Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Thu, 2 Sep 2021 16:46:24 -0700 Subject: [PATCH] status: Extract format_user_status helper. This will allow us to reuse this formatting logic for a single-user version of this endpoint. --- zerver/lib/user_status.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/zerver/lib/user_status.py b/zerver/lib/user_status.py index efc82bff16..3009a47652 100644 --- a/zerver/lib/user_status.py +++ b/zerver/lib/user_status.py @@ -6,6 +6,26 @@ from django.utils.timezone import now as timezone_now from zerver.models import UserStatus +def format_user_status(row: Dict[str, Any]) -> Dict[str, Any]: + away = row["status"] == UserStatus.AWAY + status_text = row["status_text"] + emoji_name = row["emoji_name"] + emoji_code = row["emoji_code"] + reaction_type = row["reaction_type"] + + dct = {} + if away: + dct["away"] = away + if status_text: + dct["status_text"] = status_text + if emoji_name: + dct["emoji_name"] = emoji_name + dct["emoji_code"] = emoji_code + dct["reaction_type"] = reaction_type + + return dct + + def get_user_info_dict(realm_id: int) -> Dict[str, Dict[str, Any]]: rows = ( UserStatus.objects.filter( @@ -31,24 +51,8 @@ def get_user_info_dict(realm_id: int) -> Dict[str, Dict[str, Any]]: user_dict: Dict[str, Dict[str, Any]] = {} for row in rows: - away = row["status"] == UserStatus.AWAY - status_text = row["status_text"] user_id = row["user_profile_id"] - emoji_name = row["emoji_name"] - emoji_code = row["emoji_code"] - reaction_type = row["reaction_type"] - - dct = {} - if away: - dct["away"] = away - if status_text: - dct["status_text"] = status_text - if emoji_name: - dct["emoji_name"] = emoji_name - dct["emoji_code"] = emoji_code - dct["reaction_type"] = reaction_type - - user_dict[str(user_id)] = dct + user_dict[str(user_id)] = format_user_status(row) return user_dict