From fb836a4f0aef233d2376857da114e87e54fc2dd1 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Thu, 27 Jun 2024 11:34:01 +0530 Subject: [PATCH] onboarding: Add 'OnboardingUserMessage' model. This prep commit adds a new OnboardingUserMessage model that will be used to mark the new onboarding messages for new users as unread and the first message of each onboarding topic as starred. This table won't include the old onboarding messages. --- zerver/lib/export.py | 1 + .../migrations/0542_onboardingusermessage.py | 38 +++++++++++++++++++ zerver/models/__init__.py | 1 + zerver/models/messages.py | 14 +++++++ zerver/tests/test_message_delete.py | 2 +- zerver/tests/test_retention.py | 2 +- 6 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 zerver/migrations/0542_onboardingusermessage.py diff --git a/zerver/lib/export.py b/zerver/lib/export.py index 44127a15db..be10b5b7d0 100644 --- a/zerver/lib/export.py +++ b/zerver/lib/export.py @@ -141,6 +141,7 @@ ALL_ZULIP_TABLES = { "zerver_multiuseinvite_streams", "zerver_namedusergroup", "zerver_onboardingstep", + "zerver_onboardingusermessage", "zerver_preregistrationrealm", "zerver_preregistrationuser", "zerver_preregistrationuser_streams", diff --git a/zerver/migrations/0542_onboardingusermessage.py b/zerver/migrations/0542_onboardingusermessage.py new file mode 100644 index 0000000000..848c72305a --- /dev/null +++ b/zerver/migrations/0542_onboardingusermessage.py @@ -0,0 +1,38 @@ +# Generated by Django 5.0.6 on 2024-06-27 06:03 + +import bitfield.models +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("zerver", "0541_alter_realmauditlog_options"), + ] + + operations = [ + migrations.CreateModel( + name="OnboardingUserMessage", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + ("flags", bitfield.models.BitField(["read", "historical", "starred"], default=0)), + ( + "message", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="zerver.message" + ), + ), + ( + "realm", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="zerver.realm" + ), + ), + ], + ), + ] diff --git a/zerver/models/__init__.py b/zerver/models/__init__.py index b6cf65b5a6..56cf65838c 100644 --- a/zerver/models/__init__.py +++ b/zerver/models/__init__.py @@ -26,6 +26,7 @@ from zerver.models.messages import ArchivedUserMessage as ArchivedUserMessage from zerver.models.messages import ArchiveTransaction as ArchiveTransaction from zerver.models.messages import Attachment as Attachment from zerver.models.messages import Message as Message +from zerver.models.messages import OnboardingUserMessage as OnboardingUserMessage from zerver.models.messages import Reaction as Reaction from zerver.models.messages import SubMessage as SubMessage from zerver.models.messages import UserMessage as UserMessage diff --git a/zerver/models/messages.py b/zerver/models/messages.py index ec5be1acaa..7f0199e7e4 100644 --- a/zerver/models/messages.py +++ b/zerver/models/messages.py @@ -770,3 +770,17 @@ class Attachment(AbstractAttachment): post_save.connect(flush_used_upload_space_cache, sender=Attachment) post_delete.connect(flush_used_upload_space_cache, sender=Attachment) + + +class OnboardingUserMessage(models.Model): + """ + Stores the message_id of new onboarding messages with the + flags data that should be copied while creating UserMessage + rows for a new user in 'add_new_user_history'. + """ + + realm = models.ForeignKey(Realm, on_delete=CASCADE) + message = models.ForeignKey(Message, on_delete=CASCADE) + + ALL_FLAGS = ["read", "historical", "starred"] + flags: BitHandler = BitField(flags=ALL_FLAGS, default=0) diff --git a/zerver/tests/test_message_delete.py b/zerver/tests/test_message_delete.py index 19102d2f13..2b5e310fb5 100644 --- a/zerver/tests/test_message_delete.py +++ b/zerver/tests/test_message_delete.py @@ -355,7 +355,7 @@ class DeleteMessageTest(ZulipTestCase): self.assertEqual(stream.first_message_id, message_ids[1]) all_messages = Message.objects.filter(id__in=message_ids) - with self.assert_database_query_count(23): + with self.assert_database_query_count(24): do_delete_messages(realm, all_messages) stream = get_stream(stream_name, realm) self.assertEqual(stream.first_message_id, None) diff --git a/zerver/tests/test_retention.py b/zerver/tests/test_retention.py index c019cbf47c..6266c48715 100644 --- a/zerver/tests/test_retention.py +++ b/zerver/tests/test_retention.py @@ -1143,7 +1143,7 @@ class TestDoDeleteMessages(ZulipTestCase): message_ids = [self.send_stream_message(cordelia, "Verona", str(i)) for i in range(10)] messages = Message.objects.filter(id__in=message_ids) - with self.assert_database_query_count(21): + with self.assert_database_query_count(22): do_delete_messages(realm, messages) self.assertFalse(Message.objects.filter(id__in=message_ids).exists())