Browse Source

Downsizing: Clean up db migration and provide no-magazine fallback

imwald
Nuša Pukšič 9 months ago
parent
commit
8d75cdac28
  1. 2
      .env.dist
  2. 71
      migrations/Version20250729102713.php
  3. 37
      migrations/Version20250729122617.php
  4. 33
      migrations/Version20250729122903.php
  5. 8
      src/Controller/DefaultController.php
  6. 4
      src/Entity/Article.php
  7. 2
      src/Entity/User.php
  8. 36
      src/Service/EncryptionService.php
  9. 2
      src/Service/NostrClient.php
  10. 6
      src/Twig/Components/Header.php

2
.env.dist

@ -37,7 +37,7 @@ DATABASE_URL="mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@database:3306/${MYSQL_DATA @@ -37,7 +37,7 @@ DATABASE_URL="mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@database:3306/${MYSQL_DATA
###> symfony/mercure-bundle ###
# See https://symfony.com/doc/current/mercure.html#configuration
# The URL of the Mercure hub, used by the app to publish updates (can be a local URL)
MERCURE_URL=https://newsroom-php/.well-known/mercure
MERCURE_URL=https://unfold-php/.well-known/mercure
# The public URL of the Mercure hub, used by the browser to connect
MERCURE_PUBLIC_URL="https://${SERVER_NAME}/.well-known/mercure"
# The secret used to sign the JWTs

71
migrations/Version20250729102713.php

@ -1,71 +0,0 @@ @@ -1,71 +0,0 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250729102713 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql(<<<'SQL'
CREATE TABLE app_user (id INT AUTO_INCREMENT NOT NULL, npub VARCHAR(255) NOT NULL, roles JSON DEFAULT NULL, UNIQUE INDEX UNIQ_88BDF3E95FB8BABB (npub), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4
SQL);
$this->addSql(<<<'SQL'
CREATE TABLE article (id INT AUTO_INCREMENT NOT NULL, raw JSON DEFAULT NULL, event_id VARCHAR(225) DEFAULT NULL, slug LONGTEXT DEFAULT NULL, content LONGTEXT DEFAULT NULL, kind INT DEFAULT NULL, title LONGTEXT DEFAULT NULL, summary LONGTEXT DEFAULT NULL, pubkey VARCHAR(255) NOT NULL, created_at DATETIME DEFAULT NULL, sig VARCHAR(255) NOT NULL, image LONGTEXT DEFAULT NULL, published_at DATETIME DEFAULT NULL, topics JSON DEFAULT NULL, event_status INT DEFAULT NULL, current_places JSON DEFAULT NULL, rating_negative INT DEFAULT NULL, rating_positive INT DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4
SQL);
$this->addSql(<<<'SQL'
CREATE TABLE event (id VARCHAR(225) NOT NULL, event_id VARCHAR(225) DEFAULT NULL, kind INT NOT NULL, pubkey VARCHAR(255) NOT NULL, content LONGTEXT NOT NULL, created_at BIGINT NOT NULL, tags JSON NOT NULL, sig VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4
SQL);
$this->addSql(<<<'SQL'
CREATE TABLE nzine (id INT AUTO_INCREMENT NOT NULL, npub VARCHAR(255) NOT NULL, main_categories JSON NOT NULL, lists JSON DEFAULT NULL, editor VARCHAR(255) DEFAULT NULL, slug LONGTEXT DEFAULT NULL, state VARCHAR(255) NOT NULL, nzine_bot_id INT DEFAULT NULL, UNIQUE INDEX UNIQ_65025D9871FD5427 (nzine_bot_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4
SQL);
$this->addSql(<<<'SQL'
CREATE TABLE nzine_bot (id INT AUTO_INCREMENT NOT NULL, encrypted_nsec VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4
SQL);
$this->addSql(<<<'SQL'
CREATE TABLE sessions (sess_id VARBINARY(128) NOT NULL, sess_data LONGBLOB NOT NULL, sess_lifetime INT UNSIGNED NOT NULL, sess_time INT UNSIGNED NOT NULL, INDEX sess_lifetime_idx (sess_lifetime), PRIMARY KEY(sess_id)) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
SQL);
$this->addSql(<<<'SQL'
ALTER TABLE nzine ADD CONSTRAINT FK_65025D9871FD5427 FOREIGN KEY (nzine_bot_id) REFERENCES nzine_bot (id)
SQL);
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql(<<<'SQL'
ALTER TABLE nzine DROP FOREIGN KEY FK_65025D9871FD5427
SQL);
$this->addSql(<<<'SQL'
DROP TABLE app_user
SQL);
$this->addSql(<<<'SQL'
DROP TABLE article
SQL);
$this->addSql(<<<'SQL'
DROP TABLE event
SQL);
$this->addSql(<<<'SQL'
DROP TABLE nzine
SQL);
$this->addSql(<<<'SQL'
DROP TABLE nzine_bot
SQL);
$this->addSql(<<<'SQL'
DROP TABLE sessions
SQL);
}
}

37
migrations/Version20250729122617.php

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250729122617 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE app_user (id INT NOT NULL, npub VARCHAR(255) NOT NULL, roles JSON DEFAULT NULL, UNIQUE INDEX UNIQ_88BDF3E95FB8BABB (npub), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE article (id INT NOT NULL, raw JSON DEFAULT NULL, event_id VARCHAR(225) DEFAULT NULL, slug LONGTEXT DEFAULT NULL, content LONGTEXT DEFAULT NULL, kind INT DEFAULT NULL, title LONGTEXT DEFAULT NULL, summary LONGTEXT DEFAULT NULL, pubkey VARCHAR(255) NOT NULL, created_at DATETIME DEFAULT NULL, sig VARCHAR(255) NOT NULL, image LONGTEXT DEFAULT NULL, published_at DATETIME DEFAULT NULL, topics JSON DEFAULT NULL, event_status INT DEFAULT NULL, current_places JSON DEFAULT NULL, rating_negative INT DEFAULT NULL, rating_positive INT DEFAULT NULL, PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE event (id VARCHAR(225) NOT NULL, event_id VARCHAR(225) DEFAULT NULL, kind INT NOT NULL, pubkey VARCHAR(255) NOT NULL, content LONGTEXT NOT NULL, created_at BIGINT NOT NULL, tags JSON NOT NULL, sig VARCHAR(255) NOT NULL, PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE sessions (sess_id VARBINARY(128) NOT NULL, sess_data LONGBLOB NOT NULL, sess_lifetime INT UNSIGNED NOT NULL, sess_time INT UNSIGNED NOT NULL, INDEX sess_lifetime_idx (sess_lifetime), PRIMARY KEY (sess_id)) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE app_user');
$this->addSql('DROP TABLE article');
$this->addSql('DROP TABLE event');
$this->addSql('DROP TABLE sessions');
}
}

33
migrations/Version20250729122903.php

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250729122903 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE app_user CHANGE id id INT AUTO_INCREMENT NOT NULL');
$this->addSql('ALTER TABLE article CHANGE id id INT AUTO_INCREMENT NOT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE app_user CHANGE id id INT NOT NULL');
$this->addSql('ALTER TABLE article CHANGE id id INT NOT NULL');
}
}

8
src/Controller/DefaultController.php

@ -31,6 +31,14 @@ class DefaultController extends AbstractController @@ -31,6 +31,14 @@ class DefaultController extends AbstractController
$mag = $this->redisCache->get('magazine-newsroom-magazine-by-newsroom', function (){
return null;
});
// Handle case when magazine is not found
if ($mag === null) {
return $this->render('home.html.twig', [
'indices' => []
]);
}
$tags = $mag->getTags();
$cats = array_filter($tags, function($tag) {

4
src/Entity/Article.php

@ -18,8 +18,8 @@ use Doctrine\ORM\Mapping as ORM; @@ -18,8 +18,8 @@ use Doctrine\ORM\Mapping as ORM;
class Article
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(length: 225)]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: Types::JSON, nullable: true)]

2
src/Entity/User.php

@ -16,7 +16,7 @@ use Symfony\Component\Security\Core\User\UserInterface; @@ -16,7 +16,7 @@ use Symfony\Component\Security\Core\User\UserInterface;
class User implements UserInterface, EquatableInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\Column]
private ?int $id = null;

36
src/Service/EncryptionService.php

@ -1,36 +0,0 @@ @@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class EncryptionService
{
private string $encryptionKey;
public function __construct(ParameterBagInterface $bag)
{
// Retrieve the symmetric encryption key securely from Symfony Secrets
$this->encryptionKey = $bag->get('encryption_key');
}
public function encrypt(string $data): string
{
return openssl_encrypt($data, 'aes-256-cbc', $this->encryptionKey, 0, $this->getIv());
}
public function decrypt(string $encryptedData): string
{
return openssl_decrypt($encryptedData, 'aes-256-cbc', $this->encryptionKey, 0, $this->getIv());
}
private function getIv(): string
{
return substr(hash('sha256', $this->encryptionKey), 0, 16); // AES-256 requires a 16-byte IV
}
}

2
src/Service/NostrClient.php

@ -424,7 +424,7 @@ class NostrClient @@ -424,7 +424,7 @@ class NostrClient
// Create request using the helper method
$request = $this->createNostrRequest(
kinds: [KindsEnum::COMMENTS->value, KindsEnum::TEXT_NOTE->value],
kinds: [KindsEnum::COMMENTS->value],
filters: ['tag' => ['#A', [$coordinate]]],
relaySet: $relaySet
);

6
src/Twig/Components/Header.php

@ -22,6 +22,12 @@ class Header @@ -22,6 +22,12 @@ class Header
return null;
});
// Handle case when magazine is not found
if ($mag === null) {
$this->cats = [];
return;
}
$tags = $mag->getTags();
$this->cats = array_filter($tags, function($tag) {

Loading…
Cancel
Save