Skip to content

Commit 8a45bc7

Browse files
committed
model: Add stream and subscription property accessor functions.
This commit adds stream and subscription property accessor functions to limit the mutability of stream/subscription properties where it is not needed.
1 parent a73dbaf commit 8a45bc7

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

zulipterminal/model.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import zulip
2929
from bs4 import BeautifulSoup
30-
from typing_extensions import TypedDict
30+
from typing_extensions import Literal, TypedDict
3131

3232
from zulipterminal import unicode_emojis
3333
from zulipterminal.api_types import (
@@ -1252,6 +1252,53 @@ def _get_all_stream_ids(self) -> List[int]:
12521252
id_list.extend(stream_id for stream_id in self._never_subscribed_streams)
12531253
return id_list
12541254

1255+
STREAM_KEYS = Literal[
1256+
"stream_id",
1257+
"name",
1258+
"description",
1259+
"rendered_description",
1260+
"date_created",
1261+
"invite_only",
1262+
"subscribers",
1263+
"is_announcement_only",
1264+
"stream_post_policy",
1265+
"is_web_public",
1266+
"message_retention_days",
1267+
"history_public_to_subscribers",
1268+
"first_message_id",
1269+
"stream_weekly_traffic",
1270+
]
1271+
SUBSCRIPTION_KEYS = Literal[
1272+
STREAM_KEYS,
1273+
"desktop_notifications",
1274+
"email_notifications",
1275+
"wildcard_mentions_notify",
1276+
"push_notifications",
1277+
"audible_notifications",
1278+
"pin_to_top",
1279+
"email_address",
1280+
"is_muted",
1281+
"role",
1282+
"color,",
1283+
]
1284+
1285+
def stream_property(
1286+
self, stream_id: int, property: STREAM_KEYS
1287+
) -> Optional[Union[int, str, bool, List[int]]]:
1288+
return self._get_stream_from_id(stream_id)[property]
1289+
1290+
def subscription_property(
1291+
self, stream_id: int, property: SUBSCRIPTION_KEYS
1292+
) -> Optional[Union[int, str, bool, List[int]]]:
1293+
subscription = self._get_stream_from_id(stream_id)
1294+
if property in subscription:
1295+
return subscription[property]
1296+
else:
1297+
# stream_id is not a subscribed stream.
1298+
raise RuntimeError(
1299+
f"Stream with id={stream_id} does not have '{property}' property!"
1300+
)
1301+
12551302
def _subscribe_to_streams(self, subscriptions: List[Subscription]) -> None:
12561303
def make_reduced_stream_data(stream: Subscription) -> StreamData:
12571304
# stream_id has been changed to id.

0 commit comments

Comments
 (0)