From 94f20292c3a52ca4792893bc4b755ce4de1e3359 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 13 Jul 2018 17:20:47 +0200 Subject: [PATCH] Bugfixes to combined API, perfomance improvements, additional internal processing for config/user updates --- docs | 2 +- src/danog/MadelineProto/CombinedAPI.php | 27 ++++++++++++------- src/danog/MadelineProto/Lang.php | 4 +-- .../MTProtoTools/PeerHandler.php | 2 ++ .../MTProtoTools/ResponseHandler.php | 13 +++++++++ .../MTProtoTools/UpdateHandler.php | 9 +++++++ 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/docs b/docs index 4462f6f0..1c204d1d 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 4462f6f0c4391a3a1c6ce4f2b5cb02081a635a88 +Subproject commit 1c204d1dbdaea25559ac9a89d51c8850e005b772 diff --git a/src/danog/MadelineProto/CombinedAPI.php b/src/danog/MadelineProto/CombinedAPI.php index ee1becb2..39bea774 100644 --- a/src/danog/MadelineProto/CombinedAPI.php +++ b/src/danog/MadelineProto/CombinedAPI.php @@ -9,7 +9,7 @@ MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY See the GNU Affero General Public License for more details. You should have received a copy of the GNU General Public License along with MadelineProto. If not, see . -*/ + */ namespace danog\MadelineProto; @@ -32,6 +32,10 @@ class CombinedAPI \danog\MadelineProto\Magic::class_exists(); + foreach ($paths as $path => $settings) { + $this->addInstance($path, $settings); + } + if (file_exists($realpaths['file'])) { if (!file_exists($realpaths['lockfile'])) { touch($realpaths['lockfile']); @@ -50,21 +54,26 @@ class CombinedAPI } $deserialized = unserialize($tounserialize); - foreach ($deserialized['instance_paths'] as $path) { - $this->addInstance($path, isset($paths[$path]) ? $paths[$path] : []); - } + /*foreach ($deserialized['instance_paths'] as $path) { + $this->addInstance($path, isset($paths[$path]) ? $paths[$path] : []); + }*/ $this->event_handler = $deserialized['event_handler']; $this->event_handler_instance = $deserialized['event_handler_instance']; + $this->setEventHandler($this->event_handler); } foreach ($paths as $path => $settings) { $this->addInstance($path, $settings); } + } public function addInstance($path, $settings = []) { if (isset($this->instances[$path]) && isset($this->instance_paths[$path])) { + if (isset($this->event_handler_instance)) { + $this->event_handler_instance->referenceInstance($path); + } return; } @@ -105,7 +114,7 @@ class CombinedAPI public function serialize($filename = '') { /*foreach ($this->instances as $instance) { - $instance->serialize(); + $instance->serialize(); }*/ if (is_null($this->session)) { @@ -163,7 +172,7 @@ class CombinedAPI public function event_update_handler($update, $instance) { - $method_name = 'on'.ucfirst($update['_']); + $method_name = 'on' . ucfirst($update['_']); if (method_exists($this->event_handler_instance, $method_name)) { $this->event_handler_instance->$method_name($update, $instance); } elseif (method_exists($this->event_handler_instance, 'onAny')) { @@ -190,8 +199,8 @@ class CombinedAPI } catch (\danog\MadelineProto\Exception $e) { register_shutdown_function(function () { \danog\MadelineProto\Logger::log(['Restarting script...']); - $a = fsockopen((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT']); - fwrite($a, $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL']."\r\n".'Host: '.$_SERVER['SERVER_NAME']."\r\n\r\n"); + $a = fsockopen((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp') . '://' . $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT']); + fwrite($a, $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $_SERVER['SERVER_PROTOCOL'] . "\r\n" . 'Host: ' . $_SERVER['SERVER_NAME'] . "\r\n\r\n"); }); } } @@ -227,7 +236,7 @@ class CombinedAPI } foreach ($instance->API->datacenter->sockets as $id => $connection) { - $read[$id.'-'.$path] = $connection->getSocket(); + $read[$id . '-' . $path] = $connection->getSocket(); } } if (time() - $this->serialized > $this->serialization_interval) { diff --git a/src/danog/MadelineProto/Lang.php b/src/danog/MadelineProto/Lang.php index 3b4ddc41..124f575e 100644 --- a/src/danog/MadelineProto/Lang.php +++ b/src/danog/MadelineProto/Lang.php @@ -475,7 +475,7 @@ class Lang 'method_users.getFullUser' => 'You cannot use this method directly, use the get_pwr_chat, get_info, get_full_info methods instead (see https://docs.madelineproto.xyz for more info)', 'method_users.getFullUser_param_id_type_InputUser' => 'You cannot use this method directly, use the get_pwr_chat, get_info, get_full_info methods instead (see https://docs.madelineproto.xyz for more info)', 'method_contacts.getStatuses' => 'Get online status of all users', - 'method_contacts.getContacts' => 'Get info about a certain contact', + 'method_contacts.getContacts' => 'Get all contacts', 'method_contacts.getContacts_param_hash_type_int' => '$ids is the list ids of previously fetched contacts, `$hash = $MadelineProto->gen_vector_hash($ids);`', 'method_contacts.importContacts' => 'Add phone number as contact', 'method_contacts.importContacts_param_contacts_type_Vector t' => 'The numbers to import', @@ -4758,7 +4758,7 @@ class Lang 'method_users.getFullUser' => 'You cannot use this method directly, use the get_pwr_chat, get_info, get_full_info methods instead (see https://docs.madelineproto.xyz for more info)', 'method_users.getFullUser_param_id_type_InputUser' => 'You cannot use this method directly, use the get_pwr_chat, get_info, get_full_info methods instead (see https://docs.madelineproto.xyz for more info)', 'method_contacts.getStatuses' => 'Get online status of all users', - 'method_contacts.getContacts' => 'Get info about a certain contact', + 'method_contacts.getContacts' => 'Get all contacts', 'method_contacts.getContacts_param_hash_type_int' => '$ids is the list ids of previously fetched contacts, `$hash = $MadelineProto->gen_vector_hash($ids);`', 'method_contacts.importContacts' => 'Add phone number as contact', 'method_contacts.importContacts_param_contacts_type_Vector t' => 'The numbers to import', diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index b3549a18..3116df72 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -471,11 +471,13 @@ trait PeerHandler $full = $this->method_call('channels.getFullChannel', ['channel' => $partial['InputChannel']], ['datacenter' => $this->datacenter->curdc])['full_chat']; break; } + $res = []; $res['full'] = $full; $res['last_update'] = time(); $this->full_chats[$partial['bot_api_id']] = $res; + $partial = $this->get_info($id); return array_merge($partial, $res); } diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index 655eb163..1c2c0641 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -272,6 +272,14 @@ trait ResponseHandler if (strpos($datacenter, 'cdn') === false) { $this->handle_updates($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']); } + + if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['users'])) { + unset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['users']); + } + if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['chats'])) { + unset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['chats']); + } + $only_updates = true && $only_updates; break; default: @@ -294,15 +302,20 @@ trait ResponseHandler } break; } + if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['users'])) { $this->add_users($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['users']); } if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['chats'])) { $this->add_chats($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['chats']); } + if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['user'])) { + $this->add_users([$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['user']]); + } if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['chat'])) { $this->add_chats([$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['chat']]); } + if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['result']['users'])) { $this->add_users($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['result']['users']); } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index a6c07e4e..cb0f4f29 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -511,6 +511,15 @@ trait UpdateHandler public function save_update($update) { + if ($update['_'] === 'updateConfig') { + $this->config['expires'] = 0; + $this->get_config(); + } + if (in_array($update['_'], ['updateUserName', 'updateUserPhone', 'updateUserBlocked', 'updateUserPhoto', 'updateContactRegistered', 'updateContactLink'])) { + $id = $this->get_info($update)['bot_api_id']; + $this->full_chats[$id]['last_update'] = 0; + $this->get_full_info($id); + } if ($update['_'] === 'updateDcOptions') { $this->logger->logger('Got new dc options', \danog\MadelineProto\Logger::VERBOSE); $this->parse_dc_options($update['dc_options']);