From b919dfd48944b860ebe43d0a6d6a43861f1ea081 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Tue, 11 Oct 2022 16:49:49 +0530 Subject: [PATCH] realm: Add time limit setting for moving messages between streams. This commit adds "move_messages_between_streams_limit_seconds" setting which would be used to set a time limit to move messages between streams. --- api_docs/changelog.md | 2 ++ ..._messages_between_streams_limit_seconds.py | 17 ++++++++++ zerver/models.py | 5 +++ zerver/openapi/zulip.yaml | 31 +++++++++++++++++++ zerver/tests/test_events.py | 1 + zerver/tests/test_home.py | 1 + zerver/tests/test_realm.py | 10 ++++++ zerver/views/realm.py | 22 +++++++++++++ 8 files changed, 89 insertions(+) create mode 100644 zerver/migrations/0425_realm_move_messages_between_streams_limit_seconds.py diff --git a/api_docs/changelog.md b/api_docs/changelog.md index c64a173a1f..5bde16a55e 100644 --- a/api_docs/changelog.md +++ b/api_docs/changelog.md @@ -24,6 +24,8 @@ format used by the Zulip server that they are interacting with. * [`POST /register`](/api/register-queue), [`GET /events`](/api/get-events), `PATCH /realm`: Added new `move_messages_within_stream_limit_seconds` setting. +* [`POST /register`](/api/register-queue), [`GET /events`](/api/get-events), + `PATCH /realm`: Added new `move_messages_between_streams_limit_seconds` setting. **Feature level 161** diff --git a/zerver/migrations/0425_realm_move_messages_between_streams_limit_seconds.py b/zerver/migrations/0425_realm_move_messages_between_streams_limit_seconds.py new file mode 100644 index 0000000000..528588086e --- /dev/null +++ b/zerver/migrations/0425_realm_move_messages_between_streams_limit_seconds.py @@ -0,0 +1,17 @@ +# Generated by Django 4.1.5 on 2023-01-26 14:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("zerver", "0424_realm_move_messages_within_stream_limit_seconds"), + ] + + operations = [ + migrations.AddField( + model_name="realm", + name="move_messages_between_streams_limit_seconds", + field=models.PositiveIntegerField(default=604800, null=True), + ), + ] diff --git a/zerver/models.py b/zerver/models.py index fb1d767f75..40f5c8254d 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -387,6 +387,10 @@ class Realm(models.Model): # type: ignore[django-manager-missing] # django-stub default=DEFAULT_MOVE_MESSAGE_LIMIT_SECONDS, null=True ) + move_messages_between_streams_limit_seconds = models.PositiveIntegerField( + default=DEFAULT_MOVE_MESSAGE_LIMIT_SECONDS, null=True + ) + # Who in the organization is allowed to add custom emojis. add_custom_emoji_policy = models.PositiveSmallIntegerField(default=POLICY_MEMBERS_ONLY) @@ -736,6 +740,7 @@ class Realm(models.Model): # type: ignore[django-manager-missing] # django-stub message_content_allowed_in_email_notifications=bool, message_content_edit_limit_seconds=(int, type(None)), message_content_delete_limit_seconds=(int, type(None)), + move_messages_between_streams_limit_seconds=(int, type(None)), move_messages_within_stream_limit_seconds=(int, type(None)), message_retention_days=(int, type(None)), move_messages_between_streams_policy=int, diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml index 047c440aa0..0e9ff9b20f 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -3611,6 +3611,7 @@ paths: value: description: | The new value of the property. + nullable: true oneOf: - type: string - type: boolean @@ -4046,6 +4047,20 @@ paths: **Changes**: New in Zulip 7.0 (feature level 162). Previously, this limit was always 72 hours. + move_messages_between_streams_limit_seconds: + type: integer + nullable: true + description: | + Messages sent more than this many seconds ago cannot be moved between + streams by members with this organization's [message move + policy](/help/configure-who-can-edit-topics). This setting does not + affect moderators and administrators. + + Will not be 0. A 'null' value means no limit: messages can be moved + regardless of how long ago they were sent. + + **Changes**: New in Zulip 7.0 (feature level 162). Previously, it + was not possible to add a time limit for moving messages. move_messages_between_streams_policy: type: integer description: | @@ -12232,6 +12247,22 @@ paths: **Changes**: New in Zulip 7.0 (feature level 162). Previously, this limit was always 72 hours. + realm_move_messages_between_streams_limit_seconds: + type: integer + nullable: true + description: | + Present if `realm` is present in `fetch_event_types`. + + Messages sent more than this many seconds ago cannot be moved between + streams by members with this organization's [message move + policy](/help/configure-who-can-edit-topics). This setting does not + affect moderators and administrators. + + Will not be 0. A 'null' value means no limit: messages can be moved + regardless of how long ago they were sent. + + **Changes**: New in Zulip 7.0 (feature level 162). Previously, it + was not possible to add a time limit for moving messages. realm_community_topic_editing_limit_seconds: type: integer description: | diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 48c10ebd2f..584449e2f0 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -2605,6 +2605,7 @@ class RealmPropertyActionTest(BaseAction): edit_topic_policy=Realm.COMMON_MESSAGE_POLICY_TYPES, message_content_edit_limit_seconds=[1000, 1100, 1200, None], move_messages_within_stream_limit_seconds=[1000, 1100, 1200], + move_messages_between_streams_limit_seconds=[1000, 1100, 1200], ) vals = test_values.get(name) diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index c6371e0aee..a378aed482 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -159,6 +159,7 @@ class HomeTest(ZulipTestCase): "realm_message_content_delete_limit_seconds", "realm_message_content_edit_limit_seconds", "realm_message_retention_days", + "realm_move_messages_between_streams_limit_seconds", "realm_move_messages_between_streams_policy", "realm_move_messages_within_stream_limit_seconds", "realm_name", diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py index 46c7efe1f9..d77bf9d5f1 100644 --- a/zerver/tests/test_realm.py +++ b/zerver/tests/test_realm.py @@ -640,6 +640,7 @@ class RealmTest(ZulipTestCase): edit_topic_policy=10, message_content_edit_limit_seconds=0, move_messages_within_stream_limit_seconds=0, + move_messages_between_streams_limit_seconds=0, ) # We need an admin user. @@ -1178,6 +1179,7 @@ class RealmAPITest(ZulipTestCase): edit_topic_policy=Realm.EDIT_TOPIC_POLICY_TYPES, message_content_edit_limit_seconds=[1000, 1100, 1200], move_messages_within_stream_limit_seconds=[1000, 1100, 1200], + move_messages_between_streams_limit_seconds=[1000, 1100, 1200], ) vals = test_values.get(name) @@ -1326,6 +1328,14 @@ class RealmAPITest(ZulipTestCase): ) self.assertEqual(realm.move_messages_within_stream_limit_seconds, None) + def test_update_realm_move_messages_between_streams_limit_seconds_unlimited_value(self) -> None: + realm = get_realm("zulip") + self.login("iago") + realm = self.update_with_api( + "move_messages_between_streams_limit_seconds", orjson.dumps("unlimited").decode() + ) + self.assertEqual(realm.move_messages_between_streams_limit_seconds, None) + def test_update_realm_delete_own_message_policy(self) -> None: """Tests updating the realm property 'delete_own_message_policy'.""" self.set_up_db("delete_own_message_policy", Realm.POLICY_EVERYONE) diff --git a/zerver/views/realm.py b/zerver/views/realm.py index 7250515cec..822f7b80a7 100644 --- a/zerver/views/realm.py +++ b/zerver/views/realm.py @@ -151,6 +151,11 @@ def update_realm( json_validator=check_string_or_int, default=None, ), + move_messages_between_streams_limit_seconds_raw: Optional[Union[int, str]] = REQ( + "move_messages_between_streams_limit_seconds", + json_validator=check_string_or_int, + default=None, + ), ) -> HttpResponse: realm = user_profile.realm @@ -247,6 +252,23 @@ def update_realm( "move_messages_within_stream_limit_seconds" ] = move_messages_within_stream_limit_seconds + move_messages_between_streams_limit_seconds: Optional[int] = None + if move_messages_between_streams_limit_seconds_raw is not None: + ( + move_messages_between_streams_limit_seconds, + setting_value_changed, + ) = parse_and_set_setting_value_if_required( + realm, + "move_messages_between_streams_limit_seconds", + move_messages_between_streams_limit_seconds_raw, + acting_user=user_profile, + ) + + if setting_value_changed: + data[ + "move_messages_between_streams_limit_seconds" + ] = move_messages_between_streams_limit_seconds + # The user of `locals()` here is a bit of a code smell, but it's # restricted to the elements present in realm.property_types. #