diff --git a/src/Controller/ForumController.php b/src/Controller/ForumController.php index bd64a92..1a107be 100644 --- a/src/Controller/ForumController.php +++ b/src/Controller/ForumController.php @@ -4,7 +4,10 @@ declare(strict_types=1); namespace App\Controller; +use App\Entity\User; +use App\Service\NostrClient; use App\Util\ForumTopics; +use App\Util\NostrKeyUtil; use Elastica\Aggregation\Filters as FiltersAgg; use Elastica\Query; use Elastica\Query\BoolQuery; @@ -25,7 +28,8 @@ class ForumController extends AbstractController public function index( #[Autowire(service: 'fos_elastica.index.articles')] \Elastica\Index $index, CacheInterface $cache, - Request $request + Request $request, + NostrClient $nostrClient ): Response { // Optional: small cache so we don’t hammer ES on every page view //$categoriesWithCounts = $cache->get('forum.index.counts.v2', function (ItemInterface $item) use ($index) { @@ -36,8 +40,40 @@ class ForumController extends AbstractController // return $this->hydrateCategoryCounts(self::TOPICS, $counts); //}); $categoriesWithCounts = $this->hydrateCategoryCounts(ForumTopics::TOPICS, $counts); + + $userInterests = null; + /** @var User $user */ + $user = $this->getUser(); + if (!!$user) { + try { + $pubkey = NostrKeyUtil::npubToHex($user->getNpub()); + $interests = $nostrClient->getUserInterests($pubkey); + if (!empty($interests)) { + $userInterests = $this->hydrateCategoryCounts(ForumTopics::TOPICS, $counts); + // Filter to only include subcategories that have tags in interests + foreach ($userInterests as $catKey => $cat) { + $subs = []; + foreach ($cat['subcategories'] as $subKey => $sub) { + $subTags = array_map('strtolower', $sub['tags']); + if (array_intersect($subTags, $interests)) { + $subs[$subKey] = $sub; + } + } + if (!empty($subs)) { + $userInterests[$catKey]['subcategories'] = $subs; + } else { + unset($userInterests[$catKey]); + } + } + } + } catch (\Exception $e) { + // Ignore errors, just don't show user interests + } + } + return $this->render('forum/index.html.twig', [ 'topics' => $categoriesWithCounts, + 'userInterests' => $userInterests, ]); } diff --git a/src/Enum/KindsEnum.php b/src/Enum/KindsEnum.php index d60b234..a4eb994 100644 --- a/src/Enum/KindsEnum.php +++ b/src/Enum/KindsEnum.php @@ -11,6 +11,7 @@ enum KindsEnum: int case REPOST = 6; // Only wraps kind 1, NIP-18, will not implement case GENERIC_REPOST = 16; // Generic repost, original kind signalled in a "k" tag, NIP-18 case FILE_METADATA = 1063; // NIP-94 + case INTERESTS = 10015; // NIP-51 case COMMENTS = 1111; case HTTP_AUTH = 27235; // NIP-98, HTTP Auth case CURATION_SET = 30004; // NIP-51 diff --git a/src/Service/NostrClient.php b/src/Service/NostrClient.php index b4fb6c8..5f7c455 100644 --- a/src/Service/NostrClient.php +++ b/src/Service/NostrClient.php @@ -1072,4 +1072,38 @@ class NostrClient return null; } } + + /** + * Fetch the latest interest list (kind 10015) for a pubkey and return the 't' tags. + */ + public function getUserInterests(string $pubkey): array + { + $request = $this->createNostrRequest( + kinds: [10015], + filters: ['authors' => [$pubkey]], + relaySet: $this->defaultRelaySet + ); + + $events = $this->processResponse($request->send(), function($received) use ($pubkey) { + $this->logger->info('Getting interests for pubkey', ['pubkey' => $pubkey, 'item' => $received]); + return $received; + }); + + if (empty($events)) { + return []; + } + + // Sort by created_at descending and take the latest + usort($events, fn($a, $b) => $b->created_at <=> $a->created_at); + $latest = $events[0]; + + $tTags = []; + foreach ($latest->tags as $tag) { + if (is_array($tag) && isset($tag[0]) && $tag[0] === 't' && isset($tag[1])) { + $tTags[] = strtolower(trim($tag[1])); + } + } + + return $tTags; + } } diff --git a/templates/event/index.html.twig b/templates/event/index.html.twig index 98ff6d7..f776b0f 100644 --- a/templates/event/index.html.twig +++ b/templates/event/index.html.twig @@ -172,6 +172,10 @@ {% endif %} + +
+        {{ event|json_encode(constant('JSON_PRETTY_PRINT')) }}
+    
{% endblock %} diff --git a/templates/forum/index.html.twig b/templates/forum/index.html.twig index bd39dd2..98786ed 100644 --- a/templates/forum/index.html.twig +++ b/templates/forum/index.html.twig @@ -3,6 +3,31 @@ {% block body %} +{% if userInterests %} +
+
+

Your Interests

+
+
+
+ {% for catKey, category in userInterests %} + {% for subKey, sub in category.subcategories %} +
+

{{ sub.name }}

+
+
+ {% for tag in sub.tags %} + {{ tag }} + {% endfor %} +
+
{{ sub.count|default(0) }}
+
+
+ {% endfor %} + {% endfor %} +
+{% endif %} + {% for catKey, category in topics %}