Skip to content

Commit 6cac57d

Browse files
committed
messages/views: Disable keypresses from dummy messages.
Updated tests.
1 parent 936cf2f commit 6cac57d

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

tests/ui/test_ui_tools.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ def test_mouse_event(self, mocker, msg_view, mouse_scroll_event, widget_size):
276276
@pytest.mark.parametrize("key", keys_for_command("GO_DOWN"))
277277
def test_keypress_GO_DOWN(self, mocker, msg_view, key, widget_size):
278278
size = widget_size(msg_view)
279+
msg_view.model.controller.is_in_empty_narrow = False
279280
msg_view.new_loading = False
280281
mocker.patch(MESSAGEVIEW + ".focus_position", return_value=0)
281282
mocker.patch(MESSAGEVIEW + ".set_focus_valign")
@@ -291,6 +292,7 @@ def test_keypress_GO_DOWN_exception(
291292
self, mocker, msg_view, key, widget_size, view_is_focused
292293
):
293294
size = widget_size(msg_view)
295+
msg_view.model.controller.is_in_empty_narrow = False
294296
msg_view.new_loading = False
295297
mocker.patch(MESSAGEVIEW + ".focus_position", return_value=0)
296298
mocker.patch(MESSAGEVIEW + ".set_focus_valign")
@@ -315,6 +317,7 @@ def test_keypress_GO_DOWN_exception(
315317
@pytest.mark.parametrize("key", keys_for_command("GO_UP"))
316318
def test_keypress_GO_UP(self, mocker, msg_view, key, widget_size):
317319
size = widget_size(msg_view)
320+
msg_view.model.controller.is_in_empty_narrow = False
318321
mocker.patch(MESSAGEVIEW + ".focus_position", return_value=0)
319322
mocker.patch(MESSAGEVIEW + ".set_focus_valign")
320323
msg_view.old_loading = False
@@ -330,6 +333,7 @@ def test_keypress_GO_UP_exception(
330333
self, mocker, msg_view, key, widget_size, view_is_focused
331334
):
332335
size = widget_size(msg_view)
336+
msg_view.model.controller.is_in_empty_narrow = False
333337
msg_view.old_loading = False
334338
mocker.patch(MESSAGEVIEW + ".focus_position", return_value=0)
335339
mocker.patch(MESSAGEVIEW + ".set_focus_valign")

tests/ui_tools/test_messages.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ def test_main_view_generates_stream_header(
838838
"color": "#bd6",
839839
},
840840
}
841+
self.model.controller.is_in_empty_narrow = False
841842
last_message = dict(message, **to_vary_in_last_message)
842843
msg_box = MessageBox(message, self.model, last_message)
843844
view_components = msg_box.main_view()
@@ -890,6 +891,7 @@ def test_main_view_generates_stream_header(
890891
def test_main_view_generates_PM_header(
891892
self, mocker, message, to_vary_in_last_message
892893
):
894+
self.model.controller.is_in_empty_narrow = False
893895
last_message = dict(message, **to_vary_in_last_message)
894896
msg_box = MessageBox(message, self.model, last_message)
895897
view_components = msg_box.main_view()
@@ -1032,9 +1034,11 @@ def test_msg_generates_search_and_header_bar(
10321034
},
10331035
}
10341036
self.model.narrow = msg_narrow
1037+
self.model.controller.is_in_empty_narrow = False
10351038
messages = messages_successful_response["messages"]
10361039
current_message = messages[msg_type]
10371040
msg_box = MessageBox(current_message, self.model, messages[0])
1041+
10381042
search_bar = msg_box.top_search_bar()
10391043
header_bar = msg_box.recipient_header()
10401044

