Skip to content

Commit 1fa1b2b

Browse files
committed
messages: Replace direct access to Model.narrow with functions.
This commit replaces all instances which directly access Model.narrow with usage of functions Model.get_narrow() and Model.get_narrow_length() to restrict access to narrow data structure to only Model class.
1 parent c551abc commit 1fa1b2b

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

zulipterminal/ui_tools/messages.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> None:
111111

112112
def need_recipient_header(self) -> bool:
113113
# Prevent redundant information in recipient bar
114-
if len(self.model.narrow) == 1 and self.model.narrow[0][0] == "pm_with":
114+
narrow = self.model.get_narrow()
115+
narrow_length = self.model.get_narrow_length()
116+
if narrow_length == 1 and narrow[0][0] == "pm_with":
115117
return False
116-
if len(self.model.narrow) == 2 and self.model.narrow[1][0] == "topic":
118+
if narrow_length == 2 and narrow[1][0] == "topic":
117119
return False
118120

119121
last_msg = self.last_message
@@ -193,7 +195,7 @@ def top_header_bar(self, message_view: Any) -> Any:
193195
return message_view.private_header()
194196

195197
def top_search_bar(self) -> Any:
196-
curr_narrow = self.model.narrow
198+
curr_narrow = self.model.get_narrow()
197199
is_search_narrow = self.model.is_search_narrow()
198200
if is_search_narrow:
199201
curr_narrow = [
@@ -890,6 +892,8 @@ def mouse_event(
890892
return super().mouse_event(size, event, button, col, row, focus)
891893

892894
def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
895+
narrow = self.model.get_narrow()
896+
narrow_length = self.model.get_narrow_length()
893897
if is_command_key("REPLY_MESSAGE", key):
894898
if self.message["type"] == "private":
895899
self.model.controller.view.write_box.private_box_view(
@@ -902,7 +906,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
902906
stream_id=self.stream_id,
903907
)
904908
elif is_command_key("STREAM_MESSAGE", key):
905-
if len(self.model.narrow) != 0 and self.model.narrow[0][0] == "stream":
909+
if (
910+
narrow_length != 0
911+
and narrow[0][0] == "stream"
912+
):
906913
self.model.controller.view.write_box.stream_box_view(
907914
caption=self.message["display_recipient"],
908915
stream_id=self.stream_id,
@@ -923,7 +930,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
923930
elif is_command_key("TOGGLE_NARROW", key):
924931
self.model.unset_search_narrow()
925932
if self.message["type"] == "private":
926-
if len(self.model.narrow) == 1 and self.model.narrow[0][0] == "pm_with":
933+
if (
934+
narrow_length == 1
935+
and narrow[0][0] == "pm_with"
936+
):
927937
self.model.controller.narrow_to_all_pm(
928938
contextual_message_id=self.message["id"],
929939
)
@@ -933,7 +943,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
933943
contextual_message_id=self.message["id"],
934944
)
935945
elif self.message["type"] == "stream":
936-
if len(self.model.narrow) > 1: # in a topic
946+
if narrow_length > 1: # in a topic
937947
self.model.controller.narrow_to_stream(
938948
stream_name=self.stream_name,
939949
contextual_message_id=self.message["id"],

0 commit comments

Comments
 (0)