Skip to content

Commit a4adaff

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 c0d7744 commit a4adaff

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 (
@@ -1253,6 +1253,53 @@ def _get_all_stream_ids(self) -> List[int]:
12531253
id_list.extend(stream_id for stream_id in self._never_subscribed_streams)
12541254
return id_list
12551255

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

0 commit comments

Comments
 (0)