Skip to content

Commit

Permalink
Make getPropicInfo return a BotApiFileId, make getPwrChat return more…
Browse files Browse the repository at this point in the history
… info with fullfetch=false, layer 179
  • Loading branch information
danog committed Apr 27, 2024
1 parent 6c36e32 commit 5d0f02d
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 71 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.updateBusinessWorkHours.html" name="account.updateBusinessWorkHours">account.updateBusinessWorkHours</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.updateConnectedBot.html" name="account.updateConnectedBot">account.updateConnectedBot</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.updatePersonalChannel.html" name="account.updatePersonalChannel">account.updatePersonalChannel</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/auth.reportMissingCode.html" name="auth.reportMissingCode">auth.reportMissingCode</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#base64urlDecode" name="base64urlDecode">base64URL decode: base64urlDecode</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.reportSponsoredMessage.html" name="channels.reportSponsoredMessage">channels.reportSponsoredMessage</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.restrictSponsoredMessages.html" name="channels.restrictSponsoredMessages">channels.restrictSponsoredMessages</a>
Expand Down
2 changes: 1 addition & 1 deletion schemas
Submodule schemas updated 2 files
+2,427 −0 TL_telegram_v178.tl
+2,430 −0 TL_telegram_v179.tl
2 changes: 1 addition & 1 deletion src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class API extends AbstractAPI
*
* @var string
*/
public const RELEASE = '8.0.0-beta204';
public const RELEASE = '8.0.0';
/**
* We're not logged in.
*
Expand Down
5 changes: 4 additions & 1 deletion src/BotApiFileId.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class BotApiFileId
{
/**
* @param string $fileId The file ID
* @param integer $size The file size
* @param int<1, max> $size The file size
* @param string $fileName The original file name
* @param bool $protected Whether the original file is protected
*/
Expand All @@ -46,6 +46,9 @@ public function __construct(
public readonly string $fileName,
public readonly bool $protected
) {
if ($size <= 0) {
throw new AssertionError("The specified size must be >= 0!");
}
}

