Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

user_status events: Use UserStatusEvent pydantic type.

Evy Kassirerb75f419efe08
diff --git a/zerver/actions/user_status.py b/zerver/actions/user_status.py
index ede31abcd71..adc6e03de33 100644
--- a/zerver/actions/user_status.py
+++ b/zerver/actions/user_status.py
@@ -1,12 +1,25 @@
+from typing import cast
+
from django.db import transaction
+from typing_extensions import Required, TypedDict
from zerver.actions.user_settings import do_change_user_setting
+from zerver.lib.event_types import ReactionType, UserStatusEvent
from zerver.lib.user_status import update_user_status
from zerver.lib.users import get_user_ids_who_can_access_user
from zerver.models import UserProfile
from zerver.tornado.django_api import send_event_on_commit
+class UserStatusEventDict(TypedDict, total=False):
+ user_id: Required[int]
+ away: bool
+ status_text: str
+ emoji_name: str
+ emoji_code: str | None
+ reaction_type: ReactionType | None
+
+
@transaction.atomic(durable=True)
def do_update_user_status(
user_profile: UserProfile,
@@ -37,10 +50,9 @@ def do_update_user_status(
reaction_type=reaction_type,
)
- event = dict(
- type="user_status",
- user_id=user_profile.id,
- )
+ # Only fields the caller explicitly opted into go on the event, so
+ # that the serialized event omits keys that weren't requested.
+ event: UserStatusEventDict = {"user_id": user_profile.id}
if away is not None:
event["away"] = away
@@ -51,5 +63,9 @@ def do_update_user_status(
if emoji_name is not None:
event["emoji_name"] = emoji_name
event["emoji_code"] = emoji_code
- event["reaction_type"] = reaction_type
- send_event_on_commit(realm, event, get_user_ids_who_can_access_user(user_profile))
+ # The Reaction.reaction_type column is restricted to these values via
+ # CharField choices, so the cast is safe.
+ event["reaction_type"] = cast(ReactionType | None, reaction_type)
+ send_event_on_commit(
+ realm, UserStatusEvent(**event), get_user_ids_who_can_access_user(user_profile)
+ )
diff --git a/zerver/lib/event_types.py b/zerver/lib/event_types.py
index b6349a49c35..8e6f868db5e 100644
--- a/zerver/lib/event_types.py
+++ b/zerver/lib/event_types.py
@@ -1256,7 +1256,7 @@ class UserSettingsUpdateEvent(UserSettingsUpdateCoreEvent):
class UserStatusCoreEvent(BaseEvent):
- type: Literal["user_status"]
+ type: Literal["user_status"] = "user_status"
user_id: int
@@ -1266,7 +1266,7 @@ class UserStatusEvent(UserStatusCoreEvent):
status_text: str | None = None
emoji_name: str | None = None
emoji_code: str | None = None
- reaction_type: Literal["realm_emoji", "unicode_emoji", "zulip_extra_emoji"] | None = None
+ reaction_type: ReactionType | None = None
class UserTopicEvent(BaseEvent):