From a89f95b7697b8c18a0871168dadfe5373bd4b500 Mon Sep 17 00:00:00 2001 From: Zaid _ Date: Fri, 21 Jun 2024 02:45:59 +0300 Subject: [PATCH 1/7] add ChatBackground --- hydrogram/types/user_and_chats/__init__.py | 2 + .../types/user_and_chats/chat_background.py | 113 ++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 hydrogram/types/user_and_chats/chat_background.py diff --git a/hydrogram/types/user_and_chats/__init__.py b/hydrogram/types/user_and_chats/__init__.py index 791bfa03f..6b0d8fd1f 100644 --- a/hydrogram/types/user_and_chats/__init__.py +++ b/hydrogram/types/user_and_chats/__init__.py @@ -50,6 +50,7 @@ from .video_chat_members_invited import VideoChatMembersInvited from .video_chat_scheduled import VideoChatScheduled from .video_chat_started import VideoChatStarted +from .chat_background import ChatBackground __all__ = [ "Chat", @@ -85,4 +86,5 @@ "VideoChatMembersInvited", "VideoChatScheduled", "VideoChatStarted", + "ChatBackground" ] diff --git a/hydrogram/types/user_and_chats/chat_background.py b/hydrogram/types/user_and_chats/chat_background.py new file mode 100644 index 000000000..015135360 --- /dev/null +++ b/hydrogram/types/user_and_chats/chat_background.py @@ -0,0 +1,113 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# 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 . + +from datetime import datetime +from typing import List + +import hydrogram +from hydrogram import raw, utils +from hydrogram import types +from hydrogram.file_id import ( + FileId, + FileType, + FileUniqueId, + FileUniqueType, + ThumbnailSource, +) +from ..object import Object + + +class ChatBackground(Object): + """Describes a background set for a specific chat. + + Parameters: + file_id (``str``): + Identifier for this file, which can be used to download the file. + + file_unique_id (``str``): + Unique identifier for this file, which is supposed to be the same over time and for different accounts. + Can't be used to download or reuse the file. + + file_size (``int``): + File size. + + date (:py:obj:`~datetime.datetime`): + Date the background was setted. + + slug (``str``): + Identifier of the background code. + You can combine it with `https://t.me/bg/{slug}` + to get link for this background. + + thumbs (List of :obj:`~pyrogram.types.Thumbnail`, *optional*): + Available thumbnails of this background. + + link (``str``, *property*): + Generate a link to this background code. + """ + + def __init__( + self, + *, + client: "hydrogram.Client" = None, + file_id: str, + file_unique_id: str, + file_size: int, + date: datetime, + slug: str, + thumbs: List["types.Thumbnail"] = None, + ): + super().__init__(client) + + self.file_id = file_id + self.file_unique_id = file_unique_id + self.file_size = file_size + self.date = date + self.slug = slug + self.thumbs = thumbs + + @property + def link(self) -> str: + return f"https://t.me/bg/{self.slug}" + + @staticmethod + def _parse( + client, + wallpaper: "raw.types.Wallpaper", + ) -> "ChatBackground": + return ChatBackground( + file_id=FileId( + dc_id=wallpaper.document.dc_id, + file_reference=wallpaper.document.file_reference, + access_hash=wallpaper.document.access_hash, + file_type=FileType.BACKGROUND, + media_id=wallpaper.document.id, + volume_id=0, + local_id=0, + thumbnail_source=ThumbnailSource.THUMBNAIL, + thumbnail_file_type=FileType.BACKGROUND, + ).encode(), + file_unique_id=FileUniqueId( + file_unique_type=FileUniqueType.DOCUMENT, media_id=wallpaper.document.id + ).encode(), + file_size=wallpaper.document.size, + slug=wallpaper.slug, + date=utils.timestamp_to_datetime(wallpaper.document.date), + thumbs=types.Thumbnail._parse(client, wallpaper.document), + client=client, + ) \ No newline at end of file From f07a23ff9e3e16cfd6fb392985bc6e0dd9a5eed4 Mon Sep 17 00:00:00 2001 From: Zaid _ Date: Fri, 21 Jun 2024 02:52:20 +0300 Subject: [PATCH 2/7] add .background field --- hydrogram/types/user_and_chats/chat.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/hydrogram/types/user_and_chats/chat.py b/hydrogram/types/user_and_chats/chat.py index 54de552b2..69d277eeb 100644 --- a/hydrogram/types/user_and_chats/chat.py +++ b/hydrogram/types/user_and_chats/chat.py @@ -115,6 +115,9 @@ class Chat(Object): Pinned message, for groups, supergroups channels and own chat. Returned only in :meth:`~hydrogram.Client.get_chat`. + background (:obj:`~hydrogram.types.ChatBackground`, *optional*): + A chat background. + sticker_set_name (``str``, *optional*): For supergroups, name of group sticker set. Returned only in :meth:`~hydrogram.Client.get_chat`. @@ -178,6 +181,7 @@ def __init__( has_protected_content: bool | None = None, invite_link: str | None = None, pinned_message=None, + background: types.ChatBackground | None = None, sticker_set_name: str | None = None, can_set_sticker_set: bool | None = None, members_count: int | None = None, @@ -213,6 +217,7 @@ def __init__( self.has_protected_content = has_protected_content self.invite_link = invite_link self.pinned_message = pinned_message + self.background = background self.sticker_set_name = sticker_set_name self.can_set_sticker_set = can_set_sticker_set self.members_count = members_count @@ -346,6 +351,9 @@ async def _parse_full( parsed_chat.pinned_message = await client.get_messages( parsed_chat.id, message_ids=full_user.pinned_msg_id ) + + if getattr(full_user, "wallpaper", None): + parsed_chat.background = types.ChatBackground._parse(client, full_user.wallpaper) else: full_chat = chat_full.full_chat chat_raw = chats[full_chat.id] @@ -375,6 +383,9 @@ async def _parse_full( parsed_chat.send_as_chat = Chat._parse_chat(client, send_as_raw) + if getattr(full_chat, "wallpaper", None): + parsed_chat.background = types.ChatBackground._parse(client, full_chat.wallpaper) + parsed_chat.description = full_chat.about or None if full_chat.pinned_msg_id: From eec5ec8dfc0dd3b5d223c101f7925205d69598b8 Mon Sep 17 00:00:00 2001 From: Zaid _ Date: Fri, 21 Jun 2024 02:53:19 +0300 Subject: [PATCH 3/7] fix type hint --- hydrogram/types/user_and_chats/chat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hydrogram/types/user_and_chats/chat.py b/hydrogram/types/user_and_chats/chat.py index 69d277eeb..e8d291b7f 100644 --- a/hydrogram/types/user_and_chats/chat.py +++ b/hydrogram/types/user_and_chats/chat.py @@ -180,7 +180,7 @@ def __init__( dc_id: int | None = None, has_protected_content: bool | None = None, invite_link: str | None = None, - pinned_message=None, + pinned_message: types.Message | None = None, background: types.ChatBackground | None = None, sticker_set_name: str | None = None, can_set_sticker_set: bool | None = None, From c2802f98e2ff63da3331cca2a10efd7d7ebdc25e Mon Sep 17 00:00:00 2001 From: Zaid _ Date: Fri, 21 Jun 2024 02:54:23 +0300 Subject: [PATCH 4/7] update docs --- compiler/docs/compiler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 0e11175a7..558304aff 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -391,6 +391,7 @@ def get_title_list(s: str) -> list: Dialog Restriction EmojiStatus + ChatBackground """, "messages_media": """ Messages & Media From 4fe419ccf203cd58aa100376ac10ba5bb2fcf234 Mon Sep 17 00:00:00 2001 From: Zaid _ Date: Fri, 21 Jun 2024 17:17:16 +0300 Subject: [PATCH 5/7] reformat --- hydrogram/types/user_and_chats/__init__.py | 2 +- hydrogram/types/user_and_chats/chat_background.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hydrogram/types/user_and_chats/__init__.py b/hydrogram/types/user_and_chats/__init__.py index 6b0d8fd1f..9f2a41743 100644 --- a/hydrogram/types/user_and_chats/__init__.py +++ b/hydrogram/types/user_and_chats/__init__.py @@ -86,5 +86,5 @@ "VideoChatMembersInvited", "VideoChatScheduled", "VideoChatStarted", - "ChatBackground" + "ChatBackground", ] diff --git a/hydrogram/types/user_and_chats/chat_background.py b/hydrogram/types/user_and_chats/chat_background.py index 015135360..3ed0dcb01 100644 --- a/hydrogram/types/user_and_chats/chat_background.py +++ b/hydrogram/types/user_and_chats/chat_background.py @@ -110,4 +110,4 @@ def _parse( date=utils.timestamp_to_datetime(wallpaper.document.date), thumbs=types.Thumbnail._parse(client, wallpaper.document), client=client, - ) \ No newline at end of file + ) From b3382e980abcb71e9c342ed14031a12359abc672 Mon Sep 17 00:00:00 2001 From: Zaid _ Date: Sat, 22 Jun 2024 22:33:36 +0300 Subject: [PATCH 6/7] update copyright and import --- hydrogram/types/user_and_chats/chat_background.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/hydrogram/types/user_and_chats/chat_background.py b/hydrogram/types/user_and_chats/chat_background.py index 3ed0dcb01..291e28500 100644 --- a/hydrogram/types/user_and_chats/chat_background.py +++ b/hydrogram/types/user_and_chats/chat_background.py @@ -1,20 +1,20 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-present Dan +# Hydrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2023-present Hydrogram # -# This file is part of Pyrogram. +# This file is part of Hydrogram. # -# Pyrogram is free software: you can redistribute it and/or modify +# Hydrogram 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, +# Hydrogram 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 . +# along with Hydrogram. If not, see . from datetime import datetime from typing import List @@ -29,7 +29,7 @@ FileUniqueType, ThumbnailSource, ) -from ..object import Object +from hydrogram.types.object import Object class ChatBackground(Object): From 3c773f931a940347ddc4f2ee1f92bb322a550235 Mon Sep 17 00:00:00 2001 From: Alisson Lauffer Date: Sun, 30 Jun 2024 15:12:59 -0300 Subject: [PATCH 7/7] chore: enable `annotations` future import and lint --- hydrogram/types/user_and_chats/__init__.py | 4 ++-- .../types/user_and_chats/chat_background.py | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/hydrogram/types/user_and_chats/__init__.py b/hydrogram/types/user_and_chats/__init__.py index 9f2a41743..72d65abfe 100644 --- a/hydrogram/types/user_and_chats/__init__.py +++ b/hydrogram/types/user_and_chats/__init__.py @@ -19,6 +19,7 @@ from .chat import Chat from .chat_admin_with_invite_links import ChatAdminWithInviteLinks +from .chat_background import ChatBackground from .chat_event import ChatEvent from .chat_event_filter import ChatEventFilter from .chat_invite_link import ChatInviteLink @@ -50,11 +51,11 @@ from .video_chat_members_invited import VideoChatMembersInvited from .video_chat_scheduled import VideoChatScheduled from .video_chat_started import VideoChatStarted -from .chat_background import ChatBackground __all__ = [ "Chat", "ChatAdminWithInviteLinks", + "ChatBackground", "ChatEvent", "ChatEventFilter", "ChatInviteLink", @@ -86,5 +87,4 @@ "VideoChatMembersInvited", "VideoChatScheduled", "VideoChatStarted", - "ChatBackground", ] diff --git a/hydrogram/types/user_and_chats/chat_background.py b/hydrogram/types/user_and_chats/chat_background.py index 291e28500..10be00ac7 100644 --- a/hydrogram/types/user_and_chats/chat_background.py +++ b/hydrogram/types/user_and_chats/chat_background.py @@ -16,12 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Hydrogram. If not, see . -from datetime import datetime -from typing import List +from __future__ import annotations + +from typing import TYPE_CHECKING import hydrogram -from hydrogram import raw, utils -from hydrogram import types +from hydrogram import raw, types, utils from hydrogram.file_id import ( FileId, FileType, @@ -31,6 +31,9 @@ ) from hydrogram.types.object import Object +if TYPE_CHECKING: + from datetime import datetime + class ChatBackground(Object): """Describes a background set for a specific chat. @@ -64,13 +67,13 @@ class ChatBackground(Object): def __init__( self, *, - client: "hydrogram.Client" = None, + client: hydrogram.Client = None, file_id: str, file_unique_id: str, file_size: int, date: datetime, slug: str, - thumbs: List["types.Thumbnail"] = None, + thumbs: list[types.Thumbnail] | None = None, ): super().__init__(client) @@ -88,8 +91,8 @@ def link(self) -> str: @staticmethod def _parse( client, - wallpaper: "raw.types.Wallpaper", - ) -> "ChatBackground": + wallpaper: raw.types.Wallpaper, + ) -> ChatBackground: return ChatBackground( file_id=FileId( dc_id=wallpaper.document.dc_id,