From a078dd68a65d3fcee51376c5b01836e5e7aa8c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Thu, 23 Nov 2023 19:46:05 +0330 Subject: [PATCH] fix Participants class --- src/EventHandler/Message/ChannelMessage.php | 19 ++------------ src/EventHandler/Message/GroupMessage.php | 29 +++++---------------- src/EventHandler/Participant.php | 23 +++++++++++++++- src/EventHandler/Participant/Banned.php | 16 +++++++----- src/EventHandler/Participant/Left.php | 16 +++++++----- 5 files changed, 49 insertions(+), 54 deletions(-) diff --git a/src/EventHandler/Message/ChannelMessage.php b/src/EventHandler/Message/ChannelMessage.php index 4897b2205..937ad9418 100644 --- a/src/EventHandler/Message/ChannelMessage.php +++ b/src/EventHandler/Message/ChannelMessage.php @@ -17,15 +17,9 @@ namespace danog\MadelineProto\EventHandler\Message; use AssertionError; +use danog\MadelineProto\MTProto; use danog\MadelineProto\EventHandler\Message; use danog\MadelineProto\EventHandler\Participant; -use danog\MadelineProto\EventHandler\Participant\Admin; -use danog\MadelineProto\EventHandler\Participant\Banned; -use danog\MadelineProto\EventHandler\Participant\Creator; -use danog\MadelineProto\EventHandler\Participant\Left; -use danog\MadelineProto\EventHandler\Participant\Member; -use danog\MadelineProto\EventHandler\Participant\MySelf; -use danog\MadelineProto\MTProto; /** * Represents an incoming or outgoing channel message. @@ -87,16 +81,7 @@ final class ChannelMessage extends Message 'participant' => $member, ] )['participant']; - - return match ($result['_']) { - 'channelParticipant' => new Member($result), - 'channelParticipantLeft' => new Left($client, $result), - 'channelParticipantSelf' => new MySelf($result), - 'channelParticipantAdmin' => new Admin($result), - 'channelParticipantBanned' => new Banned($client, $result), - 'channelParticipantCreator' => new Creator($result), - default => throw new AssertionError("undefined Participant type: {$result['_']}") - }; + return Participant::fromRawParticipant($result); } /** diff --git a/src/EventHandler/Message/GroupMessage.php b/src/EventHandler/Message/GroupMessage.php index de1893f6c..2807eadb4 100644 --- a/src/EventHandler/Message/GroupMessage.php +++ b/src/EventHandler/Message/GroupMessage.php @@ -17,20 +17,14 @@ namespace danog\MadelineProto\EventHandler\Message; use AssertionError; -use danog\MadelineProto\EventHandler\Message; -use danog\MadelineProto\EventHandler\Message\Service\DialogTopicCreated; -use danog\MadelineProto\EventHandler\Message\Service\DialogTopicEdited; -use danog\MadelineProto\EventHandler\Participant; -use danog\MadelineProto\EventHandler\Participant\Admin; -use danog\MadelineProto\EventHandler\Participant\Banned; -use danog\MadelineProto\EventHandler\Participant\Creator; -use danog\MadelineProto\EventHandler\Participant\Left; -use danog\MadelineProto\EventHandler\Participant\Member; -use danog\MadelineProto\EventHandler\Participant\MySelf; -use danog\MadelineProto\EventHandler\Topic\IconColor; -use danog\MadelineProto\MTProtoTools\DialogId; use Webmozart\Assert\Assert; use Webmozart\Assert\InvalidArgumentException; +use danog\MadelineProto\EventHandler\Message; +use danog\MadelineProto\MTProtoTools\DialogId; +use danog\MadelineProto\EventHandler\Participant; +use danog\MadelineProto\EventHandler\Topic\IconColor; +use danog\MadelineProto\EventHandler\Message\Service\DialogTopicCreated; +use danog\MadelineProto\EventHandler\Message\Service\DialogTopicEdited; /** * Represents an incoming or outgoing group message. @@ -54,16 +48,7 @@ final class GroupMessage extends Message 'participant' => $member, ] )['participant']; - - return match ($result['_']) { - 'channelParticipant' => new Member($result), - 'channelParticipantLeft' => new Left($client, $result), - 'channelParticipantSelf' => new MySelf($result), - 'channelParticipantAdmin' => new Admin($result), - 'channelParticipantBanned' => new Banned($client, $result), - 'channelParticipantCreator' => new Creator($result), - default => throw new AssertionError("undefined Participant type: {$result['_']}") - }; + return Participant::fromRawParticipant($result); } /** diff --git a/src/EventHandler/Participant.php b/src/EventHandler/Participant.php index db643b870..7bfafc141 100644 --- a/src/EventHandler/Participant.php +++ b/src/EventHandler/Participant.php @@ -16,15 +16,36 @@ namespace danog\MadelineProto\EventHandler; -use JsonSerializable; +use AssertionError; use ReflectionClass; use ReflectionProperty; +use JsonSerializable; +use danog\MadelineProto\EventHandler\Participant\Left; +use danog\MadelineProto\EventHandler\Participant\Admin; +use danog\MadelineProto\EventHandler\Participant\Banned; +use danog\MadelineProto\EventHandler\Participant\Member; +use danog\MadelineProto\EventHandler\Participant\MySelf; +use danog\MadelineProto\EventHandler\Participant\Creator; /** * Info about a channel participant. */ abstract class Participant implements JsonSerializable { + public static function fromRawParticipant(array $rawParticipant): self + { + return match ($rawParticipant['_']) + { + 'channelParticipant' => new Member($rawParticipant), + 'channelParticipantLeft' => new Left($rawParticipant), + 'channelParticipantSelf' => new MySelf($rawParticipant), + 'channelParticipantAdmin' => new Admin($rawParticipant), + 'channelParticipantBanned' => new Banned($rawParticipant), + 'channelParticipantCreator' => new Creator($rawParticipant), + default => throw new AssertionError("undefined Participant type: {$rawParticipant['_']}") + }; + } + /** @internal */ public function jsonSerialize(): mixed { diff --git a/src/EventHandler/Participant/Banned.php b/src/EventHandler/Participant/Banned.php index 1a1010904..a91db4c36 100644 --- a/src/EventHandler/Participant/Banned.php +++ b/src/EventHandler/Participant/Banned.php @@ -16,10 +16,9 @@ namespace danog\MadelineProto\EventHandler\Participant; +use danog\MadelineProto\MTProtoTools\DialogId; use danog\MadelineProto\EventHandler\Participant; use danog\MadelineProto\EventHandler\Participant\Rights\Banned as BannedRights; -use danog\MadelineProto\Ipc\Client; -use danog\MadelineProto\MTProto; /** * Banned/kicked user. @@ -42,14 +41,17 @@ final class Banned extends Participant public readonly BannedRights $bannedRights; /** @internal */ - public function __construct( - MTProto|Client $API, - array $rawParticipant - ) { + public function __construct(array $rawParticipant) + { + $peer = $rawParticipant['peer']; $this->left = $rawParticipant['left']; - $this->peer = $API->getIdInternal($rawParticipant['peer']); $this->kickedBy = $rawParticipant['kicked_by']; $this->date = $rawParticipant['date']; $this->bannedRights = new BannedRights($rawParticipant['banned_rights']); + $this->peer = match ($peer['_']) { + 'peerUser' => $peer['user_id'], + 'peerChat' => -$peer['chat_id'], + 'peerChannel' => DialogId::fromSupergroupOrChannel($peer['channel_id']), + }; } } diff --git a/src/EventHandler/Participant/Left.php b/src/EventHandler/Participant/Left.php index df7253bf6..d3edb1897 100644 --- a/src/EventHandler/Participant/Left.php +++ b/src/EventHandler/Participant/Left.php @@ -16,9 +16,8 @@ namespace danog\MadelineProto\EventHandler\Participant; +use danog\MadelineProto\MTProtoTools\DialogId; use danog\MadelineProto\EventHandler\Participant; -use danog\MadelineProto\Ipc\Client; -use danog\MadelineProto\MTProto; /** * A participant that left the channel/supergroup. @@ -29,10 +28,13 @@ final class Left extends Participant public readonly int $peer; /** @internal */ - public function __construct( - MTProto|Client $API, - array $rawParticipant - ) { - $this->peer = $API->getIdInternal($rawParticipant['peer']); + public function __construct(array $rawParticipant) + { + $rawParticipant = $rawParticipant['peer']; + $this->peer = match ($rawParticipant['_']) { + 'peerUser' => $rawParticipant['user_id'], + 'peerChat' => -$rawParticipant['chat_id'], + 'peerChannel' => DialogId::fromSupergroupOrChannel($rawParticipant['channel_id']), + }; } }