44
55from telethon .sessions .abstract import Session
66from telethon .crypto import AuthKey
7- from telethon .tl .types import PeerUser
7+ from telethon .tl .types import InputPeerSelf
88
99import userbot .src .db_manager as db_manager
1010from userbot .src .db .session import get_db
1414class DbSession (Session ):
1515 """
1616 A Telethon session class that stores session data in a PostgreSQL database.
17+ This implementation correctly provides all the abstract methods and properties
18+ required by Telethon's base Session class.
1719 """
1820
19- def __init__ (self , account_id : int , self_user_id : Optional [ int ] ):
21+ def __init__ (self , account_id : int ):
2022 """
2123 Initializes the database-backed session.
2224
2325 Args:
2426 account_id (int): The unique identifier for the account this session belongs to.
25- self_user_id (Optional[int]): The Telegram user ID of the account holder.
2627 """
2728 super ().__init__ ()
2829 if not isinstance (account_id , int ):
2930 raise ValueError ("DbSession requires a valid integer account_id." )
3031
3132 self .account_id : int = account_id
32- self ._self_user_id : Optional [int ] = self_user_id
3333
3434 self ._auth_key : Optional [AuthKey ] = None
3535 self ._dc_id : int = 0
@@ -41,29 +41,11 @@ def __init__(self, account_id: int, self_user_id: Optional[int]):
4141 self ._qts : Optional [int ] = None
4242 self ._date : Optional [int ] = None
4343 self ._seq : Optional [int ] = None
44-
45- @classmethod
46- async def create (cls , account_id : int ) -> "DbSession" :
47- """
48- Asynchronously creates and pre-loads a DbSession instance.
49- This factory method is used to fetch necessary data like the user_telegram_id
50- before the synchronous parts of the session are accessed by Telethon.
51-
52- Args:
53- account_id (int): The unique identifier for the account.
54-
55- Returns:
56- DbSession: A new instance of DbSession.
57- """
58- self_user_id : Optional [int ] = None
59- async with get_db () as db :
60- account = await db_manager .get_account_by_id (db , account_id )
61- if account :
62- self_user_id = account .user_telegram_id
63- return cls (account_id , self_user_id )
6444
6545 async def load (self ) -> None :
66- """Loads the session data for the current account_id from the database."""
46+ """
47+ Loads the session data for the current account_id from the database.
48+ """
6749 logger .debug (f"Attempting to load session for account_id: { self .account_id } " )
6850 async with get_db () as db :
6951 session_data = await db_manager .get_session (db , self .account_id )
@@ -140,12 +122,12 @@ def process_entities(self, tlo: object) -> None: pass
140122
141123 def get_input_entity (self , key : Any ) -> Any :
142124 """
143- Returns the input entity for the current user if key is 0.
144- This is crucial for the client to know "who it is" upon startup .
125+ Returns InputPeerSelf() if key is 0, which tells Telethon to use the
126+ currently authorized user. This is the correct way to handle "self" lookups .
145127 """
146- if key == 0 and self . _self_user_id is not None :
147- return PeerUser ( self . _self_user_id )
148- raise KeyError ("Entity not found in DbSession cache (caching is not implemented for other entities )." )
128+ if key == 0 :
129+ return InputPeerSelf ( )
130+ raise KeyError ("Entity not found in DbSession cache (caching is not implemented)." )
149131
150132 def cache_file (self , md5_digest : bytes , file_size : int , instance : Any ) -> None : pass
151133
0 commit comments