From 313a6fa052277f5803d0e4873dff5417f994c24b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Tue, 29 Jul 2025 11:45:50 +0200 Subject: [PATCH] Downsizing: remove credits --- config/packages/cache.yaml | 1 - docs/features.md | 12 +++ .../CreditTransactionController.php | 24 ----- src/Credits/Entity/CreditTransaction.php | 100 ------------------ src/Credits/Service/CreditsManager.php | 68 ------------ src/Credits/Util/RedisCreditStore.php | 90 ---------------- src/Twig/Components/GetCreditsComponent.php | 40 ------- src/Twig/Components/SearchComponent.php | 47 +------- .../components/SearchComponent.html.twig | 12 --- translations/messages.en.yaml | 3 - 10 files changed, 13 insertions(+), 384 deletions(-) delete mode 100644 src/Controller/Administration/CreditTransactionController.php delete mode 100644 src/Credits/Entity/CreditTransaction.php delete mode 100644 src/Credits/Service/CreditsManager.php delete mode 100644 src/Credits/Util/RedisCreditStore.php delete mode 100644 src/Twig/Components/GetCreditsComponent.php diff --git a/config/packages/cache.yaml b/config/packages/cache.yaml index e3805ad..86c2e75 100644 --- a/config/packages/cache.yaml +++ b/config/packages/cache.yaml @@ -15,4 +15,3 @@ framework: pools: #my.dedicated.cache: null subscriptions.cache: null - credits.cache: null diff --git a/docs/features.md b/docs/features.md index 58fcebc..6c5d716 100644 --- a/docs/features.md +++ b/docs/features.md @@ -10,6 +10,18 @@ - Article indexing commands - Controllers using Elasticsearch queries +## Credit System (REMOVED) +- **Status**: Completely removed as part of scaling down +- **Previous implementation**: Credit-based search system with Redis storage +- **Components removed**: + - Entire `src/Credits/` directory (CreditsManager, RedisCreditStore, CreditTransaction entity) + - GetCreditsComponent (Twig component for adding credits) + - CreditTransactionController (admin interface) + - Credit accounting in SearchComponent + - Credit balance display in search interface + - Credits cache configuration + - Credit translation keys + ## Core Features to Preserve - Article management (CRUD operations) - Article display and listing diff --git a/src/Controller/Administration/CreditTransactionController.php b/src/Controller/Administration/CreditTransactionController.php deleted file mode 100644 index 64e0f43..0000000 --- a/src/Controller/Administration/CreditTransactionController.php +++ /dev/null @@ -1,24 +0,0 @@ -getRepository(CreditTransaction::class)->findBy([], ['createdAt' => 'DESC']); - - return $this->render('admin/transactions.html.twig', [ - 'transactions' => $transactions, - ]); - } -} diff --git a/src/Credits/Entity/CreditTransaction.php b/src/Credits/Entity/CreditTransaction.php deleted file mode 100644 index d2db15d..0000000 --- a/src/Credits/Entity/CreditTransaction.php +++ /dev/null @@ -1,100 +0,0 @@ -npub = $npub; - $this->amount = $amount; - $this->type = $type; - $this->createdAt = new \DateTime(); - $this->reason = $reason; - } - - public function getId(): int - { - return $this->id; - } - - public function setId(int $id): void - { - $this->id = $id; - } - - public function getNpub(): string - { - return $this->npub; - } - - public function setNpub(string $npub): void - { - $this->npub = $npub; - } - - public function getAmount(): int - { - return $this->amount; - } - - public function setAmount(int $amount): void - { - $this->amount = $amount; - } - - public function getType(): string - { - return $this->type; - } - - public function setType(string $type): void - { - $this->type = $type; - } - - public function getCreatedAt(): \DateTime - { - return $this->createdAt; - } - - public function setCreatedAt(\DateTime $createdAt): void - { - $this->createdAt = $createdAt; - } - - public function getReason(): ?string - { - return $this->reason; - } - - public function setReason(?string $reason): void - { - $this->reason = $reason; - } - -} - diff --git a/src/Credits/Service/CreditsManager.php b/src/Credits/Service/CreditsManager.php deleted file mode 100644 index 439be70..0000000 --- a/src/Credits/Service/CreditsManager.php +++ /dev/null @@ -1,68 +0,0 @@ -redisStore->getBalance($npub); - } - - /** - * @throws InvalidArgumentException - */ - public function resetBalance(string $npub): int - { - return $this->redisStore->resetBalance($npub); - } - - /** - * @throws InvalidArgumentException - */ - public function addCredits(string $npub, int $amount, ?string $reason = null): void - { - $this->redisStore->addCredits($npub, $amount); - - $tx = new CreditTransaction($npub, $amount, 'credit', $reason); - $this->em->persist($tx); - $this->em->flush(); - } - - /** - * @throws InvalidArgumentException - */ - public function canAfford(string $npub, int $cost): bool - { - return $this->getBalance($npub) >= $cost; - } - - /** - * @throws InvalidArgumentException - */ - public function spendCredits(string $npub, int $cost, ?string $reason = null): void - { - if (!$this->canAfford($npub, $cost)) { - throw new \RuntimeException("Insufficient credits for $npub"); - } - - $this->redisStore->spendCredits($npub, $cost); - - $tx = new CreditTransaction($npub, $cost, 'debit', $reason); - $this->em->persist($tx); - $this->em->flush(); - } -} diff --git a/src/Credits/Util/RedisCreditStore.php b/src/Credits/Util/RedisCreditStore.php deleted file mode 100644 index b9e8d83..0000000 --- a/src/Credits/Util/RedisCreditStore.php +++ /dev/null @@ -1,90 +0,0 @@ -creditsCache->delete($this->key($npub)); - - // Fetch all transactions for the given npub - $transactions = $this->em->getRepository(CreditTransaction::class) - ->findBy(['npub' => $npub]); - - // Initialize the balance - $balance = 0; - - // Calculate the final balance based on the transactions - foreach ($transactions as $tx) { - if ($tx->getType() === 'credit') { - $balance += $tx->getAmount(); - } elseif ($tx->getType() === 'debit') { - $balance -= $tx->getAmount(); - } - } - - // Write the calculated balance into the Redis cache - $item = $this->creditsCache->getItem($this->key($npub)); - $item->set($balance); - $this->creditsCache->save($item); - - return $balance; - } - - /** - * @throws InvalidArgumentException - */ - public function getBalance(string $npub): int - { - // Use cache pool to fetch the credit balance - return $this->creditsCache->get($this->key($npub), function () use ($npub) { - return $this->resetBalance($npub); - }); - } - - /** - * @throws InvalidArgumentException - */ - public function addCredits(string $npub, int $amount): void - { - $currentBalance = $this->getBalance($npub); - $item = $this->creditsCache->getItem($this->key($npub)); - $balance = $currentBalance + $amount; - $item->set($balance); - $this->creditsCache->save($item); - } - - /** - * @throws InvalidArgumentException - */ - public function spendCredits(string $npub, int $amount): void - { - $currentBalance = $this->getBalance($npub); - $item = $this->creditsCache->getItem($this->key($npub)); - if ($currentBalance < $amount) { - throw new \RuntimeException('Insufficient credits'); - } - $balance = $currentBalance - $amount; - $item->set($balance); - $this->creditsCache->save($item); - } -} diff --git a/src/Twig/Components/GetCreditsComponent.php b/src/Twig/Components/GetCreditsComponent.php deleted file mode 100644 index 7befebc..0000000 --- a/src/Twig/Components/GetCreditsComponent.php +++ /dev/null @@ -1,40 +0,0 @@ -tokenStorage->getToken()?->getUserIdentifier(); - if ($npub) { - $this->creditsManager->addCredits($npub, 5, 'voucher'); - } - - // Dispatch event to notify parent - $this->emit('creditsAdded'); - } -} - diff --git a/src/Twig/Components/SearchComponent.php b/src/Twig/Components/SearchComponent.php index e72307b..7fc5c5a 100644 --- a/src/Twig/Components/SearchComponent.php +++ b/src/Twig/Components/SearchComponent.php @@ -2,15 +2,12 @@ namespace App\Twig\Components; -use App\Credits\Service\CreditsManager; use App\Repository\ArticleRepository; -use Psr\Cache\InvalidArgumentException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\LiveComponent\Attribute\LiveAction; -use Symfony\UX\LiveComponent\Attribute\LiveListener; use Symfony\UX\LiveComponent\Attribute\LiveProp; use Symfony\UX\LiveComponent\DefaultActionTrait; use Symfony\Contracts\Cache\CacheInterface; @@ -26,9 +23,6 @@ final class SearchComponent public bool $interactive = true; - public int $credits = 0; - public ?string $npub = null; - #[LiveProp] public int $vol = 0; @@ -43,7 +37,6 @@ final class SearchComponent public function __construct( private readonly ArticleRepository $articleRepository, - private readonly CreditsManager $creditsManager, private readonly TokenStorageInterface $tokenStorage, private readonly LoggerInterface $logger, private readonly CacheInterface $cache, @@ -54,19 +47,6 @@ final class SearchComponent public function mount(): void { - $token = $this->tokenStorage->getToken(); - $this->npub = $token?->getUserIdentifier(); - - if ($this->npub !== null) { - try { - $this->credits = $this->creditsManager->getBalance($this->npub); - $this->logger->info($this->credits); - } catch (InvalidArgumentException $e) { - $this->logger->error($e); - $this->credits = $this->creditsManager->resetBalance($this->npub); - } - } - // Restore search results from session if available and no query provided if (empty($this->query)) { $session = $this->requestStack->getSession(); @@ -78,16 +58,10 @@ final class SearchComponent } } - /** - * @throws InvalidArgumentException - */ #[LiveAction] public function search(): void { - $token = $this->tokenStorage->getToken(); - $this->npub = $token?->getUserIdentifier(); - - $this->logger->info("Query: {$this->query}, npub: {$this->npub}"); + $this->logger->info("Query: {$this->query}"); if (empty($this->query)) { $this->results = []; @@ -95,12 +69,6 @@ final class SearchComponent return; } - try { - $this->credits = $this->creditsManager->getBalance($this->npub); - } catch (InvalidArgumentException $e) { - $this->credits = $this->creditsManager->resetBalance($this->npub); - } - // Check if the same query exists in session $session = $this->requestStack->getSession(); if ($session->has(self::SESSION_QUERY_KEY) && @@ -110,15 +78,8 @@ final class SearchComponent return; } - if (!$this->creditsManager->canAfford($this->npub, 1)) { - $this->results = []; - return; - } - try { $this->results = []; - $this->creditsManager->spendCredits($this->npub, 1, 'search'); - $this->credits--; // Use database-based search instead of Elasticsearch $offset = ($this->page - 1) * $this->resultsPerPage; @@ -142,12 +103,6 @@ final class SearchComponent } } - #[LiveListener('creditsAdded')] - public function incrementCreditsCount(): void - { - $this->credits += 5; - } - /** * Save search results to session */ diff --git a/templates/components/SearchComponent.html.twig b/templates/components/SearchComponent.html.twig index 8957f1c..4b47721 100644 --- a/templates/components/SearchComponent.html.twig +++ b/templates/components/SearchComponent.html.twig @@ -10,11 +10,6 @@ /> -
- - {{ 'credit.balance'|trans({'%count%': credits, 'count': credits}) }} - -
@@ -31,11 +26,4 @@ {% elseif this.query is not empty %}

{{ 'text.noResults'|trans }}

{% endif %} - - {% block aside %} - {% if credits == 0 %} - - {% endif %} - {% endblock %} - diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index 8e3d628..3960505 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -11,6 +11,3 @@ heading: editNzine: 'Edit your N-Zine' search: 'Search' index: 'Index' -credit: - balance: '{0} No credits|{1} 1 credit|]1,Inf] %count% credits' -