From a7f54c22f3ff3c8903159fe9f27b3f5cb392fc66 Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 26 Jan 2024 20:09:48 +0200 Subject: [PATCH] implement success sending page --- src/controller/room.php | 57 +++++++++++++++++++++++++++++++---------- src/server.php | 40 ++++++++++++----------------- src/view/sent.gemini | 13 ++++++++++ 3 files changed, 72 insertions(+), 38 deletions(-) create mode 100644 src/view/sent.gemini diff --git a/src/controller/room.php b/src/controller/room.php index 5f654ac..049740b 100644 --- a/src/controller/room.php +++ b/src/controller/room.php @@ -207,24 +207,24 @@ class Room ); } - public function post(string $namespace, ?string $txid, int $session, string $message): bool + public function post(string $namespace, ?string $txid, int $session, string $message): ?string { // Validate funds available yet if (1 > $this->_kevacoin->getBalance()) { - return false; + return null; } // Validate session exists if (!$this->_memory->get($session)) { - return false; + return null; } // Validate value format allowed in settings if (!preg_match((string) $this->_config->kevachat->post->value->regex, $message)) { - return false; + return null; } // Prepare message @@ -245,29 +245,58 @@ class Room // Validate final message length if (mb_strlen($message) < 1 || mb_strlen($message) > 3072) { - return false; + return null; } // Send message - if (!$this->_kevacoin->kevaPut( + if + ( + $txid = $this->_kevacoin->kevaPut( $namespace, sprintf( '%s@anon', time() ), $message - )) + ) + ) { - return false; + // Cleanup memory + $this->_memory->delete( + $session + ); + + // Success + return $txid; } - // Cleanup memory - $this->_memory->delete( - $session - ); + return null; + } - // Success - return true; + public function sent(string $namespace, string $txid) + { + return str_replace( + [ + '{logo}', + '{txid}', + '{room}' + ], + [ + file_get_contents( + __DIR__ . '/../../logo.ascii' + ), + $txid, + $this->_link( + sprintf( + '/room/%s', + $namespace + ) + ) + ], + file_get_contents( + __DIR__ . '/../view/sent.gemini' + ) + ); } private function _post(string $namespace, string $key, array $posts = [], ?string $field = null, ?int &$time = 0): ?string diff --git a/src/server.php b/src/server.php index 03442b9..99e6a3d 100644 --- a/src/server.php +++ b/src/server.php @@ -197,20 +197,16 @@ foreach ((array) scandir(__DIR__ . '/../host') as $host) ); // Success, redirect to this room page - if ($room->post($matches[1], null, $matches[2], $request->getQuery())) + if ($txid = $room->post($matches[1], null, $matches[2], $request->getQuery())) { - $response->setCode( - 30 - ); - - $response->setMeta( - sprintf( - '/room/%s', - $matches[1] - ) - ); + if ($result = $room->sent($matches[1], $txid)) + { + $response->setContent( + $result + ); - return $response; + return $response; + } } } } @@ -246,20 +242,16 @@ foreach ((array) scandir(__DIR__ . '/../host') as $host) ); // Success, redirect to this room page - if ($room->post($matches[1], $matches[2], $matches[3], $request->getQuery())) + if ($txid = $room->post($matches[1], $matches[2], $matches[3], $request->getQuery())) { - $response->setCode( - 30 - ); - - $response->setMeta( - sprintf( - '/room/%s', - $matches[1] - ) - ); + if ($result = $room->sent($matches[1], $txid)) + { + $response->setContent( + $result + ); - return $response; + return $response; + } } } } diff --git a/src/view/sent.gemini b/src/view/sent.gemini new file mode 100644 index 0000000..9ac52e3 --- /dev/null +++ b/src/view/sent.gemini @@ -0,0 +1,13 @@ +```{logo}``` + +# Success! + +Your publication has been successfully sent to DHT pool: + +``` +{txid} +``` + +Please, wait few minutes for new block to update + +=> {room} Continue \ No newline at end of file