Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
actions: Implement do_delete_user_preserving_messages function.
Mateusz Manderaf3db618cb527
diff --git a/zerver/actions/users.py b/zerver/actions/users.pyindex 71d601643c4..c0a95cba6ab 100644--- a/zerver/actions/users.py+++ b/zerver/actions/users.py@@ -1,3 +1,4 @@+import secretsfrom collections import defaultdictfrom email.headerregistry import Addressfrom typing import Any, Dict, List, Optional@@ -23,6 +24,7 @@ from zerver.lib.user_counts import realm_user_count_by_rolefrom zerver.lib.user_groups import get_system_user_group_for_userfrom zerver.lib.users import get_active_bots_owned_by_userfrom zerver.models import (+ Message,Realm,RealmAuditLog,Recipient,@@ -97,6 +99,131 @@ def do_delete_user(user_profile: UserProfile, *, acting_user: Optional[UserProfi)+def do_delete_user_preserving_messages(user_profile: UserProfile) -> None:+ """This is a version of do_delete_user which does not delete messages+ that the user was a participant in, and thus is less potentially+ disruptive to other users.++ The code is a bit tricky, because we want to, at some point, call+ user_profile.delete() to trigger cascading deletions of related+ models - but we need to avoid the cascades deleting all messages+ sent by the user to avoid messing up history of public stream+ conversations that they may have participated in.++ Not recommended for general use due to the following quirks:+ * Does not live-update other clients via `send_event` about the+ user's new name, email, or other attributes.+ * Not guaranteed to clear caches containing the deleted users. The+ temporary user may be visible briefly in caches due to the+ UserProfile model's post_save hook.+ * Deletes `acting_user`/`modified_user` entries in RealmAuditLog,+ potentially leading to corruption in audit tables if the user had,+ for example, changed organization-level settings previously.+ * May violate invariants like deleting the only subscriber to a+ stream/group or the last owner in a realm.+ * Will remove MutedUser records for other users who might have+ muted this user.+ * Will destroy Attachment/ArchivedAttachment records for files+ uploaded by the user, making them inaccessible.+ * Will destroy ArchivedMessage records associated with the user,+ making them impossible to restore from backups.+ * Will destroy Reaction/Submessage objects for reactions/poll+ votes done by the user.++ Most of these issues are not relevant for the common case that the+ user being deleted hasn't used Zulip extensively.++ It is possible a different algorithm that worked via overwriting+ the UserProfile's values with RealmUserDefault values, as well as+ a targeted set of deletions of cascading models (`Subscription`,+ `UserMessage`, `CustomProfileFieldValue`, etc.) would be a cleaner+ path to a high quality system.++ Other lesser quirks to be aware of:+ * The deleted user will disappear from all "Read receipts"+ displays, as all UserMessage rows will have been deleted.+ * Raw Markdown syntax mentioning the user still contain their+ original name (though modern clients will look up the user via+ `data-user-id` and display the current name). This is hard to+ change, and not important, since nothing prevents other users from+ just typing the user's name in their own messages.+ * Consumes a user ID sequence number, resulting in gaps in the+ space of user IDs that contain actual users.++ """+ if user_profile.realm.is_zephyr_mirror_realm:+ raise AssertionError("Deleting zephyr mirror users is not supported")++ do_deactivate_user(user_profile, acting_user=None)++ user_id = user_profile.id+ personal_recipient = user_profile.recipient+ realm = user_profile.realm+ date_joined = user_profile.date_joined++ with transaction.atomic():+ # The strategy is that before calling user_profile.delete(), we need to+ # reassign Messages sent by the user to a dummy user, so that they don't+ # get affected by CASCADE. We cannot yet create a dummy user with .id+ # matching that of the user_profile, so the general scheme is:+ # 1. We create a *temporary* dummy for the initial re-assignment of messages.+ # 2. We delete the UserProfile.+ # 3. We create a replacement dummy user with its id matching what the UserProfile had.+ # 4. This is the intended, final replacement UserProfile, so we re-assign+ # the messages from step (1) to it and delete the temporary dummy.+ #+ # We also do the same for Subscriptions - while they could be handled like+ # in do_delete_user by re-creating the objects after CASCADE deletion, the code+ # is cleaner by using the same re-assignment approach for them together with Messages.+ random_token = secrets.token_hex(16)+ temp_replacement_user = create_user(+ email=f"temp_deleteduser{random_token}@{get_fake_email_domain(realm)}",+ password=None,+ realm=realm,+ full_name=f"Deleted User {user_id} (temp)",+ active=False,+ is_mirror_dummy=True,+ force_date_joined=date_joined,+ create_personal_recipient=False,+ )+ Message.objects.filter(sender=user_profile).update(sender=temp_replacement_user)+ Subscription.objects.filter(+ user_profile=user_profile, recipient__type=Recipient.HUDDLE+ ).update(user_profile=temp_replacement_user)+ user_profile.delete()++ replacement_user = create_user(+ force_id=user_id,+ email=f"deleteduser{user_id}@{get_fake_email_domain(realm)}",+ password=None,+ realm=realm,+ full_name=f"Deleted User {user_id}",+ active=False,+ is_mirror_dummy=True,+ force_date_joined=date_joined,+ create_personal_recipient=False,+ )+ # We don't delete the personal recipient to preserve personal messages!+ # Now, the personal recipient belong to replacement_user, because+ # personal_recipient.type_id is equal to replacement_user.id.+ replacement_user.recipient = personal_recipient+ replacement_user.save(update_fields=["recipient"])++ Message.objects.filter(sender=temp_replacement_user).update(sender=replacement_user)+ Subscription.objects.filter(+ user_profile=temp_replacement_user, recipient__type=Recipient.HUDDLE+ ).update(user_profile=replacement_user, is_user_active=replacement_user.is_active)+ temp_replacement_user.delete()++ RealmAuditLog.objects.create(+ realm=replacement_user.realm,+ modified_user=replacement_user,+ acting_user=None,+ event_type=RealmAuditLog.USER_DELETED_PRESERVING_MESSAGES,+ event_time=timezone_now(),+ )++def change_user_is_active(user_profile: UserProfile, value: bool) -> None:"""Helper function for changing the .is_active field. Not meant as a standalone functiondiff --git a/zerver/lib/create_user.py b/zerver/lib/create_user.pyindex 736d091ac5c..d54f4bebd9c 100644--- a/zerver/lib/create_user.py+++ b/zerver/lib/create_user.py@@ -161,6 +161,7 @@ def create_user(source_profile: Optional[UserProfile] = None,force_id: Optional[int] = None,force_date_joined: Optional[datetime] = None,+ create_personal_recipient: bool = True,enable_marketing_emails: Optional[bool] = None,email_address_visibility: Optional[int] = None,) -> UserProfile:@@ -227,6 +228,9 @@ def create_user(user_profile.email = get_display_email_address(user_profile)user_profile.save(update_fields=["email"])+ if not create_personal_recipient:+ return user_profile+recipient = Recipient.objects.create(type_id=user_profile.id, type=Recipient.PERSONAL)user_profile.recipient = recipientuser_profile.save(update_fields=["recipient"])diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 72865aa38b2..81251975ed5 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -25,6 +25,7 @@ from zerver.actions.users import (do_change_user_role,do_deactivate_user,do_delete_user,+ do_delete_user_preserving_messages,)from zerver.lib.avatar import avatar_url, get_gravatar_urlfrom zerver.lib.bulk_create import create_users@@ -2325,6 +2326,87 @@ class DeleteUserTest(ZulipTestCase):).exists())+ def test_do_delete_user_preserving_messages(self) -> None:+ """+ This test is extremely similar to the one for do_delete_user, with the only difference being+ that Messages are supposed to be preserved. All other effects should be identical.+ """++ realm = get_realm("zulip")+ cordelia = self.example_user("cordelia")+ othello = self.example_user("othello")+ hamlet = self.example_user("hamlet")+ hamlet_personal_recipient = hamlet.recipient+ hamlet_user_id = hamlet.id+ hamlet_date_joined = hamlet.date_joined++ self.send_personal_message(cordelia, hamlet)+ self.send_personal_message(hamlet, cordelia)++ personal_message_ids_to_hamlet = Message.objects.filter(+ recipient=hamlet_personal_recipient+ ).values_list("id", flat=True)+ self.assertGreater(len(personal_message_ids_to_hamlet), 0)+ self.assertTrue(Message.objects.filter(sender=hamlet).exists())++ huddle_message_ids_from_cordelia = [+ self.send_huddle_message(cordelia, [hamlet, othello]) for i in range(3)+ ]+ huddle_message_ids_from_hamlet = [+ self.send_huddle_message(hamlet, [cordelia, othello]) for i in range(3)+ ]++ huddle_with_hamlet_recipient_ids = list(+ Subscription.objects.filter(+ user_profile=hamlet, recipient__type=Recipient.HUDDLE+ ).values_list("recipient_id", flat=True)+ )+ self.assertGreater(len(huddle_with_hamlet_recipient_ids), 0)++ original_messages_from_hamlet_count = Message.objects.filter(+ sender_id=hamlet_user_id+ ).count()+ self.assertGreater(original_messages_from_hamlet_count, 0)++ do_delete_user_preserving_messages(hamlet)++ replacement_dummy_user = UserProfile.objects.get(id=hamlet_user_id, realm=realm)++ self.assertEqual(+ replacement_dummy_user.delivery_email, f"deleteduser{hamlet_user_id}@zulip.testserver"+ )+ self.assertEqual(replacement_dummy_user.is_mirror_dummy, True)+ self.assertEqual(replacement_dummy_user.is_active, False)+ self.assertEqual(replacement_dummy_user.date_joined, hamlet_date_joined)++ # All messages should have been preserved:+ self.assertEqual(+ Message.objects.filter(id__in=personal_message_ids_to_hamlet).count(),+ len(personal_message_ids_to_hamlet),+ )+ self.assertEqual(+ Message.objects.filter(id__in=huddle_message_ids_from_hamlet).count(),+ len(huddle_message_ids_from_hamlet),+ )+ self.assertEqual(+ Message.objects.filter(id__in=huddle_message_ids_from_cordelia).count(),+ len(huddle_message_ids_from_cordelia),+ )++ self.assertEqual(+ Message.objects.filter(sender_id=hamlet_user_id).count(),+ original_messages_from_hamlet_count,+ )++ # Verify that the dummy user is subscribed to the deleted user's huddles, to keep huddle data+ # in a correct state.+ for recipient_id in huddle_with_hamlet_recipient_ids:+ self.assertTrue(+ Subscription.objects.filter(+ user_profile=replacement_dummy_user, recipient_id=recipient_id+ ).exists()+ )+class FakeEmailDomainTest(ZulipTestCase):def test_get_fake_email_domain(self) -> None: