Browse Source

Get media from all over

imwald
Nuša Pukšič 1 month ago
parent
commit
0044644610
  1. 5
      src/Command/CacheMediaDiscoveryCommand.php
  2. 6
      src/Controller/MediaDiscoveryController.php
  3. 19
      src/Service/NostrClient.php

5
src/Command/CacheMediaDiscoveryCommand.php

@ -13,6 +13,7 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\CacheInterface;
#[AsCommand( #[AsCommand(
@ -35,6 +36,7 @@ class CacheMediaDiscoveryCommand extends Command
private readonly NostrClient $nostrClient, private readonly NostrClient $nostrClient,
private readonly CacheInterface $cache, private readonly CacheInterface $cache,
private readonly LoggerInterface $logger, private readonly LoggerInterface $logger,
private readonly ParameterBagInterface $params,
) { ) {
parent::__construct(); parent::__construct();
} }
@ -61,7 +63,8 @@ class CacheMediaDiscoveryCommand extends Command
$allHashtags = array_merge($allHashtags, self::TOPIC_HASHTAGS[$topic]); $allHashtags = array_merge($allHashtags, self::TOPIC_HASHTAGS[$topic]);
} }
$cacheKey = 'media_discovery_events_all'; $env = $this->params->get('kernel.environment');
$cacheKey = 'media_discovery_events_all_' . $env;
if ($force) { if ($force) {
$io->info('Force refresh enabled - deleting existing cache'); $io->info('Force refresh enabled - deleting existing cache');

6
src/Controller/MediaDiscoveryController.php

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\CacheInterface;
@ -22,7 +23,7 @@ class MediaDiscoveryController extends AbstractController
]; ];
#[Route('/multimedia', name: 'media-discovery')] #[Route('/multimedia', name: 'media-discovery')]
public function discover(CacheInterface $cache): Response public function discover(CacheInterface $cache, ParameterBagInterface $params): Response
{ {
// Defaulting to all, might do topics later // Defaulting to all, might do topics later
try { try {
@ -33,7 +34,8 @@ class MediaDiscoveryController extends AbstractController
} }
// Cache key for all media events // Cache key for all media events
$cacheKey = 'media_discovery_events_all'; $env = $params->get('kernel.environment');
$cacheKey = 'media_discovery_events_all_' . $env;
// Read from cache only - the cache is populated by the CacheMediaDiscoveryCommand // Read from cache only - the cache is populated by the CacheMediaDiscoveryCommand
$allCachedEvents = $cache->get($cacheKey, function () { $allCachedEvents = $cache->get($cacheKey, function () {

19
src/Service/NostrClient.php

@ -826,23 +826,6 @@ class NostrClient
{ {
$allEvents = []; $allEvents = [];
// Prefer local relay if configured
if ($this->nostrDefaultRelay) {
$this->logger->info('Using local relay for media discovery hashtag fetch', [
'relay' => $this->nostrDefaultRelay,
'hashtags' => $hashtags,
]);
$relayset = $this->createRelaySet([$this->nostrDefaultRelay]);
} else {
// Fallback to known public media-friendly relays
$relayUrls = ['wss://theforest.nostr1.com', 'wss://relay.nostr.band'];
$this->logger->info('Using public relays for media discovery hashtag fetch', [
'relays' => $relayUrls,
'hashtags' => $hashtags,
]);
$relayset = $this->createRelaySet($relayUrls);
}
// Fetch events for each hashtag // Fetch events for each hashtag
foreach ($hashtags as $hashtag) { foreach ($hashtags as $hashtag) {
$request = $this->createNostrRequest( $request = $this->createNostrRequest(
@ -851,7 +834,7 @@ class NostrClient
'tag' => ['#t', [$hashtag]], 'tag' => ['#t', [$hashtag]],
'limit' => 100 // Fetch up to 100 per hashtag 'limit' => 100 // Fetch up to 100 per hashtag
], ],
relaySet: $relayset relaySet: $this->createRelaySet(static::REPUTABLE_RELAYS)
); );
$events = $this->processResponse($request->send(), function($event) { $events = $this->processResponse($request->send(), function($event) {

Loading…
Cancel
Save