Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
do_scrub_realm: Add skip_notify=True flag for message deletion.
Mateusz Mandera279c72de3a7a
diff --git a/api_docs/changelog.md b/api_docs/changelog.mdindex 220c5ee9dc9..fa0aa9a164a 100644--- a/api_docs/changelog.md+++ b/api_docs/changelog.md@@ -20,6 +20,13 @@ format used by the Zulip server that they are interacting with.## Changes in Zulip 12.0+**Feature level 452**++* [`GET /events`](/api/get-events): Prior to Zulip 12.0 (feature level 452)+ messages deleted via a message retention policy incorrectly failed to generate+ `delete_message` events. This is now fixed, allowing clients to correctly update+ their message state, by removing the affected messages from view.+**Feature level 451*** [Message formatting](/api/message-formatting): Changed thediff --git a/version.py b/version.pyindex 88f8617b816..773dd6cb4c4 100644--- a/version.py+++ b/version.py@@ -35,7 +35,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"# new level means in api_docs/changelog.md, as well as "**Changes**"# entries in the endpoint's documentation in `zulip.yaml`.-API_FEATURE_LEVEL = 451+API_FEATURE_LEVEL = 452# Bump the minor PROVISION_VERSION to indicate that folks should provision# only when going from an old version of the code to a newer version. Bumpdiff --git a/zerver/actions/message_delete.py b/zerver/actions/message_delete.pyindex 34182e09932..ad073159035 100644--- a/zerver/actions/message_delete.py+++ b/zerver/actions/message_delete.py@@ -63,7 +63,7 @@ def do_delete_messages()-def do_delete_messages_by_sender(user: UserProfile) -> None:+def do_delete_messages_by_sender(user: UserProfile, skip_notify: bool = False) -> None:message_ids = list(# Uses index: zerver_message_realm_sender_recipient (prefix)Message.objects.filter(realm_id=user.realm_id, sender=user)@@ -71,5 +71,8 @@ def do_delete_messages_by_sender(user: UserProfile) -> None:.order_by("id"))move_messages_to_archive(- message_ids, user.realm, chunk_size=retention.STREAM_MESSAGE_BATCH_SIZE+ message_ids,+ user.realm,+ chunk_size=retention.STREAM_MESSAGE_BATCH_SIZE,+ skip_notify=skip_notify,)diff --git a/zerver/actions/realm_settings.py b/zerver/actions/realm_settings.pyindex 6e81c8d7ce1..030fb8c42e8 100644--- a/zerver/actions/realm_settings.py+++ b/zerver/actions/realm_settings.py@@ -721,7 +721,7 @@ def do_scrub_realm(realm: Realm, *, acting_user: UserProfile | None) -> None:users = UserProfile.objects.filter(realm=realm)for user in users:- do_delete_messages_by_sender(user)+ do_delete_messages_by_sender(user, skip_notify=True)do_scrub_avatar_images(user, acting_user=acting_user)user.full_name = f"Scrubbed {generate_key()[:15]}"scrubbed_email = Address(@@ -759,7 +759,11 @@ def do_scrub_realm(realm: Realm, *, acting_user: UserProfile | None) -> None:realm=realm,).values_list("id", flat=True))- move_messages_to_archive(cross_realm_bot_message_ids, realm=realm)+ move_messages_to_archive(cross_realm_bot_message_ids, realm=realm, skip_notify=True)++ # Since we delete messages with skip_notify=True, we must take care of updating the first_message_id+ # of channels in the realm.+ Stream.objects.filter(realm=realm).update(first_message_id=None)do_remove_realm_custom_profile_fields(realm)do_delete_all_realm_attachments(realm)diff --git a/zerver/lib/retention.py b/zerver/lib/retention.pyindex 377c66839b7..e6108505842 100644--- a/zerver/lib/retention.py+++ b/zerver/lib/retention.py@@ -133,6 +133,7 @@ def run_archiving(type: int,realm: Realm,chunk_size: int | None = MESSAGE_BATCH_SIZE,+ skip_notify: bool = False,acting_user: UserProfile | None = None,**kwargs: Composable,) -> int:@@ -162,7 +163,7 @@ def run_archiving()if new_chunk:move_related_objects_to_archive(new_chunk)- delete_messages(new_chunk, realm, acting_user=acting_user)+ delete_messages(new_chunk, realm, skip_notify=skip_notify, acting_user=acting_user)message_count += len(new_chunk)else:archive_transaction.delete() # Nothing was archived@@ -332,6 +333,7 @@ def _process_grouped_messages_deletion(*,stream: Stream | None,topic: str | None,+ skip_notify: bool,acting_user: UserProfile | None,) -> None:"""@@ -357,27 +359,32 @@ def _process_grouped_messages_deletion(event["message_type"] = message_type# We exclude long-term idle users, since they by definition have no active clients.- users_to_notify = event_recipient_ids_for_action_on_messages(- message_ids,- is_channel_message=message_type == "stream",- channel=stream if message_type == "stream" else None,- )-- if acting_user is not None:- # Always send event to the user who deleted the message.- users_to_notify.add(acting_user.id)+ users_to_notify = set()+ if not skip_notify:+ users_to_notify = event_recipient_ids_for_action_on_messages(+ message_ids,+ is_channel_message=message_type == "stream",+ channel=stream if message_type == "stream" else None,+ )+ if acting_user is not None:+ # Always send event to the user who deleted the message.+ users_to_notify.add(acting_user.id)# Uses index: zerver_message_pkeyMessage.objects.filter(id__in=message_ids).delete()- if stream is not None:- check_update_first_message_id(realm, stream, message_ids, users_to_notify)+ if not skip_notify:+ if stream is not None:+ check_update_first_message_id(realm, stream, message_ids, users_to_notify)- send_event_on_commit(realm, event, users_to_notify)+ send_event_on_commit(realm, event, users_to_notify)def delete_messages(- msg_ids: list[int], realm: Realm, acting_user: UserProfile | None = None+ msg_ids: list[int],+ realm: Realm,+ skip_notify: bool = False,+ acting_user: UserProfile | None = None,) -> None:# Important note: This also deletes related objects with a foreign# key to Message (due to `on_delete=CASCADE` in our models@@ -432,6 +439,7 @@ def delete_messages(message_ids,stream=stream,topic=topic_name,+ skip_notify=skip_notify,acting_user=acting_user,)@@ -568,12 +576,16 @@ def move_messages_to_archive(message_ids: list[int],realm: Realm,chunk_size: int = MESSAGE_BATCH_SIZE,+ skip_notify: bool = False,acting_user: UserProfile | None = None,) -> None:"""- Callers using this to archive a large amount of messages should ideally make sure the message_ids are- ordered, as that'll allow better performance here by keeping the batches that'll be sent to the database- ordered rather than randomly scattered.+ Callers using this to archive a large amount of messages should+ send sorted message_ids, which can improve database performance by+ accessing adjacent blocks at the same time.++ If skip_notify=True is used, the caller is responsible for+ updating Stream.first_message_id when deleting channel messages."""count = 0# In order to avoid sending a massive list of message ids to the database,@@ -601,6 +613,7 @@ def move_messages_to_archive(message_ids=Literal(tuple(message_ids_chunk)),realm=realm,chunk_size=None,+ skip_notify=skip_notify,acting_user=acting_user,)# Clean up attachments:diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 9916911d7ee..2b39f8a42ce 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -2155,8 +2155,12 @@ paths:such as when it is [moved to a channel][message-move-channel] thatthe user does not [have permission to access][channel-access].- **Changes**: Before Zulip 9.0 (feature level 274), this- event was only sent to subscribers of the message's recipient.+ **Changes**: Prior to Zulip 12.0 (feature level 452) messages+ deleted via a message retention policy incorrectly failed to+ generate `delete_message` events.++ Before Zulip 9.0 (feature level 274), this event was only sent to+ subscribers of the message's recipient.Before Zulip 5.0 (feature level 77), eventsfor direct messages contained additional `sender_id` and