Skip to content

Commit 66f45c5

Browse files
committed
streams: Add UI warnings for failing events.
Fixes #816
1 parent dc747a0 commit 66f45c5

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

tests/model/test_model.py

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

21172117
assert return_value == expected_response
21182118

2119+
@pytest.mark.parametrize('stream_id, expected_response,', [
2120+
(100, False),
2121+
(101, False),
2122+
(3, True),
2123+
],
2124+
ids=[
2125+
'subscribed_pinned_stream',
2126+
'subscribed_unpinned_stream',
2127+
'unsubscribed_stream',
2128+
]
2129+
)
2130+
def test_is_user_in_unsubscribed_stream(self, model, stream_dict,
2131+
stream_id, expected_response,
2132+
initial_pinned_streams,
2133+
initial_unpinned_streams):
2134+
2135+
model.pinned_streams = deepcopy(initial_pinned_streams)
2136+
model.unpinned_streams = deepcopy(initial_unpinned_streams)
2137+
return_value = model.is_user_in_unsubscribed_stream(stream_id)
2138+
2139+
assert model.pinned_streams == initial_pinned_streams
2140+
assert model.unpinned_streams == initial_unpinned_streams
2141+
assert return_value == expected_response
2142+
21192143
@pytest.mark.parametrize('response', [{
21202144
'result': 'success',
21212145
'msg': '',

tests/ui/test_ui_tools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def mock_external_classes(self, mocker):
7474
self.model = mocker.MagicMock()
7575
self.view = mocker.Mock()
7676
self.urwid = mocker.patch(VIEWS + ".urwid")
77+
self.model.is_user_in_unsubscribed_stream.return_value = False
7778

7879
@pytest.fixture
7980
def msg_view(self, mocker, msg_box):
@@ -811,6 +812,7 @@ def mock_external_classes(self, mocker):
811812
self.super = mocker.patch(VIEWS + '.urwid.Frame.__init__')
812813
self.super_keypress = mocker.patch(VIEWS + '.urwid.Frame.keypress')
813814
self.model.controller == mocker.Mock()
815+
self.model.is_user_in_unsubscribed_stream.return_value = False
814816

815817
@pytest.fixture
816818
def mid_col_view(self):
@@ -1259,6 +1261,7 @@ class TestMessageBox:
12591261
def mock_external_classes(self, mocker, initial_index):
12601262
self.model = mocker.MagicMock()
12611263
self.model.index = initial_index
1264+
self.model.is_user_in_unsubscribed_stream.return_value = False
12621265

12631266
@pytest.mark.parametrize('message_type, set_fields', [
12641267
('stream',

tests/ui_tools/test_boxes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TestWriteBox:
1414
def mock_external_classes(self, mocker, initial_index):
1515
self.view = mocker.Mock()
1616
self.view.model = mocker.Mock()
17+
self.view.model.is_user_in_unsubscribed_stream.return_value = False
1718

1819
@pytest.fixture()
1920
def write_box(self, mocker, users_fixture, user_groups_fixture,

zulipterminal/model.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,9 @@ def stream_id_from_name(self, stream_name: str) -> int:
718718
def is_pinned_stream(self, stream_id: int) -> bool:
719719
return stream_id in [stream['id'] for stream in self.pinned_streams]
720720

721+
def is_unpinned_stream(self, stream_id: int) -> bool:
722+
return stream_id in [stream['id'] for stream in self.unpinned_streams]
723+
721724
def toggle_stream_pinned_status(self, stream_id: int) -> bool:
722725
request = [{
723726
'stream_id': stream_id,
@@ -730,6 +733,10 @@ def toggle_stream_pinned_status(self, stream_id: int) -> bool:
730733
def is_user_subscribed_to_stream(self, stream_id: int) -> bool:
731734
return stream_id in self.stream_dict
732735

736+
def is_user_in_unsubscribed_stream(self, stream_id: int) -> bool:
737+
return not(self.is_pinned_stream(stream_id)
738+
or self.is_unpinned_stream(stream_id))
739+
733740
def make_reduced_stream_data(self, stream: Any) -> StreamData:
734741
# stream_id has been changed to id.
735742
return StreamData({'name': stream['name'],

zulipterminal/ui_tools/boxes.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
11331133
stream_id=self.stream_id,
11341134
)
11351135
elif is_command_key('STREAM_MESSAGE', key):
1136+
if(self.model.is_user_in_unsubscribed_stream(self.stream_id)):
1137+
self.model.controller.view.set_footer_text(
1138+
" You can't write messages to unsubscribed streams.",
1139+
3)
1140+
return None
11361141
if self.message['type'] == 'private':
11371142
self.model.controller.view.write_box.private_box_view(
11381143
email=self.recipients_emails,
@@ -1177,13 +1182,18 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
11771182
recipient_user_ids=[self.message['sender_id']],
11781183
)
11791184
elif is_command_key('MENTION_REPLY', key):
1180-
self.keypress(size, 'enter')
1181-
mention = '@**' + self.message['sender_full_name'] + '** '
1182-
self.model.controller.view.write_box.msg_write_box.set_edit_text(
1183-
mention)
1184-
self.model.controller.view.write_box.msg_write_box.set_edit_pos(
1185-
len(mention))
1186-
self.model.controller.view.middle_column.set_focus('footer')
1185+
if(not self.model.is_user_in_unsubscribed_stream(self.stream_id)):
1186+
self.keypress(size, 'enter')
1187+
mention = '@**' + self.message['sender_full_name'] + '** '
1188+
write_box = self.model.controller.view.write_box.msg_write_box
1189+
write_box.set_edit_text(mention)
1190+
write_box.set_edit_pos(len(mention))
1191+
self.model.controller.view.middle_column.set_focus('footer')
1192+
else:
1193+
self.model.controller.view.set_footer_text(
1194+
" You can't reply to messages from unsubscribed streams.",
1195+
3)
1196+
return key
11871197
elif is_command_key('QUOTE_REPLY', key):
11881198
self.keypress(size, 'enter')
11891199
quote = '```quote\n' + self.model.client.get_raw_message(

zulipterminal/ui_tools/views.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,22 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
538538
return key
539539

540540
elif is_command_key('STREAM_MESSAGE', key):
541+
if(self.model.is_user_in_unsubscribed_stream(
542+
self.model.stream_id)):
543+
self.model.controller.view.set_footer_text(
544+
" You can't write messages from unsubscribed streams.",
545+
3)
546+
return None
541547
self.body.keypress(size, 'c')
548+
542549
# For new streams with no previous conversation.
543550
if self.footer.focus is None:
544551
stream_id = self.model.stream_id
545552
stream_dict = self.model.stream_dict
546553
self.footer.stream_box_view(
547-
caption=stream_dict[stream_id]['name'])
554+
caption=stream_dict[stream_id]['name'],
555+
stream_id=stream_id
556+
)
548557
self.set_focus('footer')
549558
self.footer.focus_position = 0
550559
return key

0 commit comments

Comments
 (0)