Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
129 lines
4.3 KiB
Python
129 lines
4.3 KiB
Python
from collections.abc import Iterable
|
|
from typing import TypedDict
|
|
|
|
from django.db.models import Q
|
|
from pydantic import BaseModel
|
|
|
|
from zerver.lib import retention
|
|
from zerver.lib.retention import move_messages_to_archive
|
|
from zerver.lib.streams import get_public_streams_queryset
|
|
from zerver.models import Message, Realm, Stream, UserProfile
|
|
from zerver.models.recipients import Recipient
|
|
from zerver.tornado.django_api import send_event_on_commit
|
|
|
|
|
|
class DeleteMessagesEvent(TypedDict, total=False):
|
|
type: str
|
|
message_ids: list[int]
|
|
message_type: str
|
|
topic: str
|
|
stream_id: int
|
|
|
|
|
|
class DeactivateUserActions(BaseModel):
|
|
delete_profile: bool = False
|
|
delete_public_channel_messages: bool = False
|
|
delete_private_channel_messages: bool = False
|
|
delete_direct_messages: bool = False
|
|
|
|
|
|
def check_update_first_message_id(
|
|
realm: Realm, stream: Stream, message_ids: list[int], users_to_notify: Iterable[int]
|
|
) -> None:
|
|
# This will not update the `first_message_id` of streams where the
|
|
# first message was deleted prior to the implementation of this function.
|
|
assert stream.recipient_id is not None
|
|
if stream.first_message_id not in message_ids:
|
|
return
|
|
current_first_message_id = (
|
|
Message.objects.filter(realm_id=realm.id, recipient_id=stream.recipient_id)
|
|
.values_list("id", flat=True)
|
|
.order_by("id")
|
|
.first()
|
|
)
|
|
|
|
stream.first_message_id = current_first_message_id
|
|
stream.save(update_fields=["first_message_id"])
|
|
|
|
stream_event = dict(
|
|
type="stream",
|
|
op="update",
|
|
property="first_message_id",
|
|
value=stream.first_message_id,
|
|
stream_id=stream.id,
|
|
name=stream.name,
|
|
)
|
|
send_event_on_commit(realm, stream_event, users_to_notify)
|
|
|
|
|
|
def do_delete_messages(
|
|
realm: Realm, messages: Iterable[Message], *, acting_user: UserProfile | None
|
|
) -> None:
|
|
message_ids = [message.id for message in messages]
|
|
move_messages_to_archive(
|
|
message_ids,
|
|
realm=realm,
|
|
chunk_size=retention.STREAM_MESSAGE_BATCH_SIZE,
|
|
acting_user=acting_user,
|
|
)
|
|
|
|
|
|
def do_delete_messages_by_sender(user: UserProfile, skip_notify: bool = False) -> None:
|
|
message_ids = list(
|
|
# Uses index: zerver_message_realm_sender_recipient (prefix)
|
|
Message.objects.filter(realm_id=user.realm_id, sender=user)
|
|
.values_list("id", flat=True)
|
|
.order_by("id")
|
|
)
|
|
move_messages_to_archive(
|
|
message_ids,
|
|
user.realm,
|
|
chunk_size=retention.STREAM_MESSAGE_BATCH_SIZE,
|
|
skip_notify=skip_notify,
|
|
)
|
|
|
|
|
|
def delete_deactivated_user_messages(
|
|
realm: Realm,
|
|
user_profile: UserProfile,
|
|
deactivate_user_actions: DeactivateUserActions,
|
|
acting_user: UserProfile | None,
|
|
) -> None:
|
|
delete_public_channel_messages = deactivate_user_actions.delete_public_channel_messages
|
|
delete_private_channel_messages = deactivate_user_actions.delete_private_channel_messages
|
|
delete_direct_messages = deactivate_user_actions.delete_direct_messages
|
|
|
|
if not (
|
|
delete_public_channel_messages or delete_private_channel_messages or delete_direct_messages
|
|
):
|
|
return
|
|
|
|
message_filter_query = Q()
|
|
message_exclude_query = Q()
|
|
|
|
if delete_direct_messages:
|
|
message_filter_query |= Q(recipient__type=Recipient.DIRECT_MESSAGE_GROUP)
|
|
|
|
public_channel_ids = get_public_streams_queryset(realm).values_list("id", flat=True)
|
|
if delete_public_channel_messages and delete_private_channel_messages:
|
|
message_filter_query |= Q(recipient__type=Recipient.STREAM)
|
|
elif delete_public_channel_messages:
|
|
message_filter_query |= Q(
|
|
recipient__type=Recipient.STREAM,
|
|
recipient__type_id__in=list(public_channel_ids),
|
|
)
|
|
elif delete_private_channel_messages:
|
|
message_filter_query |= Q(recipient__type=Recipient.STREAM)
|
|
message_exclude_query |= Q(
|
|
recipient__type=Recipient.STREAM,
|
|
recipient__type_id__in=list(public_channel_ids),
|
|
)
|
|
|
|
messages = list(
|
|
Message.objects.filter(sender=user_profile, realm_id=user_profile.realm_id)
|
|
.filter(message_filter_query)
|
|
.exclude(message_exclude_query)
|
|
.select_related("recipient")
|
|
)
|
|
do_delete_messages(user_profile.realm, messages, acting_user=acting_user)
|