From 21a88b44333ff9706606bc6e07fcc1df2ad2c4af 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 (cherry picked from commit cbf942c48e17e13d6f64468bdbd88dba5918c5d7) --- src/Controller/AuthorController.php | 3 +- src/Service/CacheService.php | 66 +++++++++++++++++++ src/Service/MetadataRetrievalException.php | 11 ++++ src/Service/NostrClient.php | 14 ++-- templates/components/Molecules/Card.html.twig | 4 +- .../components/Organisms/CardList.html.twig | 3 +- 6 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 src/Service/CacheService.php create mode 100644 src/Service/MetadataRetrievalException.php diff --git a/src/Controller/AuthorController.php b/src/Controller/AuthorController.php index 11b91e9..4a2fb16 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 new file mode 100644 index 0000000..346bd7c --- /dev/null +++ b/src/Service/CacheService.php @@ -0,0 +1,66 @@ +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]); + throw new MetadataRetrievalException('Failed to retrieve metadata', 0, $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); + return $content; + } + } + + public function getRelays($npub) + { + $cacheKey = '3_' . $npub; + try { + return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub) { + $item->expiresAfter(3600); // 1 hour + try { + return $this->nostrClient->getNpubRelays($npub); + } catch (\Exception $e) { + $this->logger->error('Error getting relays.', ['exception' => $e]); + return []; + } + }); + } catch (InvalidArgumentException $e) { + $this->logger->error('Error getting relay data.', ['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) { @@ -115,11 +121,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 %}