Source code for pyrogram.methods.messages.delete_chat_history

#  Pyrogram - Telegram MTProto API Client Library for Python
#  Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
#  This file is part of Pyrogram.
#
#  Pyrogram is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Pyrogram is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with Pyrogram.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

import logging

import pyrogram
from pyrogram import raw, utils
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from datetime import datetime, timedelta

log = logging.getLogger(__name__)


class DeleteChatHistory:
    async def delete_chat_history(
        self: pyrogram.Client,
        chat_id: int | str,
        max_id: int = 0,
        revoke: bool | None = None,
        just_clear=None,
        min_date: datetime | timedelta | None = None,
        max_date: datetime | timedelta | None = None,
    ) -> int:
        """Delete the history of a chat.

        .. include:: /_includes/usable-by/users.rst

        Parameters:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            max_id (``int``, *optional*):
                Maximum ID of message to delete.

            revoke (``bool``, *optional*):
                Deletes messages history for everyone.
                Required ``True`` if using in channel.

            just_clear (``bool``, *optional*):
                If True, clear history for the current user, without actually removing chat.
                For private and simple group chats only.

            min_date (:py:obj:`~datetime.datetime`, *optional*):
                Delete all messages newer than this time.
                For private and simple group chats only.

            max_date (:py:obj:`~datetime.datetime`, *optional*):
                Delete all messages older than this time.
                For private and simple group chats only.

        Returns:
            ``int``: Amount of affected messages. A channel or a supergroup counts
            the messages the server named as deleted, which is none when it hid the
            history instead of deleting it, as it does unless *revoke* is set.

        Example:
            .. code-block:: python

                # Delete all messages in channel
                await app.delete_chat_history(chat_id, revoke=True)
        """
        peer = await self.resolve_peer(chat_id)

        if isinstance(peer, (raw.types.InputPeerChannel, raw.types.InputPeerChannelFromMessage)):
            r = await self.invoke(
                raw.functions.channels.DeleteHistory(
                    channel=utils.get_input_channel(peer),
                    max_id=max_id,
                    for_everyone=revoke,
                )
            )
        else:
            rpc = raw.functions.messages.DeleteHistory(
                peer=peer,
                max_id=max_id,
                just_clear=just_clear,
                revoke=revoke,
                min_date=utils.datetime_to_timestamp(min_date),
                max_date=utils.datetime_to_timestamp(max_date),
            )

            affected = 0

            while True:
                r = await self.invoke(rpc)
                affected += r.pts_count

                if not r.offset:
                    break

            return affected

        return sum(
            len(update.messages)
            for update in getattr(r, "updates", [])
            if isinstance(update, raw.types.UpdateDeleteChannelMessages)
        )