mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-08 12:59:46 +00:00
Make sessions async
SQLiteSession is not updated, don't try to use it
This commit is contained in:
@@ -597,7 +597,7 @@ class AuthMethods:
|
||||
self._state_cache.reset()
|
||||
|
||||
await self.disconnect()
|
||||
self.session.delete()
|
||||
await self.session.delete()
|
||||
return True
|
||||
|
||||
async def edit_2fa(
|
||||
|
@@ -55,7 +55,7 @@ class _DirectDownloadIter(RequestIter):
|
||||
if option.ip_address == self.client.session.server_address:
|
||||
self.client.session.set_dc(
|
||||
option.id, option.ip_address, option.port)
|
||||
self.client.session.save()
|
||||
await self.client.session.save()
|
||||
break
|
||||
|
||||
# TODO Figure out why the session may have the wrong DC ID
|
||||
@@ -402,7 +402,7 @@ class DownloadMethods:
|
||||
if isinstance(message.action,
|
||||
types.MessageActionChatEditPhoto):
|
||||
media = media.photo
|
||||
|
||||
|
||||
if isinstance(media, types.MessageMediaWebPage):
|
||||
if isinstance(media.webpage, types.WebPage):
|
||||
media = media.webpage.document or media.webpage.photo
|
||||
|
@@ -1019,7 +1019,7 @@ class MessageMethods:
|
||||
async def edit_message(
|
||||
self: 'TelegramClient',
|
||||
entity: 'typing.Union[hints.EntityLike, types.Message]',
|
||||
message: 'hints.MessageLike' = None,
|
||||
message: 'hints.MessageIDLike' = None,
|
||||
text: str = None,
|
||||
*,
|
||||
parse_mode: str = (),
|
||||
|
@@ -412,10 +412,7 @@ class TelegramBaseClient(abc.ABC):
|
||||
|
||||
self._authorized = None # None = unknown, False = no, True = yes
|
||||
|
||||
# Update state (for catching up after a disconnection)
|
||||
# TODO Get state from channels too
|
||||
self._state_cache = StateCache(
|
||||
self.session.get_update_state(0), self._log)
|
||||
self._state_cache = StateCache(None, self._log)
|
||||
|
||||
# Some further state for subclasses
|
||||
self._event_builders = []
|
||||
@@ -522,6 +519,11 @@ class TelegramBaseClient(abc.ABC):
|
||||
except OSError:
|
||||
print('Failed to connect')
|
||||
"""
|
||||
# Update state (for catching up after a disconnection)
|
||||
# TODO Get state from channels too
|
||||
self._state_cache = StateCache(
|
||||
await self.session.get_update_state(0), self._log)
|
||||
|
||||
if not await self._sender.connect(self._connection(
|
||||
self.session.server_address,
|
||||
self.session.port,
|
||||
@@ -534,7 +536,7 @@ class TelegramBaseClient(abc.ABC):
|
||||
return
|
||||
|
||||
self.session.auth_key = self._sender.auth_key
|
||||
self.session.save()
|
||||
await self.session.save()
|
||||
|
||||
self._init_request.query = functions.help.GetConfigRequest()
|
||||
|
||||
@@ -644,7 +646,7 @@ class TelegramBaseClient(abc.ABC):
|
||||
|
||||
pts, date = self._state_cache[None]
|
||||
if pts and date:
|
||||
self.session.set_update_state(0, types.updates.State(
|
||||
await self.session.set_update_state(0, types.updates.State(
|
||||
pts=pts,
|
||||
qts=0,
|
||||
date=date,
|
||||
@@ -652,7 +654,7 @@ class TelegramBaseClient(abc.ABC):
|
||||
unread_count=0
|
||||
))
|
||||
|
||||
self.session.close()
|
||||
await self.session.close()
|
||||
|
||||
async def _disconnect(self: 'TelegramClient'):
|
||||
"""
|
||||
@@ -677,17 +679,17 @@ class TelegramBaseClient(abc.ABC):
|
||||
# so it's not valid anymore. Set to None to force recreating it.
|
||||
self._sender.auth_key.key = None
|
||||
self.session.auth_key = None
|
||||
self.session.save()
|
||||
await self.session.save()
|
||||
await self._disconnect()
|
||||
return await self.connect()
|
||||
|
||||
def _auth_key_callback(self: 'TelegramClient', auth_key):
|
||||
async def _auth_key_callback(self: 'TelegramClient', auth_key):
|
||||
"""
|
||||
Callback from the sender whenever it needed to generate a
|
||||
new authorization key. This means we are not authorized.
|
||||
"""
|
||||
self.session.auth_key = auth_key
|
||||
self.session.save()
|
||||
await self.session.save()
|
||||
|
||||
# endregion
|
||||
|
||||
@@ -812,7 +814,7 @@ class TelegramBaseClient(abc.ABC):
|
||||
if not session:
|
||||
dc = await self._get_dc(cdn_redirect.dc_id, cdn=True)
|
||||
session = self.session.clone()
|
||||
await session.set_dc(dc.id, dc.ip_address, dc.port)
|
||||
session.set_dc(dc.id, dc.ip_address, dc.port)
|
||||
self._exported_sessions[cdn_redirect.dc_id] = session
|
||||
|
||||
self._log[__name__].info('Creating new CDN client')
|
||||
|
@@ -255,7 +255,7 @@ class UpdateMethods:
|
||||
state = d.intermediate_state
|
||||
|
||||
pts, date = state.pts, state.date
|
||||
self._handle_update(types.Updates(
|
||||
await self._handle_update(types.Updates(
|
||||
users=d.users,
|
||||
chats=d.chats,
|
||||
date=state.date,
|
||||
@@ -300,8 +300,8 @@ class UpdateMethods:
|
||||
# It is important to not make _handle_update async because we rely on
|
||||
# the order that the updates arrive in to update the pts and date to
|
||||
# be always-increasing. There is also no need to make this async.
|
||||
def _handle_update(self: 'TelegramClient', update):
|
||||
self.session.process_entities(update)
|
||||
async def _handle_update(self: 'TelegramClient', update):
|
||||
await self.session.process_entities(update)
|
||||
self._entity_cache.add(update)
|
||||
|
||||
if isinstance(update, (types.Updates, types.UpdatesCombined)):
|
||||
@@ -372,7 +372,7 @@ class UpdateMethods:
|
||||
# inserted because this is a rather expensive operation
|
||||
# (default's sqlite3 takes ~0.1s to commit changes). Do
|
||||
# it every minute instead. No-op if there's nothing new.
|
||||
self.session.save()
|
||||
await self.session.save()
|
||||
|
||||
# We need to send some content-related request at least hourly
|
||||
# for Telegram to keep delivering updates, otherwise they will
|
||||
|
@@ -71,7 +71,7 @@ class UserMethods:
|
||||
exceptions.append(e)
|
||||
results.append(None)
|
||||
continue
|
||||
self.session.process_entities(result)
|
||||
await self.session.process_entities(result)
|
||||
self._entity_cache.add(result)
|
||||
exceptions.append(None)
|
||||
results.append(result)
|
||||
@@ -82,7 +82,7 @@ class UserMethods:
|
||||
return results
|
||||
else:
|
||||
result = await future
|
||||
self.session.process_entities(result)
|
||||
await self.session.process_entities(result)
|
||||
self._entity_cache.add(result)
|
||||
return result
|
||||
except (errors.ServerError, errors.RpcCallFailError,
|
||||
@@ -427,7 +427,7 @@ class UserMethods:
|
||||
|
||||
# No InputPeer, cached peer, or known string. Fetch from disk cache
|
||||
try:
|
||||
return self.session.get_input_entity(peer)
|
||||
return await self.session.get_input_entity(peer)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
@@ -567,7 +567,7 @@ class UserMethods:
|
||||
try:
|
||||
# Nobody with this username, maybe it's an exact name/title
|
||||
return await self.get_entity(
|
||||
self.session.get_input_entity(string))
|
||||
await self.session.get_input_entity(string))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
Reference in New Issue
Block a user