mirror of https://github.com/zulip/zulip.git
actions: Create a separate message_delete.py file.
This is preparatory commit for #18941. Importing `do_delete_message` from `message_edit.py` was causing a circular import error. In order to avoid that, we create a separate message_delete.py file which has all the functions related to deleting messages. The tests for deleting messages are present in `zerver/tests/test_message_edit.py`. Fixes a part of #18941
This commit is contained in:
parent
7268060df3
commit
4303ba1efc
|
@ -0,0 +1,67 @@
|
|||
from typing import Iterable, List, TypedDict
|
||||
|
||||
from django.db import transaction
|
||||
|
||||
from zerver.lib import retention as retention
|
||||
from zerver.lib.retention import move_messages_to_archive
|
||||
from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_id
|
||||
from zerver.models import Message, Realm, UserMessage, UserProfile
|
||||
from zerver.tornado.django_api import send_event
|
||||
|
||||
|
||||
class DeleteMessagesEvent(TypedDict, total=False):
|
||||
type: str
|
||||
message_ids: List[int]
|
||||
message_type: str
|
||||
topic: str
|
||||
stream_id: int
|
||||
|
||||
|
||||
def do_delete_messages(realm: Realm, messages: Iterable[Message]) -> None:
|
||||
# messages in delete_message event belong to the same topic
|
||||
# or is a single private message, as any other behaviour is not possible with
|
||||
# the current callers to this method.
|
||||
messages = list(messages)
|
||||
message_ids = [message.id for message in messages]
|
||||
if not message_ids:
|
||||
return
|
||||
|
||||
event: DeleteMessagesEvent = {
|
||||
"type": "delete_message",
|
||||
"message_ids": message_ids,
|
||||
}
|
||||
|
||||
sample_message = messages[0]
|
||||
message_type = "stream"
|
||||
users_to_notify = []
|
||||
if not sample_message.is_stream_message():
|
||||
assert len(messages) == 1
|
||||
message_type = "private"
|
||||
ums = UserMessage.objects.filter(message_id__in=message_ids)
|
||||
users_to_notify = [um.user_profile_id for um in ums]
|
||||
archiving_chunk_size = retention.MESSAGE_BATCH_SIZE
|
||||
|
||||
if message_type == "stream":
|
||||
stream_id = sample_message.recipient.type_id
|
||||
event["stream_id"] = stream_id
|
||||
event["topic"] = sample_message.topic_name()
|
||||
subscriptions = get_active_subscriptions_for_stream_id(
|
||||
stream_id, include_deactivated_users=False
|
||||
)
|
||||
# We exclude long-term idle users, since they by definition have no active clients.
|
||||
subscriptions = subscriptions.exclude(user_profile__long_term_idle=True)
|
||||
users_to_notify = list(subscriptions.values_list("user_profile_id", flat=True))
|
||||
archiving_chunk_size = retention.STREAM_MESSAGE_BATCH_SIZE
|
||||
|
||||
move_messages_to_archive(message_ids, realm=realm, chunk_size=archiving_chunk_size)
|
||||
|
||||
event["message_type"] = message_type
|
||||
transaction.on_commit(lambda: send_event(realm, event, users_to_notify))
|
||||
|
||||
|
||||
def do_delete_messages_by_sender(user: UserProfile) -> None:
|
||||
message_ids = list(
|
||||
Message.objects.filter(sender=user).values_list("id", flat=True).order_by("id")
|
||||
)
|
||||
if message_ids:
|
||||
move_messages_to_archive(message_ids, chunk_size=retention.STREAM_MESSAGE_BATCH_SIZE)
|
|
@ -1,5 +1,5 @@
|
|||
import datetime
|
||||
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Set, TypedDict
|
||||
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Set
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
|
@ -9,6 +9,7 @@ from django.utils.translation import gettext as _
|
|||
from django.utils.translation import gettext_lazy
|
||||
from django.utils.translation import override as override_language
|
||||
|
||||
from zerver.actions.message_delete import DeleteMessagesEvent
|
||||
from zerver.actions.message_flags import do_update_mobile_push_notification
|
||||
from zerver.actions.message_send import (
|
||||
filter_presence_idle_user_ids,
|
||||
|
@ -18,7 +19,6 @@ from zerver.actions.message_send import (
|
|||
)
|
||||
from zerver.actions.uploads import check_attachment_reference_change
|
||||
from zerver.actions.user_topics import do_mute_topic, do_unmute_topic
|
||||
from zerver.lib import retention as retention
|
||||
from zerver.lib.exceptions import JsonableError
|
||||
from zerver.lib.markdown import MessageRenderingResult, topic_links
|
||||
from zerver.lib.markdown import version as markdown_version
|
||||
|
@ -32,7 +32,6 @@ from zerver.lib.message import (
|
|||
wildcard_mention_allowed,
|
||||
)
|
||||
from zerver.lib.queue import queue_json_publish
|
||||
from zerver.lib.retention import move_messages_to_archive
|
||||
from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_id
|
||||
from zerver.lib.stream_topic import StreamTopicTarget
|
||||
from zerver.lib.streams import access_stream_by_id, check_stream_access_based_on_stream_post_policy
|
||||
|
@ -337,14 +336,6 @@ def do_update_embedded_data(
|
|||
send_event(user_profile.realm, event, list(map(user_info, ums)))
|
||||
|
||||
|
||||
class DeleteMessagesEvent(TypedDict, total=False):
|
||||
type: str
|
||||
message_ids: List[int]
|
||||
message_type: str
|
||||
topic: str
|
||||
stream_id: int
|
||||
|
||||
|
||||
# We use transaction.atomic to support select_for_update in the attachment codepath.
|
||||
@transaction.atomic(savepoint=False)
|
||||
def do_update_message(
|
||||
|
@ -1038,53 +1029,3 @@ def check_update_message(
|
|||
queue_json_publish("embed_links", event_data)
|
||||
|
||||
return number_changed
|
||||
|
||||
|
||||
def do_delete_messages(realm: Realm, messages: Iterable[Message]) -> None:
|
||||
# messages in delete_message event belong to the same topic
|
||||
# or is a single private message, as any other behaviour is not possible with
|
||||
# the current callers to this method.
|
||||
messages = list(messages)
|
||||
message_ids = [message.id for message in messages]
|
||||
if not message_ids:
|
||||
return
|
||||
|
||||
event: DeleteMessagesEvent = {
|
||||
"type": "delete_message",
|
||||
"message_ids": message_ids,
|
||||
}
|
||||
|
||||
sample_message = messages[0]
|
||||
message_type = "stream"
|
||||
users_to_notify = []
|
||||
if not sample_message.is_stream_message():
|
||||
assert len(messages) == 1
|
||||
message_type = "private"
|
||||
ums = UserMessage.objects.filter(message_id__in=message_ids)
|
||||
users_to_notify = [um.user_profile_id for um in ums]
|
||||
archiving_chunk_size = retention.MESSAGE_BATCH_SIZE
|
||||
|
||||
if message_type == "stream":
|
||||
stream_id = sample_message.recipient.type_id
|
||||
event["stream_id"] = stream_id
|
||||
event["topic"] = sample_message.topic_name()
|
||||
subscriptions = get_active_subscriptions_for_stream_id(
|
||||
stream_id, include_deactivated_users=False
|
||||
)
|
||||
# We exclude long-term idle users, since they by definition have no active clients.
|
||||
subscriptions = subscriptions.exclude(user_profile__long_term_idle=True)
|
||||
users_to_notify = list(subscriptions.values_list("user_profile_id", flat=True))
|
||||
archiving_chunk_size = retention.STREAM_MESSAGE_BATCH_SIZE
|
||||
|
||||
move_messages_to_archive(message_ids, realm=realm, chunk_size=archiving_chunk_size)
|
||||
|
||||
event["message_type"] = message_type
|
||||
transaction.on_commit(lambda: send_event(realm, event, users_to_notify))
|
||||
|
||||
|
||||
def do_delete_messages_by_sender(user: UserProfile) -> None:
|
||||
message_ids = list(
|
||||
Message.objects.filter(sender=user).values_list("id", flat=True).order_by("id")
|
||||
)
|
||||
if message_ids:
|
||||
move_messages_to_archive(message_ids, chunk_size=retention.STREAM_MESSAGE_BATCH_SIZE)
|
||||
|
|
|
@ -9,7 +9,7 @@ from django.utils.timezone import now as timezone_now
|
|||
|
||||
from confirmation.models import Confirmation, create_confirmation_link, generate_key
|
||||
from zerver.actions.custom_profile_fields import do_remove_realm_custom_profile_fields
|
||||
from zerver.actions.message_edit import do_delete_messages_by_sender
|
||||
from zerver.actions.message_delete import do_delete_messages_by_sender
|
||||
from zerver.actions.user_groups import update_users_in_full_members_system_group
|
||||
from zerver.actions.user_settings import do_delete_avatar_image, send_user_email_update_event
|
||||
from zerver.lib.cache import flush_user_profile
|
||||
|
|
|
@ -45,11 +45,8 @@ from zerver.actions.invites import (
|
|||
do_revoke_multi_use_invite,
|
||||
do_revoke_user_invite,
|
||||
)
|
||||
from zerver.actions.message_edit import (
|
||||
do_delete_messages,
|
||||
do_update_embedded_data,
|
||||
do_update_message,
|
||||
)
|
||||
from zerver.actions.message_delete import do_delete_messages
|
||||
from zerver.actions.message_edit import do_update_embedded_data, do_update_message
|
||||
from zerver.actions.message_flags import do_update_message_flags
|
||||
from zerver.actions.muted_users import do_mute_user, do_unmute_user
|
||||
from zerver.actions.presence import do_update_user_presence, do_update_user_status
|
||||
|
|
|
@ -10,7 +10,7 @@ from django.utils.html import escape
|
|||
from pyoembed.providers import get_provider
|
||||
from requests.exceptions import ConnectionError
|
||||
|
||||
from zerver.actions.message_edit import do_delete_messages
|
||||
from zerver.actions.message_delete import do_delete_messages
|
||||
from zerver.lib.cache import cache_delete, cache_get, preview_url_cache_key
|
||||
from zerver.lib.camo import get_camo_url
|
||||
from zerver.lib.queue import queue_json_publish
|
||||
|
|
|
@ -7,9 +7,9 @@ import orjson
|
|||
from django.db import IntegrityError
|
||||
from django.utils.timezone import now as timezone_now
|
||||
|
||||
from zerver.actions.message_delete import do_delete_messages
|
||||
from zerver.actions.message_edit import (
|
||||
check_update_message,
|
||||
do_delete_messages,
|
||||
do_update_message,
|
||||
get_mentions_for_message_updates,
|
||||
)
|
||||
|
|
|
@ -23,7 +23,7 @@ from requests.models import PreparedRequest
|
|||
|
||||
from analytics.lib.counts import CountStat, LoggingCountStat
|
||||
from analytics.models import InstallationCount, RealmCount
|
||||
from zerver.actions.message_edit import do_delete_messages
|
||||
from zerver.actions.message_delete import do_delete_messages
|
||||
from zerver.actions.message_flags import do_mark_stream_messages_as_read, do_update_message_flags
|
||||
from zerver.actions.user_settings import do_regenerate_api_key
|
||||
from zerver.lib.avatar import absolute_avatar_url
|
||||
|
|
|
@ -6,7 +6,7 @@ from django.conf import settings
|
|||
from django.utils.timezone import now as timezone_now
|
||||
|
||||
from zerver.actions.create_realm import do_create_realm
|
||||
from zerver.actions.message_edit import do_delete_messages
|
||||
from zerver.actions.message_delete import do_delete_messages
|
||||
from zerver.actions.message_send import internal_send_private_message
|
||||
from zerver.actions.realm_settings import do_set_realm_property
|
||||
from zerver.actions.submessage import do_add_submessage
|
||||
|
|
|
@ -21,7 +21,7 @@ from urllib3 import encode_multipart_formdata
|
|||
|
||||
import zerver.lib.upload
|
||||
from zerver.actions.create_realm import do_create_realm
|
||||
from zerver.actions.message_edit import do_delete_messages
|
||||
from zerver.actions.message_delete import do_delete_messages
|
||||
from zerver.actions.message_send import internal_send_private_message
|
||||
from zerver.actions.realm_icon import do_change_icon_source
|
||||
from zerver.actions.realm_logo import do_change_logo_source
|
||||
|
|
|
@ -8,7 +8,8 @@ from django.http import HttpRequest, HttpResponse
|
|||
from django.utils.timezone import now as timezone_now
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from zerver.actions.message_edit import check_update_message, do_delete_messages
|
||||
from zerver.actions.message_delete import do_delete_messages
|
||||
from zerver.actions.message_edit import check_update_message
|
||||
from zerver.context_processors import get_valid_realm_from_request
|
||||
from zerver.lib.exceptions import JsonableError
|
||||
from zerver.lib.html_diff import highlight_html_differences
|
||||
|
|
|
@ -21,7 +21,7 @@ from zerver.actions.default_streams import (
|
|||
do_remove_streams_from_default_stream_group,
|
||||
get_default_streams_for_realm,
|
||||
)
|
||||
from zerver.actions.message_edit import do_delete_messages
|
||||
from zerver.actions.message_delete import do_delete_messages
|
||||
from zerver.actions.message_send import (
|
||||
do_send_messages,
|
||||
internal_prep_private_message,
|
||||
|
|
Loading…
Reference in New Issue