From 76f70de1ebb4ea1e1a3558f9606437a840d2dbed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Mon, 9 Dec 2024 19:17:42 +0100 Subject: [PATCH] Create Journal entity, add more components --- src/Controller/DefaultController.php | 24 +++++- src/Entity/Journal.php | 80 +++++++++++++++++++ src/Repository/JournalRepository.php | 43 ++++++++++ src/Service/NostrClient.php | 2 +- src/Twig/Components/Molecules/Card.php | 4 +- .../Components/Molecules/UserFromNpub.php | 24 ++++++ src/Twig/Components/Organisms/CardList.php | 1 + templates/components/Molecules/Card.html.twig | 2 +- .../Molecules/UserFromNpub.html.twig | 5 ++ .../components/Organisms/CardList.html.twig | 6 +- templates/components/UserMenu.html.twig | 4 +- templates/home.html.twig | 1 + 12 files changed, 186 insertions(+), 10 deletions(-) create mode 100644 src/Entity/Journal.php create mode 100644 src/Repository/JournalRepository.php create mode 100644 src/Twig/Components/Molecules/UserFromNpub.php create mode 100644 templates/components/Molecules/UserFromNpub.html.twig diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index f48021c..c62ebcd 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -4,19 +4,37 @@ declare(strict_types=1); namespace App\Controller; +use App\Entity\Article; +use App\Service\NostrClient; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class DefaultController extends AbstractController { + public function __construct(private readonly EntityManagerInterface $entityManager, + private readonly NostrClient $nostrClient) + { + } /** - * @Route("/default") + * @throws \Exception */ - #[\Symfony\Component\Routing\Attribute\Route('/', name: 'default')] + #[Route('/', name: 'default')] public function index(): Response { - return $this->render('home.html.twig'); + $list = $this->entityManager->getRepository(Article::class)->findAll(); + $current = array_slice($list, 0,10); + + $npubs = array_map(function($obj) { + return $obj->getPubkey(); + }, $list); + + $this->nostrClient->getMetadata(array_unique($npubs)); + + return $this->render('home.html.twig', [ + 'list' => $current + ]); } } diff --git a/src/Entity/Journal.php b/src/Entity/Journal.php new file mode 100644 index 0000000..7edfd5a --- /dev/null +++ b/src/Entity/Journal.php @@ -0,0 +1,80 @@ +id; + } + + public function getNpub(): ?string + { + return $this->npub; + } + + public function setNpub(string $npub): static + { + $this->npub = $npub; + + return $this; + } + + public function getMainCategories(): array + { + return $this->mainCategories; + } + + public function setMainCategories(array $mainCategories): static + { + $this->mainCategories = $mainCategories; + + return $this; + } + + public function getLists(): ?array + { + return $this->lists; + } + + public function setLists(?array $lists): static + { + $this->lists = $lists; + + return $this; + } + + public function getEditor(): ?string + { + return $this->editor; + } + + public function setEditor(?string $editor): static + { + $this->editor = $editor; + + return $this; + } +} diff --git a/src/Repository/JournalRepository.php b/src/Repository/JournalRepository.php new file mode 100644 index 0000000..f4846f7 --- /dev/null +++ b/src/Repository/JournalRepository.php @@ -0,0 +1,43 @@ + + */ +class JournalRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Journal::class); + } + + // /** + // * @return Journal[] Returns an array of Journal objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('j') + // ->andWhere('j.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('j.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Journal + // { + // return $this->createQueryBuilder('j') + // ->andWhere('j.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +} diff --git a/src/Service/NostrClient.php b/src/Service/NostrClient.php index 7e433e0..ca4b825 100644 --- a/src/Service/NostrClient.php +++ b/src/Service/NostrClient.php @@ -39,7 +39,7 @@ class NostrClient $filter = new Filter(); $filter->setKinds([KindsEnum::LONGFORM]); // TODO make filters configurable - $filter->setSince(strtotime('-1 day')); // + $filter->setSince(strtotime('-1 week')); // $requestMessage = new RequestMessage($subscriptionId, [$filter]); // TODO make relays configurable $relays = new RelaySet(); diff --git a/src/Twig/Components/Molecules/Card.php b/src/Twig/Components/Molecules/Card.php index 472f762..41ce453 100644 --- a/src/Twig/Components/Molecules/Card.php +++ b/src/Twig/Components/Molecules/Card.php @@ -8,4 +8,6 @@ use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; final class Card { public string $tag = 'div'; -} \ No newline at end of file + public string $category = ''; + public object $article; +} diff --git a/src/Twig/Components/Molecules/UserFromNpub.php b/src/Twig/Components/Molecules/UserFromNpub.php new file mode 100644 index 0000000..b8981b4 --- /dev/null +++ b/src/Twig/Components/Molecules/UserFromNpub.php @@ -0,0 +1,24 @@ +npub = $npub; + $this->user = $this->entityManager->getRepository(User::class)->findOneBy(['npub' => $npub]); + } +} diff --git a/src/Twig/Components/Organisms/CardList.php b/src/Twig/Components/Organisms/CardList.php index 329245b..8cfd3ab 100644 --- a/src/Twig/Components/Organisms/CardList.php +++ b/src/Twig/Components/Organisms/CardList.php @@ -7,4 +7,5 @@ use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; #[AsTwigComponent] final class CardList { + public array $list; } diff --git a/templates/components/Molecules/Card.html.twig b/templates/components/Molecules/Card.html.twig index 1f30bbf..f0e6e6f 100644 --- a/templates/components/Molecules/Card.html.twig +++ b/templates/components/Molecules/Card.html.twig @@ -5,6 +5,6 @@

{{ article.title }}

diff --git a/templates/components/Molecules/UserFromNpub.html.twig b/templates/components/Molecules/UserFromNpub.html.twig new file mode 100644 index 0000000..edc35ab --- /dev/null +++ b/templates/components/Molecules/UserFromNpub.html.twig @@ -0,0 +1,5 @@ +{% if user %} + {{ user.displayName }} +{% else %} + {{ npub }} +{% endif %} diff --git a/templates/components/Organisms/CardList.html.twig b/templates/components/Organisms/CardList.html.twig index 75ebedd..f9cf667 100644 --- a/templates/components/Organisms/CardList.html.twig +++ b/templates/components/Organisms/CardList.html.twig @@ -1,3 +1,5 @@ - - +
+ {% for item in list %} + + {% endfor %}
diff --git a/templates/components/UserMenu.html.twig b/templates/components/UserMenu.html.twig index 8a937bf..b202d9a 100644 --- a/templates/components/UserMenu.html.twig +++ b/templates/components/UserMenu.html.twig @@ -2,8 +2,8 @@ {% if app.user %}

Hello, {{ app.user.displayName }}

- Log out + Log out {% else %} - + Log in {% endif %} diff --git a/templates/home.html.twig b/templates/home.html.twig index 48ac89f..00d37a6 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -6,6 +6,7 @@ {% block body %} {# content #} + {% endblock %} {% block aside %}