Make more methods internal

This commit is contained in:
Daniil Gentili 2019-12-28 17:34:04 +01:00
parent 3a9dde8c3f
commit 75602e5de0
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
9 changed files with 245 additions and 105 deletions

View File

@ -20,11 +20,16 @@
namespace danog\MadelineProto; namespace danog\MadelineProto;
use Amp\Deferred; use Amp\Deferred;
use Amp\Promise;
use function Amp\File\exists; use function Amp\File\exists;
use function Amp\File\get; use function Amp\File\get;
use function Amp\File\put; use function Amp\File\put;
use function Amp\File\rename as renameAsync; use function Amp\File\rename as renameAsync;
/**
* Main API wrapper for MadelineProto.
*/
class API extends InternalDoc class API extends InternalDoc
{ {
use \danog\Serializable; use \danog\Serializable;
@ -38,13 +43,40 @@ class API extends InternalDoc
* @var MTProto * @var MTProto
*/ */
public $API; public $API;
/**
* Whether we're getting our API ID.
*
* @internal
*
* @var boolean
*/
public $getting_api_id = false; public $getting_api_id = false;
/**
* my.telegram.org API wrapper.
*
* @internal
*
* @var MyTelegramOrgWrapper
*/
public $my_telegram_org_wrapper; public $my_telegram_org_wrapper;
/**
* Async ini tpromise.
*
* @var Promise
*/
public $asyncAPIPromise; public $asyncAPIPromise;
private $oldInstance = false; private $oldInstance = false;
private $destructing = false; private $destructing = false;
public function __magic_construct($params = [], $settings = []) /**
* Magic constructor function.
*
* @param array $params Params
* @param array $settings Settings
*
* @return void
*/
public function __magic_construct($params = [], $settings = []): void
{ {
Magic::classExists(); Magic::classExists();
$deferred = new Deferred(); $deferred = new Deferred();
@ -63,7 +95,16 @@ class API extends InternalDoc
} }
} }
public function __construct_async($params, $settings, $deferred) /**
* Async constructor function.
*
* @param mixed $params Params
* @param mixed $settings Settings
* @param mixed $deferred Deferred
*
* @return \Generator
*/
public function __construct_async($params, $settings, $deferred): \Generator
{ {
if (\is_string($params)) { if (\is_string($params)) {
Logger::constructorFromSettings($settings); Logger::constructorFromSettings($settings);
@ -186,7 +227,14 @@ class API extends InternalDoc
\danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['madelineproto_ready'], Logger::NOTICE); \danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['madelineproto_ready'], Logger::NOTICE);
} }
public function async($async) /**
* Enable or disable async.
*
* @param bool $async Whether to enable or disable async
*
* @return void
*/
public function async(bool $async): void
{ {
$this->async = $async; $this->async = $async;
@ -197,11 +245,11 @@ class API extends InternalDoc
} }
} }
public function __wakeup() /**
{ * Destruct function.
//$this->APIFactory(); *
} * @internal
*/
public function __destruct() public function __destruct()
{ {
if (\danog\MadelineProto\Magic::$has_thread && \is_object(\Thread::getCurrentThread()) || Magic::isFork()) { if (\danog\MadelineProto\Magic::$has_thread && \is_object(\Thread::getCurrentThread()) || Magic::isFork()) {
@ -223,18 +271,37 @@ class API extends InternalDoc
//restore_error_handler(); //restore_error_handler();
} }
public function __sleep() /**
* Sleep function.
*
* @internal
*
* @return array
*/
public function __sleep(): array
{ {
return ['API', 'web_api_template', 'getting_api_id', 'my_telegram_org_wrapper']; return ['API', 'web_api_template', 'getting_api_id', 'my_telegram_org_wrapper'];
} }
/**
* Custom fast getSelf.
*
* @internal
*
* @return array|false
*/
public function myGetSelf() public function myGetSelf()
{ {
return isset($this->API) && isset($this->API->authorization['user']) ? $this->API->authorization['user'] : false; return isset($this->API) && isset($this->API->authorization['user']) ? $this->API->authorization['user'] : false;
} }
public function APIFactory() /**
* Init API wrapper.
*
* @return void
*/
private function APIFactory(): void
{ {
if ($this->API && !$this->API->asyncInitPromise) { if ($this->API && !$this->API->asyncInitPromise) {
foreach ($this->API->getMethodNamespaces() as $namespace) { foreach ($this->API->getMethodNamespaces() as $namespace) {
@ -281,7 +348,12 @@ class API extends InternalDoc
} }
} }
public function getAllMethods() /**
* Get full list of MTProto and API methods.
*
* @return array
*/
public function getAllMethods(): array
{ {
if ($this->asyncInitPromise) { if ($this->asyncInitPromise) {
$this->init(); $this->init();
@ -294,14 +366,20 @@ class API extends InternalDoc
return \array_merge($methods, \get_class_methods($this->API)); return \array_merge($methods, \get_class_methods($this->API));
} }
public function serialize($filename = null) /**
* Serialize session.
*
* @param string $filename File name
*
* @internal Do not use this manually, the session is already serialized automatically
*
* @return Promise
*/
public function serialize(string $filename = ''): Promise
{ {
return Tools::callFork((function () use ($filename) { return Tools::callFork((function () use ($filename) {
if ($filename === null) {
$filename = $this->session;
}
if (empty($filename)) { if (empty($filename)) {
return; $filename = $this->session;
} }
//Logger::log(\danog\MadelineProto\Lang::$current_lang['serializing_madelineproto']); //Logger::log(\danog\MadelineProto\Lang::$current_lang['serializing_madelineproto']);

View File

@ -23,12 +23,50 @@ use danog\MadelineProto\Async\AsyncConstruct;
abstract class AbstractAPIFactory extends AsyncConstruct abstract class AbstractAPIFactory extends AsyncConstruct
{ {
/**
* Namespace.
*
* @internal
*
* @var string
*/
public $namespace = ''; public $namespace = '';
/**
* MTProto instance.
*
* @internal
*
* @var MTProto
*/
public $API; public $API;
/**
* Whether lua is being used.
*
* @internal
*
* @var boolean
*/
public $lua = false; public $lua = false;
/**
* Whether async is enabled.
*
* @internal
*
* @var boolean
*/
public $async = false; public $async = false;
/**
* Async init promise.
*
* @var Promise
*/
public $asyncAPIPromise; public $asyncAPIPromise;
/**
* Method list.
*
* @var string[]
*/
protected $methods = []; protected $methods = [];
public function __construct($namespace, &$API, &$async) public function __construct($namespace, &$API, &$async)
@ -38,12 +76,29 @@ abstract class AbstractAPIFactory extends AsyncConstruct
$this->async = &$async; $this->async = &$async;
} }
public function async($async) /**
* Enable or disable async.
*
* @param bool $async Whether to enable or disable async
*
* @return void
*/
public function async(bool $async): void
{ {
$this->async = $async; $this->async = $async;
} }
public function __call($name, $arguments) /**
* Call async wrapper function.
*
* @param string $name Method name
* @param array $arguments Arguments
*
* @internal
*
* @return mixed
*/
public function __call(string $name, array $arguments)
{ {
$yielded = Tools::call($this->__call_async($name, $arguments)); $yielded = Tools::call($this->__call_async($name, $arguments));
$async = $this->lua === false && (\is_array(\end($arguments)) && isset(\end($arguments)['async']) ? \end($arguments)['async'] : ($this->async && $name !== 'loop')); $async = $this->lua === false && (\is_array(\end($arguments)) && isset(\end($arguments)['async']) ? \end($arguments)['async'] : ($this->async && $name !== 'loop'));
@ -65,7 +120,17 @@ abstract class AbstractAPIFactory extends AsyncConstruct
} }
} }
public function __call_async($name, $arguments) /**
* Call async wrapper function.
*
* @param string $name Method name
* @param array $arguments Arguments
*
* @internal
*
* @return \Generator
*/
public function __call_async(string $name, array $arguments): \Generator
{ {
if ($this->asyncInitPromise) { if ($this->asyncInitPromise) {
yield $this->initAsynchronously(); yield $this->initAsynchronously();
@ -108,7 +173,16 @@ abstract class AbstractAPIFactory extends AsyncConstruct
return yield $this->methods[$lower_name](...$arguments); return yield $this->methods[$lower_name](...$arguments);
} }
public function &__get($name) /**
* Get attribute.
*
* @param string $name Attribute nam
*
* @internal
*
* @return mixed
*/
public function &__get(string $name)
{ {
if ($this->asyncAPIPromise) { if ($this->asyncAPIPromise) {
Tools::wait($this->asyncAPIPromise); Tools::wait($this->asyncAPIPromise);
@ -125,7 +199,17 @@ abstract class AbstractAPIFactory extends AsyncConstruct
return $this->API->storage[$name]; return $this->API->storage[$name];
} }
public function __set($name, $value) /**
* Set an attribute.
*
* @param string $name Name
* @param mixed $value Value
*
* @internal
*
* @return mixed
*/
public function __set(string $name, $value)
{ {
if ($this->asyncAPIPromise) { if ($this->asyncAPIPromise) {
Tools::wait($this->asyncAPIPromise); Tools::wait($this->asyncAPIPromise);
@ -141,7 +225,14 @@ abstract class AbstractAPIFactory extends AsyncConstruct
return $this->API->storage[$name] = $value; return $this->API->storage[$name] = $value;
} }
public function __isset($name) /**
* Whether an attribute exists.
*
* @param string $name Attribute name
*
* @return boolean
*/
public function __isset(string $name): bool
{ {
if ($this->asyncAPIPromise) { if ($this->asyncAPIPromise) {
Tools::wait($this->asyncAPIPromise); Tools::wait($this->asyncAPIPromise);
@ -150,7 +241,14 @@ abstract class AbstractAPIFactory extends AsyncConstruct
return isset($this->API->storage[$name]); return isset($this->API->storage[$name]);
} }
public function __unset($name) /**
* Unset attribute.
*
* @param string $name Attribute name
*
* @return void
*/
public function __unset(string $name): void
{ {
if ($this->asyncAPIPromise) { if ($this->asyncAPIPromise) {
Tools::wait($this->asyncAPIPromise); Tools::wait($this->asyncAPIPromise);

View File

@ -91,7 +91,7 @@ class AnnotationsBuilder
$class = new \ReflectionClass($this->reflectionClasses['API']); $class = new \ReflectionClass($this->reflectionClasses['API']);
$methods = $class->getMethods(\ReflectionMethod::IS_STATIC | \ReflectionMethod::IS_PUBLIC); $methods = $class->getMethods(\ReflectionMethod::IS_STATIC | \ReflectionMethod::IS_PUBLIC);
$ignoreMethods = []; $ignoreMethods = ['fetchserializableobject'];
foreach ($methods as $method) { foreach ($methods as $method) {
$ignoreMethods[$method->getName()] = $method->getName(); $ignoreMethods[$method->getName()] = $method->getName();
} }

View File

@ -29,23 +29,47 @@ use danog\MadelineProto\Tools;
*/ */
class AsyncConstruct class AsyncConstruct
{ {
/**
* Async init promise.
*
* @var Promise
*/
public $asyncInitPromise; public $asyncInitPromise;
public function init() /**
* Blockingly init.
*
* @return void
*/
public function init(): void
{ {
if ($this->asyncInitPromise) { if ($this->asyncInitPromise) {
Tools::wait($this->asyncInitPromise); Tools::wait($this->asyncInitPromise);
} }
} }
public function initAsynchronously() /**
* Asynchronously init.
*
* @return \Generator
*/
public function initAsynchronously(): \Generator
{ {
if ($this->asyncInitPromise) { if ($this->asyncInitPromise) {
yield $this->asyncInitPromise; yield $this->asyncInitPromise;
} }
} }
public function setInitPromise($promise) /**
* Set init promise.
*
* @param Promise $promise
*
* @internal
*
* @return void
*/
public function setInitPromise($promise): void
{ {
$this->asyncInitPromise = Tools::callFork($promise); $this->asyncInitPromise = Tools::callFork($promise);
$this->asyncInitPromise->onResolve(function ($error, $result) { $this->asyncInitPromise->onResolve(function ($error, $result) {

View File

@ -4173,15 +4173,6 @@ class InternalDoc extends APIFactory
{ {
return $this->__call(__FUNCTION__, [$reconnectAll, $extra]); return $this->__call(__FUNCTION__, [$reconnectAll, $extra]);
} }
/**
* Clean up MadelineProto session after logout.
*
* @return void
*/
public function resetSession(array $extra = []): void
{
$this->__call(__FUNCTION__, [$extra]);
}
/** /**
* Reset the update state and fetch all updates from the beginning. * Reset the update state and fetch all updates from the beginning.
* *
@ -4357,71 +4348,6 @@ class InternalDoc extends APIFactory
{ {
return $this->__call(__FUNCTION__, [$method, $args, $aargs, $extra]); return $this->__call(__FUNCTION__, [$method, $args, $aargs, $extra]);
} }
/**
* AES KDF function for MTProto v2.
*
* @param string $msg_key Message key
* @param string $auth_key Auth key
* @param boolean $to_server To server/from server direction
*
* @return array
*/
public function aesCalculate(string $msg_key, string $auth_key, bool $to_server = true): array
{
return \danog\MadelineProto\MTProto::aesCalculate($msg_key, $auth_key, $to_server);
}
/**
* AES KDF function for MTProto v1.
*
* @param string $msg_key Message key
* @param string $auth_key Auth key
* @param boolean $to_server To server/from server direction
*
* @return array
*/
public function oldAesCalculate(string $msg_key, string $auth_key, bool $to_server = true): array
{
return \danog\MadelineProto\MTProto::oldAesCalculate($msg_key, $auth_key, $to_server);
}
/**
* CTR encrypt.
*
* @param string $message Message to encrypt
* @param string $key Key
* @param string $iv IV
*
* @return string
*/
public function ctrEncrypt(string $message, string $key, string $iv): string
{
return \danog\MadelineProto\MTProto::ctrEncrypt($message, $key, $iv);
}
/**
* IGE encrypt.
*
* @param string $message Message to encrypt
* @param string $key Key
* @param string $iv IV
*
* @return string
*/
public function igeEncrypt(string $message, string $key, string $iv): string
{
return \danog\MadelineProto\MTProto::igeEncrypt($message, $key, $iv);
}
/**
* CTR decrypt.
*
* @param string $message Message to encrypt
* @param string $key Key
* @param string $iv IV
*
* @return string
*/
public function igeDecrypt(string $message, string $key, string $iv): string
{
return \danog\MadelineProto\MTProto::igeDecrypt($message, $key, $iv);
}
/** /**
* Convert MTProto channel ID to bot API channel ID. * Convert MTProto channel ID to bot API channel ID.
* *

View File

@ -1497,6 +1497,8 @@ class MTProto extends AsyncConstruct implements TLCallback
/** /**
* Clean up MadelineProto session after logout. * Clean up MadelineProto session after logout.
* *
* @internal
*
* @return void * @return void
*/ */
public function resetSession(): void public function resetSession(): void

View File

@ -28,6 +28,8 @@ trait Crypt
* @param string $auth_key Auth key * @param string $auth_key Auth key
* @param boolean $to_server To server/from server direction * @param boolean $to_server To server/from server direction
* *
* @internal
*
* @return array * @return array
*/ */
public static function aesCalculate(string $msg_key, string $auth_key, bool $to_server = true): array public static function aesCalculate(string $msg_key, string $auth_key, bool $to_server = true): array
@ -48,6 +50,8 @@ trait Crypt
* @param string $auth_key Auth key * @param string $auth_key Auth key
* @param boolean $to_server To server/from server direction * @param boolean $to_server To server/from server direction
* *
* @internal
*
* @return array * @return array
*/ */
public static function oldAesCalculate(string $msg_key, string $auth_key, bool $to_server = true): array public static function oldAesCalculate(string $msg_key, string $auth_key, bool $to_server = true): array
@ -70,6 +74,8 @@ trait Crypt
* @param string $key Key * @param string $key Key
* @param string $iv IV * @param string $iv IV
* *
* @internal
*
* @return string * @return string
*/ */
public static function ctrEncrypt(string $message, string $key, string $iv): string public static function ctrEncrypt(string $message, string $key, string $iv): string
@ -88,6 +94,8 @@ trait Crypt
* @param string $key Key * @param string $key Key
* @param string $iv IV * @param string $iv IV
* *
* @internal
*
* @return string * @return string
*/ */
public static function igeEncrypt(string $message, string $key, string $iv): string public static function igeEncrypt(string $message, string $key, string $iv): string
@ -105,6 +113,8 @@ trait Crypt
* @param string $key Key * @param string $key Key
* @param string $iv IV * @param string $iv IV
* *
* @internal
*
* @return string * @return string
*/ */
public static function igeDecrypt(string $message, string $key, string $iv): string public static function igeDecrypt(string $message, string $key, string $iv): string

View File

@ -86,7 +86,7 @@ class APIFactory extends AbstractAPIFactory
* *
* @return mixed * @return mixed
*/ */
public function __call_async($name, $arguments) public function __call_async(string $name, array $arguments): \Generator
{ {
$lower_name = \strtolower($name); $lower_name = \strtolower($name);
if ($this->namespace !== '' || !isset($this->methods[$lower_name])) { if ($this->namespace !== '' || !isset($this->methods[$lower_name])) {
@ -95,8 +95,8 @@ class APIFactory extends AbstractAPIFactory
$aargs['apifactory'] = true; $aargs['apifactory'] = true;
$args = isset($arguments[0]) && \is_array($arguments[0]) ? $arguments[0] : []; $args = isset($arguments[0]) && \is_array($arguments[0]) ? $arguments[0] : [];
return $this->API->methodCall($name, $args, $aargs); return yield $this->API->methodCall($name, $args, $aargs);
} }
return $this->methods[$lower_name](...$arguments); return yield $this->methods[$lower_name](...$arguments);
} }
} }

View File

@ -47,7 +47,9 @@ trait Tools
* Sanify TL obtained from JSON for TL serialization. * Sanify TL obtained from JSON for TL serialization.
* *
* @param array $input Data to sanitize * @param array $input Data to sanitize
*
* @internal
*
* @return array * @return array
*/ */
public static function convertJsonTL(array $input): array public static function convertJsonTL(array $input): array