From 9051d4f64e4eb70f5adb0bf9c030652e7f704a30 Mon Sep 17 00:00:00 2001 From: Niloth P <20315308+Niloth-p@users.noreply.github.com> Date: Tue, 23 Jul 2024 00:25:44 +0530 Subject: [PATCH] refactor: views/helper/core/messages: Group message info in a TypedDict. Group the following fields that are shown in a message info popup, into a single piece of data. - topic_links - message_links - time_mentions Tests updated. Fixture added. --- tests/ui_tools/test_popups.py | 43 +++++++++++++++++++++--------- zulipterminal/core.py | 15 +++-------- zulipterminal/helper.py | 6 +++++ zulipterminal/ui_tools/messages.py | 9 +++++-- zulipterminal/ui_tools/views.py | 11 ++++---- 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/tests/ui_tools/test_popups.py b/tests/ui_tools/test_popups.py index 4d89887869..08f7dbe9d5 100644 --- a/tests/ui_tools/test_popups.py +++ b/tests/ui_tools/test_popups.py @@ -9,7 +9,11 @@ from zulipterminal.api_types import Message from zulipterminal.config.keys import is_command_key, keys_for_command from zulipterminal.config.ui_mappings import EDIT_MODE_CAPTIONS -from zulipterminal.helper import CustomProfileData, TidiedUserInfo +from zulipterminal.helper import ( + CustomProfileData, + MessageInfoPopupContent, + TidiedUserInfo, +) from zulipterminal.ui_tools.messages import MessageBox from zulipterminal.ui_tools.views import ( AboutView, @@ -922,10 +926,22 @@ def test_keypress_exit_popup( assert self.controller.exit_popup.called +@pytest.fixture +def message_info_content() -> MessageInfoPopupContent: + return MessageInfoPopupContent( + topic_links=OrderedDict(), + message_links=OrderedDict(), + time_mentions=list(), + ) + + class TestMsgInfoView: @pytest.fixture(autouse=True) def mock_external_classes( - self, mocker: MockerFixture, message_fixture: Message + self, + mocker: MockerFixture, + message_fixture: Message, + message_info_content: MessageInfoPopupContent, ) -> None: self.controller = mocker.Mock() mocker.patch.object( @@ -943,13 +959,12 @@ def mock_external_classes( "Tue Mar 13 10:55:37", ] self.message = message_fixture + self.message_info_content = message_info_content self.msg_info_view = MsgInfoView( self.controller, self.message, "Message Information", - OrderedDict(), - OrderedDict(), - list(), + self.message_info_content, ) def test_init(self) -> None: @@ -961,16 +976,22 @@ def test_init(self) -> None: def test_pop_up_info_order(self) -> None: topic_links = OrderedDict([("https://bar.com", ("topic", 1, True))]) message_links = OrderedDict([("image.jpg", ("image", 1, True))]) + message_info_content = MessageInfoPopupContent( + topic_links=topic_links, + message_links=message_links, + time_mentions=list(), + ) msg_info_view = MsgInfoView( self.controller, self.message, title="Message Information", - topic_links=topic_links, - message_links=message_links, - time_mentions=list(), + message_info_content=message_info_content, ) msg_links = msg_info_view.button_widgets - assert msg_links == [message_links, topic_links] + assert msg_links == [ + message_info_content["message_links"], + message_info_content["topic_links"], + ] def test_keypress_any_key( self, widget_size: Callable[[Widget], urwid_Size] @@ -1139,9 +1160,7 @@ def test_height_reactions( self.controller, varied_message, "Message Information", - OrderedDict(), - OrderedDict(), - list(), + self.message_info_content, ) # 12 = 7 labels + 2 blank lines + 1 'Reactions' (category) # + 4 reactions (excluding 'Message Links'). diff --git a/zulipterminal/core.py b/zulipterminal/core.py index 64a80b7833..ca1dbc46af 100644 --- a/zulipterminal/core.py +++ b/zulipterminal/core.py @@ -25,7 +25,7 @@ MAX_LINEAR_SCALING_WIDTH, MIN_SUPPORTED_POPUP_WIDTH, ) -from zulipterminal.helper import asynch, suppress_output +from zulipterminal.helper import MessageInfoPopupContent, asynch, suppress_output from zulipterminal.model import Model from zulipterminal.platform_code import PLATFORM from zulipterminal.ui import Screen, View @@ -270,19 +270,10 @@ def show_topic_edit_mode(self, button: Any) -> None: self.show_pop_up(EditModeView(self, button), "area:msg") def show_msg_info( - self, - msg: Message, - topic_links: Dict[str, Tuple[str, int, bool]], - message_links: Dict[str, Tuple[str, int, bool]], - time_mentions: List[Tuple[str, str]], + self, msg: Message, message_info_content: MessageInfoPopupContent ) -> None: msg_info_view = MsgInfoView( - self, - msg, - f"Message Information {SCROLL_PROMPT}", - topic_links, - message_links, - time_mentions, + self, msg, f"Message Information {SCROLL_PROMPT}", message_info_content ) self.show_pop_up(msg_info_view, "area:msg") diff --git a/zulipterminal/helper.py b/zulipterminal/helper.py index a71d055b74..361a9e59c1 100644 --- a/zulipterminal/helper.py +++ b/zulipterminal/helper.py @@ -57,6 +57,12 @@ class StreamData(TypedDict): description: str +class MessageInfoPopupContent(TypedDict): + topic_links: Dict[str, Tuple[str, int, bool]] + message_links: Dict[str, Tuple[str, int, bool]] + time_mentions: List[Tuple[str, str]] + + class EmojiData(TypedDict): code: str aliases: List[str] diff --git a/zulipterminal/ui_tools/messages.py b/zulipterminal/ui_tools/messages.py index b8552fdc92..e182e3454d 100644 --- a/zulipterminal/ui_tools/messages.py +++ b/zulipterminal/ui_tools/messages.py @@ -29,7 +29,7 @@ TIME_MENTION_MARKER, ) from zulipterminal.config.ui_mappings import STATE_ICON, STREAM_ACCESS_TYPE -from zulipterminal.helper import get_unused_fence +from zulipterminal.helper import MessageInfoPopupContent, get_unused_fence from zulipterminal.server_url import near_message_url from zulipterminal.ui_tools.tables import render_table from zulipterminal.urwid_types import urwid_MarkupTuple, urwid_Size @@ -1121,7 +1121,12 @@ 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, self.topic_links, self.message_links, self.time_mentions + self.message, + MessageInfoPopupContent( + topic_links=self.topic_links, + message_links=self.message_links, + time_mentions=self.time_mentions, + ), ) elif is_command_key("ADD_REACTION", key): self.model.controller.show_emoji_picker(self.message) diff --git a/zulipterminal/ui_tools/views.py b/zulipterminal/ui_tools/views.py index 6602a2285b..1b2969e0cc 100644 --- a/zulipterminal/ui_tools/views.py +++ b/zulipterminal/ui_tools/views.py @@ -36,6 +36,7 @@ ) from zulipterminal.config.ui_sizes import LEFT_WIDTH from zulipterminal.helper import ( + MessageInfoPopupContent, TidiedUserInfo, asynch, match_emoji, @@ -1578,14 +1579,12 @@ def __init__( controller: Any, msg: Message, title: str, - topic_links: Dict[str, Tuple[str, int, bool]], - message_links: Dict[str, Tuple[str, int, bool]], - time_mentions: List[Tuple[str, str]], + message_info_content: MessageInfoPopupContent, ) -> None: self.msg = msg - self.topic_links = topic_links - self.message_links = message_links - self.time_mentions = time_mentions + self.topic_links = message_info_content["topic_links"] + self.message_links = message_info_content["message_links"] + self.time_mentions = message_info_content["time_mentions"] self.server_url = controller.model.server_url date_and_time = controller.model.formatted_local_time( self.msg["timestamp"], show_seconds=True, show_year=True