Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
streams: Add notifications for posting policy changes.
Eeshan Gargc30458e1740c
diff --git a/static/js/stream_data.js b/static/js/stream_data.jsindex 16aeb7863d3..ea8d13ec08c 100644--- a/static/js/stream_data.js+++ b/static/js/stream_data.js@@ -138,6 +138,8 @@ export const stream_privacy_policy_values = {};export const stream_post_policy_values = {+ // These strings should match the strings in the+ // Stream.POST_POLICIES object in zerver/models.py.everyone: {code: 1,description: $t({defaultMessage: "All stream members can post"}),diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex 0dc27f13cdd..3b3cdc6b43d 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -5012,7 +5012,32 @@ def do_change_stream_permission(do_change_stream_invite_only(stream, invite_only, history_public_to_subscribers)-def do_change_stream_post_policy(stream: Stream, stream_post_policy: int) -> None:+def send_change_stream_post_policy_notification(+ stream: Stream, *, old_post_policy: int, new_post_policy: int, acting_user: UserProfile+) -> None:+ sender = get_system_bot(settings.NOTIFICATION_BOT, acting_user.realm_id)+ user_mention = silent_mention_syntax_for_user(acting_user)++ with override_language(stream.realm.default_language):+ notification_string = _(+ "{user} changed the [posting permissions](/help/stream-sending-policy) "+ "for this stream:\n\n"+ "* **Old permissions**: {old_policy}.\n"+ "* **New permissions**: {new_policy}.\n"+ )+ notification_string = notification_string.format(+ user=user_mention,+ old_policy=Stream.POST_POLICIES[old_post_policy],+ new_policy=Stream.POST_POLICIES[new_post_policy],+ )+ internal_send_stream_message(+ sender, stream, Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, notification_string+ )+++def do_change_stream_post_policy(+ stream: Stream, stream_post_policy: int, *, acting_user: UserProfile+) -> None:stream.stream_post_policy = stream_post_policystream.save(update_fields=["stream_post_policy"])event = dict(@@ -5039,6 +5064,13 @@ def do_change_stream_post_policy(stream: Stream, stream_post_policy: int) -> Non)send_event(stream.realm, event, can_access_stream_user_ids(stream))+ send_change_stream_post_policy_notification(+ stream,+ old_post_policy=old_post_policy,+ new_post_policy=stream_post_policy,+ acting_user=acting_user,+ )+def do_rename_stream(stream: Stream, new_name: str, user_profile: UserProfile) -> Dict[str, str]:old_name = stream.namediff --git a/zerver/models.py b/zerver/models.pyindex 12d0f29b2e4..eb76b435ae8 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -2253,12 +2253,19 @@ class Stream(models.Model):# Who in the organization has permission to send messages to this stream.stream_post_policy: int = models.PositiveSmallIntegerField(default=STREAM_POST_POLICY_EVERYONE)- STREAM_POST_POLICY_TYPES = [- STREAM_POST_POLICY_EVERYONE,- STREAM_POST_POLICY_ADMINS,- STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS,- STREAM_POST_POLICY_MODERATORS,- ]+ POST_POLICIES: Dict[int, str] = {+ # These strings should match the strings in the+ # stream_post_policy_values object in stream_data.js.+ STREAM_POST_POLICY_EVERYONE: gettext_lazy("All stream members can post"),+ STREAM_POST_POLICY_ADMINS: gettext_lazy("Only organization administrators can post"),+ STREAM_POST_POLICY_MODERATORS: gettext_lazy(+ "Only organization administrators and moderators can post"+ ),+ STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS: gettext_lazy(+ "Only organization full members can post"+ ),+ }+ STREAM_POST_POLICY_TYPES = list(POST_POLICIES.keys())# The unique thing about Zephyr public streams is that we never list their# users. We may try to generalize this concept later, but for nowdiff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.pyindex c45cd573781..29b2049a559 100644--- a/zerver/openapi/curl_param_value_generators.py+++ b/zerver/openapi/curl_param_value_generators.py@@ -126,8 +126,9 @@ def iago_message_id() -> Dict[str, object]:def add_emoji_to_message() -> Dict[str, object]:user_profile = helpers.example_user("iago")- # from OpenAPI format data in zulip.yaml- message_id = 43+ # The message ID here is hardcoded based on the corresponding value+ # for the example message IDs we use in zulip.yaml.+ message_id = 44emoji_name = "octopus"emoji_code = "1f419"reaction_type = "unicode_emoji"diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex b10e57c579f..f160bb03a49 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -4132,7 +4132,7 @@ paths:The ID of the stream to access.schema:type: integer- example: 42+ example: 43required: trueresponses:"200":@@ -4157,7 +4157,7 @@ paths:The ID of the stream to access.schema:type: integer- example: 42+ example: 43required: true- name: topic_namein: query@@ -4588,7 +4588,7 @@ paths:oneOf:- type: string- type: integer- example: 42+ example: 43- name: num_beforein: querydescription: |@@ -5542,7 +5542,7 @@ paths:if the target message is not a stream message.schema:type: integer- example: 42+ example: 43responses:"200":description: Success.@@ -6980,7 +6980,7 @@ paths:The ID of the stream to access.schema:type: integer- example: 42+ example: 43required: false- name: topicin: query@@ -8383,7 +8383,7 @@ paths:The ID of the linkifier that you want to remove.schema:type: integer- example: 42+ example: 43required: trueresponses:"200":@@ -15337,7 +15337,7 @@ components:The target message's ID.schema:type: integer- example: 42+ example: 43required: trueUserId:name: user_iddiff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.pyindex 634f769494f..8a53aad063e 100644--- a/zerver/tests/test_email_mirror.py+++ b/zerver/tests/test_email_mirror.py@@ -955,7 +955,9 @@ class TestMissedMessageEmailMessages(ZulipTestCase):self.assert_json_success(result)stream = get_stream("announce", user_profile.realm)- do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_ADMINS)+ do_change_stream_post_policy(+ stream, Stream.STREAM_POST_POLICY_ADMINS, acting_user=user_profile+ )usermessage = most_recent_usermessage(user_profile)diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex d5bdb599566..d8f6c400f6e 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -2554,9 +2554,12 @@ class SubscribeActionTest(BaseAction):check_stream_update("events[0]", events[0])# Update stream stream_post_policy property- action = lambda: do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_ADMINS)- events = self.verify_action(action, include_subscribers=include_subscribers, num_events=2)+ action = lambda: do_change_stream_post_policy(+ stream, Stream.STREAM_POST_POLICY_ADMINS, acting_user=self.example_user("hamlet")+ )+ events = self.verify_action(action, include_subscribers=include_subscribers, num_events=3)check_stream_update("events[0]", events[0])+ check_message("events[2]", events[2])action = lambda: do_change_stream_message_retention_days(stream, self.example_user("hamlet"), -1diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.pyindex 48f164787fb..df15cd6b45e 100644--- a/zerver/tests/test_message_edit.py+++ b/zerver/tests/test_message_edit.py@@ -1697,7 +1697,9 @@ class EditMessageTest(EditMessageTestCase):self.assert_length(messages, 4)# Check when stream_post_policy is STREAM_POST_POLICY_ADMINS.- do_change_stream_post_policy(new_stream, Stream.STREAM_POST_POLICY_ADMINS)+ do_change_stream_post_policy(+ new_stream, Stream.STREAM_POST_POLICY_ADMINS, acting_user=user_profile+ )error_msg = "Only organization administrators can send to this stream."check_move_message_to_stream(UserProfile.ROLE_MODERATOR, error_msg)check_move_message_to_stream(UserProfile.ROLE_REALM_ADMINISTRATOR)@@ -1707,7 +1709,9 @@ class EditMessageTest(EditMessageTestCase):)# Check when stream_post_policy is STREAM_POST_POLICY_MODERATORS.- do_change_stream_post_policy(new_stream, Stream.STREAM_POST_POLICY_MODERATORS)+ do_change_stream_post_policy(+ new_stream, Stream.STREAM_POST_POLICY_MODERATORS, acting_user=user_profile+ )error_msg = "Only organization administrators and moderators can send to this stream."check_move_message_to_stream(UserProfile.ROLE_MEMBER, error_msg)check_move_message_to_stream(UserProfile.ROLE_MODERATOR)@@ -1717,7 +1721,9 @@ class EditMessageTest(EditMessageTestCase):)# Check when stream_post_policy is STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS.- do_change_stream_post_policy(new_stream, Stream.STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS)+ do_change_stream_post_policy(+ new_stream, Stream.STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS, acting_user=user_profile+ )error_msg = "New members cannot send to this stream."do_set_realm_property(@@ -1736,7 +1742,9 @@ class EditMessageTest(EditMessageTestCase):# In this case also, guest is not allowed as we do not allow guest to move# messages between streams in any case, so stream_post_policy of new stream does# not matter.- do_change_stream_post_policy(new_stream, Stream.STREAM_POST_POLICY_EVERYONE)+ do_change_stream_post_policy(+ new_stream, Stream.STREAM_POST_POLICY_EVERYONE, acting_user=user_profile+ )do_set_realm_property(user_profile.realm, "waiting_period_threshold", 100000, acting_user=None)diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex 820c8468b01..aa647b8cf0a 100644--- a/zerver/tests/test_message_send.py+++ b/zerver/tests/test_message_send.py@@ -173,7 +173,9 @@ class MessagePOSTTest(ZulipTestCase):stream_name = "Verona"stream = get_stream(stream_name, admin_profile.realm)- do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_ADMINS)+ do_change_stream_post_policy(+ stream, Stream.STREAM_POST_POLICY_ADMINS, acting_user=admin_profile+ )# Admins and their owned bots can send to STREAM_POST_POLICY_ADMINS streamsself._send_and_verify_message(admin_profile, stream_name)@@ -261,7 +263,9 @@ class MessagePOSTTest(ZulipTestCase):stream_name = "Verona"stream = get_stream(stream_name, admin_profile.realm)- do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_MODERATORS)+ do_change_stream_post_policy(+ stream, Stream.STREAM_POST_POLICY_MODERATORS, acting_user=admin_profile+ )# Admins and their owned bots can send to STREAM_POST_POLICY_MODERATORS streamsself._send_and_verify_message(admin_profile, stream_name)@@ -349,7 +353,9 @@ class MessagePOSTTest(ZulipTestCase):stream_name = "Verona"stream = get_stream(stream_name, admin_profile.realm)- do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS)+ do_change_stream_post_policy(+ stream, Stream.STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS, acting_user=admin_profile+ )# Admins and their owned bots can send to STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS streams,# even if the admin is a new userdiff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.pyindex d355d9b8acb..7a30e9d1c01 100644--- a/zerver/tests/test_openapi.py+++ b/zerver/tests/test_openapi.py@@ -78,7 +78,7 @@ class OpenAPIToolsTest(ZulipTestCase):"name": "message_id","in": "path","description": "The target message's ID.\n",- "example": 42,+ "example": 43,"required": True,"schema": {"type": "integer"},}@@ -810,7 +810,7 @@ class TestCurlExampleGeneration(ZulipTestCase):"```curl","curl -sSX GET -G http://localhost:9991/api/v1/messages \\"," -u BOT_EMAIL_ADDRESS:BOT_API_KEY \\",- " --data-urlencode anchor=42 \\",+ " --data-urlencode anchor=43 \\"," --data-urlencode num_before=4 \\"," --data-urlencode num_after=8 \\",' --data-urlencode \'narrow=[{"operand": "Denmark", "operator": "stream"}]\' \\',@@ -884,7 +884,7 @@ class TestCurlExampleGeneration(ZulipTestCase):"```curl","curl -sSX GET -G http://localhost:9991/api/v1/messages \\"," -u BOT_EMAIL_ADDRESS:BOT_API_KEY \\",- " --data-urlencode anchor=42 \\",+ " --data-urlencode anchor=43 \\"," --data-urlencode num_before=4 \\"," --data-urlencode num_after=8 \\",' --data-urlencode \'narrow=[{"operand": "Denmark", "operator": "stream"}]\' \\',diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex 337148de66c..7ea4ae549b0 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -1323,7 +1323,16 @@ class StreamAdminTest(ZulipTestCase):)self.assert_json_success(result)stream = get_stream("stream_name1", user_profile.realm)- self.assertTrue(stream.stream_post_policy == Stream.STREAM_POST_POLICY_ADMINS)+ self.assertEqual(stream.stream_post_policy, Stream.STREAM_POST_POLICY_ADMINS)++ messages = get_topic_messages(user_profile, stream, "stream events")+ expected_notification = (+ f"@_**{user_profile.full_name}|{user_profile.id}** changed the "+ "[posting permissions](/help/stream-sending-policy) for this stream:\n\n"+ "* **Old permissions**: All stream members can post.\n"+ "* **New permissions**: Only organization administrators can post."+ )+ self.assertEqual(messages[-1].content, expected_notification)def test_change_stream_post_policy_requires_admin(self) -> None:user_profile = self.example_user("hamlet")@@ -1377,6 +1386,15 @@ class StreamAdminTest(ZulipTestCase):stream = get_stream("stream_name1", user_profile.realm)self.assertEqual(stream.stream_post_policy, policy)+ messages = get_topic_messages(user_profile, stream, "stream events")+ expected_notification = (+ f"@_**{user_profile.full_name}|{user_profile.id}** changed the "+ "[posting permissions](/help/stream-sending-policy) for this stream:\n\n"+ f"* **Old permissions**: {Stream.POST_POLICIES[old_post_policy]}.\n"+ f"* **New permissions**: {Stream.POST_POLICIES[policy]}."+ )+ self.assertEqual(messages[-1].content, expected_notification)+do_change_user_role(user_profile, UserProfile.ROLE_REALM_ADMINISTRATOR, acting_user=None)for policy in policies:@@ -1388,6 +1406,16 @@ class StreamAdminTest(ZulipTestCase):stream = get_stream("stream_name1", user_profile.realm)self.assertEqual(stream.stream_post_policy, policy)+ messages = get_topic_messages(user_profile, stream, "stream events")+ expected_notification = (+ f"@_**{user_profile.full_name}|{user_profile.id}** changed the "+ "[posting permissions](/help/stream-sending-policy) for this stream:\n\n"+ f"* **Old permissions**: {Stream.POST_POLICIES[old_post_policy]}.\n"+ f"* **New permissions**: {Stream.POST_POLICIES[policy]}."+ )++ self.assertEqual(messages[-1].content, expected_notification)+def test_change_stream_message_retention_days_notifications(self) -> None:user_profile = self.example_user("desdemona")self.login_user(user_profile)@@ -3905,7 +3933,7 @@ class SubscriptionAPITest(ZulipTestCase):"""member = self.example_user("AARON")stream = self.make_stream("stream1")- do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_ADMINS)+ do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_ADMINS, acting_user=member)result = self.common_subscribe_to_streams(member, ["stream1"])self.assert_json_success(result)@@ -3925,7 +3953,9 @@ class SubscriptionAPITest(ZulipTestCase):self.assertTrue(new_member.is_provisional_member)stream = self.make_stream("stream1")- do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS)+ do_change_stream_post_policy(+ stream, Stream.STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS, acting_user=new_member+ )result = self.common_subscribe_to_streams(new_member, ["stream1"])self.assert_json_success(result)@@ -3942,7 +3972,9 @@ class SubscriptionAPITest(ZulipTestCase):# Make sure that we are testing this with full member which is just below the moderator# in the role hierarchy.self.assertFalse(member.is_provisional_member)- do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_MODERATORS)+ do_change_stream_post_policy(+ stream, Stream.STREAM_POST_POLICY_MODERATORS, acting_user=member+ )result = self.common_subscribe_to_streams(member, ["stream1"])self.assert_json_success(result)diff --git a/zerver/views/streams.py b/zerver/views/streams.pyindex 7ce854a17b7..081dc621c15 100644--- a/zerver/views/streams.py+++ b/zerver/views/streams.py@@ -303,7 +303,7 @@ def update_stream_backend(if is_announcement_only:stream_post_policy = Stream.STREAM_POST_POLICY_ADMINSif stream_post_policy is not None:- do_change_stream_post_policy(stream, stream_post_policy)+ do_change_stream_post_policy(stream, stream_post_policy, acting_user=user_profile)# But we require even realm administrators to be actually# subscribed to make a private stream public.