Skip to content

Commit b32c52f

Browse files
committed
streams: Add UI warnings for failing events.
Fixes #816.
1 parent 451729b commit b32c52f

File tree

7 files changed

+55
-3
lines changed

7 files changed

+55
-3
lines changed

tests/model/test_model.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,30 @@ def test_is_user_subscribed_to_stream(self, model, stream_dict, stream_id,
20432043

20442044
assert return_value == expected_response
20452045

2046+
@pytest.mark.parametrize('stream_id, expected_response,', [
2047+
(100, False),
2048+
(101, False),
2049+
(3, True),
2050+
],
2051+
ids=[
2052+
'subscribed_pinned_stream',
2053+
'subscribed_unpinned_stream',
2054+
'unsubscribed_stream',
2055+
]
2056+
)
2057+
def test_is_user_in_unsubscribed_stream(self, model, stream_dict,
2058+
stream_id, expected_response,
2059+
initial_pinned_streams,
2060+
initial_unpinned_streams):
2061+
2062+
model.pinned_streams = deepcopy(initial_pinned_streams)
2063+
model.unpinned_streams = deepcopy(initial_unpinned_streams)
2064+
return_value = model.is_user_in_unsubscribed_stream(stream_id)
2065+
2066+
assert model.pinned_streams == initial_pinned_streams
2067+
assert model.unpinned_streams == initial_unpinned_streams
2068+
assert return_value == expected_response
2069+
20462070
@pytest.mark.parametrize('response', [{
20472071
'result': 'success',
20482072
'msg': '',

tests/ui/test_ui_tools.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def mock_external_classes(self, mocker):
8181
self.model = mocker.MagicMock()
8282
self.view = mocker.Mock()
8383
self.urwid = mocker.patch(VIEWS + ".urwid")
84+
self.model.controller.check_for_invalid_operation.return_value = False
8485

8586
@pytest.fixture
8687
def msg_view(self, mocker, msg_box):
@@ -820,6 +821,7 @@ def mock_external_classes(self, mocker):
820821
self.super = mocker.patch(VIEWS + '.urwid.Frame.__init__')
821822
self.super_keypress = mocker.patch(VIEWS + '.urwid.Frame.keypress')
822823
self.model.controller == mocker.Mock()
824+
self.model.controller.check_for_invalid_operation.return_value = False
823825

824826
@pytest.fixture
825827
def mid_col_view(self):
@@ -1266,6 +1268,7 @@ class TestMessageBox:
12661268
def mock_external_classes(self, mocker, initial_index):
12671269
self.model = mocker.MagicMock()
12681270
self.model.index = initial_index
1271+
self.model.controller.check_for_invalid_operation.return_value = False
12691272

12701273
@pytest.mark.parametrize('message_type, set_fields', [
12711274
('stream',
@@ -1875,7 +1878,9 @@ def test_main_view_generates_EDITED_label(self, mocker,
18751878
])
18761879
def test_keypress_STREAM_MESSAGE(self, mocker, msg_box, widget_size,
18771880
narrow, expect_to_prefill, key):
1878-
write_box = msg_box.model.controller.view.write_box
1881+
controller = msg_box.model.controller
1882+
controller.check_for_invalid_operation.return_value = False
1883+
write_box = controller.view.write_box
18791884
msg_box.model.narrow = narrow
18801885
size = widget_size(msg_box)
18811886

tests/ui_tools/test_boxes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class TestWriteBox:
1818
def mock_external_classes(self, mocker, initial_index):
1919
self.view = mocker.Mock()
2020
self.view.model = mocker.Mock()
21+
self.view.model.is_user_in_unsubscribed_stream.return_value = False
2122

2223
@pytest.fixture()
2324
def write_box(self, mocker, users_fixture, user_groups_fixture,

zulipterminal/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ def raise_exception_in_main_thread(self,
102102
self._critical_exception = True
103103
os.write(self._exception_pipe, b'1')
104104

105+
def check_for_invalid_operation(self, stream_id: Optional[int]) -> bool:
106+
assert stream_id is not None
107+
if self.model.is_user_in_unsubscribed_stream(stream_id):
108+
self.model.controller.view.set_footer_text(
109+
" This is not allowed in unsubscribed streams.",
110+
3)
111+
return True
112+
return False
113+
105114
def is_in_editor_mode(self) -> bool:
106115
return self._editor is not None
107116

zulipterminal/model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,10 @@ def toggle_stream_pinned_status(self, stream_id: int) -> bool:
819819
def is_user_subscribed_to_stream(self, stream_id: int) -> bool:
820820
return stream_id in self.stream_dict
821821

822+
def is_user_in_unsubscribed_stream(self, stream_id: int) -> bool:
823+
return not(self.is_stream_in_list(stream_id, self.pinned_streams)
824+
or self.is_stream_in_list(stream_id, self.unpinned_streams))
825+
822826
def _get_stream_by_id(self, streams: List[StreamData], stream_id: int
823827
) -> StreamData:
824828
for stream in streams:

zulipterminal/ui_tools/boxes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,13 +1388,16 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
13881388
stream_id=self.stream_id,
13891389
)
13901390
elif is_command_key('STREAM_MESSAGE', key):
1391+
stream_id = self.stream_id
1392+
if self.model.controller.check_for_invalid_operation(stream_id):
1393+
return None
13911394
if (
13921395
len(self.model.narrow) != 0
13931396
and self.model.narrow[0][0] == "stream"
13941397
):
13951398
self.model.controller.view.write_box.stream_box_view(
13961399
caption=self.message['display_recipient'],
1397-
stream_id=self.stream_id,
1400+
stream_id=stream_id,
13981401
)
13991402
else:
14001403
self.model.controller.view.write_box.stream_box_view(0)
@@ -1459,6 +1462,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
14591462
recipient_user_ids=[self.message['sender_id']],
14601463
)
14611464
elif is_command_key('MENTION_REPLY', key):
1465+
stream_id = self.stream_id
1466+
if self.model.controller.check_for_invalid_operation(stream_id):
1467+
return key
1468+
14621469
self.keypress(size, 'enter')
14631470
mention = f"@**{self.message['sender_full_name']}** "
14641471
self.model.controller.view.write_box.msg_write_box.set_edit_text(

zulipterminal/ui_tools/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,12 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
555555
return key
556556

557557
elif is_command_key('STREAM_MESSAGE', key):
558+
stream_id = self.model.stream_id
559+
if self.model.controller.check_for_invalid_operation(stream_id):
560+
return None
558561
self.body.keypress(size, 'c')
559562
# For new streams with no previous conversation.
560563
if self.footer.focus is None:
561-
stream_id = self.model.stream_id
562564
stream_dict = self.model.stream_dict
563565
self.footer.stream_box_view(
564566
caption=stream_dict[stream_id]['name'])

0 commit comments

Comments
 (0)