/**
Expand Down
14 changes: 4 additions & 10 deletions src/InternalDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ final public function getDialogIds(): array
* @param mixed $messageMedia File ID
*
* @return array{
* ext: string,
* ext?: string,
* name: string,
* mime: string,
* size: int,
Expand Down Expand Up @@ -995,17 +995,11 @@ final public function getPlugin(string $class): \danog\MadelineProto\PluginEvent
return $this->wrapper->getAPI()->getPlugin($class);
}
/**
* Get download info of the propic of a user
* Returns an array with the following structure:.
*
* `$info['ext']` - The file extension
* `$info['name']` - The file name, without the extension
* `$info['mime']` - The file mime type
* `$info['size']` - The file size
* Gets info of the propic of a user.
*/
final public function getPropicInfo($data): array
final public function getPropicInfo($data, bool $big = true): \danog\MadelineProto\BotApiFileId
{
return $this->wrapper->getAPI()->getPropicInfo($data);
return $this->wrapper->getAPI()->getPropicInfo($data, $big);
}
/**
* Get PSR logger.
Expand Down
31 changes: 20 additions & 11 deletions src/MTProtoTools/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Amp\Http\Client\Request;
use AssertionError;
use danog\Decoder\FileIdType;
use danog\MadelineProto\BotApiFileId;
use danog\MadelineProto\EventHandler\Media;
use danog\MadelineProto\EventHandler\Media\AnimatedSticker;
use danog\MadelineProto\EventHandler\Media\Audio;
Expand Down Expand Up @@ -658,17 +659,15 @@ public function getFileInfo(mixed $constructor): array
return $this->genAllFile($constructor);
}
/**
* Get download info of the propic of a user
* Returns an array with the following structure:.
*
* `$info['ext']` - The file extension
* `$info['name']` - The file name, without the extension
* `$info['mime']` - The file mime type
* `$info['size']` - The file size
* Gets info of the propic of a user.
*/
public function getPropicInfo($data): array
public function getPropicInfo($data, bool $big = true): BotApiFileId
{
return $this->getDownloadInfo($this->peerDatabase->get($this->getId($data)));
$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);
}
/**
* Extract file info from bot API message.
Expand Down Expand Up @@ -713,7 +712,7 @@ public static function extractBotAPIFile(array $info): array|null
* @param mixed $messageMedia File ID
*
* @return array{
* ext: string,
* ext?: string,
* name: string,
* mime: string,
* size: int,
Expand All @@ -726,6 +725,16 @@ public static function extractBotAPIFile(array $info): array|null
*/
public function getDownloadInfo(mixed $messageMedia): array
{
if ($messageMedia instanceof BotApiFileId) {
$res = $this->getDownloadInfo($messageMedia->fileId);
$res['size'] = $messageMedia->size;
$pathinfo = pathinfo($messageMedia->fileName);
if (isset($pathinfo['extension'])) {
$res['ext'] = '.'.$pathinfo['extension'];
}
$res['name'] = $pathinfo['filename'];
return $res;
}
if ($messageMedia instanceof Message) {
$messageMedia = $messageMedia->media;
}
Expand Down Expand Up @@ -1051,7 +1060,7 @@ public function downloadToCallable(mixed $messageMedia, callable $callable, ?cal
});
};
}
if ($end === -1 && isset($messageMedia['size'])) {
if ($end === -1 && isset($messageMedia['size']) && $messageMedia['size'] !== 0) {
$end = $messageMedia['size'];
}
$part_size ??= 1024 * 1024;
Expand Down
4 changes: 1 addition & 3 deletions src/MTProtoTools/FilesAbstraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,7 @@ public function sendMedia(
} elseif ($file instanceof RemoteUrl) {
$fileName ??= basename($file->url);
} elseif ($file instanceof BotApiFileId) {
if ($fileName === null) {
throw new AssertionError("A file name must be provided when uploading a bot API file ID!");
}
$fileName ??= $file->fileName;
} elseif ($file instanceof ReadableStream) {
if ($fileName === null) {
throw new AssertionError("A file name must be provided when uploading a stream!");
Expand Down
19 changes: 8 additions & 11 deletions src/MTProtoTools/PeerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
use AssertionError;
use danog\Decoder\FileId;
use danog\Decoder\FileIdType;
use danog\Decoder\PhotoSizeSource\PhotoSizeSourceDialogPhotoBig;
use danog\Decoder\PhotoSizeSource\PhotoSizeSourceDialogPhotoSmall;
use danog\Decoder\PhotoSizeSource\PhotoSizeSourceThumbnail;
use danog\MadelineProto\API;
use danog\MadelineProto\Exception;
use danog\MadelineProto\Logger;
Expand Down Expand Up @@ -676,7 +675,7 @@ public function getFullInfo(mixed $id): array
public function getPwrChat(mixed $id, bool $fullfetch = true): array
{
try {
$full = $fullfetch && $this->getSettings()->getDb()->getEnableFullPeerDb() ? $this->getFullInfo($id) : $this->getInfo($id);
$full = $this->getSettings()->getDb()->getEnableFullPeerDb() ? $this->getFullInfo($id) : $this->getInfo($id);
} catch (Throwable) {
$full = $this->getInfo($id);
}
Expand Down Expand Up @@ -821,25 +820,23 @@ public function getPwrChat(mixed $id, bool $fullfetch = true): array
'small' => $res['photo']['sizes'][0],
'big' => Tools::maxSize($res['photo']['sizes']),
] as $type => $size) {
$photoSize = $type === 'small'
? PhotoSizeSourceDialogPhotoSmall::class
: PhotoSizeSourceDialogPhotoBig::class;
$photoSize = new $photoSize(
dialogId: $res['id'],
dialogAccessHash: $full['Chat']['access_hash'] ?? $full['User']['access_hash'],
$photoSizeSource = new PhotoSizeSourceThumbnail(
thumbType: $size['type'],
thumbFileType: FileIdType::PHOTO
);

$fileId = new FileId(
dcId: $res['photo']['dc_id'],
type: FileIdType::PROFILE_PHOTO,
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: $photoSize
photoSizeSource: $photoSizeSource
);

$photo[$type.'_file_size'] = $size['size'];
$photo[$type.'_file_id'] = (string) $fileId;
$photo[$type.'_file_unique_id'] = $fileId->getUniqueBotAPI();
}
Expand Down
Loading

0 comments on commit 5d0f02d

Please sign in to comment.