From c2b09b610107fe3e6e806b96661c916d9d7d8ba4 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 27 Apr 2024 16:03:13 +0200 Subject: [PATCH] Return a Photo object from getPropicInfo, getPwrChat, getFullInfo --- psalm-baseline.xml | 44 ++++++++--------------------- src/API.php | 2 +- src/InternalDoc.php | 4 +-- src/MTProtoTools/Files.php | 8 ++---- src/MTProtoTools/PeerDatabase.php | 14 +++++++-- src/MTProtoTools/PeerHandler.php | 47 ++++++++----------------------- src/TL/Conversion/BotAPI.php | 3 ++ 7 files changed, 43 insertions(+), 79 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 37e774ad4..1607e4ec4 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -3768,9 +3768,7 @@ - - methodCallAsyncRead('upload.getCdnFileHashes', ['file_token' => $file, 'offset' => $offset, 'cancellation' => $cancellation], $this->authorized_dc)]]> methodCallAsyncRead('upload.reuploadCdnFile', ['file_token' => $messageMedia['file_token'], 'request_token' => $res['request_token'], 'cancellation' => $cancellation], $this->authorized_dc)]]> @@ -3873,10 +3871,6 @@ - - - - @@ -3941,7 +3935,6 @@ - @@ -3979,7 +3972,6 @@ - @@ -4004,8 +3996,6 @@ - - cdn_hashes]]> @@ -4369,6 +4359,7 @@ + @@ -4378,6 +4369,7 @@ + API->authorization['user']['id']]]> API->methodCallAsyncRead('help.getSupport', [])['user']]]> @@ -4413,8 +4405,8 @@ pendingDb]]> - getFull($id)['last_update'] ?? 0]]> - getFull($id)['last_update'] ?? 0]]> + getFull($id)['inserted'] ?? 0]]> + getFull($id)['inserted'] ?? 0]]> @@ -4477,11 +4469,6 @@ - - - - - @@ -4496,10 +4483,12 @@ + + @@ -4530,16 +4519,6 @@ - - - - - - - - - - TL->getConstructors()->findByPredicate($constructor['_'])['type']]]> authorization['user']['id']]]> @@ -4580,7 +4559,6 @@ - @@ -4592,16 +4570,16 @@ + - + - supportUser]]> @@ -4610,11 +4588,10 @@ - + - @@ -6718,9 +6695,11 @@ + + @@ -6738,6 +6717,7 @@ + diff --git a/src/API.php b/src/API.php index 499ca8e24..088e0227e 100644 --- a/src/API.php +++ b/src/API.php @@ -51,7 +51,7 @@ final class API extends AbstractAPI * * @var string */ - public const RELEASE = '8.0.0'; + public const RELEASE = '8.0.0-beta205'; /** * We're not logged in. * diff --git a/src/InternalDoc.php b/src/InternalDoc.php index f4a6e2743..4614462b9 100644 --- a/src/InternalDoc.php +++ b/src/InternalDoc.php @@ -997,9 +997,9 @@ abstract class InternalDoc /** * Gets info of the propic of a user. */ - final public function getPropicInfo($data, bool $big = true): \danog\MadelineProto\BotApiFileId + final public function getPropicInfo($data): ?\danog\MadelineProto\EventHandler\Media\Photo { - return $this->wrapper->getAPI()->getPropicInfo($data, $big); + return $this->wrapper->getAPI()->getPropicInfo($data); } /** * Get PSR logger. diff --git a/src/MTProtoTools/Files.php b/src/MTProtoTools/Files.php index 774dee0d1..3a1eb5b7e 100644 --- a/src/MTProtoTools/Files.php +++ b/src/MTProtoTools/Files.php @@ -661,13 +661,9 @@ trait Files /** * Gets info of the propic of a user. */ - public function getPropicInfo($data, bool $big = true): BotApiFileId + public function getPropicInfo($data): ?Photo { - $res = $this->getPwrChat($data, false); - $photo = $res['photo'][$big ? 'big_file_id' : 'small_file_id']; - $size = $res['photo'][$big ? 'big_file_size' : 'small_file_size']; - $name = $res['photo']['id'].'_'.($big ? 'big' : 'small').'_'.$res['photo']['dc_id']; - return new BotApiFileId($photo, $size, $name, false); + return $this->getPwrChat($data, false)['photo'] ?? null; } /** * Extract file info from bot API message. diff --git a/src/MTProtoTools/PeerDatabase.php b/src/MTProtoTools/PeerDatabase.php index f64e46b2f..aff16b0fd 100644 --- a/src/MTProtoTools/PeerDatabase.php +++ b/src/MTProtoTools/PeerDatabase.php @@ -28,6 +28,7 @@ use danog\AsyncOrm\DbArray; use danog\AsyncOrm\DbArrayBuilder; use danog\AsyncOrm\KeyType; use danog\AsyncOrm\ValueType; +use danog\MadelineProto\EventHandler\Media\Photo; use danog\MadelineProto\Exception; use danog\MadelineProto\LegacyMigrator; use danog\MadelineProto\Logger; @@ -255,7 +256,7 @@ final class PeerDatabase implements TLCallback */ public function fullChatLastUpdated(mixed $id): int { - return $this->getFull($id)['last_update'] ?? 0; + return $this->getFull($id)['inserted'] ?? 0; } private function recacheChatUsername(int $id, ?array $old, array $new): void @@ -584,9 +585,18 @@ final class PeerDatabase implements TLCallback */ private function addFullChat(array $full): void { + foreach (['chat_photo', 'personal_photo', 'fallback_photo', 'profile_photo'] as $k) { + if (isset($full[$k])) { + if ($full[$k]['_'] === 'photoEmpty') { + unset($full[$k]); + } else { + $full[$k] = new Photo($this->API, $full[$k], false); + } + } + } $this->fullDb[$this->API->getIdInternal($full)] = [ 'full' => $full, - 'last_update' => time(), + 'inserted' => time(), ]; } diff --git a/src/MTProtoTools/PeerHandler.php b/src/MTProtoTools/PeerHandler.php index 018a7f37c..e57ea5d62 100644 --- a/src/MTProtoTools/PeerHandler.php +++ b/src/MTProtoTools/PeerHandler.php @@ -21,9 +21,6 @@ declare(strict_types=1); namespace danog\MadelineProto\MTProtoTools; use AssertionError; -use danog\Decoder\FileId; -use danog\Decoder\FileIdType; -use danog\Decoder\PhotoSizeSource\PhotoSizeSourceThumbnail; use danog\MadelineProto\API; use danog\MadelineProto\Exception; use danog\MadelineProto\Logger; @@ -633,7 +630,7 @@ trait PeerHandler */ public function fullChatLastUpdated(mixed $id): int { - return $this->peerDatabase->getFull($this->getIdInternal($id))['last_update'] ?? 0; + return $this->peerDatabase->getFull($this->getIdInternal($id))['inserted'] ?? 0; } /** * Get full info about peer, returns an FullInfo object. @@ -648,7 +645,7 @@ trait PeerHandler return $partial; } $full = $this->peerDatabase->getFull($partial['bot_api_id']); - if (time() - ($full['last_update'] ?? 0) < $this->getSettings()->getPeer()->getFullInfoCacheTime()) { + if (time() - ($full['inserted'] ?? 0) < $this->getSettings()->getPeer()->getFullInfoCacheTime()) { return array_merge($partial, $full); } switch ($partial['type']) { @@ -693,9 +690,15 @@ trait PeerHandler $res[$key] = $full['full'][$key]; } } - if (isset($full['full']['profile_photo']['sizes'])) { + if (isset($full['full']['profile_photo'])) { $res['photo'] = $full['full']['profile_photo']; } + if (isset($full['full']['fallback_photo'])) { + $res['fallback_photo'] = $full['full']['fallback_photo']; + } + if (isset($full['full']['personal_photo'])) { + $res['personal_photo'] = $full['full']['personal_photo']; + } break; case 'chat': foreach (['title', 'participants_count', 'admin', 'admins_enabled'] as $key) { @@ -711,7 +714,7 @@ trait PeerHandler if (isset($res['admins_enabled'])) { $res['all_members_are_administrators'] = !$res['admins_enabled']; } - if (isset($full['full']['chat_photo']['sizes'])) { + if (isset($full['full']['chat_photo'])) { $res['photo'] = $full['full']['chat_photo']; } if (isset($full['full']['exported_invite']['link'])) { @@ -733,7 +736,7 @@ trait PeerHandler $res[$key] = $full['full'][$key]; } } - if (isset($full['full']['chat_photo']['sizes'])) { + if (isset($full['full']['chat_photo'])) { $res['photo'] = $full['full']['chat_photo']; } if (isset($full['full']['exported_invite']['link'])) { @@ -814,34 +817,6 @@ trait PeerHandler if (!$fullfetch) { unset($res['participants']); } - if (isset($res['photo'])) { - $photo = []; - foreach ([ - 'small' => $res['photo']['sizes'][0], - 'big' => Tools::maxSize($res['photo']['sizes']), - ] as $type => $size) { - $photoSizeSource = new PhotoSizeSourceThumbnail( - thumbType: $size['type'], - thumbFileType: FileIdType::PHOTO - ); - - $fileId = new FileId( - dcId: $res['photo']['dc_id'], - type: FileIdType::PHOTO, - id: $res['photo']['id'], - accessHash: $res['photo']['access_hash'], - fileReference: $res['photo']['file_reference'] === null - ? null - : (string) $res['photo']['file_reference'], - photoSizeSource: $photoSizeSource - ); - - $photo[$type.'_file_size'] = $size['size']; - $photo[$type.'_file_id'] = (string) $fileId; - $photo[$type.'_file_unique_id'] = $fileId->getUniqueBotAPI(); - } - $res['photo'] += $photo; - } return $res; } private function recurseAlphabetSearchParticipants(int $channel, string $filter, string $q, int $total_count, array &$res, int $depth): array diff --git a/src/TL/Conversion/BotAPI.php b/src/TL/Conversion/BotAPI.php index bdb0c0eb6..a4115847e 100644 --- a/src/TL/Conversion/BotAPI.php +++ b/src/TL/Conversion/BotAPI.php @@ -227,6 +227,9 @@ trait BotAPI $data['user'] = ($this->getPwrChat($data['user_id'], false)); unset($data['user_id']); return $data; + case 'photo': + $data = ['photo' => $data]; + // no break case 'messageMediaPhoto': if (isset($data['caption'])) { $res['caption'] = $data['caption'];