Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

create_user: Mark only tracked onboarding topic messages as unread.

Prakhar Pratyush3c5dc73f50fb
diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.py
index 7f652492b33..0593673b388 100644
--- a/zerver/actions/create_user.py
+++ b/zerver/actions/create_user.py
@@ -43,6 +43,7 @@ from zerver.models import (
Message,
NamedUserGroup,
OnboardingStep,
+ OnboardingUserMessage,
PreregistrationRealm,
PreregistrationUser,
Realm,
@@ -176,12 +177,15 @@ def add_new_user_history(
realm_creation: bool = False,
) -> None:
"""
- Give the user some messages in their feed, so that they can learn how to
- use the home view in a realistic way after finishing the tutorial.
+ Give the user some messages in their feed, so that they can learn
+ how to use the home view in a realistic way.
- Mark the very most recent messages as unread.
+ For realms having older onboarding messages, mark the very
+ most recent messages as unread. Otherwise, ONLY mark the
+ messages tracked in 'OnboardingUserMessage' as unread.
"""
+ realm = user_profile.realm
# Find recipient ids for the user's streams, limiting to just
# those where we can access the streams' full history.
#
@@ -196,7 +200,7 @@ def add_new_user_history(
recent_message_ids = set(
Message.objects.filter(
# Uses index: zerver_message_realm_recipient_id
- realm_id=user_profile.realm_id,
+ realm_id=realm.id,
recipient_id__in=recipient_ids,
date_sent__gt=cutoff_date,
)
@@ -204,9 +208,17 @@ def add_new_user_history(
.values_list("id", flat=True)[0:MAX_NUM_RECENT_MESSAGES]
)
- if len(recent_message_ids) > 0:
+ tracked_onboarding_message_ids = set()
+ onboarding_user_messages_queryset = OnboardingUserMessage.objects.filter(realm_id=realm.id)
+ for onboarding_user_message in onboarding_user_messages_queryset:
+ tracked_onboarding_message_ids.add(onboarding_user_message.message_id)
+ tracked_onboarding_messages_exist = len(tracked_onboarding_message_ids) > 0
+
+ message_history_ids = recent_message_ids.union(tracked_onboarding_message_ids)
+
+ if len(message_history_ids) > 0:
# Handle the race condition where a message arrives between
- # bulk_add_subscriptions above and the Message query just above
+ # bulk_add_subscriptions above and the recent message query just above
already_used_ids = set(
UserMessage.objects.filter(
message_id__in=recent_message_ids, user_profile=user_profile
@@ -214,11 +226,13 @@ def add_new_user_history(
)
# Exclude the already-used ids and sort them.
- backfill_message_ids = sorted(recent_message_ids - already_used_ids)
+ backfill_message_ids = sorted(message_history_ids - already_used_ids)
# Find which message ids we should mark as read.
# (We don't want too many unread messages.)
- older_message_ids = set(backfill_message_ids[:-MAX_NUM_RECENT_UNREAD_MESSAGES])
+ older_message_ids = set()
+ if not tracked_onboarding_messages_exist:
+ older_message_ids = set(backfill_message_ids[:-MAX_NUM_RECENT_UNREAD_MESSAGES])
# Create UserMessage rows for the backfill.
ums_to_create = []
@@ -228,7 +242,12 @@ def add_new_user_history(
# They are not marked as historical.
if not realm_creation:
um.flags = UserMessage.flags.historical
- if message_id in older_message_ids:
+ if (
+ tracked_onboarding_messages_exist
+ and message_id not in tracked_onboarding_message_ids
+ ):
+ um.flags |= UserMessage.flags.read
+ if not tracked_onboarding_messages_exist and message_id in older_message_ids:
um.flags |= UserMessage.flags.read
ums_to_create.append(um)
diff --git a/zerver/tests/test_invite.py b/zerver/tests/test_invite.py
index 99f87dfa8a7..903fd7d07f0 100644
--- a/zerver/tests/test_invite.py
+++ b/zerver/tests/test_invite.py
@@ -109,7 +109,7 @@ class StreamSetupTest(ZulipTestCase):
new_user = self.create_simple_new_user(realm, "[email protected]")
- with self.assert_database_query_count(13):
+ with self.assert_database_query_count(14):
set_up_streams_for_new_human_user(
user_profile=new_user,
prereg_user=None,
@@ -145,7 +145,7 @@ class StreamSetupTest(ZulipTestCase):
new_user = self.create_simple_new_user(realm, new_user_email)
- with self.assert_database_query_count(13):
+ with self.assert_database_query_count(14):
set_up_streams_for_new_human_user(
user_profile=new_user,
prereg_user=prereg_user,
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py
index dd21f77d730..f1e88f141e1 100644
--- a/zerver/tests/test_signup.py
+++ b/zerver/tests/test_signup.py
@@ -19,7 +19,7 @@ from django.utils import translation
from confirmation import settings as confirmation_settings
from confirmation.models import Confirmation, one_click_unsubscribe_link
from zerver.actions.create_realm import do_change_realm_subdomain, do_create_realm
-from zerver.actions.create_user import add_new_user_history
+from zerver.actions.create_user import add_new_user_history, do_create_user
from zerver.actions.default_streams import do_add_default_stream, do_create_default_stream_group
from zerver.actions.realm_settings import (
do_deactivate_realm,
@@ -65,6 +65,7 @@ from zerver.models import (
CustomProfileFieldValue,
DefaultStream,
Message,
+ OnboardingUserMessage,
PreregistrationRealm,
PreregistrationUser,
Realm,
@@ -314,6 +315,55 @@ class AddNewUserHistoryTest(ZulipTestCase):
self.assertTrue(msg.flags.read.is_set)
self.assertTrue(msg.flags.historical.is_set)
+ def test_only_tracked_onboarding_messages_marked_unread(self) -> None:
+ """
+ Realms with tracked onboarding messages have only
+ those messages marked as unread.
+ """
+ realm = do_create_realm("realm_string_id", "realm name")
+ hamlet = do_create_user(
+ "hamlet", "password", realm, "hamlet", realm_creation=True, acting_user=None
+ )
+ stream = Stream.objects.get(realm=realm, name=str(realm.ZULIP_SANDBOX_CHANNEL_NAME))
+
+ # Onboarding messages sent during realm creation are tracked.
+ self.assertTrue(OnboardingUserMessage.objects.filter(realm=realm).exists())
+ onboarding_message_ids = OnboardingUserMessage.objects.filter(realm=realm).values_list(
+ "message_id", flat=True
+ )
+
+ # Other messages sent before a new user joins.
+ for i in range(3):
+ self.send_stream_message(hamlet, stream.name, f"test {i}")
+
+ new_user = do_create_user("new_user", "password", realm, "new_user", acting_user=None)
+
+ # The onboarding messages are in the user's history and marked unread.
+ onboarding_user_messages = UserMessage.objects.filter(
+ user_profile=new_user, message_id__in=onboarding_message_ids
+ )
+ for user_message in onboarding_user_messages:
+ self.assertFalse(user_message.flags.read.is_set)
+ self.assertTrue(user_message.flags.historical.is_set)
+
+ # Other messages are in user's history and marked as read.
+ other_user_messages = UserMessage.objects.filter(
+ user_profile=new_user, message__recipient__type=Recipient.STREAM
+ ).exclude(message_id__in=onboarding_message_ids)
+ self.assertTrue(other_user_messages.exists())
+ for user_message in other_user_messages:
+ self.assertTrue(user_message.flags.read.is_set)
+ self.assertTrue(user_message.flags.historical.is_set)
+
+ # Onboarding messages for hamlet (realm creator) are not
+ # marked as historical.
+ onboarding_user_messages = UserMessage.objects.filter(
+ user_profile=hamlet, message_id__in=onboarding_message_ids
+ )
+ for user_message in onboarding_user_messages:
+ self.assertFalse(user_message.flags.read.is_set)
+ self.assertFalse(user_message.flags.historical.is_set)
+
def test_auto_subbed_to_personals(self) -> None:
"""
Newly created users are auto-subbed to the ability to receive
@@ -946,7 +996,7 @@ class LoginTest(ZulipTestCase):
# seem to be any O(N) behavior. Some of the cache hits are related
# to sending messages, such as getting the welcome bot, looking up
# the alert words for a realm, etc.
- with self.assert_database_query_count(91), self.assert_memcached_count(14):
+ with self.assert_database_query_count(92), self.assert_memcached_count(14):
with self.captureOnCommitCallbacks(execute=True):
self.register(self.nonreg_email("test"), "test")
diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py
index 045340863a6..54861714d81 100644
--- a/zerver/tests/test_users.py
+++ b/zerver/tests/test_users.py
@@ -908,7 +908,7 @@ class QueryCountTest(ZulipTestCase):
prereg_user = PreregistrationUser.objects.get(email="[email protected]")
- with self.assert_database_query_count(81):
+ with self.assert_database_query_count(82):
with self.assert_memcached_count(19):
with self.capture_send_event_calls(expected_num_events=10) as events:
fred = do_create_user(