From 7e281c1589ce67514f95ea8b53d70d69c549c940 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 6 Oct 2020 22:02:04 +0200 Subject: [PATCH] Disable garbage collection logs when reading from console and polyfill from_id --- examples/bot.php | 1 + src/danog/MadelineProto/Coroutine.php | 5 ++++ .../MadelineProto/Loop/Update/FeedLoop.php | 17 +++++++++++- .../MTProtoTools/GarbageCollector.php | 13 ++++++++-- .../MTProtoTools/PeerHandler.php | 4 ++- .../MTProtoTools/UpdateHandler.php | 13 +++++++++- src/danog/MadelineProto/Tools.php | 26 ++++++++++++------- 7 files changed, 64 insertions(+), 15 deletions(-) diff --git a/examples/bot.php b/examples/bot.php index 24f70816..fed847d9 100755 --- a/examples/bot.php +++ b/examples/bot.php @@ -80,6 +80,7 @@ class MyEventHandler extends EventHandler return; } + $res = \json_encode($update, JSON_PRETTY_PRINT); yield $this->messages->sendMessage(['peer' => $update, 'message' => "$res", 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'parse_mode' => 'HTML']); if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') { diff --git a/src/danog/MadelineProto/Coroutine.php b/src/danog/MadelineProto/Coroutine.php index 7662e705..035a035e 100644 --- a/src/danog/MadelineProto/Coroutine.php +++ b/src/danog/MadelineProto/Coroutine.php @@ -272,4 +272,9 @@ final class Coroutine implements Promise, \ArrayAccess } return []; } + + public function __debugInfo() + { + return ['_' => 'To obtain a result from a Coroutine object, yield the result or disable async (not recommended). See https://docs.madelineproto.xyz/docs/ASYNC.html for more information on async.']; + } } diff --git a/src/danog/MadelineProto/Loop/Update/FeedLoop.php b/src/danog/MadelineProto/Loop/Update/FeedLoop.php index 79f0f4e8..d64f6f34 100644 --- a/src/danog/MadelineProto/Loop/Update/FeedLoop.php +++ b/src/danog/MadelineProto/Loop/Update/FeedLoop.php @@ -212,7 +212,22 @@ class FeedLoop extends ResumableSignalLoop $from = false; $via_bot = false; $entities = false; - if ($update['message']['_'] !== 'messageEmpty' && (($from = isset($update['message']['from_id']) && !(yield from $this->API->peerIsset($update['message']['from_id']))) || ($to = !(yield from $this->API->peerIsset($update['message']['peer_id']))) || ($via_bot = isset($update['message']['via_bot_id']) && !(yield from $this->API->peerIsset($update['message']['via_bot_id']))) || ($entities = isset($update['message']['entities']) && !(yield from $this->API->entitiesPeerIsset($update['message']['entities']))))) { + if ($update['message']['_'] !== 'messageEmpty' && ( + ( + $from = isset($update['message']['from_id']) + && !(yield from $this->API->peerIsset($update['message']['from_id'])) + ) || ( + $to = !(yield from $this->API->peerIsset($update['message']['peer_id'])) + ) + || ( + $via_bot = isset($update['message']['via_bot_id']) + && !(yield from $this->API->peerIsset($update['message']['via_bot_id'])) + ) || ( + $entities = isset($update['message']['entities']) + && !(yield from $this->API->entitiesPeerIsset($update['message']['entities'])) + ) + ) + ) { $log = ''; if ($from) { $from_id = $this->API->getId($update['message']['from_id']); diff --git a/src/danog/MadelineProto/MTProtoTools/GarbageCollector.php b/src/danog/MadelineProto/MTProtoTools/GarbageCollector.php index 1e84ae13..c2da5e41 100644 --- a/src/danog/MadelineProto/MTProtoTools/GarbageCollector.php +++ b/src/danog/MadelineProto/MTProtoTools/GarbageCollector.php @@ -26,6 +26,11 @@ class GarbageCollector */ public static int $memoryDiffMb = 1; + /** + * Whether to enable logging. + */ + public static bool $log = false; + /** * Memory consumption after last cleanup. * @var int @@ -45,7 +50,9 @@ class GarbageCollector \gc_collect_cycles(); static::$memoryConsumption = static::getMemoryConsumption(); $cleanedMemory = $currentMemory - static::$memoryConsumption; - Logger::log("gc_collect_cycles done. Cleaned memory: $cleanedMemory Mb", Logger::VERBOSE); + if (static::$log) { + Logger::log("gc_collect_cycles done. Cleaned memory: $cleanedMemory Mb", Logger::VERBOSE); + } } }); } @@ -53,7 +60,9 @@ class GarbageCollector private static function getMemoryConsumption(): int { $memory = \round(\memory_get_usage()/1024/1024, 1); - Logger::log("Memory consumption: $memory Mb", Logger::ULTRA_VERBOSE); + if (static::$log) { + Logger::log("Memory consumption: $memory Mb", Logger::ULTRA_VERBOSE); + } return (int) $memory; } } diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index dd97d213..b1ee3d83 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -386,8 +386,10 @@ trait PeerHandler return $this->toSupergroup($id['channel_id']); case 'message': case 'messageService': - if (!isset($id['from_id']) + if (!isset($id['from_id']) // No other option + // It's a channel/chat, 100% what we need || $id['peer_id']['_'] !== 'peerUser' + // It is a user, and it's not ourselves || $id['peer_id']['user_id'] !== $this->authorization['user']['id'] ) { return $this->getId($id['peer_id']); diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index d01c2d53..ee5a5601 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -412,9 +412,20 @@ trait UpdateHandler if (isset($update['message']['_']) && $update['message']['_'] === 'messageEmpty') { return; } - if (isset($update['message']['from_id']['user_id']) && $update['message']['from_id']['user_id'] === $this->authorization['user']['id']) { + 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']) { $update['message']['out'] = true; } + + if (!isset($update['message']['from_id']) && isset($update['message']['peer_id']['user_id'])) { + $update['message']['from_id'] = $update['message']['peer_id']; + } + if (isset($update['message']['peer_id'])) { $update['message']['to_id'] = $update['message']['peer_id']; } diff --git a/src/danog/MadelineProto/Tools.php b/src/danog/MadelineProto/Tools.php index 2dee580c..07018eda 100644 --- a/src/danog/MadelineProto/Tools.php +++ b/src/danog/MadelineProto/Tools.php @@ -26,6 +26,7 @@ use Amp\Loop; use Amp\Promise; use Amp\Success; use Amp\TimeoutException; +use danog\MadelineProto\MTProtoTools\GarbageCollector; use tgseclib\Math\BigInteger; use function Amp\ByteStream\getOutputBufferStream; use function Amp\ByteStream\getStdin; @@ -690,16 +691,21 @@ abstract class Tools extends StrTools */ public static function readLineGenerator(string $prompt = ''): \Generator { - $stdin = getStdin(); - $stdout = getStdout(); - if ($prompt) { - yield $stdout->write($prompt); - } - static $lines = ['']; - while (\count($lines) < 2 && ($chunk = yield $stdin->read()) !== null) { - $chunk = \explode("\n", \str_replace(["\r", "\n\n"], "\n", $chunk)); - $lines[\count($lines) - 1] .= \array_shift($chunk); - $lines = \array_merge($lines, $chunk); + try { + GarbageCollector::$log = false; + $stdin = getStdin(); + $stdout = getStdout(); + if ($prompt) { + yield $stdout->write($prompt); + } + static $lines = ['']; + while (\count($lines) < 2 && ($chunk = yield $stdin->read()) !== null) { + $chunk = \explode("\n", \str_replace(["\r", "\n\n"], "\n", $chunk)); + $lines[\count($lines) - 1] .= \array_shift($chunk); + $lines = \array_merge($lines, $chunk); + } + } finally { + GarbageCollector::$log = true; } return \array_shift($lines); }