Revisit catch_up (#808)

This commit is contained in:
Lonami Exo 2018-08-13 12:32:12 +02:00
parent 6b57dba5e1
commit 653686996a

View File

@ -129,29 +129,20 @@ class UpdateMethods(UserMethods):
async def catch_up(self): async def catch_up(self):
state = self.session.get_update_state(0) state = self.session.get_update_state(0)
if not state or not state.pts: if not state or not state.pts:
return state = await self(functions.updates.GetStateRequest())
self.session.catching_up = True self.session.catching_up = True
try: try:
while True: while True:
d = await self(functions.updates.GetDifferenceRequest( d = await self(functions.updates.GetDifferenceRequest(
state.pts, state.date, state.qts)) state.pts, state.date, state.qts
if isinstance(d, types.updates.DifferenceEmpty): ))
state.date = d.date if isinstance(d, (types.updates.DifferenceSlice,
state.seq = d.seq
break
elif isinstance(d, (types.updates.DifferenceSlice,
types.updates.Difference)): types.updates.Difference)):
if isinstance(d, types.updates.Difference): if isinstance(d, types.updates.Difference):
state = d.state state = d.state
elif d.intermediate_state.pts > state.pts:
state = d.intermediate_state
else: else:
# TODO Figure out why other applications can rely on state = d.intermediate_state
# using always the intermediate_state to eventually
# reach a DifferenceEmpty, but that leads to an
# infinite loop here (so check against old pts to stop)
break
self._handle_update(types.Updates( self._handle_update(types.Updates(
users=d.users, users=d.users,
@ -163,7 +154,12 @@ class UpdateMethods(UserMethods):
for m in d.new_messages for m in d.new_messages
] ]
)) ))
else:
if isinstance(d, types.updates.DifferenceEmpty):
state.date = d.date
state.seq = d.seq
elif isinstance(d, types.updates.DifferenceTooLong): elif isinstance(d, types.updates.DifferenceTooLong):
state.pts = d.pts
break break
finally: finally:
self.session.set_update_state(0, state) self.session.set_update_state(0, state)