From 3680f649ae59b467cc9a7098ebf5ab63d60c9e60 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 24 Sep 2024 17:45:07 +0200 Subject: [PATCH] Peer database improvements --- src/MTProtoTools/PeerDatabase.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/MTProtoTools/PeerDatabase.php b/src/MTProtoTools/PeerDatabase.php index 34d2405b7..a91eab01c 100644 --- a/src/MTProtoTools/PeerDatabase.php +++ b/src/MTProtoTools/PeerDatabase.php @@ -190,6 +190,7 @@ final class PeerDatabase implements TLCallback } else { $this->processUser($id); } + $this->recacheChatUsername($id, $this->db[$id] ?? null, []); unset($this->db[$id]); } @@ -313,7 +314,13 @@ final class PeerDatabase implements TLCallback } } $result = $this->usernames[$username]; - return $result === null ? $result : (int) $result; + $id = $result === null ? $result : (int) $result; + if (!$this->isset($id)) { + $this->API->logger("No peer entry for cached username @$username => {$id}, dropping entry!"); + unset($this->usernames[$username]); + return null; + } + return $id; } private array $caching_simple_username = []; @@ -419,7 +426,7 @@ final class PeerDatabase implements TLCallback $user['access_hash'] = $existingChat['access_hash']; } } - $this->recacheChatUsername($user['id'], $existingChat, $user); + $userUnchanged = $user; if (!$this->API->settings->getDb()->getEnablePeerInfoDb()) { $user = [ '_' => $user['_'], @@ -429,6 +436,7 @@ final class PeerDatabase implements TLCallback ]; } $this->db[$user['id']] = $user; + $this->recacheChatUsername($user['id'], $existingChat, $userUnchanged); if ($existingChat && ($existingChat['min'] ?? false) && !($user['min'] ?? false)) { $this->API->minDatabase->clearPeer($user['id']); } @@ -545,7 +553,6 @@ final class PeerDatabase implements TLCallback } } if ($existingChat != $chat) { - $this->recacheChatUsername($bot_api_id, $existingChat, $chat); $this->API->logger("Updated chat {$bot_api_id}", Logger::ULTRA_VERBOSE); if (($chat['min'] ?? false) && $existingChat && !($existingChat['min'] ?? false)) { $this->API->logger("{$bot_api_id} is min, filling missing fields", Logger::ULTRA_VERBOSE); @@ -557,6 +564,7 @@ final class PeerDatabase implements TLCallback } $chat = $newchat; } + $chatUnchanged = $chat; if (!$this->API->settings->getDb()->getEnablePeerInfoDb()) { $chat = [ '_' => $chat['_'], @@ -566,6 +574,7 @@ final class PeerDatabase implements TLCallback ]; } $this->db[$bot_api_id] = $chat; + $this->recacheChatUsername($bot_api_id, $existingChat, $chatUnchanged); if ($existingChat && ($existingChat['min'] ?? false) && !($chat['min'] ?? false)) { $this->API->minDatabase->clearPeer($bot_api_id); }