Skip to content

Commit a0233ec

Browse files
Niloth-ptimabbott
authored andcommitted
model: Add get_user_id_from_reaction for messages and reaction events.
In preparation for deprecation of `user` field from reactions. Tests added.
1 parent c005b7f commit a0233ec

File tree

2 files changed

+85
-19
lines changed

2 files changed

+85
-19
lines changed

tests/model/test_model.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,75 @@ def test_has_user_reacted_to_message(
790790

791791
assert has_reacted == expected_has_user_reacted
792792

793+
@pytest.mark.parametrize(
794+
"user_data, expected_user_id",
795+
[
796+
case({"user_id": 456}, 456, id="reaction_event_user_id"),
797+
case(
798+
{"user": {"user_id": 123}},
799+
123,
800+
id="reaction_event_user",
801+
),
802+
case(
803+
{"user_id": 101},
804+
101,
805+
id="reaction_user_id",
806+
),
807+
case(
808+
{"user": {"id": 202}},
809+
202,
810+
id="reaction_user",
811+
),
812+
],
813+
)
814+
def test_get_user_id_from_reaction_success(
815+
self,
816+
model: Model,
817+
user_data: Dict[str, Any],
818+
expected_user_id: int,
819+
):
820+
reaction = {
821+
"type": "reaction",
822+
"op": "add",
823+
"reaction_type": "unicode_emoji",
824+
"emoji_code": "1f44d",
825+
"emoji_name": "thumbs_up",
826+
"message_id": "24757",
827+
**user_data,
828+
}
829+
assert model.get_user_id_from_reaction(reaction) == expected_user_id
830+
831+
@pytest.mark.parametrize(
832+
"user_data",
833+
[
834+
case({}, id="reaction_event_missing_user_id_user"),
835+
case(
836+
{"user": {"id": "not_an_int"}},
837+
id="invalid_user",
838+
),
839+
case(
840+
{"user_id": "not_an_int"},
841+
id="invalid_user_id",
842+
),
843+
],
844+
)
845+
def test_get_user_id_from_reaction_failure(
846+
self,
847+
model: Model,
848+
user_data: Dict[str, Any],
849+
):
850+
reaction = {
851+
"type": "reaction",
852+
"op": "add",
853+
"reaction_type": "unicode_emoji",
854+
"emoji_code": "1f44d",
855+
"emoji_name": "thumbs_up",
856+
"message_id": "24757",
857+
**user_data,
858+
}
859+
with pytest.raises((AssertionError, AttributeError)):
860+
model.get_user_id_from_reaction(reaction)
861+
793862
@pytest.mark.parametrize("recipient_user_ids", [[5140], [5140, 5179]])
794863
@pytest.mark.parametrize("status", ["start", "stop"])
795864
def test_send_typing_status_by_user_ids(

zulipterminal/model.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
MessagesFlagChange,
4949
PrivateComposition,
5050
PrivateMessageUpdateRequest,
51+
ReactionEvent,
5152
RealmEmojiData,
5253
RealmUser,
5354
StreamComposition,
@@ -479,18 +480,22 @@ def has_user_reacted_to_message(self, message: Message, *, emoji_code: str) -> b
479480
for reaction in message["reactions"]:
480481
if reaction["emoji_code"] != emoji_code:
481482
continue
482-
# The reaction.user_id field was added in Zulip v3.0, ZFL 2 so we need to
483-
# check both the reaction.user.{user_id/id} fields too for pre v3 support.
484-
user = reaction.get("user", {})
485-
has_user_reacted = (
486-
user.get("user_id", None) == self.user_id
487-
or user.get("id", None) == self.user_id
488-
or reaction.get("user_id", None) == self.user_id
489-
)
490-
if has_user_reacted:
483+
user_id = self.get_user_id_from_reaction(reaction)
484+
if user_id == self.user_id:
491485
return True
492486
return False
493487

488+
def get_user_id_from_reaction(
489+
self, reaction: Union[Dict[str, Any], ReactionEvent]
490+
) -> int:
491+
# The reaction.user_id field was added in Zulip v3.0, ZFL 2 so we need to
492+
# check both the reaction.user.{user_id/id} fields too for pre v3 support.
493+
user = reaction.get("user", {})
494+
assert isinstance(user, dict)
495+
user_id = user.get("id") or user.get("user_id") or reaction.get("user_id")
496+
assert isinstance(user_id, int)
497+
return user_id
498+
494499
def session_draft_message(self) -> Optional[Composition]:
495500
return deepcopy(self._draft)
496501

@@ -1859,16 +1864,8 @@ def _handle_reaction_event(self, event: Event) -> None:
18591864
message["reactions"].append(reactions_entry)
18601865
else:
18611866
for reaction in message["reactions"]:
1862-
reaction_user_id = (
1863-
reaction["user"]["id"]
1864-
if "user" in reaction
1865-
else reaction["user_id"]
1866-
)
1867-
event_user_id = (
1868-
event["user"]["user_id"]
1869-
if event.get("user") and isinstance(event["user"], dict)
1870-
else event["user_id"]
1871-
)
1867+
reaction_user_id = self.get_user_id_from_reaction(reaction)
1868+
event_user_id = self.get_user_id_from_reaction(event)
18721869
if (
18731870
reaction["emoji_code"] == event["emoji_code"]
18741871
and reaction_user_id == event_user_id

0 commit comments

Comments
 (0)