Skip to content

Commit

Permalink
Fixes #46:
Browse files Browse the repository at this point in the history
  • Loading branch information
TiiFuchs committed May 11, 2024
1 parent 52c61f7 commit 1dbe3d5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,14 @@ protected function getAvailableConversationHandler(Update $update): ?Conversatio
}

$cache = $this->cache();
$conversation = $cache->get(Conversation::cacheKey($update));
$cacheKey = Conversation::cacheKey($update);

if ($cacheKey === null) {
// Cannot identify conversation
return null;
}

$conversation = $cache->get($cacheKey);

if ($conversation === null) {
// No Conversation available
Expand Down
13 changes: 10 additions & 3 deletions src/Conversations/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Telepath\Conversations;

use Telepath\Telegram\Update;
use Telepath\Bot;
use Telepath\Telegram\Update;

abstract class Conversation
{
Expand All @@ -18,9 +18,16 @@ public function __construct(
}
}

public static function cacheKey(Update $update)
public static function cacheKey(Update $update): ?string
{
return "telepath.conversation.{$update->user()->id}.{$update->chat()->id}";
$userId = $update->user()?->id;
$chatId = $update->chat()?->id;

if ($userId === null || $chatId === null) {
return null;
}

return "telepath.conversation:{$userId}:{$chatId}";
}

public function next(string $method, ?string $class = null): static
Expand Down
6 changes: 4 additions & 2 deletions src/Types/Extensions/UpdateExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ trait UpdateExtension
public function user(): ?User
{
foreach (get_object_vars($this) as $field) {
// Search for 'from' field
if (is_object($field) && ($field?->from ?? null) !== null) {
return $field->from;
}
}

return $this->poll_answer?->user
?? null;
return null;
}

public function chat(): ?Chat
{
foreach (get_object_vars($this) as $field) {
// Search for 'chat' field
if (is_object($field) && ($field?->chat ?? null) !== null) {
return $field->chat;
}
}

// Special case for CallbackQuery
return $this->callback_query?->message?->chat
?? null;
}
Expand Down

0 comments on commit 1dbe3d5

Please sign in to comment.