Skip to content

Commit

Permalink
refactor: views/helper/core/messages: Group message info in a TypedDict.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Niloth-p committed Jul 22, 2024
1 parent 9e5036e commit 9051d4f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 32 deletions.
43 changes: 31 additions & 12 deletions tests/ui_tools/test_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand All @@ -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]
Expand Down Expand Up @@ -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').
Expand Down
15 changes: 3 additions & 12 deletions zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
6 changes: 6 additions & 0 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 7 additions & 2 deletions zulipterminal/ui_tools/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
)
from zulipterminal.config.ui_sizes import LEFT_WIDTH
from zulipterminal.helper import (
MessageInfoPopupContent,
TidiedUserInfo,
asynch,
match_emoji,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9051d4f

Please sign in to comment.