From 3c16430b06c020bc0fa8daef0aedd9e7f3ad0c97 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 14 Mar 2018 12:36:50 +0000 Subject: [PATCH] Improvements to update and peer handling. --- .../MadelineProto/MTProtoTools/PeerHandler.php | 18 +++++++++--------- .../MTProtoTools/ResponseHandler.php | 9 ++++++--- .../MTProtoTools/UpdateHandler.php | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 9d65286e..2e430cdf 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -146,18 +146,18 @@ trait PeerHandler if (!isset($this->chats[$bot_api_id]) || $this->chats[$bot_api_id] !== $chat) { $this->chats[$bot_api_id] = $chat; - if (!isset($this->full_chats[$bot_api_id]) || $this->full_chats[$bot_api_id]['full']['participants_count'] !== $this->get_full_info($bot_api_id)['full']['participants_count']) { - if ($this->postpone_pwrchat) { - $this->pending_pwrchat[$this->to_supergroup($chat['id'])] = [$this->settings['peer']['full_fetch'], true]; - } else { - try { + try { + if (!isset($this->full_chats[$bot_api_id]) || $this->full_chats[$bot_api_id]['full']['participants_count'] !== $this->get_full_info($bot_api_id)['full']['participants_count']) { + if ($this->postpone_pwrchat) { + $this->pending_pwrchat[$this->to_supergroup($chat['id'])] = [$this->settings['peer']['full_fetch'], true]; + } else { $this->get_pwr_chat($this->to_supergroup($chat['id']), $this->settings['peer']['full_fetch'], true); - } catch (\danog\MadelineProto\Exception $e) { - \danog\MadelineProto\Logger::log($e->getMessage(), \danog\MadelineProto\Logger::WARNING); - } catch (\danog\MadelineProto\RPCErrorException $e) { - \danog\MadelineProto\Logger::log($e->getMessage(), \danog\MadelineProto\Logger::WARNING); } } + } catch (\danog\MadelineProto\Exception $e) { + \danog\MadelineProto\Logger::log($e->getMessage(), \danog\MadelineProto\Logger::WARNING); + } catch (\danog\MadelineProto\RPCErrorException $e) { + \danog\MadelineProto\Logger::log($e->getMessage(), \danog\MadelineProto\Logger::WARNING); } } break; diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index 04b88306..3b3301fd 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -407,9 +407,12 @@ trait ResponseHandler } if (count($this->pending_updates)) { \danog\MadelineProto\Logger::log('Parsing pending updates...'); - foreach ($this->pending_updates as $key => $updates) { - unset($this->pending_updates[$key]); - $this->handle_updates($updates); + foreach (array_keys($this->pending_updates) as $key) { + if (isset($this->pending_updates[$key])) { + $updates = $this->pending_updates[$key]; + unset($this->pending_updates[$key]); + $this->handle_updates($updates); + } } } } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index e5e0d261..f0a54176 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -184,6 +184,7 @@ trait UpdateHandler return; } $this->load_channel_state($channel)['sync_loading'] = true; + $this->postpone_updates = true; try { $input = $this->get_info('channel#'.$channel); @@ -196,10 +197,12 @@ trait UpdateHandler } catch (\danog\MadelineProto\RPCErrorException $e) { return false; } finally { + $this->postpone_updates = false; $this->load_channel_state($channel)['sync_loading'] = false; } - $this->load_channel_state($channel)['sync_loading'] = true; \danog\MadelineProto\Logger::log('Fetching '.$channel.' difference...', \danog\MadelineProto\Logger::ULTRA_VERBOSE); + $this->load_channel_state($channel)['sync_loading'] = true; + $this->postpone_updates = true; try { $difference = $this->method_call('updates.getChannelDifference', ['channel' => $input, 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $this->load_channel_state($channel)['pts'], 'limit' => 30], ['datacenter' => $this->datacenter->curdc]); @@ -215,21 +218,25 @@ trait UpdateHandler return $this->get_channel_difference($channel); } finally { + $this->postpone_updates = false; $this->load_channel_state($channel)['sync_loading'] = false; } unset($input); + switch ($difference['_']) { case 'updates.channelDifferenceEmpty': $this->set_channel_state($channel, $difference); break; case 'updates.channelDifference': $this->load_channel_state($channel)['sync_loading'] = true; + $this->postpone_updates = true; try { $this->set_channel_state($channel, $difference); $this->handle_update_messages($difference['new_messages'], $channel); $this->handle_multiple_update($difference['other_updates'], [], $channel); } finally { + $this->postpone_updates = false; $this->load_channel_state($channel)['sync_loading'] = false; } if (!$difference['final']) { @@ -240,12 +247,14 @@ trait UpdateHandler case 'updates.channelDifferenceTooLong': \danog\MadelineProto\Logger::log('Got '.$difference['_'], \danog\MadelineProto\Logger::VERBOSE); $this->load_channel_state($channel)['sync_loading'] = true; + $this->postpone_updates = true; try { $this->set_channel_state($channel, $difference); $this->handle_update_messages($difference['messages'], $channel); unset($difference); } finally { + $this->postpone_updates = false; $this->load_channel_state($channel)['sync_loading'] = false; } $this->get_channel_difference($channel); @@ -254,6 +263,7 @@ trait UpdateHandler throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference, true)); break; } + $this->handle_pending_updates(); } public function set_update_state($data) @@ -296,6 +306,7 @@ trait UpdateHandler return false; } $this->updates_state['sync_loading'] = true; + $this->postpone_updates = true; \danog\MadelineProto\Logger::log('Fetching normal difference...', \danog\MadelineProto\Logger::ULTRA_VERBOSE); while (!isset($difference)) { try { @@ -304,10 +315,13 @@ trait UpdateHandler $this->updates_state['sync_loading'] = false; $this->got_state = false; } finally { + $this->postpone_updates = false; $this->updates_state['sync_loading'] = false; } } \danog\MadelineProto\Logger::log('Got '.$difference['_'], \danog\MadelineProto\Logger::ULTRA_VERBOSE); + $this->postpone_updates = true; + $this->updates_state['sync_loading'] = true; try { switch ($difference['_']) { @@ -337,8 +351,10 @@ trait UpdateHandler break; } } finally { + $this->postpone_updates = false; $this->updates_state['sync_loading'] = false; } + $this->handle_pending_updates(); } public function get_updates_state()