Skip to content

Commit 85b2d50

Browse files
committed
bugfix: model: Avoid creating empty messages from reaction events.
This updates an approach taken in the original reaction implementation. Since locally cached messages are currently stored in a `defaultdict`, direct indexing with a non-present message-id creates an empty message. Such messages then generate `KeyError`s when flags are updated later (eg. marking read, toggling star status), if the message has not been fetched. Tests updated to explicitly exclude generation of empty index for reaction events. Fixes #920.
1 parent 4318eb3 commit 85b2d50

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

tests/model/test_model.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,9 +2041,7 @@ def reaction_event_index_factory(self):
20412041
def _factory(msgs: MsgsType):
20422042
return {
20432043
"messages": {
2044-
message_id: {}
2045-
if reactions is None
2046-
else {
2044+
message_id: {
20472045
"id": message_id,
20482046
"content": f"message content {message_id}",
20492047
"reactions": [
@@ -2061,6 +2059,7 @@ def _factory(msgs: MsgsType):
20612059
],
20622060
}
20632061
for message_id, reactions in msgs
2062+
if reactions is not None
20642063
}
20652064
}
20662065

@@ -2082,7 +2081,7 @@ def test__handle_reaction_event_not_in_index(
20822081
)
20832082
model.index = reaction_event_index_factory(
20842083
[
2085-
(unindexed_message_id, None),
2084+
(unindexed_message_id, None), # explicitly exclude
20862085
(2, [(1, "unicode_emoji", "1232", "thumbs_up")]),
20872086
(3, []),
20882087
]

zulipterminal/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ def _handle_reaction_event(self, event: Event) -> None:
14271427
assert event["type"] == "reaction"
14281428
message_id = event["message_id"]
14291429
# If the message is indexed
1430-
if self.index["messages"][message_id] != {}:
1430+
if message_id in self.index["messages"]:
14311431

14321432
message = self.index["messages"][message_id]
14331433
if event["op"] == "add":

0 commit comments

Comments
 (0)