Skip to content

Commit 75a3e75

Browse files
committed
streams: Add UI warnings for failing events.
Fixes #816.
1 parent ccfbbbe commit 75a3e75

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
@@ -2054,6 +2054,30 @@ def test_is_user_subscribed_to_stream(self, model, stream_dict, stream_id,
20542054

20552055
assert return_value == expected_response
20562056

2057+
@pytest.mark.parametrize('stream_id, expected_response,', [
2058+
(100, False),
2059+
(101, False),
2060+
(3, True),
2061+
],
2062+
ids=[
2063+
'subscribed_pinned_stream',
2064+
'subscribed_unpinned_stream',
2065+
'unsubscribed_stream',
2066+
]
2067+
)
2068+
def test_is_user_in_unsubscribed_stream(self, model, stream_dict,
2069+
stream_id, expected_response,
2070+
initial_pinned_streams,
2071+
initial_unpinned_streams):
2072+
2073+
model.pinned_streams = deepcopy(initial_pinned_streams)
2074+
model.unpinned_streams = deepcopy(initial_unpinned_streams)
2075+
return_value = model.is_user_in_unsubscribed_stream(stream_id)
2076+
2077+
assert model.pinned_streams == initial_pinned_streams
2078+
assert model.unpinned_streams == initial_unpinned_streams
2079+
assert return_value == expected_response
2080+
20572081
@pytest.mark.parametrize('response', [{
20582082
'result': 'success',
20592083
'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):
@@ -1268,6 +1270,7 @@ class TestMessageBox:
12681270
def mock_external_classes(self, mocker, initial_index):
12691271
self.model = mocker.MagicMock()
12701272
self.model.index = initial_index
1273+
self.model.controller.check_for_invalid_operation.return_value = False
12711274

12721275
@pytest.mark.parametrize('message_type, set_fields', [
12731276
('stream',
@@ -1877,7 +1880,9 @@ def test_main_view_generates_EDITED_label(self, mocker,
18771880
])
18781881
def test_keypress_STREAM_MESSAGE(self, mocker, msg_box, widget_size,
18791882
narrow, expect_to_prefill, key):
1880-
write_box = msg_box.model.controller.view.write_box
1883+
controller = msg_box.model.controller
1884+
controller.check_for_invalid_operation.return_value = False
1885+
write_box = controller.view.write_box
18811886
msg_box.model.narrow = narrow
18821887
size = widget_size(msg_box)
18831888

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
@@ -817,6 +817,10 @@ def toggle_stream_pinned_status(self, stream_id: int) -> bool:
817817
def is_user_subscribed_to_stream(self, stream_id: int) -> bool:
818818
return stream_id in self.stream_dict
819819

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

zulipterminal/ui_tools/boxes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,10 +1388,13 @@ 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 len(self.model.narrow) == 2:
13921395
self.model.controller.view.write_box.stream_box_view(
13931396
caption=self.message['display_recipient'],
1394-
stream_id=self.stream_id,
1397+
stream_id=stream_id,
13951398
)
13961399
else:
13971400
self.model.controller.view.write_box.stream_box_view(0)
@@ -1429,6 +1432,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
14291432
recipient_user_ids=[self.message['sender_id']],
14301433
)
14311434
elif is_command_key('MENTION_REPLY', key):
1435+
stream_id = self.stream_id
1436+
if self.model.controller.check_for_invalid_operation(stream_id):
1437+
return key
1438+
14321439
self.keypress(size, 'enter')
14331440
mention = f"@**{self.message['sender_full_name']}** "
14341441
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
@@ -556,10 +556,12 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
556556
return key
557557

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

0 commit comments

Comments
 (0)