Skip to content

Commit 6ab6a7d

Browse files
committed
streams: Add UI warnings for failing events.
1 parent 8b5dc26 commit 6ab6a7d

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

19131913
assert return_value == expected_response
19141914

1915+
@pytest.mark.parametrize('stream_id, expected_response,', [
1916+
(100, False),
1917+
(101, False),
1918+
(3, True),
1919+
],
1920+
ids=[
1921+
'subscribed_pinned_stream',
1922+
'subscribed_unpinned_stream',
1923+
'unsubscribed_stream',
1924+
]
1925+
)
1926+
def test_is_user_in_unsubscribed_stream(self, model, stream_dict,
1927+
stream_id, expected_response,
1928+
initial_pinned_streams,
1929+
initial_unpinned_streams):
1930+
1931+
model.pinned_streams = deepcopy(initial_pinned_streams)
1932+
model.unpinned_streams = deepcopy(initial_unpinned_streams)
1933+
return_value = model.is_user_in_unsubscribed_stream(stream_id)
1934+
1935+
assert model.pinned_streams == initial_pinned_streams
1936+
assert model.unpinned_streams == initial_unpinned_streams
1937+
assert return_value == expected_response
1938+
19151939
@pytest.mark.parametrize('response', [{
19161940
'result': 'success',
19171941
'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
@@ -17,6 +17,7 @@ class TestWriteBox:
1717
def mock_external_classes(self, mocker, initial_index):
1818
self.view = mocker.Mock()
1919
self.view.model = mocker.Mock()
20+
self.view.model.is_user_in_unsubscribed_stream.return_value = False
2021

2122
@pytest.fixture()
2223
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
@@ -724,6 +724,9 @@ def stream_id_from_name(self, stream_name: str) -> int:
724724
def is_pinned_stream(self, stream_id: int) -> bool:
725725
return stream_id in [stream['id'] for stream in self.pinned_streams]
726726

727+
def is_unpinned_stream(self, stream_id: int) -> bool:
728+
return stream_id in [stream['id'] for stream in self.unpinned_streams]
729+
727730
def toggle_stream_pinned_status(self, stream_id: int) -> bool:
728731
request = [{
729732
'stream_id': stream_id,
@@ -736,6 +739,10 @@ def toggle_stream_pinned_status(self, stream_id: int) -> bool:
736739
def is_user_subscribed_to_stream(self, stream_id: int) -> bool:
737740
return stream_id in self.stream_dict
738741

742+
def is_user_in_unsubscribed_stream(self, stream_id: int) -> bool:
743+
return not(self.is_pinned_stream(stream_id)
744+
or self.is_unpinned_stream(stream_id))
745+
739746
def _get_stream_by_id(self, streams: List[StreamData], stream_id: int
740747
) -> StreamData:
741748
for stream in streams:

zulipterminal/ui_tools/boxes.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
11701170
stream_id=self.stream_id,
11711171
)
11721172
elif is_command_key('STREAM_MESSAGE', key):
1173+
if(self.model.is_user_in_unsubscribed_stream(self.stream_id)):
1174+
self.model.controller.view.set_footer_text(
1175+
" You can't write messages to unsubscribed streams.",
1176+
3)
1177+
return None
11731178
if self.message['type'] == 'private':
11741179
self.model.controller.view.write_box.private_box_view(
11751180
email=self.recipients_emails,
@@ -1214,13 +1219,18 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
12141219
recipient_user_ids=[self.message['sender_id']],
12151220
)
12161221
elif is_command_key('MENTION_REPLY', key):
1217-
self.keypress(size, 'enter')
1218-
mention = '@**' + self.message['sender_full_name'] + '** '
1219-
self.model.controller.view.write_box.msg_write_box.set_edit_text(
1220-
mention)
1221-
self.model.controller.view.write_box.msg_write_box.set_edit_pos(
1222-
len(mention))
1223-
self.model.controller.view.middle_column.set_focus('footer')
1222+
if(not self.model.is_user_in_unsubscribed_stream(self.stream_id)):
1223+
self.keypress(size, 'enter')
1224+
mention = '@**' + self.message['sender_full_name'] + '** '
1225+
write_box = self.model.controller.view.write_box.msg_write_box
1226+
write_box.set_edit_text(mention)
1227+
write_box.set_edit_pos(len(mention))
1228+
self.model.controller.view.middle_column.set_focus('footer')
1229+
else:
1230+
self.model.controller.view.set_footer_text(
1231+
" You can't reply to messages from unsubscribed streams.",
1232+
3)
1233+
return key
12241234
elif is_command_key('QUOTE_REPLY', key):
12251235
self.keypress(size, 'enter')
12261236
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
@@ -539,13 +539,22 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
539539
return key
540540

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

0 commit comments

Comments
 (0)