Skip to content

Commit 84caa04

Browse files
committed
boxes: Add warning when sending message to announcement streams
Normal users are not allowed to send messages in announcement streams, currently when an unauthorised user sends message to this stream, nothing happens. This commit changes this behaviour and adds a footer warning for the same. In case if the user is composing a new message `c` from another stream the same warning is displayed or incorrect stream name warning if the stream name does not exist. Fixes: #682
1 parent f4dc819 commit 84caa04

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

zulipterminal/helper.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,37 @@ def notify(title: str, html_text: str) -> str:
589589
# This likely means the notification command could not be found
590590
return command_list[0]
591591
return ""
592+
593+
594+
def unauthorised_warning(model: Any, stream_identifier: Any):
595+
# Identify target stream based on stream id or stream name
596+
stream_id = None
597+
if isinstance(stream_identifier, int):
598+
stream_id = stream_identifier
599+
elif isinstance(stream_identifier, str):
600+
for s_id, stream in model.stream_dict.items():
601+
if stream['name'] == stream_identifier:
602+
stream_id = s_id
603+
break
604+
605+
if not stream_id:
606+
msg_footer = (
607+
model.controller.view.set_footer_text(
608+
"Specified stream does not exist.",
609+
5))
610+
return True
611+
else:
612+
if (model.stream_dict[stream_id].
613+
get('is_announcement_only', None) == 1
614+
or (model.initial_data.get('is_admin', None)
615+
or model.initial_data.get('is_owner', None)
616+
and model.stream_dict[stream_id].
617+
get('stream_post_policy', None) == 2)):
618+
msg_footer = (
619+
model.controller.view.set_footer_text(
620+
"You are not authorised to send messages "
621+
"to this stream.",
622+
5))
623+
return True
624+
else:
625+
return False

zulipterminal/ui_tools/boxes.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from zulipterminal.config.keys import is_command_key, keys_for_command
1515
from zulipterminal.helper import (
1616
Message, match_emoji, match_group, match_stream, match_user,
17+
unauthorised_warning
1718
)
1819
from zulipterminal.ui_tools.tables import render_table
1920
from zulipterminal.urwid_types import urwid_Size
@@ -156,7 +157,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
156157
msg_id=self.msg_edit_id,
157158
)
158159
else:
159-
if not self.to_write_box:
160+
if unauthorised_warning(self.model,
161+
self.stream_write_box.edit_text):
162+
success = None
163+
elif not self.to_write_box:
160164
success = self.model.send_stream_message(
161165
stream=self.stream_write_box.edit_text,
162166
topic=self.title_write_box.edit_text,
@@ -751,10 +755,12 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
751755
email=self.recipients_emails
752756
)
753757
elif self.message['type'] == 'stream':
754-
self.model.controller.view.write_box.stream_box_view(
755-
caption=self.message['display_recipient'],
756-
title=self.message['subject']
757-
)
758+
# Check if user is allowed to post
759+
if not unauthorised_warning(self.model, self.message.get('stream_id')):
760+
self.model.controller.view.write_box.stream_box_view(
761+
caption=self.message['display_recipient'],
762+
title=self.message['subject']
763+
)
758764
elif is_command_key('STREAM_MESSAGE', key):
759765
if self.message['type'] == 'private':
760766
self.model.controller.view.write_box.private_box_view(

0 commit comments

Comments
 (0)