Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
settings: Add moderators and members option in delete_own_message_policy.
sahil8399dd69c17eef9
diff --git a/zerver/models.py b/zerver/models.pyindex 89b0c7f0720..e92815d00b6 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -294,6 +294,9 @@ class Realm(models.Model):# Who in the organization is allowed to create streams.create_stream_policy: int = models.PositiveSmallIntegerField(default=POLICY_MEMBERS_ONLY)+ # Who in the organization is allowed to delete messages they themselves sent.+ delete_own_message_policy: bool = models.PositiveSmallIntegerField(default=POLICY_ADMINS_ONLY)+# Who in the organization is allowed to edit topics of any message.edit_topic_policy: int = models.PositiveSmallIntegerField(default=POLICY_EVERYONE)@@ -367,13 +370,6 @@ class Realm(models.Model):# some other actions.waiting_period_threshold: int = models.PositiveIntegerField(default=0)- DELETE_OWN_MESSAGE_POLICY_TYPES = [- POLICY_EVERYONE,- POLICY_ADMINS_ONLY,- ]-- delete_own_message_policy: bool = models.PositiveSmallIntegerField(default=POLICY_ADMINS_ONLY)-DEFAULT_MESSAGE_CONTENT_DELETE_LIMIT_SECONDS = (600 # if changed, also change in admin.js, setting_org.js)@@ -1839,6 +1835,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):if policy_name not in ["add_custom_emoji_policy","create_stream_policy",+ "delete_own_message_policy","edit_topic_policy","invite_to_stream_policy","invite_to_realm_policy",@@ -1851,6 +1848,9 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):if policy_value == Realm.POLICY_NOBODY:return False+ if policy_value == Realm.POLICY_EVERYONE:+ return True+if self.is_realm_admin:return True@@ -1895,6 +1895,9 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):def can_add_custom_emoji(self) -> bool:return self.has_permission("add_custom_emoji_policy")+ def can_delete_own_message(self) -> bool:+ return self.has_permission("delete_own_message_policy")+def can_access_public_streams(self) -> bool:return not (self.is_guest or self.realm.is_zephyr_mirror_realm)diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 5d0c42c3bae..bfaa5545584 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -10344,7 +10344,10 @@ paths:The policy defining which users can deletemessages that they had sent.+ - 1 = members only- 2 = admins only+ - 3 = full members only+ - 4 = admins and moderators only- 5 = everyone**Changes**: New in Zulip 5.0 (feature level 101), replacing thediff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex b2db53c1ab8..d77776c1786 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -2145,7 +2145,7 @@ class RealmPropertyActionTest(BaseAction):invite_to_realm_policy=[6, 4, 3, 2, 1],move_messages_between_streams_policy=[4, 3, 2, 1],add_custom_emoji_policy=[4, 3, 2, 1],- delete_own_message_policy=[5, 2],+ delete_own_message_policy=[5, 4, 3, 2, 1],)vals = test_values.get(name)diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.pyindex a89cd33996f..03a23523116 100644--- a/zerver/tests/test_message_edit.py+++ b/zerver/tests/test_message_edit.py@@ -2243,6 +2243,63 @@ class DeleteMessageTest(ZulipTestCase):result = test_delete_message_by_owner(msg_id=msg_id)self.assert_json_error(result, "Message already deleted")+ def test_delete_message_according_to_delete_own_message_policy(self) -> None:+ def check_delete_message_by_sender(+ sender_name: str, error_msg: Optional[str] = None+ ) -> None:+ sender = self.example_user(sender_name)+ msg_id = self.send_stream_message(sender, "Verona")+ self.login_user(sender)+ result = self.client_delete(f"/json/messages/{msg_id}")+ if error_msg is None:+ self.assert_json_success(result)+ else:+ self.assert_json_error(result, error_msg)++ realm = get_realm("zulip")++ do_set_realm_property(+ realm, "delete_own_message_policy", Realm.POLICY_ADMINS_ONLY, acting_user=None+ )+ check_delete_message_by_sender("shiva", "You don't have permission to delete this message")+ check_delete_message_by_sender("iago")++ do_set_realm_property(+ realm, "delete_own_message_policy", Realm.POLICY_MODERATORS_ONLY, acting_user=None+ )+ check_delete_message_by_sender(+ "cordelia", "You don't have permission to delete this message"+ )+ check_delete_message_by_sender("shiva")++ do_set_realm_property(+ realm, "delete_own_message_policy", Realm.POLICY_MEMBERS_ONLY, acting_user=None+ )+ check_delete_message_by_sender(+ "polonius", "You don't have permission to delete this message"+ )+ check_delete_message_by_sender("cordelia")++ do_set_realm_property(+ realm, "delete_own_message_policy", Realm.POLICY_FULL_MEMBERS_ONLY, acting_user=None+ )+ do_set_realm_property(realm, "waiting_period_threshold", 10, acting_user=None)+ cordelia = self.example_user("cordelia")+ cordelia.date_joined = timezone_now() - datetime.timedelta(days=9)+ cordelia.save()+ check_delete_message_by_sender(+ "cordelia", "You don't have permission to delete this message"+ )+ cordelia.date_joined = timezone_now() - datetime.timedelta(days=11)+ cordelia.save()+ check_delete_message_by_sender("cordelia")++ do_set_realm_property(+ realm, "delete_own_message_policy", Realm.POLICY_EVERYONE, acting_user=None+ )+ check_delete_message_by_sender("cordelia")+ check_delete_message_by_sender("polonius")+def test_delete_event_sent_after_transaction_commits(self) -> None:"""Tests that `send_event` is hooked to `transaction.on_commit`. This is important, becausediff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.pyindex 2fbd5e2f269..3d40998173e 100644--- a/zerver/tests/test_realm.py+++ b/zerver/tests/test_realm.py@@ -839,7 +839,7 @@ class RealmAPITest(ZulipTestCase):invite_to_realm_policy=Realm.INVITE_TO_REALM_POLICY_TYPES,move_messages_between_streams_policy=Realm.COMMON_POLICY_TYPES,add_custom_emoji_policy=Realm.COMMON_POLICY_TYPES,- delete_own_message_policy=Realm.DELETE_OWN_MESSAGE_POLICY_TYPES,+ delete_own_message_policy=Realm.COMMON_MESSAGE_POLICY_TYPES,)vals = test_values.get(name)@@ -1021,6 +1021,12 @@ class RealmAPITest(ZulipTestCase):realm = self.update_with_api("message_content_delete_limit_seconds", 600)self.assertEqual(realm.delete_own_message_policy, Realm.POLICY_EVERYONE)self.assertEqual(realm.message_content_delete_limit_seconds, 600)+ realm = self.update_with_api("delete_own_message_policy", Realm.POLICY_MODERATORS_ONLY)+ self.assertEqual(realm.delete_own_message_policy, Realm.POLICY_MODERATORS_ONLY)+ realm = self.update_with_api("delete_own_message_policy", Realm.POLICY_FULL_MEMBERS_ONLY)+ self.assertEqual(realm.delete_own_message_policy, Realm.POLICY_FULL_MEMBERS_ONLY)+ realm = self.update_with_api("delete_own_message_policy", Realm.POLICY_MEMBERS_ONLY)+ self.assertEqual(realm.delete_own_message_policy, Realm.POLICY_MEMBERS_ONLY)# Test that 0 is invalid value.req = dict(message_content_delete_limit_seconds=orjson.dumps(0).decode())diff --git a/zerver/views/message_edit.py b/zerver/views/message_edit.pyindex 9ba1986edb4..0bf0ef4374b 100644--- a/zerver/views/message_edit.py+++ b/zerver/views/message_edit.py@@ -18,7 +18,7 @@ from zerver.lib.response import json_successfrom zerver.lib.timestamp import datetime_to_timestampfrom zerver.lib.topic import LEGACY_PREV_TOPIC, REQ_topicfrom zerver.lib.validator import check_bool, check_string_in, to_non_negative_int-from zerver.models import Message, Realm, UserProfile+from zerver.models import Message, UserProfiledef fill_edit_history_entries(message_history: List[Dict[str, Any]], message: Message) -> None:@@ -133,8 +133,8 @@ def validate_can_delete_message(user_profile: UserProfile, message: Message) ->if message.sender != user_profile:# Users can only delete messages sent by them.raise JsonableError(_("You don't have permission to delete this message"))- if user_profile.realm.delete_own_message_policy == Realm.POLICY_ADMINS_ONLY:- # User can not delete message, if message deleting is not allowed in realm.+ if not user_profile.can_delete_own_message():+ # Only user with roles as allowed by delete_own_message_policy can delete message.raise JsonableError(_("You don't have permission to delete this message"))deadline_seconds: Optional[int] = user_profile.realm.message_content_delete_limit_secondsdiff --git a/zerver/views/realm.py b/zerver/views/realm.pyindex 7c58b017616..5da4d8e7d8e 100644--- a/zerver/views/realm.py+++ b/zerver/views/realm.py@@ -68,7 +68,7 @@ def update_realm(json_validator=check_int_in(Realm.COMMON_POLICY_TYPES), default=None),delete_own_message_policy: Optional[int] = REQ(- json_validator=check_int_in(Realm.DELETE_OWN_MESSAGE_POLICY_TYPES), default=None+ json_validator=check_int_in(Realm.COMMON_MESSAGE_POLICY_TYPES), default=None),message_content_delete_limit_seconds_raw: Optional[Union[int, str]] = REQ("message_content_delete_limit_seconds", json_validator=check_string_or_int, default=None