Browse Source

Renaming things

imwald
Nuša Pukšič 3 months ago
parent
commit
1dbdb75950
  1. 13
      src/Service/NostrClient.php
  2. 62
      src/Service/RedisCacheService.php
  3. 2
      templates/static/unfold.html.twig

13
src/Service/NostrClient.php

@ -104,27 +104,26 @@ class NostrClient
/** /**
* @throws \Exception * @throws \Exception
*/ */
public function getNpubMetadata($npub): \stdClass public function getPubkeyMetadata($pubkey): \stdClass
{ {
$relaySet = $this->defaultRelaySet; $relaySet = $this->defaultRelaySet;
$relaySet->addRelay(new Relay('wss://profiles.nostr1.com')); // profile aggregator $relaySet->addRelay(new Relay('wss://profiles.nostr1.com')); // profile aggregator
$this->logger->info('Getting metadata for npub', ['npub' => $npub]); $this->logger->info('Getting metadata for pubkey ' . $pubkey );
// Npubs are converted to hex for the request down the line
$request = $this->createNostrRequest( $request = $this->createNostrRequest(
kinds: [KindsEnum::METADATA], kinds: [KindsEnum::METADATA],
filters: ['authors' => [$npub]], filters: ['authors' => [$pubkey]],
relaySet: $relaySet relaySet: $relaySet
); );
$events = $this->processResponse($request->send(), function($received) { $events = $this->processResponse($request->send(), function($received) {
$this->logger->info('Getting metadata for npub', ['item' => $received]); $this->logger->info('Getting metadata for pubkey', ['item' => $received]);
return $received; return $received;
}); });
$this->logger->info('Getting metadata for npub', ['response' => $events]); $this->logger->info('Getting metadata for pubkey', ['response' => $events]);
if (empty($events)) { if (empty($events)) {
throw new \Exception('No metadata found for npub: ' . $npub); throw new \Exception('No metadata found for pubkey: ' . $pubkey);
} }
// Sort by date and return newest // Sort by date and return newest

62
src/Service/RedisCacheService.php

@ -18,7 +18,7 @@ readonly class RedisCacheService
{ {
public function __construct( public function __construct(
private NostrClient $nostrClient, private NostrClient $nostrClient,
private CacheItemPoolInterface $appCache, private CacheItemPoolInterface $npubCache,
private EntityManagerInterface $entityManager, private EntityManagerInterface $entityManager,
private LoggerInterface $logger private LoggerInterface $logger
) {} ) {}
@ -50,7 +50,7 @@ readonly class RedisCacheService
$content->name = $defaultName; $content->name = $defaultName;
try { try {
$content = $this->appCache->get($cacheKey, function (ItemInterface $item) use ($pubkey) { $content = $this->npubCache->get($cacheKey, function (ItemInterface $item) use ($pubkey) {
$item->expiresAfter(3600); // 1 hour, adjust as needed $item->expiresAfter(3600); // 1 hour, adjust as needed
$rawEvent = $this->fetchRawUserEvent($pubkey); $rawEvent = $this->fetchRawUserEvent($pubkey);
return $this->parseUserMetadata($rawEvent, $pubkey); return $this->parseUserMetadata($rawEvent, $pubkey);
@ -60,9 +60,9 @@ readonly class RedisCacheService
} }
// If content is still default, delete cache to retry next time // If content is still default, delete cache to retry next time
if (isset($content->name) && $content->name === $defaultName if (isset($content->name) && $content->name === $defaultName
&& $this->appCache->hasItem($cacheKey)) { && $this->npubCache->hasItem($cacheKey)) {
try { try {
$this->appCache->deleteItem($cacheKey); $this->npubCache->deleteItem($cacheKey);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error deleting user cache item.', ['exception' => $e]); $this->logger->error('Error deleting user cache item.', ['exception' => $e]);
} }
@ -77,7 +77,7 @@ readonly class RedisCacheService
private function fetchRawUserEvent(string $pubkey): \stdClass private function fetchRawUserEvent(string $pubkey): \stdClass
{ {
try { try {
return $this->nostrClient->getNpubMetadata(NostrKeyUtil::hexToNpub($pubkey)); return $this->nostrClient->getPubkeyMetadata($pubkey);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error getting user data.', ['exception' => $e]); $this->logger->error('Error getting user data.', ['exception' => $e]);
$rawEvent = new \stdClass(); $rawEvent = new \stdClass();
@ -145,7 +145,7 @@ readonly class RedisCacheService
} }
$cacheKey = '0_with_raw_' . $pubkey; $cacheKey = '0_with_raw_' . $pubkey;
try { try {
return $this->appCache->get($cacheKey, function (ItemInterface $item) use ($pubkey) { return $this->npubCache->get($cacheKey, function (ItemInterface $item) use ($pubkey) {
$item->expiresAfter(3600); // 1 hour, adjust as needed $item->expiresAfter(3600); // 1 hour, adjust as needed
$rawEvent = $this->fetchRawUserEvent($pubkey); $rawEvent = $this->fetchRawUserEvent($pubkey);
$contentData = $this->parseUserMetadata($rawEvent, $pubkey); $contentData = $this->parseUserMetadata($rawEvent, $pubkey);
@ -186,7 +186,7 @@ readonly class RedisCacheService
$result = []; $result = [];
$cacheKeys = array_map(fn($pubkey) => $this->getUserCacheKey($pubkey), $pubkeys); $cacheKeys = array_map(fn($pubkey) => $this->getUserCacheKey($pubkey), $pubkeys);
$pubkeyMap = array_combine($cacheKeys, $pubkeys); $pubkeyMap = array_combine($cacheKeys, $pubkeys);
$items = $this->appCache->getItems($cacheKeys); $items = $this->npubCache->getItems($cacheKeys);
foreach ($items as $cacheKey => $item) { foreach ($items as $cacheKey => $item) {
$pubkey = $pubkeyMap[$cacheKey]; $pubkey = $pubkeyMap[$cacheKey];
if ($item->isHit()) { if ($item->isHit()) {
@ -205,7 +205,7 @@ readonly class RedisCacheService
$cacheKey = '10002_' . $npub; $cacheKey = '10002_' . $npub;
try { try {
return $this->appCache->get($cacheKey, function (ItemInterface $item) use ($npub) { return $this->npubCache->get($cacheKey, function (ItemInterface $item) use ($npub) {
$item->expiresAfter(3600); // 1 hour, adjust as needed $item->expiresAfter(3600); // 1 hour, adjust as needed
try { try {
$relays = $this->nostrClient->getNpubRelays($npub); $relays = $this->nostrClient->getNpubRelays($npub);
@ -230,7 +230,7 @@ readonly class RedisCacheService
{ {
// redis cache lookup of magazine index by slug // redis cache lookup of magazine index by slug
$key = 'magazine-index-' . $slug; $key = 'magazine-index-' . $slug;
return $this->appCache->get($key, function (ItemInterface $item) use ($slug) { return $this->npubCache->get($key, function (ItemInterface $item) use ($slug) {
$item->expiresAfter(3600); // 1 hour $item->expiresAfter(3600); // 1 hour
$nzines = $this->entityManager->getRepository(Event::class)->findBy(['kind' => KindsEnum::PUBLICATION_INDEX]); $nzines = $this->entityManager->getRepository(Event::class)->findBy(['kind' => KindsEnum::PUBLICATION_INDEX]);
@ -271,9 +271,9 @@ readonly class RedisCacheService
// Insert the new article tag at the top // Insert the new article tag at the top
array_unshift($index->tags, $articleTag); array_unshift($index->tags, $articleTag);
try { try {
$item = $this->appCache->getItem($key); $item = $this->npubCache->getItem($key);
$item->set($index); $item->set($index);
$this->appCache->save($item); $this->npubCache->save($item);
return true; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error updating magazine index.', ['exception' => $e]); $this->logger->error('Error updating magazine index.', ['exception' => $e]);
@ -292,7 +292,7 @@ readonly class RedisCacheService
{ {
$cacheKey = 'media_' . $npub . '_' . $limit; $cacheKey = 'media_' . $npub . '_' . $limit;
try { try {
return $this->appCache->get($cacheKey, function (ItemInterface $item) use ($npub, $limit) { return $this->npubCache->get($cacheKey, function (ItemInterface $item) use ($npub, $limit) {
$item->expiresAfter(600); // 10 minutes cache for media events $item->expiresAfter(600); // 10 minutes cache for media events
try { try {
@ -340,7 +340,7 @@ readonly class RedisCacheService
try { try {
// Fetch and cache all media events // Fetch and cache all media events
$allMediaEvents = $this->appCache->get($cacheKey, function (ItemInterface $item) use ($pubkey) { $allMediaEvents = $this->npubCache->get($cacheKey, function (ItemInterface $item) use ($pubkey) {
$item->expiresAfter(600); // 10 minutes cache $item->expiresAfter(600); // 10 minutes cache
try { try {
@ -411,7 +411,7 @@ readonly class RedisCacheService
$cacheKey = 'event_' . $eventId . ($relays ? '_' . md5(json_encode($relays)) : ''); $cacheKey = 'event_' . $eventId . ($relays ? '_' . md5(json_encode($relays)) : '');
try { try {
return $this->appCache->get($cacheKey, function (ItemInterface $item) use ($eventId, $relays) { return $this->npubCache->get($cacheKey, function (ItemInterface $item) use ($eventId, $relays) {
$item->expiresAfter(1800); // 30 minutes cache for events $item->expiresAfter(1800); // 30 minutes cache for events
try { try {
@ -428,44 +428,16 @@ readonly class RedisCacheService
} }
} }
/** public function setMetadata(\swentel\nostr\Event\Event $event): void
* Get a parameterized replaceable event (naddr) with caching
*
* @param array $decodedData The decoded naddr data
* @return object|null The event object or null if not found
*/
public function getNaddrEvent(array $decodedData): ?object
{
$cacheKey = 'naddr_' . $decodedData['kind'] . '_' . $decodedData['pubkey'] . '_' . $decodedData['identifier'] . '_' . md5(json_encode($decodedData['relays'] ?? []));
try {
return $this->appCache->get($cacheKey, function (ItemInterface $item) use ($decodedData) {
$item->expiresAfter(1800); // 30 minutes cache for naddr events
try {
$event = $this->nostrClient->getEventByNaddr($decodedData);
return $event;
} catch (\Exception $e) {
$this->logger->error('Error getting naddr event.', ['exception' => $e, 'decodedData' => $decodedData]);
return null;
}
});
} catch (InvalidArgumentException $e) {
$this->logger->error('Cache error getting naddr event.', ['exception' => $e, 'decodedData' => $decodedData]);
return null;
}
}
public function setMetadata(\swentel\nostr\Event\Event $event)
{ {
$key = new Key(); $key = new Key();
$npub = $key->convertPublicKeyToBech32($event->getPublicKey()); $npub = $key->convertPublicKeyToBech32($event->getPublicKey());
$cacheKey = '0_' . $npub; $cacheKey = '0_' . $npub;
try { try {
$item = $this->appCache->getItem($cacheKey); $item = $this->npubCache->getItem($cacheKey);
$item->set(json_decode($event->getContent())); $item->set(json_decode($event->getContent()));
$item->expiresAfter(3600); // 1 hour $item->expiresAfter(3600); // 1 hour
$this->appCache->save($item); $this->npubCache->save($item);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error setting user metadata.', ['exception' => $e]); $this->logger->error('Error setting user metadata.', ['exception' => $e]);
} }

2
templates/static/unfold.html.twig

@ -1,4 +1,4 @@
{% extends 'layout-full.html.twig' %} {% extends 'layout.html.twig' %}
{% block title %}Decent Newsroom — A decentralized platform for collaborative publishing{% endblock %} {% block title %}Decent Newsroom — A decentralized platform for collaborative publishing{% endblock %}

Loading…
Cancel
Save