diff --git a/README.md b/README.md index ed942ae42..d2399e702 100644 --- a/README.md +++ b/README.md @@ -604,6 +604,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc * Get the participants of a supergroup/channel: channels.getParticipants * Get the phone call with the specified user ID: getCallByPeer * Get the progress of a currently running broadcast: getBroadcastProgress + * Get the type of a dialog using just its bot API ID: getDialogIdType * Get theme information: account.getTheme * Get topics of a forum: channels.getForumTopics * Get type of peer: getType diff --git a/src/AnnotationsBuilder.php b/src/AnnotationsBuilder.php index 2de3e53d9..25dfa9b4f 100644 --- a/src/AnnotationsBuilder.php +++ b/src/AnnotationsBuilder.php @@ -535,7 +535,7 @@ final class Blacklist { \fwrite($handle, "use Amp\\Cancellation;\n"); \fwrite($handle, "use Amp\\Http\\Server\\Request as ServerRequest;\n"); \fwrite($handle, "use danog\\MadelineProto\\Broadcast\\Action;\n"); - \fwrite($handle, "use danog\\MadelineProto\\MTProtoTools\\DialogIdType;\n"); + \fwrite($handle, "use danog\\MadelineProto\\MTProtoTools\\DialogId;\n"); $had = []; foreach (ClassFinder::getClassesInNamespace(\danog\MadelineProto\EventHandler::class, ClassFinder::RECURSIVE_MODE) as $class) { $name = \basename(\str_replace('\\', '//', $class)); diff --git a/src/EventHandler/AbstractMessage.php b/src/EventHandler/AbstractMessage.php index 62188f4f2..31f4a3c8c 100644 --- a/src/EventHandler/AbstractMessage.php +++ b/src/EventHandler/AbstractMessage.php @@ -18,6 +18,7 @@ namespace danog\MadelineProto\EventHandler; use AssertionError; use danog\MadelineProto\MTProto; +use danog\MadelineProto\MTProtoTools\DialogId; use danog\MadelineProto\ParseMode; /** @@ -145,7 +146,7 @@ abstract class AbstractMessage extends Update implements SimpleFilters return $this->replyCache; } $messages = $this->getClient()->methodCallAsyncRead( - MTProto::isSupergroupOrChannel($this->chatId) ? 'channels.getMessages' : 'messages.getMessages', + DialogId::getType($this->chatId) === DialogId::CHANNEL_OR_SUPERGROUP ? 'channels.getMessages' : 'messages.getMessages', [ 'channel' => $this->chatId, 'id' => [['_' => 'inputMessageReplyTo', 'id' => $this->id]] @@ -168,7 +169,7 @@ abstract class AbstractMessage extends Update implements SimpleFilters public function delete(bool $revoke = true): void { $this->getClient()->methodCallAsyncRead( - MTProto::isSupergroupOrChannel($this->chatId) ? 'channels.deleteMessages' : 'messages.deleteMessages', + DialogId::getType($this->chatId) === DialogId::CHANNEL_OR_SUPERGROUP ? 'channels.deleteMessages' : 'messages.deleteMessages', [ 'channel' => $this->chatId, 'id' => [$this->id], diff --git a/src/EventHandler/Message.php b/src/EventHandler/Message.php index ee16aaab3..ac124faeb 100644 --- a/src/EventHandler/Message.php +++ b/src/EventHandler/Message.php @@ -32,6 +32,7 @@ use danog\MadelineProto\EventHandler\Media\Voice; use danog\MadelineProto\EventHandler\Message\Entities\MessageEntity; use danog\MadelineProto\EventHandler\Message\ReportReason; use danog\MadelineProto\MTProto; +use danog\MadelineProto\MTProtoTools\DialogId; use danog\MadelineProto\ParseMode; use danog\MadelineProto\StrTools; @@ -392,7 +393,7 @@ abstract class Message extends AbstractMessage public function read(?int $maxId = null): bool { return $this->getClient()->methodCallAsyncRead( - API::isSupergroupOrChannel($this->chatId) ? 'channels.readHistory':'messages.readHistory', + DialogId::getType($this->chatId) === DialogId::CHANNEL_OR_SUPERGROUP ? 'channels.readHistory':'messages.readHistory', [ 'peer' => $this->chatId, 'channel' => $this->chatId, diff --git a/src/InternalDoc.php b/src/InternalDoc.php index 306b8076a..2e91f7fe3 100644 --- a/src/InternalDoc.php +++ b/src/InternalDoc.php @@ -138,17 +138,6 @@ abstract class InternalDoc { return $this->wrapper->getAPI()->MTProtoToBotAPI($data); } - /** - * Convert MTProto secret chat ID to bot API secret chat ID. - * - * @param int $id MTProto secret chat ID - * - * @return int Bot API secret chat ID - */ - public static function MTProtoToBotAPISecretChatId(int $id): int - { - return \danog\MadelineProto\MTProto::MTProtoToBotAPISecretChatId($id); - } /** * MTProto to TD params. * @@ -219,17 +208,6 @@ abstract class InternalDoc { return $this->wrapper->getAPI()->botAPIToMTProto($arguments); } - /** - * Convert bot API secret chat ID to MTProto secret chat ID. - * - * @param int $id Bot API secret chat ID - * - * @return int MTProto secret chat ID - */ - public static function botAPIToMTProtoSecretChatId(int $id): int - { - return \danog\MadelineProto\MTProto::botAPIToMTProtoSecretChatId($id); - } /** * Login as bot. * @@ -618,15 +596,6 @@ abstract class InternalDoc { return \danog\MadelineProto\AsyncTools::flock($file, $operation, $polling, $token, $failureCb); } - /** - * Convert bot API channel ID to MTProto channel ID. - * - * @param int $id Bot API channel ID - */ - public static function fromSupergroup(int $id): int - { - return \danog\MadelineProto\MTProto::fromSupergroup($id); - } /** * When was full info for this chat last cached. * @@ -758,17 +727,6 @@ abstract class InternalDoc { return $this->wrapper->getAPI()->getDhConfig(); } - /** - * Get the type of a dialog using just its bot API ID. - * - * For more detailed types, use getType, instead. - * - * @param integer $id Bot API ID. - */ - public static function getDialogIdType(int $id): \danog\MadelineProto\MTProtoTools\DialogIdType - { - return \danog\MadelineProto\MTProto::getDialogIdType($id); - } /** * Get dialog IDs. * @@ -1287,15 +1245,6 @@ abstract class InternalDoc { return $this->wrapper->getAPI()->isPremium(); } - /** - * Check whether provided bot API ID is a secret chat. - * - * @param int $id Bot API ID - */ - public static function isSecretChat(int $id): bool - { - return \danog\MadelineProto\MTProto::isSecretChat($id); - } /** * Returns whether the current user is a bot. */ @@ -1310,15 +1259,6 @@ abstract class InternalDoc { return $this->wrapper->getAPI()->isSelfUser(); } - /** - * Check whether provided bot API ID is a channel or supergroup. - * - * @param int $id Bot API ID - */ - public static function isSupergroupOrChannel(int $id): bool - { - return \danog\MadelineProto\MTProto::isSupergroupOrChannel($id); - } /** * Whether we're currently connected to the test DCs. * @@ -1906,15 +1846,6 @@ abstract class InternalDoc { return \danog\MadelineProto\StrTools::toSnakeCase($input); } - /** - * Convert MTProto channel ID to bot API channel ID. - * - * @param int $id MTProto channel ID - */ - public static function toSupergroup(int $id): int - { - return \danog\MadelineProto\MTProto::toSupergroup($id); - } /** * Unpack binary double. * diff --git a/src/Loop/Update/FeedLoop.php b/src/Loop/Update/FeedLoop.php index 35c978381..7733ddb61 100644 --- a/src/Loop/Update/FeedLoop.php +++ b/src/Loop/Update/FeedLoop.php @@ -25,6 +25,7 @@ use danog\MadelineProto\AsyncTools; use danog\MadelineProto\Logger; use danog\MadelineProto\Loop\InternalLoop; use danog\MadelineProto\MTProto; +use danog\MadelineProto\MTProtoTools\DialogId; use danog\MadelineProto\MTProtoTools\UpdatesState; /** @@ -232,7 +233,7 @@ final class FeedLoop extends Loop } break; default: - if ($channelId && !($this->API->peerIsset($this->API->toSupergroup($channelId)))) { + if ($channelId && !($this->API->peerIsset(DialogId::toSupergroupOrChannel($channelId)))) { $this->API->logger->logger('Skipping update, I do not have the channel id '.$channelId, Logger::ERROR); return false; } diff --git a/src/Loop/Update/UpdateLoop.php b/src/Loop/Update/UpdateLoop.php index 7cfcb9f76..82b2730c9 100644 --- a/src/Loop/Update/UpdateLoop.php +++ b/src/Loop/Update/UpdateLoop.php @@ -25,6 +25,7 @@ use danog\MadelineProto\Exception; use danog\MadelineProto\Logger; use danog\MadelineProto\Loop\InternalLoop; use danog\MadelineProto\MTProto; +use danog\MadelineProto\MTProtoTools\DialogId; use danog\MadelineProto\PeerNotInDbException; use danog\MadelineProto\PTSException; use danog\MadelineProto\RPCErrorException; @@ -93,7 +94,7 @@ final class UpdateLoop extends Loop } $request_pts = $state->pts(); try { - $difference = $this->API->methodCallAsyncRead('updates.getChannelDifference', ['channel' => $this->API->toSupergroup($this->channelId), 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $request_pts, 'limit' => $limit, 'force' => true], ['FloodWaitLimit' => 86400]); + $difference = $this->API->methodCallAsyncRead('updates.getChannelDifference', ['channel' => DialogId::toSupergroupOrChannel($this->channelId), 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $request_pts, 'limit' => $limit, 'force' => true], ['FloodWaitLimit' => 86400]); } catch (RPCErrorException $e) { if ($e->rpc === '-503') { delay(1.0); diff --git a/src/MTProtoTools/DialogIdType.php b/src/MTProtoTools/DialogIdType.php deleted file mode 100644 index 8cb9277e9..000000000 --- a/src/MTProtoTools/DialogIdType.php +++ /dev/null @@ -1,14 +0,0 @@ -API->getIdInternal($location['from_id'])] = true; } if (isset($location['channel_id'])) { - $peers[$this->API->toSupergroup($location['channel_id'])] = true; + $peers[DialogId::toSupergroupOrChannel($location['channel_id'])] = true; } break; case 'messageActionChatCreate': diff --git a/src/MTProtoTools/PeerDatabase.php b/src/MTProtoTools/PeerDatabase.php index d485e8252..1c686df38 100644 --- a/src/MTProtoTools/PeerDatabase.php +++ b/src/MTProtoTools/PeerDatabase.php @@ -460,7 +460,7 @@ final class PeerDatabase implements TLCallback } else { $this->pendingDb[$chat] = [ '_' => 'channel', - 'id' => MTProto::fromSupergroup($chat), + 'id' => DialogId::fromSupergroupOrChannel($chat), ]; $this->processChat($chat); } @@ -528,7 +528,7 @@ final class PeerDatabase implements TLCallback if ($chat['_'] !== 'channel' && $chat['_'] !== 'channelForbidden') { throw new InvalidArgumentException('Invalid chat type '.$chat['_']); } - $bot_api_id = MTProto::toSupergroup($chat['id']); + $bot_api_id = DialogId::toSupergroupOrChannel($chat['id']); $existingChat = $this->db[$bot_api_id]; if (!isset($chat['access_hash']) && !($chat['min'] ?? false)) { if (isset($existingChat['access_hash'])) { diff --git a/src/MTProtoTools/PeerHandler.php b/src/MTProtoTools/PeerHandler.php index 5ca1ddf2e..566f467cd 100644 --- a/src/MTProtoTools/PeerHandler.php +++ b/src/MTProtoTools/PeerHandler.php @@ -26,8 +26,6 @@ use danog\Decoder\PhotoSizeSource\PhotoSizeSourceDialogPhoto; use danog\MadelineProto\API; use danog\MadelineProto\Exception; use danog\MadelineProto\Logger; -use danog\MadelineProto\Magic; -use danog\MadelineProto\MTProto; use danog\MadelineProto\PeerNotInDbException; use danog\MadelineProto\RPCErrorException; use danog\MadelineProto\SecretPeerNotInDbException; @@ -54,88 +52,6 @@ use function Amp\Future\await; */ trait PeerHandler { - /** - * Convert MTProto channel ID to bot API channel ID. - * - * @param int $id MTProto channel ID - */ - public static function toSupergroup(int $id): int - { - return Magic::ZERO_CHANNEL_ID - $id; - } - /** - * Convert bot API channel ID to MTProto channel ID. - * - * @param int $id Bot API channel ID - */ - public static function fromSupergroup(int $id): int - { - return (-$id) + Magic::ZERO_CHANNEL_ID; - } - /** - * Get the type of a dialog using just its bot API ID. - * - * For more detailed types, use getType, instead. - * - * @param integer $id Bot API ID. - */ - public static function getDialogIdType(int $id): DialogIdType - { - if ($id < 0) { - if (-Magic::MAX_CHAT_ID <= $id) { - return DialogIdType::CHAT; - } - if (Magic::ZERO_CHANNEL_ID - Magic::MAX_CHANNEL_ID <= $id && $id !== Magic::ZERO_CHANNEL_ID) { - return DialogIdType::CHANNEL_OR_SUPERGROUP; - } - if (Magic::ZERO_SECRET_CHAT_ID + Magic::MIN_INT32 <= $id && $id !== Magic::ZERO_SECRET_CHAT_ID) { - return DialogIdType::SECRET_CHAT; - } - } elseif (0 < $id && $id <= Magic::MAX_USER_ID) { - return DialogIdType::USER; - } - throw new AssertionError("Invalid ID $id provided!"); - } - /** - * Check whether provided bot API ID is a channel or supergroup. - * - * @param int $id Bot API ID - */ - public static function isSupergroupOrChannel(int $id): bool - { - return self::getDialogIdType($id) === DialogIdType::CHANNEL_OR_SUPERGROUP; - } - /** - * Convert MTProto secret chat ID to bot API secret chat ID. - * - * @param int $id MTProto secret chat ID - * - * @return int Bot API secret chat ID - */ - public static function MTProtoToBotApiSecretChatId(int $id): int - { - return Magic::ZERO_SECRET_CHAT_ID + $id; - } - /** - * Convert bot API secret chat ID to MTProto secret chat ID. - * - * @param int $id Bot API secret chat ID - * - * @return int MTProto secret chat ID - */ - public static function botApiToMTProtoSecretChatId(int $id): int - { - return $id - Magic::ZERO_SECRET_CHAT_ID; - } - /** - * Check whether provided bot API ID is a secret chat. - * - * @param int $id Bot API ID - */ - public static function isSecretChat(int $id): bool - { - return self::getDialogIdType($id) === DialogIdType::SECRET_CHAT; - } /** * Set support info. * @@ -221,7 +137,7 @@ trait PeerHandler if (isset($fwd['user_id']) && !($this->peerIsset($fwd['user_id']))) { return false; } - if (isset($fwd['channel_id']) && !($this->peerIsset($this->toSupergroup($fwd['channel_id'])))) { + if (isset($fwd['channel_id']) && !($this->peerIsset(DialogId::toSupergroupOrChannel($fwd['channel_id'])))) { return false; } } catch (Exception $e) { @@ -288,7 +204,7 @@ trait PeerHandler case 'inputChannelFromMessage': case 'inputPeerChannelFromMessage': case 'updateChannelParticipant': - return $this->toSupergroup($id['channel_id']); + return DialogId::toSupergroupOrChannel($id['channel_id']); case 'inputUserSelf': case 'inputPeerSelf': return $this->authorization['user']['id']; @@ -314,11 +230,11 @@ trait PeerHandler case 'channelForbidden': case 'channel': case 'channelFull': - return $this->toSupergroup($id['id']); + return DialogId::toSupergroupOrChannel($id['id']); case 'inputPeerChannel': case 'inputChannel': case 'peerChannel': - return $this->toSupergroup($id['channel_id']); + return DialogId::toSupergroupOrChannel($id['channel_id']); case 'message': case 'messageService': if (!isset($id['from_id']) // No other option @@ -340,7 +256,7 @@ trait PeerHandler case 'updateDeleteChannelMessages': case 'updateChannelPinnedMessage': case 'updateChannelTooLong': - return $this->toSupergroup($id['channel_id']); + return DialogId::toSupergroupOrChannel($id['channel_id']); case 'updateChatParticipants': $id = $id['participants']; // no break @@ -386,7 +302,7 @@ trait PeerHandler if (\is_string($id)) { if (\strpos($id, '#') !== false) { if (\preg_match('/^channel#(\\d*)/', $id, $matches)) { - return $this->toSupergroup((int) $matches[1]); + return DialogId::toSupergroupOrChannel((int) $matches[1]); } if (\preg_match('/^chat#(\\d*)/', $id, $matches)) { $id = '-'.$matches[1]; @@ -491,13 +407,13 @@ trait PeerHandler if (\is_numeric($id)) { $id = (int) $id; Assert::true($id !== 0, "An invalid ID was specified!"); - if (self::isSecretChat($id)) { - return $this->getSecretChat(self::botApiToMTProtoSecretChatId($id)); + if (DialogId::getType($id) === DialogId::SECRET_CHAT) { + return $this->getSecretChat(DialogId::toSecretChatId($id)); } if (!$this->peerDatabase->isset($id)) { try { $this->logger->logger("Try fetching {$id} with access hash 0"); - if (self::isSupergroupOrChannel($id)) { + if (DialogId::getType($id) === DialogId::CHANNEL_OR_SUPERGROUP) { $this->peerDatabase->addChatBlocking($id); } elseif ($id < 0) { $this->methodCallAsyncRead('messages.getChats', ['id' => [-$id]]); @@ -622,7 +538,7 @@ trait PeerHandler return $constructor['id']; } if ($constructor['_'] === 'channel') { - return $this->toSupergroup($constructor['id']); + return DialogId::toSupergroupOrChannel($constructor['id']); } if ($constructor['_'] === 'chat' || $constructor['_'] === 'chatForbidden') { return -$constructor['id']; @@ -689,7 +605,7 @@ trait PeerHandler $res['InputNotifyPeer'] = ['_' => 'inputNotifyPeer', 'peer' => $res['InputPeer']]; $res['InputChannel'] = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash'], 'min' => $constructor['min'] ?? false]; $res['channel_id'] = $constructor['id']; - $res['bot_api_id'] = $this->toSupergroup($constructor['id']); + $res['bot_api_id'] = DialogId::toSupergroupOrChannel($constructor['id']); $res['type'] = $constructor['megagroup'] ?? false ? 'supergroup' : 'channel'; break; case 'channelForbidden': @@ -747,12 +663,18 @@ trait PeerHandler } $id = $this->getIdInternal($id); Assert::notNull($id); - if (self::isSupergroupOrChannel($id)) { - $supergroups []= $id; - } elseif ($id < 0) { - $chats []= $id; - } else { - $users []= $id; + switch (DialogId::getType($id)) { + case DialogId::CHANNEL_OR_SUPERGROUP: + $supergroups []= $id; + break; + case DialogId::CHAT: + $chats []= $id; + break; + case DialogId::USER: + $users []= $id; + break; + default: + throw new AssertionError("Invalid ID $id!"); } } if ($supergroups) { @@ -783,7 +705,7 @@ trait PeerHandler public function getFullInfo(mixed $id): array { $partial = $this->getInfo($id); - if (self::isSecretChat($partial['bot_api_id'])) { + if (DialogId::getType($partial['bot_api_id']) === DialogId::SECRET_CHAT) { return $partial; } $full = $this->peerDatabase->getFull($partial['bot_api_id']); diff --git a/src/MTProtoTools/ReferenceDatabase.php b/src/MTProtoTools/ReferenceDatabase.php index d431f5fa7..6651ee55c 100644 --- a/src/MTProtoTools/ReferenceDatabase.php +++ b/src/MTProtoTools/ReferenceDatabase.php @@ -308,7 +308,7 @@ final class ReferenceDatabase implements TLCallback break; case 'channelFull': case 'channel': - $origin['peer'] = MTProto::toSupergroup($data['id']); + $origin['peer'] = DialogId::toSupergroupOrChannel($data['id']); break; case 'document': foreach ($data['attributes'] as $attribute) { diff --git a/src/MTProtoTools/UpdateHandler.php b/src/MTProtoTools/UpdateHandler.php index fdd37b5c6..f786414c9 100644 --- a/src/MTProtoTools/UpdateHandler.php +++ b/src/MTProtoTools/UpdateHandler.php @@ -70,7 +70,6 @@ use danog\MadelineProto\Lang; use danog\MadelineProto\Logger; use danog\MadelineProto\Loop\Update\FeedLoop; use danog\MadelineProto\Loop\Update\UpdateLoop; -use danog\MadelineProto\MTProto; use danog\MadelineProto\ParseMode; use danog\MadelineProto\PeerNotInDbException; use danog\MadelineProto\ResponseException; @@ -846,10 +845,10 @@ trait UpdateHandler public function subscribeToUpdates(mixed $channel): bool { $channelId = $this->getInfo($channel, API::INFO_TYPE_ID); - if (!MTProto::isSupergroupOrChannel($channelId)) { + if (DialogId::getType($channelId) !== DialogId::CHANNEL_OR_SUPERGROUP) { throw new Exception("You can only subscribe to channels or supergroups!"); } - $channelId = MTProto::fromSupergroup($channelId); + $channelId = DialogId::fromSupergroupOrChannel($channelId); if (!$this->getChannelStates()->has($channelId)) { $this->loadChannelState($channelId, ['_' => 'updateChannelTooLong', 'pts' => 1]); $this->feeders[$channelId] ??= new FeedLoop($this, $channelId); diff --git a/src/TL/Conversion/BotAPI.php b/src/TL/Conversion/BotAPI.php index 7a6fc5d59..28950051a 100644 --- a/src/TL/Conversion/BotAPI.php +++ b/src/TL/Conversion/BotAPI.php @@ -24,6 +24,7 @@ use danog\Decoder\FileId; use danog\MadelineProto\Lang; use danog\MadelineProto\Logger; use danog\MadelineProto\MTProto; +use danog\MadelineProto\MTProtoTools\DialogId; use danog\MadelineProto\StrTools; use danog\MadelineProto\Tools; use Throwable; @@ -207,7 +208,7 @@ trait BotAPI } if (isset($data['fwd_from']['channel_id'])) { try { - $newd['forward_from_chat'] = $this->getPwrChat(MTProto::toSupergroup($data['fwd_from']['channel_id']), false); + $newd['forward_from_chat'] = $this->getPwrChat(DialogId::toSupergroupOrChannel($data['fwd_from']['channel_id']), false); } catch (Throwable $e) { } } diff --git a/src/TL/Conversion/BotAPIFiles.php b/src/TL/Conversion/BotAPIFiles.php index 41b50d79d..c5f24268b 100644 --- a/src/TL/Conversion/BotAPIFiles.php +++ b/src/TL/Conversion/BotAPIFiles.php @@ -27,6 +27,7 @@ use danog\Decoder\PhotoSizeSource\PhotoSizeSourceStickersetThumbnail; use danog\Decoder\PhotoSizeSource\PhotoSizeSourceThumbnail; use danog\MadelineProto\API; use danog\MadelineProto\Lang; +use danog\MadelineProto\MTProtoTools\DialogId; use const danog\Decoder\ANIMATION; use const danog\Decoder\AUDIO; @@ -105,7 +106,7 @@ trait BotAPIFiles if ($photoSize->getDialogId() < 0) { $res['Chat'] = [ '_' => $photoSize->getDialogId() < -1000000000000 ? 'channel' : 'chat', - 'id' => $photoSize->getDialogId() < -1000000000000 ? API::fromSupergroup($photoSize->getDialogId()) : -$photoSize->getDialogId(), + 'id' => $photoSize->getDialogId() < -1000000000000 ? DialogId::fromSupergroupOrChannel($photoSize->getDialogId()) : -$photoSize->getDialogId(), 'access_hash' => $photoSize->getDialogAccessHash(), 'photo' => [ '_' => 'chatPhoto',