Skip to content

Commit

Permalink
boxes/core: Add support for viewing a message in the web browser.
Browse files Browse the repository at this point in the history
This adds view_in_browser to add support for viewing any message in the
web browser. The message is opened in the exact/topic narrow for
streams and in the pm-with narrow for PMs and huddles.

Test added.

Co-authored-by: Preet Mishra <[email protected]>
  • Loading branch information
punchagan and preetmishra committed Jun 19, 2020
1 parent 3c60e60 commit 3c9cfb1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
16 changes: 16 additions & 0 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from platform import platform
from typing import Any

Expand Down Expand Up @@ -229,6 +230,21 @@ 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

@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 in order to run test in Travis.
os.environ['DISPLAY'] = ':0'
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.message_url)

def test_main(self, mocker, controller):
ret_mock = mocker.Mock()
mock_loop = mocker.patch('urwid.MainLoop', return_value=ret_mock)
Expand Down
15 changes: 14 additions & 1 deletion zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import signal
import sys
import time
import webbrowser
from functools import partial
from platform import platform
from typing import Any, List, Optional, Tuple
Expand All @@ -10,7 +11,7 @@
import zulip

from zulipterminal.config.themes import ThemeSpec
from zulipterminal.helper import Message, asynch
from zulipterminal.helper import MACOS, WSL, Message, asynch, near_message_url
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 @@ -270,6 +271,18 @@ def show_all_mentions(self, button: Any) -> None:
anchor=None,
mentioned=True)

def view_in_browser(self, message: Message) -> None:
# Don't try to open the browser if the client is running without a GUI.
if (not(MACOS or WSL or os.environ.get('DISPLAY'))
and os.environ.get('TERM')):
return

# Truncate extra '/' from the server url.
self.message_url = near_message_url(self.model.server_url[:-1],
message)

webbrowser.open(self.message_url)

def _finalize_show(self, w_list: List[Any]) -> None:
focus_position = self.model.get_focus_in_current_narrow()
if focus_position == set():
Expand Down
2 changes: 2 additions & 0 deletions zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
self.model.controller.view.middle_column.set_focus('footer')
elif is_command_key('MSG_INFO', key):
self.model.controller.show_msg_info(self.message)
elif is_command_key('VIEW_IN_BROWSER', key):
self.model.controller.view_in_browser(self.message)
return key


Expand Down

0 comments on commit 3c9cfb1

Please sign in to comment.