From 2cd117f3012524847483a6798b98ae3bfeb93564 Mon Sep 17 00:00:00 2001 From: wulan17 Date: Sat, 8 Apr 2023 01:48:20 +0700 Subject: [PATCH] PyroFork: Add usernames field to Chat Signed-off-by: wulan17 --- pyrogram/types/user_and_chats/__init__.py | 2 + pyrogram/types/user_and_chats/chat.py | 22 ++++++++- pyrogram/types/user_and_chats/username.py | 58 +++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 pyrogram/types/user_and_chats/username.py diff --git a/pyrogram/types/user_and_chats/__init__.py b/pyrogram/types/user_and_chats/__init__.py index 981a5c77..8d2834c3 100644 --- a/pyrogram/types/user_and_chats/__init__.py +++ b/pyrogram/types/user_and_chats/__init__.py @@ -36,6 +36,7 @@ from .emoji_status import EmojiStatus from .invite_link_importer import InviteLinkImporter from .restriction import Restriction from .user import User +from .username import Username from .forum_topic import ForumTopic from .forum_topic_created import ForumTopicCreated from .forum_topic_closed import ForumTopicClosed @@ -58,6 +59,7 @@ __all__ = [ "ChatPreview", "Dialog", "User", + "Username", "Restriction", "ChatEvent", "ChatEventFilter", diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index c816fcc8..23ecea79 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -135,6 +135,10 @@ class Chat(Object): full_name (``str``, *property*): Full name of the other party in a private chat, for private chats and bots. + + usernames (List of :obj:`~pyrogram.types.Username`, *optional*): + List of all chat (fragment) usernames; for private chats, supergroups and channels. + Returned only in :meth:`~pyrogram.Client.get_chat`. """ def __init__( @@ -169,7 +173,8 @@ class Chat(Object): distance: int = None, linked_chat: "types.Chat" = None, send_as_chat: "types.Chat" = None, - available_reactions: Optional["types.ChatReactions"] = None + available_reactions: Optional["types.ChatReactions"] = None, + usernames: List["types.Username"] = None ): super().__init__(client) @@ -202,6 +207,7 @@ class Chat(Object): self.linked_chat = linked_chat self.send_as_chat = send_as_chat self.available_reactions = available_reactions + self.usernames = usernames @property def full_name(self) -> str: @@ -231,6 +237,12 @@ class Chat(Object): @staticmethod def _parse_chat_chat(client, chat: raw.types.Chat) -> "Chat": peer_id = -chat.id + active_usernames = getattr(channel, "usernames", []) + usernames = None + if len(active_usernames) >= 1: + usernames = [] + for username in active_usernames: + usernames.append(types.Username._parse(username)) return Chat( id=peer_id, @@ -242,6 +254,7 @@ class Chat(Object): members_count=getattr(chat, "participants_count", None), dc_id=getattr(getattr(chat, "photo", None), "dc_id", None), has_protected_content=getattr(chat, "noforwards", None), + usernames=usernames, client=client ) @@ -249,6 +262,12 @@ class Chat(Object): def _parse_channel_chat(client, channel: raw.types.Channel) -> "Chat": peer_id = utils.get_channel_id(channel.id) restriction_reason = getattr(channel, "restriction_reason", []) + active_usernames = getattr(channel, "usernames", []) + usernames = None + if len(active_usernames) >= 1: + usernames = [] + for username in active_usernames: + usernames.append(types.Username._parse(username)) return Chat( id=peer_id, @@ -261,6 +280,7 @@ class Chat(Object): is_forum=getattr(channel, "forum", None), title=channel.title, username=getattr(channel, "username", None), + usernames=usernames, photo=types.ChatPhoto._parse(client, getattr(channel, "photo", None), peer_id, getattr(channel, "access_hash", 0)), restrictions=types.List([types.Restriction._parse(r) for r in restriction_reason]) or None, diff --git a/pyrogram/types/user_and_chats/username.py b/pyrogram/types/user_and_chats/username.py new file mode 100644 index 00000000..53f68069 --- /dev/null +++ b/pyrogram/types/user_and_chats/username.py @@ -0,0 +1,58 @@ +# 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 pyrogram import raw +from ..object import Object + + +class Username(Object): + """A Username. + + + Parameters: + username (``String``): + The channel/user username. + + editable (``bool``, *optional*): + Can the username edited. + + active (``bool``, *optional*) + Is the username active. + """ + + def __init__( + self, *, + username: str, + editable: bool = None, + active: bool = None + ): + super().__init__() + + self.username = username + self.editable = editable + self.active = active + + @staticmethod + def _parse(action: "raw.types.Username") -> "Username": + + + return Username( + username=getattr(action,"username", None), + editable=getattr(action,"editable", None), + active=getattr(action,"active", None) + )