This commit is contained in:
Daniil Gentili 2020-02-05 17:18:22 +01:00
parent e96f3ab593
commit 066a10e2dc
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
10 changed files with 44 additions and 20 deletions

1
.gitignore vendored
View File

@ -123,3 +123,4 @@ vendor
coverage
tempConv
extracted.json
.phpunit.result.cache

View File

@ -69,7 +69,7 @@ abstract class AbstractAPIFactory extends AsyncConstruct
protected $methods = [];
public function __construct($namespace, &$API, &$async)
{
$this->namespace = $namespace . '.';
$this->namespace = $namespace.'.';
$this->API =& $API;
$this->async =& $async;
}
@ -153,14 +153,15 @@ abstract class AbstractAPIFactory extends AsyncConstruct
}
$lower_name = \strtolower($name);
if ($this->namespace !== '' || !isset($this->methods[$lower_name])) {
$name = $this->namespace . $name;
$name = $this->namespace.$name;
$aargs = isset($arguments[1]) && \is_array($arguments[1]) ? $arguments[1] : [];
$aargs['apifactory'] = true;
$aargs['datacenter'] = $this->API->datacenter->curdc;
$args = isset($arguments[0]) && \is_array($arguments[0]) ? $arguments[0] : [];
return yield $this->API->methodCallAsyncRead($name, $args, $aargs);
}
return yield $this->methods[$lower_name](...$arguments);
$res = yield $this->methods[$lower_name](...$arguments);
return $res instanceof \Generator ? yield from $res : yield $res;
}
/**
* Get attribute.

View File

@ -161,7 +161,7 @@ final class Coroutine implements Promise, \ArrayAccess
*
* @internal
*
* @return void
* @return mixed
*/
public function throw(\Throwable $reason)
{
@ -172,7 +172,7 @@ final class Coroutine implements Promise, \ArrayAccess
$reason->yieldedFrames = $this->getTrace();
}
}
$this->generator->throw($reason);
return $this->generator->throw($reason);
}
/**
* @param \Throwable $reason Failure reason.

View File

@ -88,7 +88,7 @@ trait CallHandler
{
$deferred = new Deferred();
$this->methodCallAsyncWrite($method, $args, $aargs)->onResolve(
static function (\Throwable $e, $readDeferred) use ($deferred): void {
static function (?\Throwable $e, $readDeferred) use ($deferred, $method): void {
if ($e) {
$deferred->fail($e);
} else {
@ -96,13 +96,12 @@ trait CallHandler
$readDeferred = \array_map(fn (Deferred $value) => $value->promise(), $readDeferred);
$deferred->resolve(all($readDeferred));
} else {
$readDeferred->promise()->onResolve(fn(\Throwable $e, $res) => var_dump($e, $res));
$deferred->resolve($readDeferred->promise());
}
}
}
);
return $aargs['noResponse'] ?? false ? new Success() : $deferred->promise();
return ($aargs['noResponse'] ?? false) ? new Success() : $deferred->promise();
}
/**
* Call method and make sure it is asynchronously sent.
@ -130,12 +129,12 @@ trait CallHandler
{
if (\is_array($args) && isset($args['id']['_']) && isset($args['id']['dc_id']) && $args['id']['_'] === 'inputBotInlineMessageID' && $this->datacenter !== $args['id']['dc_id']) {
$aargs['datacenter'] = $args['id']['dc_id'];
return $this->API->methodCallAsyncWriteGenerator($method, $args, $aargs);
return $this->API->methodCallAsyncWrite($method, $args, $aargs);
}
if (($aargs['file'] ?? false) && !$this->isMedia() && $this->API->datacenter->has($this->datacenter.'_media')) {
$this->logger->logger('Using media DC');
$aargs['datacenter'] = $this->datacenter.'_media';
return $this->API->methodCallAsyncWriteGenerator($method, $args, $aargs);
return $this->API->methodCallAsyncWrite($method, $args, $aargs);
}
if (\in_array($method, ['messages.setEncryptedTyping', 'messages.readEncryptedHistory', 'messages.sendEncrypted', 'messages.sendEncryptedFile', 'messages.sendEncryptedService', 'messages.receivedQueue'])) {
$aargs['queue'] = 'secret';
@ -158,7 +157,7 @@ trait CallHandler
unset($args['multiple']);
}
foreach ($args as $single_args) {
$promises[] = $this->methodCallAsyncWrite($method, $single_args, $new_aargs);
$promises[] = yield from $this->methodCallAsyncWriteGenerator($method, $single_args, $new_aargs);
}
if (!isset($aargs['postpone'])) {
$this->writer->resume();

View File

@ -21,6 +21,7 @@ namespace danog\MadelineProto\MTProtoTools;
use Amp\Deferred;
use Amp\Promise;
use danog\MadelineProto\Connection;
/**
* Manages method and object calls.
@ -54,9 +55,10 @@ trait CallHandler
public function methodCallAsyncRead(string $method, $args = [], array $aargs = ['msg_id' => null]): Promise
{
$deferred = new Deferred();
$this->datacenter->waitGetConnection($aargs['datacenter'] ?? $this->datacenter->curdc)->onResolve(static function ($e, $res) use (&$method, &$args, &$aargs, &$deferred) {
$this->datacenter->waitGetConnection($aargs['datacenter'] ?? $this->datacenter->curdc)->onResolve(static function (?\Throwable $e, ?Connection $res) use (&$method, &$args, &$aargs, &$deferred): void {
if ($e) {
throw $e;
$deferred->fail($e);
return;
}
$deferred->resolve($res->methodCallAsyncRead($method, $args, $aargs));
});

View File

@ -39,6 +39,7 @@ use danog\MadelineProto\Stream\Transport\PremadeStream;
use danog\MadelineProto\Tools;
use function Amp\File\exists;
use function Amp\File\open;
use function Amp\Filestat;
use function Amp\Promise\all;
/**
@ -80,7 +81,7 @@ trait Files
$fileName = \basename($file);
}
StatCache::clear($file);
$size = (yield \stat($file))['size'];
$size = (yield stat($file))['size'];
if ($size > 512 * 1024 * 3000) {
throw new \danog\MadelineProto\Exception('Given file is too big!');
}
@ -939,7 +940,7 @@ trait Files
$file = \realpath($file);
$message_media = (yield from $this->getDownloadInfo($message_media));
StatCache::clear($file);
$size = (yield \stat($file))['size'];
$size = (yield stat($file))['size'];
$stream = yield open($file, 'cb');
$this->logger->logger('Waiting for lock of file to download...');
$unlock = yield \danog\MadelineProto\Tools::flock($file, LOCK_EX);
@ -1090,6 +1091,7 @@ trait Files
$origCb(100, 0, 0);
return true;
}
$parallel_chunks = 1;
if ($params) {
$previous_promise = new Success(true);
$promises = [];
@ -1232,7 +1234,7 @@ trait Files
$res['bytes'] = \substr($res['bytes'], $offset['part_start_at'], $offset['part_end_at'] - $offset['part_start_at']);
}
if (!$seekable) {
yield from $offset['previous_promise'];
yield $offset['previous_promise'];
}
$res = yield $callable($res['bytes'], $offset['offset'] + $offset['part_start_at']);
$cb();

View File

@ -183,6 +183,9 @@ trait Tools
if (\is_int($value)) {
return (string) $value;
}
if (\is_array($value) && count($value) === 2) {
$value = pack('l2', $value);
}
if (\strlen($value) !== 8) {
throw new TL\Exception(\danog\MadelineProto\Lang::$current_lang['length_not_8']);
}

View File

@ -5,7 +5,6 @@ namespace danog\MadelineProto\Test;
use danog\MadelineProto\DataCenter;
use danog\MadelineProto\Logger;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\Stream\Transport\DefaultStream;
use danog\MadelineProto\Tools;
use PHPUnit\Framework\TestCase;
@ -36,14 +35,19 @@ final class DataCenterTest extends TestCase
'obfuscated' => $obfuscated,
'transport' => $transport
],
],
'logger' => [
'logger' => Logger::FILE_LOGGER,
'logger_param' => getcwd().'/MadelineProto.log',
'logger_level' => Logger::ULTRA_VERBOSE
]
]
);
$datacenter = new DataCenter(
new class($settings) {
$API = new class($settings) {
/**
* Constructor.
*
*
* @param array $settings Logger settings
*/
public function __construct(array $settings)
@ -64,6 +68,7 @@ final class DataCenterTest extends TestCase
$settings['connection'],
$settings['connection_settings'],
);
$API->datacenter = $datacenter;
Tools::wait($datacenter->dcConnect(2));
$this->assertTrue(true);

