diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.py index 27624c4cca..18c1d69464 100644 --- a/zerver/actions/create_user.py +++ b/zerver/actions/create_user.py @@ -67,24 +67,6 @@ MAX_NUM_ONBOARDING_UNREAD_MESSAGES = 20 # several weeks old and still be a good place to start. ONBOARDING_RECENT_TIMEDELTA = timedelta(weeks=12) -DEFAULT_HISTORICAL_FLAGS = UserMessage.flags.historical | UserMessage.flags.read - - -def create_historical_user_messages( - *, user_id: int, message_ids: Iterable[int], flags: int = DEFAULT_HISTORICAL_FLAGS -) -> None: - # Users can see and interact with messages sent to streams with - # public history for which they do not have a UserMessage because - # they were not a subscriber at the time the message was sent. - # In order to add emoji reactions or mutate message flags for - # those messages, we create UserMessage objects for those messages; - # these have the special historical flag which keeps track of the - # fact that the user did not receive the message at the time it was sent. - UserMessage.objects.bulk_create( - UserMessage(user_profile_id=user_id, message_id=message_id, flags=flags) - for message_id in message_ids - ) - def send_message_to_signup_notification_stream( sender: UserProfile, realm: Realm, message: str diff --git a/zerver/actions/message_flags.py b/zerver/actions/message_flags.py index 4b3f9abe45..81da8a4858 100644 --- a/zerver/actions/message_flags.py +++ b/zerver/actions/message_flags.py @@ -8,7 +8,6 @@ from django.utils.timezone import now as timezone_now from django.utils.translation import gettext as _ from analytics.lib.counts import COUNT_STATS, do_increment_logging_stat -from zerver.actions.create_user import DEFAULT_HISTORICAL_FLAGS, create_historical_user_messages from zerver.lib.exceptions import JsonableError from zerver.lib.message import ( bulk_access_messages, @@ -18,6 +17,7 @@ from zerver.lib.message import ( from zerver.lib.queue import queue_json_publish from zerver.lib.stream_subscription import get_subscribed_stream_recipient_ids_for_user from zerver.lib.topic import filter_by_topic_name_via_message +from zerver.lib.user_message import DEFAULT_HISTORICAL_FLAGS, create_historical_user_messages from zerver.models import Message, Recipient, UserMessage, UserProfile from zerver.tornado.django_api import send_event diff --git a/zerver/actions/reactions.py b/zerver/actions/reactions.py index 24b2ced9fd..2462b19ea1 100644 --- a/zerver/actions/reactions.py +++ b/zerver/actions/reactions.py @@ -1,6 +1,5 @@ from typing import Any, Dict, Optional -from zerver.actions.create_user import create_historical_user_messages from zerver.actions.user_topics import do_set_user_topic_visibility_policy from zerver.lib.emoji import check_emoji_request, get_emoji_data from zerver.lib.exceptions import ReactionExistsError @@ -13,6 +12,7 @@ from zerver.lib.message import ( from zerver.lib.message_cache import update_message_cache from zerver.lib.stream_subscription import subscriber_ids_with_stream_history_access from zerver.lib.streams import access_stream_by_id +from zerver.lib.user_message import create_historical_user_messages from zerver.models import Message, Reaction, Recipient, Stream, UserMessage, UserProfile from zerver.tornado.django_api import send_event_on_commit diff --git a/zerver/lib/user_message.py b/zerver/lib/user_message.py index 3ab86d1ec9..4c856fd4e1 100644 --- a/zerver/lib/user_message.py +++ b/zerver/lib/user_message.py @@ -1,4 +1,4 @@ -from typing import List +from typing import Iterable, List from django.db import connection from psycopg2.extras import execute_values @@ -23,6 +23,25 @@ class UserMessageLite: return UserMessage.flags_list_for_flags(self.flags) +DEFAULT_HISTORICAL_FLAGS = UserMessage.flags.historical | UserMessage.flags.read + + +def create_historical_user_messages( + *, user_id: int, message_ids: Iterable[int], flags: int = DEFAULT_HISTORICAL_FLAGS +) -> None: + # Users can see and interact with messages sent to streams with + # public history for which they do not have a UserMessage because + # they were not a subscriber at the time the message was sent. + # In order to add emoji reactions or mutate message flags for + # those messages, we create UserMessage objects for those messages; + # these have the special historical flag which keeps track of the + # fact that the user did not receive the message at the time it was sent. + UserMessage.objects.bulk_create( + UserMessage(user_profile_id=user_id, message_id=message_id, flags=flags) + for message_id in message_ids + ) + + def bulk_insert_ums(ums: List[UserMessageLite]) -> None: """ Doing bulk inserts this way is much faster than using Django,