From ac35f1e4a3b5cec86f8ac469451f019db20e196c Mon Sep 17 00:00:00 2001 From: code-a1 <68858676+code-a1@users.noreply.github.com> Date: Wed, 10 Feb 2021 15:40:12 +0100 Subject: [PATCH 1/2] Add updates handling on status Add updates handling on status of a user/chat (onChatStatus and onUserStatus) --- src/NovaGram/HandlersTrait.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/NovaGram/HandlersTrait.php b/src/NovaGram/HandlersTrait.php index e4ed776..c505868 100644 --- a/src/NovaGram/HandlersTrait.php +++ b/src/NovaGram/HandlersTrait.php @@ -96,6 +96,24 @@ public function onText(string $pattern, Closure $handler): void } }); } + + public function onUserStatus(string $status, Closure $handler): void + { + $this->onTextMessage(function (Message $message) use ($handler, $status) { + if($message->from->status() === $status){ + $handler($message); + } + }); + } + + public function onChatStatus(string $status, Closure $handler): void + { + $this->onTextMessage(function (Message $message) use ($handler, $status) { + if($message->chat->status() === $status){ + $handler($message); + } + }); + } public function onCommand($commands, Closure $handler, string $description = null): void { From ba4388ad572ff89f41f9326a4f5e46ffce0aa360 Mon Sep 17 00:00:00 2001 From: skrtdev <64538010+skrtdev@users.noreply.github.com> Date: Wed, 17 Feb 2021 18:19:05 +0100 Subject: [PATCH 2/2] add status caching using WeakMap --- composer.json | 3 ++- src/NovaGram/HandlersTrait.php | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 5330b36..a7bed11 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "ext-json":"*", "ext-curl":"*", "skrtdev/async":"~0.7", - "monolog/monolog":"^2.1" + "monolog/monolog":"^2.1", + "benmorel/weakmap-polyfill": "^0.2.0" }, "suggest": { "ext-mbstring":"Needed to use the built-in Telegram Entites Parser", diff --git a/src/NovaGram/HandlersTrait.php b/src/NovaGram/HandlersTrait.php index c505868..2b05480 100644 --- a/src/NovaGram/HandlersTrait.php +++ b/src/NovaGram/HandlersTrait.php @@ -3,11 +3,12 @@ namespace skrtdev\NovaGram; use skrtdev\Telegram\{Message, CallbackQuery}; -use Closure; +use Closure, WeakMap; trait HandlersTrait{ protected array $commands = []; + protected WeakMap $statuses_cache; // closure handlers @@ -99,8 +100,15 @@ public function onText(string $pattern, Closure $handler): void public function onUserStatus(string $status, Closure $handler): void { + $this->statuses_cache ??= new WeakMap(); $this->onTextMessage(function (Message $message) use ($handler, $status) { - if($message->from->status() === $status){ + if(!isset($message->from)){ + return; + } + + $user = $message->from; + $real_status = $this->statuses_cache[$user] ??= $user->status(); + if($real_status === $status){ $handler($message); } }); @@ -108,8 +116,11 @@ public function onUserStatus(string $status, Closure $handler): void public function onChatStatus(string $status, Closure $handler): void { + $this->statuses_cache ??= new WeakMap(); $this->onTextMessage(function (Message $message) use ($handler, $status) { - if($message->chat->status() === $status){ + $chat = $message->chat; + $real_status = $this->statuses_cache[$chat] ??= $chat->status(); + if($real_status === $status){ $handler($message); } });