2016-12-23 12:19:13 +01:00
< ? php
2018-02-24 17:54:13 +01:00
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
/**
* UpdateHandler module .
*
* This file is part of MadelineProto .
* MadelineProto is free software : you can redistribute it and / or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation , either version 3 of the License , or ( at your option ) any later version .
* MadelineProto is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
* 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 < http :// www . gnu . org / licenses />.
*
* @ author Daniil Gentili < daniil @ daniil . it >
2020-02-17 14:13:46 +01:00
* @ copyright 2016 - 2020 Daniil Gentili < daniil @ daniil . it >
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
* @ license https :// opensource . org / licenses / AGPL - 3.0 AGPLv3
*
2019-10-31 15:07:35 +01:00
* @ link https :// docs . madelineproto . xyz MadelineProto documentation
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
*/
2018-02-24 17:54:39 +01:00
2016-12-23 12:19:13 +01:00
namespace danog\MadelineProto\MTProtoTools ;
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
use Amp\Deferred ;
2019-05-30 17:22:28 +02:00
use Amp\Loop ;
2019-12-26 15:14:27 +01:00
use danog\MadelineProto\Logger ;
2020-07-28 20:39:32 +02:00
use danog\MadelineProto\Loop\Update\FeedLoop ;
use danog\MadelineProto\Loop\Update\UpdateLoop ;
2020-09-22 11:48:12 +02:00
use danog\MadelineProto\MTProto ;
2019-12-26 15:14:27 +01:00
use danog\MadelineProto\RPCErrorException ;
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
2020-09-22 11:48:12 +02:00
use danog\MadelineProto\Settings ;
2020-09-22 23:10:56 +02:00
use danog\MadelineProto\Tools ;
2020-09-22 11:48:12 +02:00
2016-12-23 12:19:13 +01:00
/**
* Manages updates .
2020-09-22 11:48:12 +02:00
*
2020-10-03 15:04:35 +02:00
* @ extend MTProto
2020-09-22 11:48:12 +02:00
* @ property Settings $settings Settings
2016-12-23 12:19:13 +01:00
*/
trait UpdateHandler
{
2020-09-22 11:48:12 +02:00
/**
* Update handler callback .
*
* @ var ? callable
*/
private $updateHandler ;
2017-06-05 12:23:23 +02:00
private $got_state = false ;
2019-05-11 20:38:26 +02:00
private $channels_state ;
2017-06-05 12:23:23 +02:00
public $updates = [];
public $updates_key = 0 ;
2020-09-22 11:48:12 +02:00
2019-12-28 16:07:09 +01:00
/**
* Get updates .
*
* @ param array $params Params
*
* @ internal
*
2020-10-01 21:36:07 +02:00
* @ return \Generator
*
* @ psalm - return \Generator < int , \Amp\Promise < mixed | null > , mixed , list < array { update_id : mixed , update : mixed } >| mixed >
2019-12-28 16:07:09 +01:00
*/
public function getUpdates ( $params = []) : \Generator
2016-12-26 20:24:24 +01:00
{
2020-09-22 11:48:12 +02:00
$this -> updateHandler = MTProto :: GETUPDATES_HANDLER ;
2020-09-22 23:10:56 +02:00
$params = MTProto :: DEFAULT_GETUPDATES_PARAMS + $params ;
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
if ( empty ( $this -> updates )) {
$this -> update_deferred = new Deferred ();
2020-09-22 23:10:56 +02:00
$params [ 'timeout' ] *= 1000 ;
yield Tools :: timeoutWithDefault ( $this -> waitUpdate (), $params [ 'timeout' ] ? : 100000 );
2017-09-25 00:26:06 +02:00
}
2017-01-04 13:22:03 +01:00
if ( empty ( $this -> updates )) {
2020-09-22 11:48:12 +02:00
return $this -> updates ;
2017-01-04 13:22:03 +01:00
}
2017-01-07 12:40:51 +01:00
if ( $params [ 'offset' ] < 0 ) {
2019-09-02 17:08:36 +02:00
$params [ 'offset' ] = \array_reverse ( \array_keys (( array ) $this -> updates ))[ \abs ( $params [ 'offset' ]) - 1 ];
2017-01-07 12:40:51 +01:00
}
2016-12-26 18:23:46 +01:00
$updates = [];
2018-12-26 23:46:10 +01:00
foreach ( $this -> updates as $key => $value ) {
2017-01-04 13:22:03 +01:00
if ( $params [ 'offset' ] > $key ) {
unset ( $this -> updates [ $key ]);
2019-09-02 17:08:36 +02:00
} elseif ( $params [ 'limit' ] === null || \count ( $updates ) < $params [ 'limit' ]) {
2017-01-04 13:22:03 +01:00
$updates [] = [ 'update_id' => $key , 'update' => $value ];
}
2016-12-26 18:23:46 +01:00
}
return $updates ;
}
2019-05-30 17:22:28 +02:00
public $update_resolved = false ;
public $update_deferred ;
2019-12-28 16:07:09 +01:00
/**
* Wait for update .
*
* @ internal
*
* @ return \Generator
*/
public function waitUpdate () : \Generator
2019-05-30 17:22:28 +02:00
{
if ( ! $this -> update_deferred ) {
2019-06-04 14:55:58 +02:00
$this -> update_deferred = new Deferred ();
2019-05-30 17:22:28 +02:00
}
yield $this -> update_deferred -> promise ();
$this -> update_resolved = false ;
2019-06-04 14:55:58 +02:00
$this -> update_deferred = new Deferred ();
2019-05-30 17:22:28 +02:00
}
2019-12-28 16:07:09 +01:00
/**
* Signal update .
*
* @ internal
*
* @ return void
*/
public function signalUpdate () : void
2019-05-30 17:22:28 +02:00
{
if ( ! $this -> update_deferred ) {
2019-06-04 14:55:58 +02:00
$this -> update_deferred = new Deferred ();
2019-05-30 17:22:28 +02:00
}
Loop :: defer ( function () {
if ( ! $this -> update_resolved ) {
$this -> update_resolved = true ;
$this -> update_deferred -> resolve ();
}
});
}
2019-12-28 16:07:09 +01:00
/**
* Check message ID .
*
* @ param array $message Message
*
* @ internal
*
* @ return boolean
*/
public function checkMsgId ( array $message ) : bool
2017-02-11 15:18:18 +01:00
{
2020-10-01 18:02:54 +02:00
if ( ! isset ( $message [ 'peer_id' ])) {
2019-06-04 14:55:58 +02:00
return true ;
}
2017-12-25 17:06:37 +01:00
try {
2020-10-01 18:02:54 +02:00
$peer_id = $this -> getId ( $message [ 'peer_id' ]);
2017-12-25 17:07:18 +01:00
} catch ( \danog\MadelineProto\Exception $e ) {
return true ;
2018-03-13 13:21:28 +01:00
} catch ( \danog\MadelineProto\RPCErrorException $e ) {
return true ;
2017-12-25 17:07:18 +01:00
}
2017-08-16 11:58:24 +02:00
$message_id = $message [ 'id' ];
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 ;
2017-02-11 15:16:57 +01:00
}
2019-12-28 16:07:09 +01:00
/**
* Get channel state .
*
* @ internal
*
2020-10-04 16:27:22 +02:00
* @ return \Generator < mixed , mixed , mixed , UpdatesState >
2019-12-28 16:07:09 +01:00
*/
2020-01-31 19:29:43 +01:00
public function loadUpdateState () : \Generator
2016-12-30 16:32:25 +01:00
{
2017-04-30 13:33:59 +02:00
if ( ! $this -> got_state ) {
2017-06-08 14:30:02 +02:00
$this -> got_state = true ;
2020-07-28 20:39:32 +02:00
$this -> channels_state -> get ( 0 , yield from $this -> getUpdatesState ());
2017-04-30 13:33:59 +02:00
}
2020-07-28 20:39:32 +02:00
return $this -> channels_state -> get ( 0 );
2016-12-30 16:32:25 +01:00
}
2019-12-28 16:07:09 +01:00
/**
* Load channel state .
*
* @ param ? int $channelId Channel ID
* @ param array $init Init
*
* @ internal
*
* @ return UpdatesState | UpdatesState []
*/
2019-05-30 13:28:50 +02:00
public function loadChannelState ( $channelId = null , $init = [])
2019-05-29 15:17:14 +02:00
{
2019-05-30 13:28:50 +02:00
return $this -> channels_state -> get ( $channelId , $init );
}
2019-12-28 16:07:09 +01:00
/**
* Get channel states .
*
* @ internal
*
* @ return CombinedUpdatesState
*/
2019-05-30 13:28:50 +02:00
public function getChannelStates ()
{
return $this -> channels_state ;
2019-05-29 15:17:14 +02:00
}
2019-12-28 16:07:09 +01:00
/**
* Get update state .
*
* @ internal
*
* @ return \Generator
*/
public function getUpdatesState () : \Generator
2016-12-30 16:36:44 +01:00
{
2020-09-22 11:48:12 +02:00
$data = yield from $this -> methodCallAsyncRead ( 'updates.getState' , [], $this -> settings -> getDefaultDcParams ());
yield from $this -> getCdnConfig ( $this -> settings -> getDefaultDc ());
2017-06-08 14:30:02 +02:00
return $data ;
2016-12-23 12:19:13 +01:00
}
2019-12-28 16:07:09 +01:00
/**
* Undocumented function .
*
* @ param array $updates Updates
* @ param array $actual_updates Actual updates for deferred
*
* @ internal
*
* @ return \Generator
*/
public function handleUpdates ( $updates , $actual_updates = null ) : \Generator
2019-08-31 23:07:20 +02:00
{
if ( $actual_updates ) {
$updates = $actual_updates ;
}
2020-04-05 22:22:47 +02:00
$this -> logger -> logger ( 'Parsing updates (' . $updates [ '_' ] . ') received via the socket...' , \danog\MadelineProto\Logger :: VERBOSE );
2019-08-31 23:07:20 +02:00
switch ( $updates [ '_' ]) {
case 'updates' :
case 'updatesCombined' :
$result = [];
foreach ( $updates [ 'updates' ] as $key => $update ) {
2020-01-31 19:29:43 +01:00
if ( $update [ '_' ] === 'updateNewMessage' || $update [ '_' ] === 'updateReadMessagesContents' || $update [ '_' ] === 'updateEditMessage' || $update [ '_' ] === 'updateDeleteMessages' || $update [ '_' ] === 'updateReadHistoryInbox' || $update [ '_' ] === 'updateReadHistoryOutbox' || $update [ '_' ] === 'updateWebPage' || $update [ '_' ] === 'updateMessageID' ) {
2020-07-28 20:39:32 +02:00
$result [ yield from $this -> feeders [ FeedLoop :: GENERIC ] -> feedSingle ( $update )] = true ;
2019-08-31 23:07:20 +02:00
unset ( $updates [ 'updates' ][ $key ]);
}
}
$this -> seqUpdater -> addPendingWakeups ( $result );
if ( $updates [ 'updates' ]) {
if ( $updates [ '_' ] === 'updatesCombined' ) {
$updates [ 'options' ] = [ 'seq_start' => $updates [ 'seq_start' ], 'seq_end' => $updates [ 'seq' ], 'date' => $updates [ 'date' ]];
} else {
$updates [ 'options' ] = [ 'seq_start' => $updates [ 'seq' ], 'seq_end' => $updates [ 'seq' ], 'date' => $updates [ 'date' ]];
}
$this -> seqUpdater -> feed ( $updates );
}
$this -> seqUpdater -> resume ();
break ;
case 'updateShort' :
2020-07-28 20:39:32 +02:00
$this -> feeders [ yield from $this -> feeders [ FeedLoop :: GENERIC ] -> feedSingle ( $updates [ 'update' ])] -> resume ();
2019-08-31 23:07:20 +02:00
break ;
case 'updateShortSentMessage' :
if ( ! isset ( $updates [ 'request' ][ 'body' ])) {
break ;
}
2020-01-31 19:49:58 +01:00
$updates [ 'user_id' ] = ( yield from $this -> getInfo ( $updates [ 'request' ][ 'body' ][ 'peer' ]))[ 'bot_api_id' ];
2019-08-31 23:07:20 +02:00
$updates [ 'message' ] = $updates [ 'request' ][ 'body' ][ 'message' ];
unset ( $updates [ 'request' ]);
2020-01-31 19:29:43 +01:00
// no break
2019-08-31 23:07:20 +02:00
case 'updateShortMessage' :
case 'updateShortChatMessage' :
$from_id = isset ( $updates [ 'from_id' ]) ? $updates [ 'from_id' ] : ( $updates [ 'out' ] ? $this -> authorization [ 'user' ][ 'id' ] : $updates [ 'user_id' ]);
$to_id = isset ( $updates [ 'chat_id' ]) ? - $updates [ 'chat_id' ] : ( $updates [ 'out' ] ? $updates [ 'user_id' ] : $this -> authorization [ 'user' ][ 'id' ]);
2020-01-31 20:28:47 +01:00
if ( ! (( yield from $this -> peerIsset ( $from_id )) || ! (( yield from $this -> peerIsset ( $to_id )) || isset ( $updates [ 'via_bot_id' ]) && ! (( yield from $this -> peerIsset ( $updates [ 'via_bot_id' ])) || isset ( $updates [ 'entities' ]) && ! (( yield from $this -> entitiesPeerIsset ( $updates [ 'entities' ])) || isset ( $updates [ 'fwd_from' ]) && ! ( yield from $this -> fwdPeerIsset ( $updates [ 'fwd_from' ]))))))) {
2020-07-28 20:39:32 +02:00
yield $this -> updaters [ FeedLoop :: GENERIC ] -> resume ();
2019-08-31 23:07:20 +02:00
return ;
}
$message = $updates ;
$message [ '_' ] = 'message' ;
try {
2020-10-01 18:02:54 +02:00
$message [ 'from_id' ] = ( yield from $this -> getInfo ( $from_id ))[ 'Peer' ];
$message [ 'peer_id' ] = ( yield from $this -> getInfo ( $to_id ))[ 'Peer' ];
2019-08-31 23:07:20 +02:00
} catch ( \danog\MadelineProto\Exception $e ) {
$this -> logger -> logger ( 'Still did not get user in database, postponing update' , \danog\MadelineProto\Logger :: ERROR );
//$this->pending_updates[] = $updates;
break ;
} catch ( \danog\MadelineProto\RPCErrorException $e ) {
$this -> logger -> logger ( 'Still did not get user in database, postponing update' , \danog\MadelineProto\Logger :: ERROR );
//$this->pending_updates[] = $updates;
break ;
}
$update = [ '_' => 'updateNewMessage' , 'message' => $message , 'pts' => $updates [ 'pts' ], 'pts_count' => $updates [ 'pts_count' ]];
2020-07-28 20:39:32 +02:00
$this -> feeders [ yield from $this -> feeders [ FeedLoop :: GENERIC ] -> feedSingle ( $update )] -> resume ();
2019-08-31 23:07:20 +02:00
break ;
case 'updatesTooLong' :
2020-07-28 20:39:32 +02:00
$this -> updaters [ UpdateLoop :: GENERIC ] -> resume ();
2019-08-31 23:07:20 +02:00
break ;
default :
2020-04-05 22:22:47 +02:00
throw new \danog\MadelineProto\ResponseException ( 'Unrecognized update received: ' . \var_export ( $updates , true ));
2019-08-31 23:07:20 +02:00
break ;
}
}
2019-12-28 16:07:09 +01:00
/**
* Save update .
*
* @ param array $update Update to save
*
* @ internal
*
* @ return \Generator
*/
public function saveUpdate ( array $update ) : \Generator
2016-12-26 20:24:24 +01:00
{
2018-07-13 17:20:47 +02:00
if ( $update [ '_' ] === 'updateConfig' ) {
$this -> config [ 'expires' ] = 0 ;
2020-01-31 19:49:58 +01:00
yield from $this -> getConfig ();
2018-07-13 17:20:47 +02:00
}
2019-09-02 17:08:36 +02:00
if ( \in_array ( $update [ '_' ], [ 'updateUserName' , 'updateUserPhone' , 'updateUserBlocked' , 'updateUserPhoto' , 'updateContactRegistered' , 'updateContactLink' ])) {
2019-10-29 21:33:23 +01:00
$id = $this -> getId ( $update );
2020-05-03 03:33:54 +02:00
$chat = yield $this -> full_chats [ $id ];
2020-04-27 02:21:18 +02:00
$chat [ 'last_update' ] = 0 ;
$this -> full_chats [ $id ] = $chat ;
2020-01-31 19:49:58 +01:00
yield from $this -> getFullInfo ( $id );
2018-07-13 17:20:47 +02:00
}
2017-02-11 15:16:57 +01:00
if ( $update [ '_' ] === 'updateDcOptions' ) {
2018-04-08 19:53:30 +02:00
$this -> logger -> logger ( 'Got new dc options' , \danog\MadelineProto\Logger :: VERBOSE );
2019-09-17 21:35:53 +02:00
$this -> config [ 'dc_options' ] = $update [ 'dc_options' ];
2020-01-31 19:49:58 +01:00
yield from $this -> parseConfig ();
2017-02-11 15:16:57 +01:00
return ;
}
2017-03-24 21:01:08 +01:00
if ( $update [ '_' ] === 'updatePhoneCall' ) {
2019-09-02 17:08:36 +02:00
if ( ! \class_exists ( '\\danog\\MadelineProto\\VoIP' )) {
2018-04-08 19:53:30 +02:00
$this -> logger -> logger ( 'The php-libtgvoip extension is required to accept and manage calls. See daniil.it/MadelineProto for more info.' , \danog\MadelineProto\Logger :: WARNING );
2017-07-20 16:20:19 +02:00
return ;
}
2017-03-24 21:01:08 +01:00
switch ( $update [ 'phone_call' ][ '_' ]) {
case 'phoneCallRequested' :
2018-02-24 17:54:13 +01:00
if ( isset ( $this -> calls [ $update [ 'phone_call' ][ 'id' ]])) {
return ;
}
2019-03-29 20:25:42 +01:00
$controller = new \danog\MadelineProto\VoIP ( false , $update [ 'phone_call' ][ 'admin_id' ], $this , \danog\MadelineProto\VoIP :: CALL_STATE_INCOMING );
$controller -> setCall ( $update [ 'phone_call' ]);
2018-02-24 17:54:13 +01:00
$controller -> storage = [ 'g_a_hash' => $update [ 'phone_call' ][ 'g_a_hash' ]];
$update [ 'phone_call' ] = $this -> calls [ $update [ 'phone_call' ][ 'id' ]] = $controller ;
break ;
2017-04-02 16:42:17 +02:00
case 'phoneCallAccepted' :
2020-01-31 19:49:58 +01:00
if ( ! ( yield from $this -> confirmCall ( $update [ 'phone_call' ]))) {
2018-02-24 17:54:13 +01:00
return ;
}
$update [ 'phone_call' ] = $this -> calls [ $update [ 'phone_call' ][ 'id' ]];
break ;
2017-03-24 21:01:08 +01:00
case 'phoneCall' :
2020-01-31 19:49:58 +01:00
if ( ! ( yield from $this -> completeCall ( $update [ 'phone_call' ]))) {
2018-02-24 17:54:13 +01:00
return ;
}
$update [ 'phone_call' ] = $this -> calls [ $update [ 'phone_call' ][ 'id' ]];
break ;
2017-04-02 16:42:17 +02:00
case 'phoneCallDiscarded' :
2018-02-24 17:54:13 +01:00
if ( ! isset ( $this -> calls [ $update [ 'phone_call' ][ 'id' ]])) {
return ;
}
2020-03-03 00:15:21 +01:00
return $this -> calls [ $update [ 'phone_call' ][ 'id' ]] -> discard ( $update [ 'phone_call' ][ 'reason' ] ? ? [], [], $update [ 'phone_call' ][ 'need_debug' ] ? ? false );
2017-03-24 21:01:08 +01:00
}
}
2017-03-11 19:54:51 +01:00
if ( $update [ '_' ] === 'updateNewEncryptedMessage' && ! isset ( $update [ 'message' ][ 'decrypted_message' ])) {
2019-06-21 16:49:22 +02:00
if ( isset ( $update [ 'qts' ])) {
2020-01-31 19:29:43 +01:00
$cur_state = ( yield from $this -> loadUpdateState ());
2019-06-21 16:49:22 +02:00
if ( $cur_state -> qts () === - 1 ) {
$cur_state -> qts ( $update [ 'qts' ]);
}
if ( $update [ 'qts' ] < $cur_state -> qts ()) {
2020-04-05 22:22:47 +02:00
$this -> logger -> logger ( 'Duplicate update. update qts: ' . $update [ 'qts' ] . ' <= current qts ' . $cur_state -> qts () . ', chat id: ' . $update [ 'message' ][ 'chat_id' ], \danog\MadelineProto\Logger :: ERROR );
2019-06-21 16:49:22 +02:00
return false ;
}
if ( $update [ 'qts' ] > $cur_state -> qts () + 1 ) {
2020-04-05 22:22:47 +02:00
$this -> logger -> logger ( 'Qts hole. Fetching updates manually: update qts: ' . $update [ 'qts' ] . ' > current qts ' . $cur_state -> qts () . '+1, chat id: ' . $update [ 'message' ][ 'chat_id' ], \danog\MadelineProto\Logger :: ERROR );
2020-07-28 20:39:32 +02:00
$this -> updaters [ UpdateLoop :: GENERIC ] -> resumeDefer ();
2019-06-21 16:49:22 +02:00
return false ;
}
2020-04-05 22:22:47 +02:00
$this -> logger -> logger ( 'Applying qts: ' . $update [ 'qts' ] . ' over current qts ' . $cur_state -> qts () . ', chat id: ' . $update [ 'message' ][ 'chat_id' ], \danog\MadelineProto\Logger :: VERBOSE );
2020-09-22 11:48:12 +02:00
yield from $this -> methodCallAsyncRead ( 'messages.receivedQueue' , [ 'max_qts' => $cur_state -> qts ( $update [ 'qts' ])], $this -> settings -> getDefaultDcParams ());
2017-03-11 19:54:51 +01:00
}
2020-01-31 19:49:58 +01:00
yield from $this -> handleEncryptedUpdate ( $update );
2017-03-11 19:54:51 +01:00
return ;
}
/*
2020-01-31 19:29:43 +01:00
if ( $update [ '_' ] === 'updateEncryptedChatTyping' ) {
$update = [ '_' => 'updateUserTyping' , 'user_id' => $this -> encrypted_chats [ $update [ 'chat_id' ]][ 'user_id' ], 'action' => [ '_' => 'sendMessageTypingAction' ]];
}
*/
2017-03-11 19:54:51 +01:00
if ( $update [ '_' ] === 'updateEncryption' ) {
switch ( $update [ 'chat' ][ '_' ]) {
case 'encryptedChatRequested' :
2020-09-22 11:48:12 +02:00
if ( ! $this -> settings -> getSecretChats () -> canAccept ( $update [ 'chat' ][ 'admin_id' ])) {
2018-02-24 17:54:13 +01:00
return ;
}
2020-04-05 22:22:47 +02:00
$this -> logger -> logger ( 'Accepting secret chat ' . $update [ 'chat' ][ 'id' ], \danog\MadelineProto\Logger :: NOTICE );
2019-12-26 15:14:27 +01:00
try {
2020-01-31 19:49:58 +01:00
yield from $this -> acceptSecretChat ( $update [ 'chat' ]);
2019-12-26 15:14:27 +01:00
} catch ( RPCErrorException $e ) {
2020-01-31 19:29:43 +01:00
$this -> logger -> logger ( " Error while accepting secret chat: { $e } " , Logger :: FATAL_ERROR );
2019-12-26 15:14:27 +01:00
}
2018-02-24 17:54:13 +01:00
break ;
2017-03-11 19:54:51 +01:00
case 'encryptedChatDiscarded' :
2020-04-05 22:22:47 +02:00
$this -> logger -> logger ( 'Deleting secret chat ' . $update [ 'chat' ][ 'id' ] . ' because it was revoked by the other user' , \danog\MadelineProto\Logger :: NOTICE );
2018-02-24 17:54:13 +01:00
if ( isset ( $this -> secret_chats [ $update [ 'chat' ][ 'id' ]])) {
unset ( $this -> secret_chats [ $update [ 'chat' ][ 'id' ]]);
}
if ( isset ( $this -> temp_requested_secret_chats [ $update [ 'chat' ][ 'id' ]])) {
unset ( $this -> temp_requested_secret_chats [ $update [ 'chat' ][ 'id' ]]);
}
2018-08-02 14:12:07 +02:00
if ( isset ( $this -> temp_rekeyed_secret_chats [ $update [ 'chat' ][ 'id' ]])) {
unset ( $this -> temp_rekeyed_secret_chats [ $update [ 'chat' ][ 'id' ]]);
}
2018-02-24 17:54:13 +01:00
break ;
2017-03-11 19:54:51 +01:00
case 'encryptedChat' :
2020-04-05 22:22:47 +02:00
$this -> logger -> logger ( 'Completing creation of secret chat ' . $update [ 'chat' ][ 'id' ], \danog\MadelineProto\Logger :: NOTICE );
2020-01-31 19:49:58 +01:00
yield from $this -> completeSecretChat ( $update [ 'chat' ]);
2018-02-24 17:54:13 +01:00
break ;
2017-03-11 19:54:51 +01:00
}
2018-04-08 19:53:30 +02:00
//$this->logger->logger($update, \danog\MadelineProto\Logger::NOTICE);
2017-03-11 19:54:51 +01:00
}
2019-04-30 22:11:58 +02:00
//if ($update['_'] === 'updateServiceNotification' && strpos($update['type'], 'AUTH_KEY_DROP_') === 0) {
//}
2020-09-22 11:48:12 +02:00
if ( ! $this -> updateHandler ) {
2017-01-07 12:40:51 +01:00
return ;
}
2017-01-29 22:42:21 +01:00
if ( isset ( $update [ 'message' ][ '_' ]) && $update [ 'message' ][ '_' ] === 'messageEmpty' ) {
return ;
}
2020-10-06 22:02:04 +02:00
if ( isset ( $update [ 'message' ][ 'from_id' ][ 'user_id' ])) {
if ( $update [ 'message' ][ 'from_id' ][ 'user_id' ] === $this -> authorization [ 'user' ][ 'id' ]) {
$update [ 'message' ][ 'out' ] = true ;
}
} elseif ( ! isset ( $update [ 'message' ][ 'from_id' ])
&& isset ( $update [ 'message' ][ 'peer_id' ][ 'user_id' ])
&& $update [ 'message' ][ 'peer_id' ][ 'user_id' ] === $this -> authorization [ 'user' ][ 'id' ]) {
2017-01-08 13:21:35 +01:00
$update [ 'message' ][ 'out' ] = true ;
}
2020-10-06 22:02:04 +02:00
if ( ! isset ( $update [ 'message' ][ 'from_id' ]) && isset ( $update [ 'message' ][ 'peer_id' ][ 'user_id' ])) {
$update [ 'message' ][ 'from_id' ] = $update [ 'message' ][ 'peer_id' ];
}
2020-10-03 17:41:21 +02:00
if ( isset ( $update [ 'message' ][ 'peer_id' ])) {
$update [ 'message' ][ 'to_id' ] = $update [ 'message' ][ 'peer_id' ];
}
2020-09-22 11:48:12 +02:00
// First save to array, then once the feed loop signals resumal of loop, resume and handle
$this -> updates [ $this -> updates_key ++ ] = $update ;
2017-01-29 02:47:06 +01:00
}
2018-02-24 17:54:39 +01:00
}