Add username to session

Needed by filters.Command.
This commit is contained in:
Lonami Exo 2023-09-17 22:53:26 +02:00
parent 8c00d2f088
commit 2541c409e2
4 changed files with 21 additions and 6 deletions

View File

@ -30,9 +30,7 @@ async def complete_login(client: Client, auth: abcs.auth.Authorization) -> User:
assert isinstance(auth.user, types.User)
user = User._from_raw(auth.user)
client._config.session.user = SessionUser(
id=user.id,
dc=client._dc_id,
bot=user.bot,
id=user.id, dc=client._dc_id, bot=user.bot, username=user.username
)
packed = user.pack()

View File

@ -12,6 +12,7 @@ from ...mtsender import Sender
from ...mtsender import connect as connect_without_auth
from ...mtsender import connect_with_auth
from ...session import DataCenter, Session
from ...session import User as SessionUser
from ...tl import LAYER, Request, functions
from .updates import dispatcher, process_socket_updates
@ -149,6 +150,16 @@ async def connect(self: Client) -> None:
self._config.session.user = None
except Exception as e:
pass
else:
if not self._config.session.user:
me = await self.get_me()
assert me is not None
self._config.session.user = SessionUser(
id=me.id, dc=self._dc_id, bot=me.bot, username=me.username
)
packed = me.pack()
assert packed is not None
self._chat_hashes.set_self_user(packed)
self._dispatcher = asyncio.create_task(dispatcher(self))

View File

@ -26,14 +26,16 @@ class User:
:var id: User identifier.
:var dc: Data-center identifier of the user's "home" DC.
:var bot: :data:`True` if the user is from a bot account.
:var username: User's primary username.
"""
__slots__ = ("id", "dc", "bot")
__slots__ = ("id", "dc", "bot", "username")
def __init__(self, *, id: int, dc: int, bot: bool) -> None:
def __init__(self, *, id: int, dc: int, bot: bool, username: Optional[str]) -> None:
self.id = id
self.dc = dc
self.bot = bot
self.username = username
class ChannelState:
@ -134,6 +136,7 @@ class Session:
"id": self.user.id,
"dc": self.user.dc,
"bot": self.user.bot,
"username": self.user.username,
}
if self.user
else None,
@ -174,6 +177,7 @@ class Session:
id=dict["user"]["id"],
dc=dict["user"]["dc"],
bot=dict["user"]["bot"],
username=dict["user"]["username"],
)
if dict["user"]
else None,

View File

@ -106,7 +106,9 @@ class SqliteSession(Storage):
DataCenter(id=id, addr=addr, auth=auth)
for (id, addr, auth) in datacenter
],
user=User(id=user[0], dc=user[1], bot=bool(user[2])) if user else None,
user=User(id=user[0], dc=user[1], bot=bool(user[2]), username=user[3])
if user
else None,
state=UpdateState(
pts=state[0],
qts=state[1],