diff --git a/src/Loop/Connection/ReadLoop.php b/src/Loop/Connection/ReadLoop.php index d43c83b94..d3321d2c1 100644 --- a/src/Loop/Connection/ReadLoop.php +++ b/src/Loop/Connection/ReadLoop.php @@ -71,7 +71,7 @@ final class ReadLoop extends Loop $this->API->logger("Got NothingInTheSocketException in DC {$this->datacenter}, disconnecting because we have nothing to do...", Logger::ERROR); $this->connection->disconnect(true); } else { - $this->API->logger($e); + $this->API->logger($e, Logger::ERROR); $this->API->logger("Got exception in DC {$this->datacenter}, reconnecting...", Logger::ERROR); $this->connection->reconnect(); } diff --git a/src/MTProto.php b/src/MTProto.php index d5ee6ecf7..1bad2f58d 100644 --- a/src/MTProto.php +++ b/src/MTProto.php @@ -408,6 +408,7 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter */ #[OrmMappedArray(KeyType::STRING, ValueType::SCALAR, cacheTtl: 0, optimizeIfWastedMb: 1, tablePostfix: 'session')] public DbArray $sessionDb; + private bool $cleaned = false; /** * Returns an instance of a client by session name. @@ -1003,6 +1004,11 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter $this->updateQueue = $q; } + if ($this->cleaned) { + return; + } + $this->cleaned = true; + if (isset($this->channels_state)) { $this->updateState = new CombinedUpdatesState; foreach ($this->channels_state->get() as $channelId => $state) { @@ -1125,6 +1131,9 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter $this->initPromise = $deferred->getFuture(); try { + // Update settings from constructor + $this->updateSettings($settings); + // Setup logger $this->setupLogger(); @@ -1154,8 +1163,6 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter } // Reset MTProto session (not related to user session) $this->resetMTProtoSession("wakeup"); - // Update settings from constructor - $this->updateSettings($settings); // Update TL callbacks $callbacks = [$this, $this->peerDatabase]; if ($this->settings->getDb()->getEnableFileReferenceDb()) { diff --git a/src/MTProtoTools/FilesLogic.php b/src/MTProtoTools/FilesLogic.php index 746373b71..94290bb84 100644 --- a/src/MTProtoTools/FilesLogic.php +++ b/src/MTProtoTools/FilesLogic.php @@ -240,7 +240,14 @@ trait FilesLogic if ($result->shouldServe()) { $pipe = new Pipe(1024 * 1024); [$start, $end] = $result->getServeRange(); - EventLoop::queue($this->downloadToStream(...), $messageMedia, $pipe->getSink(), $cb, $start, $end, $cancellation); + EventLoop::queue(function() use($messageMedia, $pipe, $cb, $start, $end, $cancellation) { + try { + $this->downloadToStream($messageMedia, $pipe->getSink(), $cb, $start, $end, $cancellation); + } catch (\Throwable $e) { + $this->logger->logger($e, Logger::ERROR); + } + $pipe->getSink()->close(); + }); $body = $pipe->getSource(); } elseif (!\in_array($result->getCode(), [HttpStatus::OK, HttpStatus::PARTIAL_CONTENT], true)) { $body = $result->getCodeExplanation(); diff --git a/src/MTProtoTools/PeerDatabase.php b/src/MTProtoTools/PeerDatabase.php index 34d2405b7..7482cc5ba 100644 --- a/src/MTProtoTools/PeerDatabase.php +++ b/src/MTProtoTools/PeerDatabase.php @@ -266,11 +266,13 @@ final class PeerDatabase implements TLCallback } $new = self::getUsernames($new); $old = $old ? self::getUsernames($old) : []; - $diffToRemove = array_diff($old, $new); - $diffToAdd = array_diff($new, $old); - if (!$diffToAdd && !$diffToRemove) { - return; + foreach ($old as $key => $username) { + if (!isset($this->usernames[$username])) { + unset($old[$key]); + } } + $diffToRemove = array_diff($old, $new); + $diffToAdd = array_diff($new, $diffToRemove); $lock = $this->decacheMutex->acquire(); try { foreach ($diffToRemove as $username) { @@ -410,6 +412,9 @@ final class PeerDatabase implements TLCallback return; } } + + $this->recacheChatUsername($user['id'], $existingChat, $user); + if ($existingChat != $user) { $this->API->logger("Updated user {$user['id']}", Logger::ULTRA_VERBOSE); if (($user['min'] ?? false) && !($existingChat['min'] ?? false)) { @@ -419,7 +424,7 @@ final class PeerDatabase implements TLCallback $user['access_hash'] = $existingChat['access_hash']; } } - $this->recacheChatUsername($user['id'], $existingChat, $user); + if (!$this->API->settings->getDb()->getEnablePeerInfoDb()) { $user = [ '_' => $user['_'], @@ -544,8 +549,8 @@ final class PeerDatabase implements TLCallback return; } } + $this->recacheChatUsername($bot_api_id, $existingChat, $chat); 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);