Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Add method to set the History TTL #681

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pyrogram/methods/chats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from .unpin_chat_message import UnpinChatMessage
from .update_chat_username import UpdateChatUsername
from .get_chat_online_count import GetChatOnlineCount
from .set_chat_history_ttl import SetChatHistoryTTL


class Chats(
Expand Down Expand Up @@ -94,6 +95,7 @@ class Chats(
UnpinAllChatMessages,
MarkChatUnread,
GetChatEventLog,
GetChatOnlineCount
GetChatOnlineCount,
SetChatHistoryTTL,
):
pass
71 changes: 71 additions & 0 deletions pyrogram/methods/chats/set_chat_history_ttl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2021 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 typing import Union, Optional

from pyrogram import raw
from pyrogram import types
from pyrogram.scaffold import Scaffold


class SetChatHistoryTTL(Scaffold):
async def set_chat_history_ttl(
self, chat_id: Union[int, str], period: int
) -> "types.Message":
"""Set the time-to-live for the chat history.

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

period (``int``):
The time-to-live for the chat history.
Either 86000 for 1 day, 604800 for 1 week or 0 (zero) to disable it.

Returns:
:obj:`~pyrogram.types.Message`: On success, the generated Service Message is returned.

Example:
.. code-block:: python

# One Day
app.set_chat_history_ttl(chat_id, 86400)

# A Week
app.set_chat_history_ttl(chat_id, 604800)

# Disabling
app.set_chat_history_ttl(chat_id, 0)
"""

r = await self.send(
raw.functions.messages.SetHistoryTTL(
peer=await self.resolve_peer(chat_id),
period=period,
)
)

for i in r.updates:
if isinstance(i, (raw.types.UpdateNewMessage,
raw.types.UpdateNewChannelMessage)):
return await types.Message._parse(
self,
i.message,
{i.id: i for i in r.users},
{i.id: i for i in r.chats},
)
9 changes: 8 additions & 1 deletion pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Message(Object, Update):
A service message has one and only one of these fields set: new_chat_members, left_chat_member,
new_chat_title, new_chat_photo, delete_chat_photo, group_chat_created, channel_chat_created,
migrate_to_chat_id, migrate_from_chat_id, pinned_message, game_high_score, voice_chat_started,
voice_chat_ended, voice_chat_scheduled, voice_chat_members_invited.
voice_chat_ended, voice_chat_scheduled, voice_chat_members_invited, chat_history_ttl.

media (``str``, *optional*):
The message is a media message. This field will contain the name of the media message.
Expand Down Expand Up @@ -352,6 +352,7 @@ def __init__(
voice_chat_started: "types.VoiceChatStarted" = None,
voice_chat_ended: "types.VoiceChatEnded" = None,
voice_chat_members_invited: "types.VoiceChatMembersInvited" = None,
chat_history_ttl: int = None,
reply_markup: Union[
"types.InlineKeyboardMarkup",
"types.ReplyKeyboardMarkup",
Expand Down Expand Up @@ -423,6 +424,7 @@ def __init__(
self.voice_chat_started = voice_chat_started
self.voice_chat_ended = voice_chat_ended
self.voice_chat_members_invited = voice_chat_members_invited
self.chat_history_ttl = chat_history_ttl

@staticmethod
async def _parse(
Expand Down Expand Up @@ -452,6 +454,7 @@ async def _parse(
voice_chat_started = None
voice_chat_ended = None
voice_chat_members_invited = None
chat_history_ttl = None

service_type = None

Expand Down Expand Up @@ -498,6 +501,9 @@ async def _parse(
elif isinstance(action, raw.types.MessageActionInviteToGroupCall):
voice_chat_members_invited = types.VoiceChatMembersInvited._parse(client, action, users)
service_type = "voice_chat_members_invited"
elif isinstance(action, raw.types.MessageActionSetMessagesTTL):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading this made me think that maybe we should rename the whole thing to set_chat_messages_ttl or just set_chat_ttl since the feature applies to new messages not to the whole history (or can be seen as a global chat setting regardless of messages or history). What do you think? There's also a ttl_period field for messages (see the raw Message object) which I guess it will be set for all new messages when ttl is enabled.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, set_chat_ttl(), Chat.set_ttl(), Message.chat_ttl_period.

chat_history_ttl = action.period
service_type = "chat_history_ttl"

user = utils.get_raw_peer_id(message.from_id) or utils.get_raw_peer_id(message.peer_id)
from_user = types.User._parse(client, users.get(user, None))
Expand All @@ -523,6 +529,7 @@ async def _parse(
voice_chat_started=voice_chat_started,
voice_chat_ended=voice_chat_ended,
voice_chat_members_invited=voice_chat_members_invited,
chat_history_ttl=chat_history_ttl,
client=client
# TODO: supergroup_chat_created
)
Expand Down
19 changes: 19 additions & 0 deletions pyrogram/types/user_and_chats/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,3 +979,22 @@ async def mark_unread(self, ) -> bool:
"""

return await self._client.mark_chat_unread(self.id)

async def set_history_ttl(self, period: int) -> "types.Message":
"""Bound method *set_history_ttl* of :obj:`~pyrogram.types.Chat`.

Use as a shortcut for:

.. code-block:: python

client.set_chat_history_ttl(chat_id, 604800)

Example:
.. code-block:: python

chat.set_history_ttl(604800)

Returns:
:obj:`~pyrogram.types.Message`: On success, the generated service message is returned.
"""
return await self._client.set_chat_history_ttl(self.id, period=period)