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 Jul 30, 2020
1 parent e9df5b7 commit 5316996
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
21 changes: 12 additions & 9 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,20 @@ def test_show_all_mentions(self, mocker, controller, index_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):
ret_mock = mocker.Mock()
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/test_ui_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ def test_keypress_view_in_browser(self, message_fixture, key):
size = (200, 20)
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
11 changes: 7 additions & 4 deletions zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import zulip

from zulipterminal.config.themes import ThemeSpec
from zulipterminal.helper import MACOS, WSL, Message, asynch, suppress_output
from zulipterminal.helper import (
MACOS, WSL, Message, asynch, near_message_url, suppress_output,
)
from zulipterminal.model import Model
from zulipterminal.ui import Screen, View
from zulipterminal.ui_tools.utils import create_msg_box_list
Expand Down Expand Up @@ -287,15 +289,16 @@ def show_all_mentions(self, button: Any) -> None:
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 @@ -1116,5 +1116,5 @@ def __init__(self, controller: Any, msg: Message, title: str,

def keypress(self, size: urwid_Size, key: str) -> str:
if 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)

0 comments on commit 5316996

Please sign in to comment.