From d3fa21feb66d7966c79e7fbfe1bf83e014cb8082 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 14 Aug 2023 21:49:08 +0200 Subject: [PATCH] Fixes --- examples/magnaluna | 2 +- src/MTProtoTools/PeerDatabase.php | 42 +++++++++++++++++++++++++------ src/MTProtoTools/PeerHandler.php | 10 ++------ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/examples/magnaluna b/examples/magnaluna index 538081f6c..25f83d460 160000 --- a/examples/magnaluna +++ b/examples/magnaluna @@ -1 +1 @@ -Subproject commit 538081f6ccfcc94a80144427a627da96b0d14408 +Subproject commit 25f83d460a9269b778435395935d9417530bcdf5 diff --git a/src/MTProtoTools/PeerDatabase.php b/src/MTProtoTools/PeerDatabase.php index 69cf4c471..4fafdea4b 100644 --- a/src/MTProtoTools/PeerDatabase.php +++ b/src/MTProtoTools/PeerDatabase.php @@ -293,6 +293,13 @@ final class PeerDatabase implements TLCallback public function getIdFromUsername(string $username): ?int { + foreach ($this->pendingDb as $key => $_) { + if ($key < 0) { + $this->processChat($key); + } else { + $this->processUser($key); + } + } return $this->usernames[$username]; } @@ -326,6 +333,13 @@ final class PeerDatabase implements TLCallback } finally { unset($this->caching_simple_username[$username]); } + foreach ($this->pendingDb as $key => $_) { + if ($key < 0) { + $this->processChat($key); + } else { + $this->processUser($key); + } + } return $result; } /** @@ -407,17 +421,29 @@ final class PeerDatabase implements TLCallback } } } - public function addChatBlocking(array $chat): void + public function addChatBlocking(int $chat): void { - $id = $this->API->getIdInternal($chat); - $this->pendingDb[$id] = $chat; - $this->processChat($id); + if (isset($this->pendingDb[$chat])) { + $this->processChat($chat); + } else { + $this->pendingDb[$chat] = [ + '_' => 'channel', + 'id' => MTProto::fromSupergroup($chat), + ]; + $this->processChat($chat); + } } - public function addUserBlocking(array $user): void + public function addUserBlocking(int $user): void { - $id = $this->API->getIdInternal($user); - $this->pendingDb[$id] = $user; - $this->processUser($id); + if (isset($this->pendingDb[$user])) { + $this->processChat($user); + } else { + $this->pendingDb[$user] = [ + '_' => 'user', + 'id' => $user, + ]; + $this->processUser($user); + } } /** * Add chat to database. diff --git a/src/MTProtoTools/PeerHandler.php b/src/MTProtoTools/PeerHandler.php index 83bcbfd62..41180fe59 100644 --- a/src/MTProtoTools/PeerHandler.php +++ b/src/MTProtoTools/PeerHandler.php @@ -430,17 +430,11 @@ trait PeerHandler try { $this->logger->logger("Try fetching {$id} with access hash 0"); if ($this->isSupergroup($id)) { - $this->peerDatabase->addChatBlocking([ - '_' => 'channel', - 'id' => $this->fromSupergroup($id), - ]); + $this->peerDatabase->addChatBlocking($id); } elseif ($id < 0) { $this->methodCallAsyncRead('messages.getChats', ['id' => [-$id]]); } else { - $this->peerDatabase->addUserBlocking([ - '_' => 'user', - 'id' => $id, - ]); + $this->peerDatabase->addUserBlocking($id); } } catch (Exception $e) { $this->logger->logger($e->getMessage(), Logger::WARNING);