From b8244ce0c29905e37b4285e84eb8bff385247a12 Mon Sep 17 00:00:00 2001 From: rsashank Date: Thu, 14 Dec 2023 11:48:24 +0530 Subject: [PATCH] model/views: refactor: Get stream email address from new endpoint. --- tests/ui_tools/test_popups.py | 2 ++ zulipterminal/model.py | 28 +++++++++++++++++++++++++++- zulipterminal/ui_tools/views.py | 13 ++++++------- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/tests/ui_tools/test_popups.py b/tests/ui_tools/test_popups.py index 2c5ce92d84..73c18f215e 100644 --- a/tests/ui_tools/test_popups.py +++ b/tests/ui_tools/test_popups.py @@ -1398,9 +1398,11 @@ def test_stream_info_content__email_copy_text( general_stream: Dict[str, Any], stream_email_present: bool, expected_copy_text: str, + mocker: MockerFixture, ) -> None: if not stream_email_present: del general_stream["email_address"] + self.controller.model.stream_email_address.return_value = "" model = self.controller.model stream_id = general_stream["stream_id"] diff --git a/zulipterminal/model.py b/zulipterminal/model.py index 9c6fce61b5..1f51fcba4a 100644 --- a/zulipterminal/model.py +++ b/zulipterminal/model.py @@ -53,7 +53,7 @@ UpdateMessageContentEvent, UpdateMessagesLocationEvent, ) -from zulipterminal.config.keys import primary_key_for_command +from zulipterminal.config.keys import primary_key_for_command, keys_for_command from zulipterminal.config.symbols import STREAM_TOPIC_SEPARATOR from zulipterminal.config.ui_mappings import EDIT_TOPIC_POLICY, ROLE_BY_ID, STATE_ICON from zulipterminal.helper import ( @@ -868,6 +868,32 @@ def topics_in_stream(self, stream_id: int) -> List[str]: return list(self.index["topics"][stream_id]) + def _fetch_stream_email_address(self, stream_id: int) -> str: + """ + Fetch stream's email address with specified stream_id and + returns stream_email_address as a String. + Endpoint added in Zulip 7.5 (ZFL 226) + """ + url = f"/streams/{stream_id}/email_address" + + response = self.client.call_endpoint(url, method="GET") + + if response.get("result") == "success": + email_address = response.get("email", "") + return str(email_address) + return "" + + def stream_copy_text(self, stream_id: int) -> str: + stream_email = self.stream_dict.get("email_address", None) + if stream_email is None: + stream_email = self._fetch_stream_email_address(stream_id) + if stream_email == "" or stream_email is None: + stream_copy_text = "< Stream email is unavailable >" + else: + email_keys = ", ".join(map(repr, keys_for_command("COPY_STREAM_EMAIL"))) + stream_copy_text = f"Press {email_keys} to copy Stream email address" + return stream_email, stream_copy_text + @staticmethod def exception_safe_result(future: "Future[str]") -> str: try: diff --git a/zulipterminal/ui_tools/views.py b/zulipterminal/ui_tools/views.py index 1d63b5eb92..333b228aa3 100644 --- a/zulipterminal/ui_tools/views.py +++ b/zulipterminal/ui_tools/views.py @@ -1378,14 +1378,13 @@ def __init__(self, controller: Any, stream_id: int) -> None: ) member_keys = ", ".join(map(repr, keys_for_command("STREAM_MEMBERS"))) - # FIXME: This field was removed from the subscription data in Zulip 7.5 / ZFL226 - # We should use the new /streams/{stream_id}/email_address endpoint instead + # This field was removed from the subscription data in Zulip 7.5 / ZFL226 + # Using the new /streams/{stream_id}/email_address endpoint + # if field isn't present in subscription data self._stream_email = stream.get("email_address", None) - if self._stream_email is None: - stream_copy_text = "< Stream email is unavailable >" - else: - email_keys = ", ".join(map(repr, keys_for_command("COPY_STREAM_EMAIL"))) - stream_copy_text = f"Press {email_keys} to copy Stream email address" + self._stream_email, stream_copy_text = controller.model.stream_copy_text( + stream_id + ) weekly_traffic = stream["stream_weekly_traffic"] weekly_msg_count = (