Skip to content

Commit 03aedca

Browse files
theViz343Abhirup-99
andcommitted
model/api_types: Handle add/remove subscription events.
This commit adds event handling code for newly added/removed subscriptions. To store the stream subscriptions which are being updated in the event, a new field named "subscriptions" is added to the SubscriptionEvent class. Co-authored-by: Abhirup Pal <abhiruppalmethodist@gmail.com> Co-authored-by: Vishwesh Pillai <vishwesh103@gmail.com>
1 parent d3b5bb6 commit 03aedca

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

tests/model/test_model.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4310,6 +4310,70 @@ def test__handle_subscription_event_subscribers_one_user_multiple_streams(
43104310
new_subscribers = model._subscribed_streams[stream_id]["subscribers"]
43114311
assert new_subscribers == expected_subscribers
43124312

4313+
@pytest.mark.parametrize(
4314+
"event, action",
4315+
[
4316+
case(
4317+
{
4318+
"type": "subscription",
4319+
"op": "add",
4320+
"subscriptions": [
4321+
{
4322+
"name": "Stream 10",
4323+
"date_created": 1472047124,
4324+
"invite_only": False,
4325+
"color": "#b0a5fd",
4326+
"pin_to_top": False,
4327+
"stream_id": 10,
4328+
"is_muted": False,
4329+
"audible_notifications": False,
4330+
"description": "A description of stream 10",
4331+
"rendered_description": "A description of stream 10",
4332+
"desktop_notifications": False,
4333+
"stream_weekly_traffic": 0,
4334+
"push_notifications": False,
4335+
"message_retention_days": 30,
4336+
"email_address": "stream10@example.com",
4337+
"email_notifications": False,
4338+
"wildcard_mentions_notify": False,
4339+
"subscribers": [1001, 11, 12],
4340+
"history_public_to_subscribers": True,
4341+
"is_announcement_only": True,
4342+
"stream_post_policy": 0,
4343+
"is_web_public": True,
4344+
"first_message_id": None,
4345+
}
4346+
],
4347+
},
4348+
"add",
4349+
),
4350+
case(
4351+
{
4352+
"type": "subscription",
4353+
"op": "remove",
4354+
"subscriptions": [
4355+
{
4356+
"name": "Stream 10",
4357+
"stream_id": 10,
4358+
}
4359+
],
4360+
},
4361+
"remove",
4362+
),
4363+
],
4364+
)
4365+
def test__handle_subscription_event_add_remove_subscription(
4366+
self, mocker, model, event, action
4367+
):
4368+
if action == "add":
4369+
model._subscribe_to_streams = mocker.Mock()
4370+
model._handle_subscription_event(event)
4371+
model._subscribe_to_streams.assert_called_once()
4372+
else:
4373+
model._unsubscribe_from_streams = mocker.Mock()
4374+
model._handle_subscription_event(event)
4375+
model._unsubscribe_from_streams.assert_called_once()
4376+
43134377
@pytest.mark.parametrize(
43144378
"person, event_field, updated_field_if_different",
43154379
[

zulipterminal/api_types.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,18 @@ class SubscriptionPeerAddRemoveEvent(TypedDict):
438438
user_ids: List[int] # NOTE: replaces 'user_id' in ZFL 35
439439

440440

441+
class SubscriptionAddEvent(TypedDict):
442+
type: Literal["subscription"]
443+
op: Literal["add"]
444+
subscriptions: List[Subscription]
445+
446+
447+
class SubscriptionRemoveEvent(TypedDict):
448+
type: Literal["subscription"]
449+
op: Literal["remove"]
450+
subscriptions: List[RemovedSubscription]
451+
452+
441453
# -----------------------------------------------------------------------------
442454
# See https://zulip.com/api/get-events#typing-start and -stop
443455
class _TypingEventUser(TypedDict):
@@ -532,6 +544,8 @@ class UpdateDisplaySettingsEvent(TypedDict):
532544
UpdateMessagesLocationEvent,
533545
ReactionEvent,
534546
SubscriptionUpdateEvent,
547+
SubscriptionAddEvent,
548+
SubscriptionRemoveEvent,
535549
SubscriptionPeerAddRemoveEvent,
536550
TypingEvent,
537551
UpdateMessageFlagsEvent,

zulipterminal/model.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,9 @@ def make_reduced_stream_data(stream: Subscription) -> StreamData:
13081308
new_visual_notified_streams
13091309
)
13101310

1311+
self.normalize_and_cache_message_retention_text()
1312+
self.normalize_date_created_field()
1313+
13111314
def _unsubscribe_from_streams(
13121315
self, subscriptions: List[RemovedSubscription]
13131316
) -> None:
@@ -1487,7 +1490,7 @@ def get_stream_by_id(streams: List[StreamData], stream_id: int) -> StreamData:
14871490
self.visual_notified_streams.add(stream_id)
14881491
else:
14891492
self.visual_notified_streams.discard(stream_id)
1490-
elif event["op"] in ("peer_add", "peer_remove"):
1493+
elif event["op"] == "peer_add" or event["op"] == "peer_remove":
14911494
# NOTE: ZFL 35 commit was not atomic with API change
14921495
# (ZFL >=35 can use new plural style)
14931496
if "stream_ids" not in event or "user_ids" not in event:
@@ -1505,6 +1508,14 @@ def get_stream_by_id(streams: List[StreamData], stream_id: int) -> StreamData:
15051508
else:
15061509
for user_id in user_ids:
15071510
subscribers.remove(user_id)
1511+
elif event["op"] == "add":
1512+
self._subscribe_to_streams(event["subscriptions"])
1513+
self.controller.view.left_panel.update_stream_view()
1514+
self.controller.update_screen()
1515+
elif event["op"] == "remove":
1516+
self._unsubscribe_from_streams(event["subscriptions"])
1517+
self.controller.view.left_panel.update_stream_view()
1518+
self.controller.update_screen()
15081519

15091520
def _handle_typing_event(self, event: Event) -> None:
15101521
"""

0 commit comments

Comments
 (0)