@@ -1405,6 +1409,7 @@ def test_keypress_EDIT_MESSAGE(
14051409
report_error = msg_box.model.controller.report_error
14061410
report_warning = msg_box.model.controller.report_warning
14071411
mocker.patch(MODULE + ".time", return_value=100)
1412+
msg_box.model.controller.is_in_empty_narrow = False
14081413

14091414
msg_box.keypress(size, key)
14101415

@@ -1820,6 +1825,7 @@ def test_reactions_view(
18201825
varied_message = dict(message_fixture, **to_vary_in_each_message)
18211826
msg_box = MessageBox(varied_message, self.model, None)
18221827
reactions = to_vary_in_each_message["reactions"]
1828+
msg_box.model.controller.is_in_empty_narrow = False
18231829

18241830
reactions_view = msg_box.reactions_view(reactions)
18251831

@@ -1976,6 +1982,7 @@ def test_mouse_event_left_click(
19761982
mocker.patch.object(msg_box, "keypress")
19771983
msg_box.model = mocker.Mock()
19781984
msg_box.model.controller.is_in_editor_mode.return_value = compose_box_is_open
1985+
msg_box.model.controller.is_in_empty_narrow = False
19791986

19801987
msg_box.mouse_event(size, "mouse press", 1, col, row, focus)
19811988

zulipterminal/ui_tools/messages.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,8 @@ def mouse_event(
904904
return super().mouse_event(size, event, button, col, row, focus)
905905

906906
def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
907-
if is_command_key("REPLY_MESSAGE", key):
907+
is_in_empty_narrow = self.model.controller.is_in_empty_narrow
908+
if is_command_key("REPLY_MESSAGE", key) and not is_in_empty_narrow:
908909
if self.message["type"] == "private":
909910
self.model.controller.view.write_box.private_box_view(
910911
recipient_user_ids=self.recipient_ids,
@@ -923,7 +924,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
923924
)
924925
else:
925926
self.model.controller.view.write_box.stream_box_view(0)
926-
elif is_command_key("STREAM_NARROW", key):
927+
elif is_command_key("STREAM_NARROW", key) and not is_in_empty_narrow:
927928
if self.message["type"] == "private":
928929
self.model.controller.narrow_to_user(
929930
recipient_emails=self.recipient_emails,
@@ -934,7 +935,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
934935
stream_name=self.stream_name,
935936
contextual_message_id=self.message["id"],
936937
)
937-
elif is_command_key("TOGGLE_NARROW", key):
938+
elif is_command_key("TOGGLE_NARROW", key) and not is_in_empty_narrow:
938939
self.model.unset_search_narrow()
939940
if self.message["type"] == "private":
940941
if len(self.model.narrow) == 1 and self.model.narrow[0][0] == "pm-with":
@@ -958,7 +959,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
958959
topic_name=self.topic_name,
959960
contextual_message_id=self.message["id"],
960961
)
961-
elif is_command_key("TOPIC_NARROW", key):
962+
elif is_command_key("TOPIC_NARROW", key) and not is_in_empty_narrow:
962963
if self.message["type"] == "private":
963964
self.model.controller.narrow_to_user(
964965
recipient_emails=self.recipient_emails,
@@ -974,20 +975,20 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
974975
self.model.controller.narrow_to_all_messages(
975976
contextual_message_id=self.message["id"]
976977
)
977-
elif is_command_key("REPLY_AUTHOR", key):
978+
elif is_command_key("REPLY_AUTHOR", key) and not is_in_empty_narrow:
978979
# All subscribers from recipient_ids are not needed here.
979980
self.model.controller.view.write_box.private_box_view(
980981
recipient_user_ids=[self.message["sender_id"]],
981982
)
982-
elif is_command_key("MENTION_REPLY", key):
983+
elif is_command_key("MENTION_REPLY", key) and not is_in_empty_narrow:
983984
self.keypress(size, primary_key_for_command("REPLY_MESSAGE"))
984985
mention = f"@**{self.message['sender_full_name']}** "
985986
self.model.controller.view.write_box.msg_write_box.set_edit_text(mention)
986987
self.model.controller.view.write_box.msg_write_box.set_edit_pos(
987988
len(mention)
988989
)
989990
self.model.controller.view.middle_column.set_focus("footer")
990-
elif is_command_key("QUOTE_REPLY", key):
991+
elif is_command_key("QUOTE_REPLY", key) and not is_in_empty_narrow:
991992
self.keypress(size, primary_key_for_command("REPLY_MESSAGE"))
992993

993994
# To correctly quote a message that contains quote/code-blocks,
@@ -1015,7 +1016,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
10151016
self.model.controller.view.write_box.msg_write_box.set_edit_text(quote)
10161017
self.model.controller.view.write_box.msg_write_box.set_edit_pos(len(quote))
10171018
self.model.controller.view.middle_column.set_focus("footer")
1018-
elif is_command_key("EDIT_MESSAGE", key):
1019+
elif is_command_key("EDIT_MESSAGE", key) and not is_in_empty_narrow:
10191020
# User can't edit messages of others that already have a subject
10201021
# For private messages, subject = "" (empty string)
10211022
# This also handles the realm_message_content_edit_limit_seconds == 0 case
@@ -1119,12 +1120,12 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
11191120
write_box.header_write_box.focus_col = write_box.FOCUS_HEADER_BOX_TOPIC
11201121

11211122
self.model.controller.view.middle_column.set_focus("footer")
1122-
elif is_command_key("MSG_INFO", key):
1123+
elif is_command_key("MSG_INFO", key) and not is_in_empty_narrow:
11231124
self.model.controller.show_msg_info(
11241125
self.message, self.topic_links, self.message_links, self.time_mentions
11251126
)
1126-
elif is_command_key("ADD_REACTION", key):
1127+
elif is_command_key("ADD_REACTION", key) and not is_in_empty_narrow:
11271128
self.model.controller.show_emoji_picker(self.message)
1128-
elif is_command_key("MSG_SENDER_INFO", key):
1129+
elif is_command_key("MSG_SENDER_INFO", key) and not is_in_empty_narrow:
11291130
self.model.controller.show_msg_sender_info(self.message["sender_id"])
11301131
return key

zulipterminal/ui_tools/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def mouse_event(
193193
return super().mouse_event(size, event, button, col, row, focus)
194194

195195
def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
196+
if self.model.controller.is_in_empty_narrow:
197+
return super().keypress(size, key)
196198
if is_command_key("GO_DOWN", key) and not self.new_loading:
197199
try:
198200
position = self.log.next_position(self.focus_position)

0 commit comments

Comments
 (0)