Skip to content

Commit

Permalink
Fix bug with username and active_usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
SpEcHiDe committed Apr 4, 2024
1 parent 2affe4f commit a572c08
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions pyrogram/types/user_and_chats/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,19 @@ def __init__(
def _parse_user_chat(client, user: raw.types.User) -> "Chat":
peer_id = user.id

active_usernames = types.List(
[
types.Username._parse(u)
for u in getattr(user, "usernames", [])
]
) or None
_tmp_username = None
if (
active_usernames and
len(active_usernames) > 0
):
_tmp_username = active_usernames[0].username

return Chat(
id=peer_id,
type=enums.ChatType.BOT if user.bot else enums.ChatType.PRIVATE,
Expand All @@ -302,19 +315,14 @@ def _parse_user_chat(client, user: raw.types.User) -> "Chat":
is_scam=getattr(user, "scam", None),
is_fake=getattr(user, "fake", None),
is_support=getattr(user, "support", None),
username=user.username,
username=user.username or _tmp_username,
first_name=user.first_name,
last_name=user.last_name,
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),
client=client,
active_usernames=types.List(
[
types.Username._parse(u)
for u in getattr(user, "usernames", [])
]
) or None,
active_usernames=active_usernames,
_raw=user
)

Expand Down Expand Up @@ -349,6 +357,19 @@ def _parse_channel_chat(client, channel: raw.types.Channel) -> "Chat":
_raw=channel
)

active_usernames = types.List(
[
types.Username._parse(u)
for u in getattr(channel, "usernames", [])
]
) or None
_tmp_username = None
if (
active_usernames and
len(active_usernames) > 0
):
_tmp_username = active_usernames[0].username

return Chat(
id=peer_id,
type=enums.ChatType.SUPERGROUP if channel.megagroup else enums.ChatType.CHANNEL,
Expand All @@ -358,7 +379,7 @@ def _parse_channel_chat(client, channel: raw.types.Channel) -> "Chat":
is_scam=channel.scam,
is_fake=channel.fake,
title=channel.title,
username=getattr(channel, "username", None),
username=channel.username or _tmp_username,
photo=types.ChatPhoto._parse(
client,
getattr(channel, "photo", None),
Expand All @@ -377,12 +398,7 @@ def _parse_channel_chat(client, channel: raw.types.Channel) -> "Chat":
has_protected_content=getattr(channel, "noforwards", None),
is_forum=getattr(channel, "forum", None),
client=client,
active_usernames=types.List(
[
types.Username._parse(u)
for u in getattr(channel, "usernames", [])
]
) or None,
active_usernames=active_usernames,
_raw=channel
)

Expand Down

0 comments on commit a572c08

Please sign in to comment.