Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add username type and usernames parameter in user and chat #1257

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyrogram/types/user_and_chats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .invite_link_importer import InviteLinkImporter
from .restriction import Restriction
from .user import User
from .username import Username
from .video_chat_ended import VideoChatEnded
from .video_chat_members_invited import VideoChatMembersInvited
from .video_chat_scheduled import VideoChatScheduled
Expand All @@ -48,6 +49,7 @@
"ChatPreview",
"Dialog",
"User",
"Username",
"Restriction",
"ChatEvent",
"ChatEventFilter",
Expand Down
12 changes: 11 additions & 1 deletion pyrogram/types/user_and_chats/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ class Chat(Object):
available_reactions (:obj:`~pyrogram.types.ChatReactions`, *optional*):
Available reactions in the chat.
Returned only in :meth:`~pyrogram.Client.get_chat`.

usernames (List of :obj:`~pyrogram.types.Username`, *optional*):
The list of chat's collectible (and basic) usernames if availables.
"""

def __init__(
Expand Down Expand Up @@ -162,7 +165,8 @@ def __init__(
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)

Expand Down Expand Up @@ -194,6 +198,7 @@ def __init__(
self.linked_chat = linked_chat
self.send_as_chat = send_as_chat
self.available_reactions = available_reactions
self.usernames = usernames

@staticmethod
def _parse_user_chat(client, user: raw.types.User) -> "Chat":
Expand All @@ -213,12 +218,14 @@ def _parse_user_chat(client, user: raw.types.User) -> "Chat":
photo=types.ChatPhoto._parse(client, user.photo, peer_id, user.access_hash),
restrictions=types.List([types.Restriction._parse(r) for r in user.restriction_reason]) or None,
dc_id=getattr(getattr(user, "photo", None), "dc_id", None),
usernames=types.List([types.Username._parse(r) for r in user.usernames]) or None,
client=client
)

@staticmethod
def _parse_chat_chat(client, chat: raw.types.Chat) -> "Chat":
peer_id = -chat.id
usernames = getattr(chat, "usernames", [])

return Chat(
id=peer_id,
Expand All @@ -230,13 +237,15 @@ def _parse_chat_chat(client, chat: raw.types.Chat) -> "Chat":
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=types.List([types.Username._parse(r) for r in usernames]) or None,
client=client
)

@staticmethod
def _parse_channel_chat(client, channel: raw.types.Channel) -> "Chat":
peer_id = utils.get_channel_id(channel.id)
restriction_reason = getattr(channel, "restriction_reason", [])
usernames = getattr(channel, "usernames", [])

return Chat(
id=peer_id,
Expand All @@ -255,6 +264,7 @@ def _parse_channel_chat(client, channel: raw.types.Channel) -> "Chat":
members_count=getattr(channel, "participants_count", None),
dc_id=getattr(getattr(channel, "photo", None), "dc_id", None),
has_protected_content=getattr(channel, "noforwards", None),
usernames=types.List([types.Username._parse(r) for r in usernames]) or None,
client=client
)

Expand Down
8 changes: 7 additions & 1 deletion pyrogram/types/user_and_chats/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ class User(Object, Update):
You can use ``user.mention()`` to mention the user using their first name (styled using html), or
``user.mention("another name")`` for a custom name. To choose a different style
("html" or "md"/"markdown") use ``user.mention(style="md")``.

usernames (List of :obj:`~pyrogram.types.Username`, *optional*):
The list of user's collectible (and basic) usernames if availables.
"""

def __init__(
Expand Down Expand Up @@ -174,7 +177,8 @@ def __init__(
dc_id: int = None,
phone_number: str = None,
photo: "types.ChatPhoto" = None,
restrictions: List["types.Restriction"] = None
restrictions: List["types.Restriction"] = None,
usernames: List["types.Username"] = None
):
super().__init__(client)

Expand Down Expand Up @@ -202,6 +206,7 @@ def __init__(
self.phone_number = phone_number
self.photo = photo
self.restrictions = restrictions
self.usernames = usernames

@property
def mention(self):
Expand Down Expand Up @@ -239,6 +244,7 @@ def _parse(client, user: "raw.base.User") -> Optional["User"]:
phone_number=user.phone,
photo=types.ChatPhoto._parse(client, user.photo, user.id, user.access_hash),
restrictions=types.List([types.Restriction._parse(r) for r in user.restriction_reason]) or None,
usernames=types.List([types.Username._parse(r) for r in user.usernames]) or None,
client=client
)

Expand Down
51 changes: 51 additions & 0 deletions pyrogram/types/user_and_chats/username.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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 user's or chat's username.

Parameters:
username (``str``):
User's or chat's username.

editable (``bool``, *optional*):
True, if it's a basic username; False, if it's a collectible username.

active (``bool``, *optional*):
True, if the collectible username is active.

"""

def __init__(self, *, username: str, editable: bool = None, active: bool = None):
super().__init__(None)

self.username = username
self.editable = editable
self.active = active

@staticmethod
def _parse(username: "raw.types.Username") -> "Username":
return Username(
username=username.username,
editable=username.editable,
active=username.active
)