PyroFork: Add usernames field to Chat

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2023-04-08 01:48:20 +07:00
parent 4401c62244
commit 2cd117f301
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
3 changed files with 81 additions and 1 deletions

View file

@ -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",

View file

@ -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,

View file

@ -0,0 +1,58 @@
# 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 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)
)