Improvements to update and peer handling.

This commit is contained in:
Daniil Gentili 2018-03-14 12:36:50 +00:00
parent f2a3f460a6
commit 3c16430b06
3 changed files with 32 additions and 13 deletions

View File

@ -146,18 +146,18 @@ trait PeerHandler
if (!isset($this->chats[$bot_api_id]) || $this->chats[$bot_api_id] !== $chat) { if (!isset($this->chats[$bot_api_id]) || $this->chats[$bot_api_id] !== $chat) {
$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']) { try {
if ($this->postpone_pwrchat) { 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']) {
$this->pending_pwrchat[$this->to_supergroup($chat['id'])] = [$this->settings['peer']['full_fetch'], true]; if ($this->postpone_pwrchat) {
} else { $this->pending_pwrchat[$this->to_supergroup($chat['id'])] = [$this->settings['peer']['full_fetch'], true];
try { } else {
$this->get_pwr_chat($this->to_supergroup($chat['id']), $this->settings['peer']['full_fetch'], true); $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; break;

View File

@ -407,9 +407,12 @@ trait ResponseHandler
} }
if (count($this->pending_updates)) { if (count($this->pending_updates)) {
\danog\MadelineProto\Logger::log('Parsing pending updates...'); \danog\MadelineProto\Logger::log('Parsing pending updates...');
foreach ($this->pending_updates as $key => $updates) { foreach (array_keys($this->pending_updates) as $key) {
unset($this->pending_updates[$key]); if (isset($this->pending_updates[$key])) {
$this->handle_updates($updates); $updates = $this->pending_updates[$key];
unset($this->pending_updates[$key]);
$this->handle_updates($updates);
}
} }
} }
} }

View File

@ -184,6 +184,7 @@ trait UpdateHandler
return; return;
} }
$this->load_channel_state($channel)['sync_loading'] = true; $this->load_channel_state($channel)['sync_loading'] = true;
$this->postpone_updates = true;
try { try {
$input = $this->get_info('channel#'.$channel); $input = $this->get_info('channel#'.$channel);
@ -196,10 +197,12 @@ trait UpdateHandler
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
return false; return false;
} finally { } finally {
$this->postpone_updates = false;
$this->load_channel_state($channel)['sync_loading'] = 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); \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 { try {
$difference = $this->method_call('updates.getChannelDifference', ['channel' => $input, 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $this->load_channel_state($channel)['pts'], 'limit' => 30], ['datacenter' => $this->datacenter->curdc]); $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); return $this->get_channel_difference($channel);
} finally { } finally {
$this->postpone_updates = false;
$this->load_channel_state($channel)['sync_loading'] = false; $this->load_channel_state($channel)['sync_loading'] = false;
} }
unset($input); unset($input);
switch ($difference['_']) { switch ($difference['_']) {
case 'updates.channelDifferenceEmpty': case 'updates.channelDifferenceEmpty':
$this->set_channel_state($channel, $difference); $this->set_channel_state($channel, $difference);
break; break;
case 'updates.channelDifference': case 'updates.channelDifference':
$this->load_channel_state($channel)['sync_loading'] = true; $this->load_channel_state($channel)['sync_loading'] = true;
$this->postpone_updates = true;
try { try {
$this->set_channel_state($channel, $difference); $this->set_channel_state($channel, $difference);
$this->handle_update_messages($difference['new_messages'], $channel); $this->handle_update_messages($difference['new_messages'], $channel);
$this->handle_multiple_update($difference['other_updates'], [], $channel); $this->handle_multiple_update($difference['other_updates'], [], $channel);
} finally { } finally {
$this->postpone_updates = false;
$this->load_channel_state($channel)['sync_loading'] = false; $this->load_channel_state($channel)['sync_loading'] = false;
} }
if (!$difference['final']) { if (!$difference['final']) {
@ -240,12 +247,14 @@ trait UpdateHandler
case 'updates.channelDifferenceTooLong': case 'updates.channelDifferenceTooLong':
\danog\MadelineProto\Logger::log('Got '.$difference['_'], \danog\MadelineProto\Logger::VERBOSE); \danog\MadelineProto\Logger::log('Got '.$difference['_'], \danog\MadelineProto\Logger::VERBOSE);
$this->load_channel_state($channel)['sync_loading'] = true; $this->load_channel_state($channel)['sync_loading'] = true;
$this->postpone_updates = true;
try { try {
$this->set_channel_state($channel, $difference); $this->set_channel_state($channel, $difference);
$this->handle_update_messages($difference['messages'], $channel); $this->handle_update_messages($difference['messages'], $channel);
unset($difference); unset($difference);
} finally { } finally {
$this->postpone_updates = false;
$this->load_channel_state($channel)['sync_loading'] = false; $this->load_channel_state($channel)['sync_loading'] = false;
} }
$this->get_channel_difference($channel); $this->get_channel_difference($channel);
@ -254,6 +263,7 @@ trait UpdateHandler
throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference, true)); throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference, true));
break; break;
} }
$this->handle_pending_updates();
} }
public function set_update_state($data) public function set_update_state($data)
@ -296,6 +306,7 @@ trait UpdateHandler
return false; return false;
} }
$this->updates_state['sync_loading'] = true; $this->updates_state['sync_loading'] = true;
$this->postpone_updates = true;
\danog\MadelineProto\Logger::log('Fetching normal difference...', \danog\MadelineProto\Logger::ULTRA_VERBOSE); \danog\MadelineProto\Logger::log('Fetching normal difference...', \danog\MadelineProto\Logger::ULTRA_VERBOSE);
while (!isset($difference)) { while (!isset($difference)) {
try { try {
@ -304,10 +315,13 @@ trait UpdateHandler
$this->updates_state['sync_loading'] = false; $this->updates_state['sync_loading'] = false;
$this->got_state = false; $this->got_state = false;
} finally { } finally {
$this->postpone_updates = false;
$this->updates_state['sync_loading'] = false; $this->updates_state['sync_loading'] = false;
} }
} }
\danog\MadelineProto\Logger::log('Got '.$difference['_'], \danog\MadelineProto\Logger::ULTRA_VERBOSE); \danog\MadelineProto\Logger::log('Got '.$difference['_'], \danog\MadelineProto\Logger::ULTRA_VERBOSE);
$this->postpone_updates = true;
$this->updates_state['sync_loading'] = true;
try { try {
switch ($difference['_']) { switch ($difference['_']) {
@ -337,8 +351,10 @@ trait UpdateHandler
break; break;
} }
} finally { } finally {
$this->postpone_updates = false;
$this->updates_state['sync_loading'] = false; $this->updates_state['sync_loading'] = false;
} }
$this->handle_pending_updates();
} }
public function get_updates_state() public function get_updates_state()