From 4c62140c535d74d423cef780ef39f8b37f319b5e Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 16 Aug 2017 11:58:24 +0200 Subject: [PATCH] Fixed update handling (closes #156) --- .../MTProtoTools/UpdateHandler.php | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 1482c8a9..13879e8a 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -127,20 +127,21 @@ trait UpdateHandler } } - /* - public function get_msg_id($peer) + + + public function check_msg_id($message) { - $id = $this->get_info($peer)['bot_api_id']; + $peer_id = $this->get_info($message['to_id'])['bot_api_id']; + $message_id = $message['id']; - return isset($this->msg_ids[$id]) ? $this->msg_ids[$id] : false; + if (!isset($this->msg_ids[$peer_id]) || $message_id > $this->msg_ids[$peer_id]) { + $this->msg_ids[$peer_id] = $message_id; + return true; + } + return false; } - public function set_msg_id($peer, $msg_id) - { - $id = $this->get_info($peer)['bot_api_id']; - $this->msg_ids[$id] = $msg_id; - } - */ + public function get_channel_difference($channel) { if (!$this->settings['updates']['handle_updates']) { @@ -381,7 +382,7 @@ trait UpdateHandler } if (isset($update['pts'])) { - if ($update['pts'] <= $cur_state['pts']) { + if ($update['pts'] < $cur_state['pts']) { \danog\MadelineProto\Logger::log(['Duplicate update, channel id: '.$channel_id], \danog\MadelineProto\Logger::ERROR); return false; @@ -396,10 +397,15 @@ trait UpdateHandler return false; } - // if ($cur_state['pts'] < $update['pts']) { + if (isset($update['message']['id'], $update['message']['to_id'])) { + if (!$this->check_msg_id($update['message'])) { + \danog\MadelineProto\Logger::log(['Duplicate update by message id, channel id: '.$channel_id], \danog\MadelineProto\Logger::ERROR); + return false; + } + } + \danog\MadelineProto\Logger::log(['Applying pts. current pts: '.$cur_state['pts'].', new pts: '.$update['pts'].', channel id: '.$channel_id], \danog\MadelineProto\Logger::VERBOSE); $cur_state['pts'] = $update['pts']; - // } if ($channel_id === false && isset($options['date']) && $cur_state['date'] < $options['date']) { $cur_state['date'] = $options['date'];