From cbf942c48e17e13d6f64468bdbd88dba5918c5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Sun, 3 Aug 2025 12:30:37 +0200 Subject: [PATCH] Better caching --- src/Controller/AuthorController.php | 3 ++- src/Service/CacheService.php | 15 ++++++--------- src/Service/MetadataRetrievalException.php | 11 +++++++++++ src/Service/NostrClient.php | 14 ++++++++------ templates/components/Molecules/Card.html.twig | 4 +++- templates/components/Organisms/CardList.html.twig | 3 ++- 6 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 src/Service/MetadataRetrievalException.php diff --git a/src/Controller/AuthorController.php b/src/Controller/AuthorController.php index 4509dd6..6ca8001 100644 --- a/src/Controller/AuthorController.php +++ b/src/Controller/AuthorController.php @@ -52,7 +52,8 @@ class AuthorController extends AbstractController return $this->render('pages/author.html.twig', [ 'author' => $author, 'npub' => $npub, - 'articles' => $articles + 'articles' => $articles, + 'is_author_profile' => true, ]); } diff --git a/src/Service/CacheService.php b/src/Service/CacheService.php index b0aba8b..346bd7c 100644 --- a/src/Service/CacheService.php +++ b/src/Service/CacheService.php @@ -26,21 +26,18 @@ readonly class CacheService { $cacheKey = '0_' . $npub; try { - return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub) { + return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub, $cacheKey) { $item->expiresAfter(3600); // 1 hour, adjust as needed try { $meta = $this->nostrClient->getNpubMetadata($npub); + $this->logger->info('Metadata:', ['meta' => json_encode($meta)]); + return json_decode($meta->content); } catch (\Exception $e) { $this->logger->error('Error getting user data.', ['exception' => $e]); - $meta = new \stdClass(); - $content = new \stdClass(); - $meta->name = substr($npub, 0, 8) . '…' . substr($npub, -4); - $meta->content = json_encode($content); + throw new MetadataRetrievalException('Failed to retrieve metadata', 0, $e); } - $this->logger->info('Metadata:', ['meta' => json_encode($meta)]); - return json_decode($meta->content); }); - } catch (InvalidArgumentException $e) { + } catch (\Exception|InvalidArgumentException $e) { $this->logger->error('Error getting user data.', ['exception' => $e]); $content = new \stdClass(); $content->name = substr($npub, 0, 8) . '…' . substr($npub, -4); @@ -55,7 +52,7 @@ readonly class CacheService return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub) { $item->expiresAfter(3600); // 1 hour try { - return $this->nostrClient->getRelaysForNpub($npub); + return $this->nostrClient->getNpubRelays($npub); } catch (\Exception $e) { $this->logger->error('Error getting relays.', ['exception' => $e]); return []; diff --git a/src/Service/MetadataRetrievalException.php b/src/Service/MetadataRetrievalException.php new file mode 100644 index 0000000..bcf0733 --- /dev/null +++ b/src/Service/MetadataRetrievalException.php @@ -0,0 +1,11 @@ +defaultRelaySet; + $relaySet->addRelay(new Relay('wss://profiles.nostr1.com')); // profile aggregator $this->logger->info('Getting metadata for npub', ['npub' => $npub]); // Npubs are converted to hex for the request down the line $request = $this->createNostrRequest( kinds: [KindsEnum::METADATA], - filters: ['authors' => [$npub]] + filters: ['authors' => [$npub]], + relaySet: $relaySet ); $events = $this->processResponse($request->send(), function($received) { @@ -89,11 +95,7 @@ class NostrClient $this->logger->info('Getting metadata for npub', ['response' => $events]); if (empty($events)) { - $meta = new \stdClass(); - $content = new \stdClass(); - $content->name = substr($npub, 0, 8) . '…' . substr($npub, -4); - $meta->content = json_encode($content); - return $meta; + throw new \Exception('No metadata found for npub: ' . $npub); } // Sort by date and return newest diff --git a/templates/components/Molecules/Card.html.twig b/templates/components/Molecules/Card.html.twig index 2f8306b..21f4008 100644 --- a/templates/components/Molecules/Card.html.twig +++ b/templates/components/Molecules/Card.html.twig @@ -4,7 +4,9 @@ {% if category %} {{ category }} {% else %} -

by

+ {% if not is_author_profile %} +

by

+ {% endif %} {{ article.createdAt|date('F j Y') }} {% endif %} diff --git a/templates/components/Organisms/CardList.html.twig b/templates/components/Organisms/CardList.html.twig index c68c23f..65bb74a 100644 --- a/templates/components/Organisms/CardList.html.twig +++ b/templates/components/Organisms/CardList.html.twig @@ -1,7 +1,8 @@
+ {% set is_author_profile = is_author_profile|default(false) %} {% for item in list %} {% if item.slug is not empty and item.title is not empty %} - + {% endif %} {% endfor %}