Process entities from the MtProtoSender instead TelegramBareClient

This way, update objects will also be processed when they occur.
This commit is contained in:
Lonami Exo 2017-10-01 16:02:29 +02:00
parent 71b6e527a6
commit 36dabc4928
3 changed files with 26 additions and 25 deletions

View File

@ -213,14 +213,8 @@ class MtProtoSender:
# If the code is not parsed manually then it should be a TLObject.
if code in tlobjects:
result = reader.tgread_object()
if state is None:
self._logger.debug(
'Ignoring unhandled TLObject %s', repr(result)
)
else:
self._logger.debug(
'Processing TLObject %s', repr(result)
)
self.session.process_entities(result)
if state:
state.process(result)
return True
@ -364,6 +358,7 @@ class MtProtoSender:
reader.seek(-4)
request.on_response(reader)
self.session.process_entities(request.result)
request.confirm_received.set()
return True
else:

View File

@ -494,21 +494,10 @@ class TelegramBareClient:
# rejected by the other party as a whole."
return None
# Save all input entities we know of
entities = []
results = []
for x in requests:
y = x.result
results.append(y)
if hasattr(y, 'chats') and hasattr(y.chats, '__iter__'):
entities.extend(y.chats)
if hasattr(y, 'users') and hasattr(y.users, '__iter__'):
entities.extend(y.users)
if self.session.add_entities(entities):
self.session.save() # Save if any new entities got added
return results[0] if len(results) == 1 else results
if len(requests) == 1:
return requests[0].result
else:
return [x.result for x in requests]
except (PhoneMigrateError, NetworkMigrateError,
UserMigrateError) as e:

View File

@ -185,11 +185,28 @@ class Session:
correct = correct_msg_id >> 32
self.time_offset = correct - now
def process_entities(self, tlobject):
"""Processes all the found entities on the given TLObject,
unless .save_entities is False, and saves the session file.
"""
if not self.save_entities:
return
# Save all input entities we know of
entities = []
if hasattr(tlobject, 'chats') and hasattr(tlobject.chats, '__iter__'):
entities.extend(tlobject.chats)
if hasattr(tlobject, 'users') and hasattr(tlobject.users, '__iter__'):
entities.extend(tlobject.users)
if self.add_entities(entities):
self.save() # Save if any new entities got added
def add_entities(self, entities):
"""Adds new input entities to the local database of them.
"""Adds new input entities to the local database unconditionally.
Unknown types will be ignored.
"""
if not entities or not self.save_entities:
if not entities:
return False
new = {}