Browse Source

Relays

imwald
Nuša Pukšič 3 months ago
parent
commit
13f3b46b55
  1. 2
      src/Controller/AuthorController.php
  2. 5
      src/Service/NostrClient.php
  3. 12
      src/Service/RedisCacheService.php

2
src/Controller/AuthorController.php

@ -29,7 +29,7 @@ class AuthorController extends AbstractController @@ -29,7 +29,7 @@ class AuthorController extends AbstractController
$author = $redisCacheService->getMetadata($keyUtil->npubToHex($npub));
// Use paginated cached media events - fetches 200 from relays, serves first 24
$paginatedData = $redisCacheService->getMediaEventsPaginated($npub, 1, 24);
$paginatedData = $redisCacheService->getMediaEventsPaginated($keyUtil->npubToHex($npub), 1, 24);
$mediaEvents = $paginatedData['events'];
// Encode event IDs as note1... for each event

5
src/Service/NostrClient.php

@ -34,7 +34,7 @@ class NostrClient @@ -34,7 +34,7 @@ class NostrClient
'wss://relay.primal.net',
'wss://nos.lol',
'wss://relay.snort.social',
'wss://nostr.land',
// 'wss://nostr.land', // requires auth that doesn't currently work!
'wss://purplepag.es',
];
@ -46,7 +46,8 @@ class NostrClient @@ -46,7 +46,8 @@ class NostrClient
{
$this->defaultRelaySet = new RelaySet();
$this->defaultRelaySet->addRelay(new Relay('wss://theforest.nostr1.com')); // public aggregator relay
$this->defaultRelaySet->addRelay(new Relay('wss://nostr.land')); // public aggregator relay
$this->defaultRelaySet->addRelay(new Relay('wss://relay.damus.io')); // public aggregator relay
$this->defaultRelaySet->addRelay(new Relay('wss://relay.primal.net')); // public aggregator relay
}
/**

12
src/Service/RedisCacheService.php

@ -328,25 +328,25 @@ readonly class RedisCacheService @@ -328,25 +328,25 @@ readonly class RedisCacheService
/**
* Get all media events for pagination (fetches large batch, caches, returns paginated)
*
* @param string $npub The author's npub
* @param string $pubkey The author's pubkey
* @param int $page Page number (1-based)
* @param int $pageSize Number of items per page
* @return array ['events' => array, 'hasMore' => bool, 'total' => int]
*/
public function getMediaEventsPaginated(string $npub, int $page = 1, int $pageSize = 60): array
public function getMediaEventsPaginated(string $pubkey, int $page = 1, int $pageSize = 60): array
{
// Cache key for all media events (not page-specific)
$cacheKey = 'media_all_' . $npub;
$cacheKey = 'media_all_' . $pubkey;
try {
// Fetch and cache all media events
$allMediaEvents = $this->redisCache->get($cacheKey, function (ItemInterface $item) use ($npub) {
$allMediaEvents = $this->redisCache->get($cacheKey, function (ItemInterface $item) use ($pubkey) {
$item->expiresAfter(600); // 10 minutes cache
try {
// Fetch a large batch to account for deduplication
// Nostr relays are unstable, so we fetch more than we need
$mediaEvents = $this->nostrClient->getAllMediaEventsForPubkey($npub, 200);
$mediaEvents = $this->nostrClient->getAllMediaEventsForPubkey($pubkey, 200);
// Deduplicate by event ID
$uniqueEvents = [];
@ -364,7 +364,7 @@ readonly class RedisCacheService @@ -364,7 +364,7 @@ readonly class RedisCacheService
return $mediaEvents;
} catch (\Exception $e) {
$this->logger->error('Error getting media events.', ['exception' => $e, 'npub' => $npub]);
$this->logger->error('Error getting media events.', ['exception' => $e, 'pubkey' => $pubkey]);
return [];
}
});

Loading…
Cancel
Save