user_message: Move create_historical_user_messages to zerver.lib.user_message.

It is not even used in zerver.actions.create_user.
This commit is contained in:
Alex Vandiver 2024-03-26 14:18:32 +00:00 committed by Tim Abbott
parent 19a7c75400
commit 52e3c8e1b2
4 changed files with 22 additions and 21 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,