From a3c3f0bad04d0aaf5f1d3008514ebbc84a3682fc Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 5 May 2024 00:17:21 +0300 Subject: [PATCH] init base routing --- src/Controller/Server/Nex.php | 146 ++++++++++++++++++++++++++++++++-- src/Model/Database.php | 71 +++++++++++++++++ 2 files changed, 210 insertions(+), 7 deletions(-) diff --git a/src/Controller/Server/Nex.php b/src/Controller/Server/Nex.php index 4d8c288..176af38 100644 --- a/src/Controller/Server/Nex.php +++ b/src/Controller/Server/Nex.php @@ -70,15 +70,142 @@ class Nex implements MessageComponentInterface \Ratchet\ConnectionInterface $connection, $request ) { - // Filter request - $request = trim( - (string) $request + // Format request + $request = '/' . ltrim( + trim($request), '/' ); - // Send response - $connection->send( - 'test' - ); + // Route request + switch (true) + { + // Item + case (bool) preg_match('/^\/(?\d+)($|\.gmi)$/i', $request, $attribute): + + $lines = []; + + // Get channel item info + if ($channelItem = $this->_database->getChannelItem($attribute['id'])) + { + if ($channelItem->title) + { + $lines[] = sprintf( + '# %s', + \Yggverse\Pulsar\Model\Filter::title( + $channelItem->title + ) + ); + } + + if ($channelItem->pubTime) + { + $lines[] = date( + 'c', + $channelItem->pubTime + ) . PHP_EOL; + } + + if ($channelItem->description) + { + $lines[] = \Yggverse\Pulsar\Model\Filter::description( + $channelItem->description + ) . PHP_EOL; + } + + if ($channelItem->content) + { + $lines[] = \Yggverse\Pulsar\Model\Filter::description( + $channelItem->content + ) . PHP_EOL; + } + + if ($channelItem->link) + { + $lines[] = sprintf( + '=> %s', + $channelItem->link + ) . PHP_EOL; + } + } + + // Build response + $response = implode( + PHP_EOL, + $lines + ); + + break; + + // Chanel + case (bool) preg_match('/^\/(?\d+)\/($|index\.gmi)$/i', $request, $attribute): + + $lines = []; + + // Get channel info + if ($channel = $this->_database->getChannel($attribute['id'])) + { + if ($channel->title) + { + $lines[] = sprintf( + '# %s', + \Yggverse\Pulsar\Model\Filter::title( + $channel->title + ) + ); + } + + if ($channel->description) + { + $lines[] = $channel->description . PHP_EOL; + } + } + + // Get channel items + foreach ($this->_database->getChannelItems(0, 20) as $channelItem) + { + $lines[] = sprintf( + '=> /%d.gmi %s %s', + $channelItem->id, + $channelItem->pubTime ? + date( + 'Y-m-d', + $channelItem->pubTime + ) : '', + \Yggverse\Pulsar\Model\Filter::title( + $channelItem->title + ) + ); + } + + // Build response + $response = implode( + PHP_EOL, + $lines + ); + + break; + + // Main + // Not found + default: + + $lines = []; + + // Get channels + foreach ($this->_database->getChannels() as $channel) + { + $lines[] = sprintf( + '=> /%d/index.gmi %s', + $channel->id, + $channel->title + ); + } + + // Build response + $response = implode( + PHP_EOL, + $lines + ); + } // Debug message event on enabled if ($this->_config->event->message->debug->enabled) @@ -103,6 +230,11 @@ class Nex implements MessageComponentInterface ); } + // Send response + $connection->send( + $response + ); + // Disconnect $connection->close(); } diff --git a/src/Model/Database.php b/src/Model/Database.php index 208fb79..2ae7b1b 100644 --- a/src/Model/Database.php +++ b/src/Model/Database.php @@ -70,6 +70,38 @@ class Database '); } + public function getChannel( + int $id + ): ?object + { + $query = $this->_database->prepare( + 'SELECT * FROM `channel` WHERE `id` = ?' + ); + + $query->execute([$id]); + + if ($result = $query->fetch()) + { + return $result; + } + + return null; + } + + public function getChannels(): ?array + { + $query = $this->_database->query( + 'SELECT * FROM `channel`' + ); + + if ($result = $query->fetchAll()) + { + return $result; + } + + return null; + } + public function getChannelIdBySource( string $source ): ?int @@ -123,6 +155,45 @@ class Database return null; } + public function getChannelItem( + int $id + ): ?object + { + $query = $this->_database->prepare( + 'SELECT * FROM `channelItem` WHERE `id` = ? LIMIT 1' + ); + + $query->execute([$id]); + + if ($result = $query->fetch()) + { + return $result; + } + + return null; + } + + public function getChannelItems( + int $start = 0, + int $limit = 20 + ): ?array + { + $query = $this->_database->query( + sprintf( + 'SELECT * FROM `channelItem` ORDER BY `time` DESC LIMIT %d,%d', + $start, + $limit + ) + ); + + if ($result = $query->fetchAll()) + { + return $result; + } + + return null; + } + public function isChannelItemExist( int $channelId, string $guid