Merge branch 'LonamiWebs:v1' into main

This commit is contained in:
Darskiy 2025-04-23 22:36:25 +03:00 committed by GitHub
commit 5603a25c63
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 28 deletions

View File

@ -49,9 +49,6 @@ class EntityCache:
if getattr(c, 'access_hash', None) and not getattr(c, 'min', None) if getattr(c, 'access_hash', None) and not getattr(c, 'min', None)
) )
def get_all_entities(self):
return [Entity(ty, id, hash) for id, (hash, ty) in self.hash_map.items()]
def put(self, entity): def put(self, entity):
self.hash_map[entity.id] = (entity.hash, entity.ty) self.hash_map[entity.id] = (entity.hash, entity.ty)

View File

@ -460,30 +460,14 @@ class MessageBox:
return pts.pts - pts.pts_count if pts else 0 return pts.pts - pts.pts_count if pts else 0
reset_deadlines = set() # temporary buffer reset_deadlines = set() # temporary buffer
any_pts_applied = [False] # using a list to pass "by reference"
result.extend(filter(None, ( result.extend(filter(None, (
self.apply_pts_info(u, reset_deadlines=reset_deadlines, any_pts_applied=any_pts_applied) self.apply_pts_info(u, reset_deadlines=reset_deadlines)
# Telegram can send updates out of order (e.g. ReadChannelInbox first # Telegram can send updates out of order (e.g. ReadChannelInbox first
# and then NewChannelMessage, both with the same pts, but the count is # and then NewChannelMessage, both with the same pts, but the count is
# 0 and 1 respectively), so we sort them first. # 0 and 1 respectively), so we sort them first.
for u in sorted(updates, key=_sort_gaps)))) for u in sorted(updates, key=_sort_gaps))))
# > If the updates were applied, local *Updates* state must be updated
# > with `seq` (unless it's 0) and `date` from the constructor.
#
# By "were applied", we assume it means "some other pts was applied".
# Updates which can be applied in any order, such as `UpdateChat`,
# should not cause `seq` to be updated (or upcoming updates such as
# `UpdateChatParticipant` could be missed).
if any_pts_applied[0]:
if __debug__:
self._trace('Updating seq as local pts was updated too')
if date != epoch():
self.date = date
if seq != NO_SEQ:
self.seq = seq
self.reset_deadlines(reset_deadlines, next_updates_deadline()) self.reset_deadlines(reset_deadlines, next_updates_deadline())
if self.possible_gaps: if self.possible_gaps:
@ -510,6 +494,16 @@ class MessageBox:
real_result.extend(u for u in result if not u._self_outgoing) real_result.extend(u for u in result if not u._self_outgoing)
if result and not self.possible_gaps:
# > If the updates were applied, local *Updates* state must be updated
# > with `seq` (unless it's 0) and `date` from the constructor.
if __debug__:
self._trace('Updating seq as all updates were applied')
if date != epoch():
self.date = date
if seq != NO_SEQ:
self.seq = seq
return (users, chats) return (users, chats)
# Tries to apply the input update if its `PtsInfo` follows the correct order. # Tries to apply the input update if its `PtsInfo` follows the correct order.
@ -522,7 +516,6 @@ class MessageBox:
update, update,
*, *,
reset_deadlines, reset_deadlines,
any_pts_applied=[True], # mutable default is fine as it's write-only
): ):
# This update means we need to call getChannelDifference to get the updates from the channel # This update means we need to call getChannelDifference to get the updates from the channel
if isinstance(update, tl.UpdateChannelTooLong): if isinstance(update, tl.UpdateChannelTooLong):
@ -574,7 +567,6 @@ class MessageBox:
return None return None
else: else:
# Apply # Apply
any_pts_applied[0] = True
if __debug__: if __debug__:
self._trace('Applying update pts since local pts %r = %r: %s', local_pts, pts, update) self._trace('Applying update pts since local pts %r = %r: %s', local_pts, pts, update)

View File

@ -687,14 +687,10 @@ class TelegramBaseClient(abc.ABC):
connection._proxy = proxy connection._proxy = proxy
def _save_states_and_entities(self: 'TelegramClient'): def _save_states_and_entities(self: 'TelegramClient'):
entities = self._mb_entity_cache.get_all_entities()
# Piggy-back on an arbitrary TL type with users and chats so the session can understand to read the entities.
# It doesn't matter if we put users in the list of chats.
self.session.process_entities(types.contacts.ResolvedPeer(None, [e._as_input_peer() for e in entities], []))
# As a hack to not need to change the session files, save ourselves with ``id=0`` and ``access_hash`` of our ``id``. # As a hack to not need to change the session files, save ourselves with ``id=0`` and ``access_hash`` of our ``id``.
# This way it is possible to determine our own ID by querying for 0. However, whether we're a bot is not saved. # This way it is possible to determine our own ID by querying for 0. However, whether we're a bot is not saved.
# Piggy-back on an arbitrary TL type with users and chats so the session can understand to read the entities.
# It doesn't matter if we put users in the list of chats.
if self._mb_entity_cache.self_id: if self._mb_entity_cache.self_id:
self.session.process_entities(types.contacts.ResolvedPeer(None, [types.InputPeerUser(0, self._mb_entity_cache.self_id)], [])) self.session.process_entities(types.contacts.ResolvedPeer(None, [types.InputPeerUser(0, self._mb_entity_cache.self_id)], []))