Skip to content

Commit

Permalink
model: Add support for stream muting via is_muted property in events.
Browse files Browse the repository at this point in the history
This commit contains changes made to _handle_subscription_event()
for handling is_muted events added under ZFL 139.

At some later stage we will remove support for in_home_view as a valid
property. For now we migrate to support newer servers (only is_muted) and older
servers (only in_home_view).

Tests added.

Fixes #1251.
  • Loading branch information
mounilKshah authored and neiljp committed Oct 2, 2022
1 parent 01b3853 commit 7d69b92
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
30 changes: 28 additions & 2 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3031,8 +3031,31 @@ def test__handle_typing_event(
},
{15, 19, 30},
),
(
{
"property": "is_muted",
"op": "update",
"stream_id": 19,
"value": False,
},
{15},
),
(
{
"property": "is_muted",
"op": "update",
"stream_id": 30,
"value": True,
},
{15, 19, 30},
),
],
ids=[
"remove_19_in_home_view:ZFLNone",
"add_30_in_home_view:ZFLNone",
"remove_19_is_muted:ZFL139",
"add_30_is_muted:ZFL139",
],
ids=["remove_19", "add_30"],
)
def test__handle_subscription_event_mute_streams(
self, model, mocker, stream_button, event, final_muted_streams
Expand All @@ -3054,7 +3077,10 @@ def test__handle_subscription_event_mute_streams(
model._handle_subscription_event(event)

assert model.muted_streams == final_muted_streams
if event["value"]:
# This condition is to check if the stream is unmuted or not
if (event["value"] and event["property"] == "in_home_view") or (
not event["value"] and event["property"] == "is_muted"
):
mark_unmuted.assert_called_once_with(99)
assert model.unread_counts["all_msg"] == 399
else:
Expand Down
32 changes: 22 additions & 10 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,23 +1287,35 @@ def get_stream_by_id(streams: List[StreamData], stream_id: int) -> StreamData:

if event["op"] == "update":
if hasattr(self.controller, "view"):
# NOTE: Currently, the response from the API still contains in_home_view
# and not is_muted, hence, the conditional is left unaltered, for now.
if event.get("property", None) == "in_home_view":
# NOTE: As per ZFL 139, is_muted is supported now, but the server
# also sends event with in_home_view to support older versions
if (
event.get("property", None) == "in_home_view"
or event.get("property", None) == "is_muted"
):
# Account for in_home_view and is_muted having opposite boolean values
event_stream_muted_value: bool
if event.get("property", None) == "in_home_view":
event_stream_muted_value = not event["value"]
else:
event_stream_muted_value = event["value"]

stream_id = event["stream_id"]

# FIXME: Does this always contain the stream_id?
stream_button = self.controller.view.stream_id_to_button[stream_id]

unread_count = self.unread_counts["streams"][stream_id]
if event["value"]: # Unmuting streams
self.muted_streams.remove(stream_id)
self.unread_counts["all_msg"] += unread_count
stream_button.mark_unmuted(unread_count)
if not event_stream_muted_value: # Unmuting streams
if stream_id in self.muted_streams:
self.muted_streams.remove(stream_id)
self.unread_counts["all_msg"] += unread_count
stream_button.mark_unmuted(unread_count)
else: # Muting streams
self.muted_streams.add(stream_id)
self.unread_counts["all_msg"] -= unread_count
stream_button.mark_muted()
if stream_id not in self.muted_streams:
self.muted_streams.add(stream_id)
self.unread_counts["all_msg"] -= unread_count
stream_button.mark_muted()
self.controller.update_screen()
elif event.get("property", None) == "pin_to_top":
stream_id = event["stream_id"]
Expand Down

0 comments on commit 7d69b92

Please sign in to comment.