Skip to content

Commit

Permalink
core/views: Use near_message_url() to open messages in the topic narrow.
Browse files Browse the repository at this point in the history
This improves view_in_browser() to open messages in the exact/topic
narrow for streams and in the pm-with narrow for PMs and huddles.

Tests amended.
  • Loading branch information
preetmishra committed Apr 4, 2021
1 parent 7c0c9fc commit 584c94e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
21 changes: 12 additions & 9 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,20 @@ def test_narrow_to_all_mentions(
msg_ids = {widget.original_widget.message['id'] for widget in widgets}
assert msg_ids == id_list

def test_view_in_browser(self, mocker, controller):
@pytest.mark.parametrize('server_url', [
'https://chat.zulip.org/',
'https://foo.zulipchat.com/',
])
def test_view_in_browser(self, mocker, controller, message_fixture,
server_url):
# Set DISPLAY environ to be able to run test in Travis
os.environ['DISPLAY'] = ':0'
mock_open = mocker.patch('webbrowser.open', mocker.Mock())
message_id = 123456
controller.model.server_url = 'https://foo.zulipchat.com/'
controller.view_in_browser(message_id)
assert mock_open.call_count == 1
url = mock_open.call_args[0][0]
assert url.startswith(controller.model.server_url)
assert url.endswith('/{}'.format(message_id))
controller.model.server_url = server_url
mocked_open = mocker.patch(CORE + '.webbrowser.open')

controller.view_in_browser(message_fixture)

mocked_open.assert_called_once_with(controller.url)

def test_main(self, mocker, controller):
controller.view.palette = {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui_tools/test_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def test_keypress_view_in_browser(self, widget_size, message_fixture, key):
self.msg_info_view.keypress(size, key)

(self.controller.view_in_browser.
assert_called_once_with(message_fixture['id']))
assert_called_once_with(message_fixture))

def test_height_noreactions(self):
expected_height = 4
Expand Down
8 changes: 5 additions & 3 deletions zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from zulipterminal.config.themes import ThemeSpec
from zulipterminal.helper import MACOS, WSL, asynch, suppress_output
from zulipterminal.model import Model
from zulipterminal.server_url import near_message_url
from zulipterminal.ui import Screen, View
from zulipterminal.ui_tools.utils import create_msg_box_list
from zulipterminal.ui_tools.views import (
Expand Down Expand Up @@ -365,15 +366,16 @@ def narrow_to_all_mentions(self) -> None:
# (nothing currently requires narrowing around a message id)
self._narrow_to(anchor=None, mentioned=True)

def view_in_browser(self, message_id: int) -> None:
url = '{}#narrow/near/{}'.format(self.model.server_url, message_id)
def view_in_browser(self, message: Message) -> None:
# Truncate extra '/' from the server url.
self.url = near_message_url(self.model.server_url[:-1], message)
if (not MACOS and not WSL and not os.environ.get('DISPLAY')
and os.environ.get('TERM')):
# Don't try to open web browser if running without a GUI
return
with suppress_output():
# Suppress anything on stdout or stderr when opening the browser
webbrowser.open(url)
webbrowser.open(self.url)

def deregister_client(self) -> None:
queue_id = self.model.queue_id
Expand Down
2 changes: 1 addition & 1 deletion zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ def keypress(self, size: urwid_Size, key: str) -> str:
time_mentions=self.time_mentions,
)
elif is_command_key('VIEW_IN_BROWSER', key):
self.controller.view_in_browser(self.msg['id'])
self.controller.view_in_browser(self.msg)
return super().keypress(size, key)


Expand Down

0 comments on commit 584c94e

Please sign in to comment.