View File

@ -31,7 +31,8 @@ class FileIdTest extends TestCase
'api_hash' => \getenv('API_HASH'),
],
'logger' => [
'logger' => Logger::ECHO_LOGGER,
'logger' => Logger::FILE_LOGGER,
'logger_param' => getcwd().'/MadelineProto.log',
'logger_level' => Logger::ULTRA_VERBOSE
]
]
@ -47,7 +48,9 @@ class FileIdTest extends TestCase
*/
public function testDownload(string $type, string $fileIdStr, string $uniqueFileIdStr)
{
self::$MadelineProto->logger("Trying to download $fileIdStr");
self::$MadelineProto->downloadToFile($fileIdStr, '/dev/null');
$this->assertTrue(true);
}
/**
* @param string $fileId File ID
@ -57,12 +60,15 @@ class FileIdTest extends TestCase
*/
public function testResend(string $type, string $fileIdStr, string $uniqueFileIdStr)
{
self::$MadelineProto->logger("Trying to resend $fileIdStr");
self::$MadelineProto->messages->sendMedia(
[
'peer' => \getenv('DEST'),
'media' => $fileIdStr
]
);
$this->assertTrue(true);
}
public function provideFileIdsAndType(): \Generator

View File

@ -17,4 +17,9 @@
<directory>../tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">../src</directory>
</whitelist>
</filter>
</phpunit>