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.

reactions events: Use ReactionAdd/Remove pydantic types.

Evy Kassirer8c800ffcbe9c
diff --git a/zerver/actions/reactions.py b/zerver/actions/reactions.py
index 5ad4123c309..41aad2b346e 100644
--- a/zerver/actions/reactions.py
+++ b/zerver/actions/reactions.py
@@ -1,9 +1,8 @@
-from typing import Literal
-
-from typing_extensions import TypedDict
+from typing import Literal, cast
from zerver.actions.user_topics import do_set_user_topic_visibility_policy
from zerver.lib.emoji import check_emoji_request, get_emoji_data
+from zerver.lib.event_types import BaseEvent, ReactionAddEvent, ReactionRemoveEvent, ReactionType
from zerver.lib.exceptions import ReactionExistsError
from zerver.lib.message import (
access_message_and_usermessage,
@@ -19,31 +18,32 @@ from zerver.models import Message, Reaction, UserProfile
from zerver.tornado.django_api import send_event_on_commit
-class ReactionEvent(TypedDict):
- type: Literal["reaction"]
- op: Literal["add", "remove"]
- user_id: int
- message_id: int
- emoji_name: str
- emoji_code: str
- reaction_type: str
-
-
def notify_reaction_update(
user_profile: UserProfile,
message: Message,
reaction: Reaction,
op: Literal["add", "remove"],
) -> None:
- event: ReactionEvent = {
- "type": "reaction",
- "op": op,
- "user_id": user_profile.id,
- "message_id": message.id,
- "emoji_name": reaction.emoji_name,
- "emoji_code": reaction.emoji_code,
- "reaction_type": reaction.reaction_type,
- }
+ # The reaction_type column is a CharField restricted to these
+ # values via choices, so the cast is safe.
+ reaction_type = cast(ReactionType, reaction.reaction_type)
+ event: BaseEvent
+ if op == "add":
+ event = ReactionAddEvent(
+ user_id=user_profile.id,
+ message_id=message.id,
+ emoji_name=reaction.emoji_name,
+ emoji_code=reaction.emoji_code,
+ reaction_type=reaction_type,
+ )
+ else:
+ event = ReactionRemoveEvent(
+ user_id=user_profile.id,
+ message_id=message.id,
+ emoji_name=reaction.emoji_name,
+ emoji_code=reaction.emoji_code,
+ reaction_type=reaction_type,
+ )
# Update the cached message since new reaction is added.
update_message_cache([message])
diff --git a/zerver/lib/event_types.py b/zerver/lib/event_types.py
index 10c1600e87c..60f62e152df 100644
--- a/zerver/lib/event_types.py
+++ b/zerver/lib/event_types.py
@@ -18,6 +18,8 @@ def check_url(val: str) -> str:
Url = Annotated[str, AfterValidator(check_url)]
+ReactionType = Literal["realm_emoji", "unicode_emoji", "zulip_extra_emoji"]
+
class BaseEvent(BaseModel):
pass
@@ -373,22 +375,22 @@ class ModernPresenceEvent(BaseEvent):
class ReactionAddEvent(BaseEvent):
- type: Literal["reaction"]
- op: Literal["add"]
+ type: Literal["reaction"] = "reaction"
+ op: Literal["add"] = "add"
message_id: int
emoji_name: str
emoji_code: str
- reaction_type: Literal["realm_emoji", "unicode_emoji", "zulip_extra_emoji"]
+ reaction_type: ReactionType
user_id: int
class ReactionRemoveEvent(BaseEvent):
- type: Literal["reaction"]
- op: Literal["remove"]
+ type: Literal["reaction"] = "reaction"
+ op: Literal["remove"] = "remove"
message_id: int
emoji_name: str
emoji_code: str
- reaction_type: Literal["realm_emoji", "unicode_emoji", "zulip_extra_emoji"]
+ reaction_type: ReactionType
user_id: int
diff --git a/zerver/tests/test_reactions.py b/zerver/tests/test_reactions.py
index 9a04710a487..d31632eb3b5 100644
--- a/zerver/tests/test_reactions.py
+++ b/zerver/tests/test_reactions.py
@@ -1238,7 +1238,7 @@ class ReactionAPIEventTest(EmojiReactionBase):
message=message,
emoji_name="whatever",
emoji_code="whatever",
- reaction_type="whatever",
+ reaction_type="unicode_emoji",
)
with (