Skip to content

Commit

Permalink
model/views: refactor: Get stream email address from new endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsashank committed Feb 14, 2024
1 parent 6d855de commit b8244ce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 2 additions & 0 deletions tests/ui_tools/test_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
28 changes: 27 additions & 1 deletion zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit b8244ce

Please sign in to comment.