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) assert isinstance(auth.user, types.User)
user = User._from_raw(auth.user) user = User._from_raw(auth.user)
client._config.session.user = SessionUser( client._config.session.user = SessionUser(
id=user.id, id=user.id, dc=client._dc_id, bot=user.bot, username=user.username
dc=client._dc_id,
bot=user.bot,
) )
packed = user.pack() 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 as connect_without_auth
from ...mtsender import connect_with_auth from ...mtsender import connect_with_auth
from ...session import DataCenter, Session from ...session import DataCenter, Session
from ...session import User as SessionUser
from ...tl import LAYER, Request, functions from ...tl import LAYER, Request, functions
from .updates import dispatcher, process_socket_updates from .updates import dispatcher, process_socket_updates
@ -149,6 +150,16 @@ async def connect(self: Client) -> None:
self._config.session.user = None self._config.session.user = None
except Exception as e: except Exception as e:
pass 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)) self._dispatcher = asyncio.create_task(dispatcher(self))

View File

@ -26,14 +26,16 @@ class User:
:var id: User identifier. :var id: User identifier.
:var dc: Data-center identifier of the user's "home" DC. :var dc: Data-center identifier of the user's "home" DC.
:var bot: :data:`True` if the user is from a bot account. :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.id = id
self.dc = dc self.dc = dc
self.bot = bot self.bot = bot
self.username = username
class ChannelState: class ChannelState:
@ -134,6 +136,7 @@ class Session:
"id": self.user.id, "id": self.user.id,
"dc": self.user.dc, "dc": self.user.dc,
"bot": self.user.bot, "bot": self.user.bot,
"username": self.user.username,
} }
if self.user if self.user
else None, else None,
@ -174,6 +177,7 @@ class Session:
id=dict["user"]["id"], id=dict["user"]["id"],
dc=dict["user"]["dc"], dc=dict["user"]["dc"],
bot=dict["user"]["bot"], bot=dict["user"]["bot"],
username=dict["user"]["username"],
) )
if dict["user"] if dict["user"]
else None, else None,

View File

@ -106,7 +106,9 @@ class SqliteSession(Storage):
DataCenter(id=id, addr=addr, auth=auth) DataCenter(id=id, addr=addr, auth=auth)
for (id, addr, auth) in datacenter 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( state=UpdateState(
pts=state[0], pts=state[0],
qts=state[1], qts=state[1],