Skip to content

Commit

Permalink
buttons: Add support for opening external links in browser.
Browse files Browse the repository at this point in the history
This commit uses the open_in_browser() function from previous
commits and allows opening external links in the browser on pressing
ENTER on the MessageLinkButton.
After this commit ZT, should be able to narrow to a message if the
link is an internal link, and open in browser if the link is an
external link. This commit does not handles internal media links in
any way currently.

Tests amended.
  • Loading branch information
zee-bit committed Jun 10, 2021
1 parent 218a6ae commit 150e3f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 8 additions & 5 deletions tests/ui_tools/test_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,19 +449,21 @@ def test_update_widget(
assert isinstance(mocked_button._w, AttrMap)

@pytest.mark.parametrize(
"link, handle_narrow_link_called",
"link, handle_narrow_link_called, open_in_browser_called",
[
(SERVER_URL + "/#narrow/stream/1-Stream-1", True),
(SERVER_URL + "/user_uploads/some/path/image.png", False),
("https://foo.com", False),
(SERVER_URL + "/#narrow/stream/1-Stream-1", True, False),
(SERVER_URL + "/user_uploads/some/path/image.png", False, False),
("https://foo.com", False, True),
],
ids=[
"internal_narrow_link",
"internal_media_link",
"external_link",
],
)
def test_handle_link(self, mocker, link, handle_narrow_link_called):
def test_handle_link(
self, mocker, link, handle_narrow_link_called, open_in_browser_called
):
self.controller.model.server_url = SERVER_URL
self.handle_narrow_link = mocker.patch(
BUTTONS + ".MessageLinkButton.handle_narrow_link"
Expand All @@ -471,6 +473,7 @@ def test_handle_link(self, mocker, link, handle_narrow_link_called):
mocked_button.handle_link()

assert self.handle_narrow_link.called == handle_narrow_link_called
assert self.controller.open_in_browser.called == open_in_browser_called

@pytest.mark.parametrize(
"stream_data, expected_response",
Expand Down
2 changes: 2 additions & 0 deletions zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ def handle_link(self, *_: Any) -> None:
server_url = self.model.server_url
if self.link.startswith(urljoin(server_url, "/#narrow/")):
self.handle_narrow_link()
elif not self.link.startswith(urljoin(server_url, "/user_uploads/")):
self.controller.open_in_browser(self.link)

@staticmethod
def _decode_stream_data(encoded_stream_data: str) -> DecodedStream:
Expand Down

0 comments on commit 150e3f0

Please sign in to comment.