diff --git a/composer.json b/composer.json index 3eb0681..cd463b1 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "symfony/flex": "^2", "symfony/framework-bundle": "7.0.*", "symfony/runtime": "7.0.*", + "symfony/translation": "7.0.*", "symfony/twig-bundle": "7.0.*", "symfony/yaml": "7.0.*" }, diff --git a/config/packages/translation.yaml b/config/packages/translation.yaml new file mode 100644 index 0000000..888f0ba --- /dev/null +++ b/config/packages/translation.yaml @@ -0,0 +1,15 @@ +framework: + default_locale: en + translator: + default_path: '%kernel.project_dir%/translations' + fallbacks: + - en +# providers: +# crowdin: +# dsn: '%env(CROWDIN_DSN)%' +# loco: +# dsn: '%env(LOCO_DSN)%' +# lokalise: +# dsn: '%env(LOKALISE_DSN)%' +# phrase: +# dsn: '%env(PHRASE_DSN)%' diff --git a/config/services.yaml b/config/services.yaml index 5484d03..8936ea0 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -33,3 +33,9 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones + + App\Twig\AppExtension: + arguments: + - '@service_container' + tags: + - { name: twig.extension} \ No newline at end of file diff --git a/public/css/default.css b/public/css/default.css index 684c1ac..e720ca4 100644 --- a/public/css/default.css +++ b/public/css/default.css @@ -80,17 +80,34 @@ main > form > input[type="text"] width: 100%; } -main > ul +main ul +{ + list-style: none; +} + +main ul ul { margin-bottom: 16px; margin-left: 16px; margin-top: 16px; } -main > ul > li +main ul > li { margin: 8px 0; + padding: 8px 6px; word-wrap: break-word; + border-bottom: 1px #999 solid; +} + +main ul > li:last-child +{ + border-bottom: none; +} + +main ul > li > p +{ + padding: 8px 0; } footer diff --git a/src/Controller/RoomController.php b/src/Controller/RoomController.php index 94e3acc..90eaa63 100644 --- a/src/Controller/RoomController.php +++ b/src/Controller/RoomController.php @@ -53,26 +53,53 @@ class RoomController extends AbstractController $this->getParameter('app.kevacoin.password') ); - // Get room posts - $posts = []; + // Get room feed + $feed = []; - foreach ((array) $client->kevaFilter($request->get('namespace')) as $message) + foreach ((array) $client->kevaFilter($request->get('namespace')) as $post) { - $posts[] = - [ - 'key' => $message['key'], - 'value' => $message['value'], - 'height' => $message['height'], - 'vout' => $message['vout'], - 'txid' => $message['txid'], - ]; + // Skip values with meta keys + if (false !== stripos($post['key'], '_KEVA_')) + { + continue; + } + + // Get more info + if ($transaction = $client->getRawTransaction($post['txid'])) + { + $feed[] = + [ + 'key' => $post['key'], + 'value' => $post['value'], + 'height' => $post['height'], + # 'vout' => $post['vout'], + 'txid' => $post['txid'], + 'transaction' => + [ + 'time' => date('c', $transaction['time']), + 'timestamp' => $transaction['time'], + 'confirmations' => $transaction['confirmations'], + ], + 'sort' => $transaction['time'] // sort order field + ]; + } } + // Sort posts by newest on top + array_multisort( + array_column( + $feed, + 'sort' + ), + SORT_DESC, + $feed + ); + // Return result return $this->render( 'default/room/index.html.twig', [ - 'posts' => $posts, + 'feed' => $feed, 'request' => $request ] ); diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php new file mode 100644 index 0000000..028323c --- /dev/null +++ b/src/Twig/AppExtension.php @@ -0,0 +1,132 @@ +container = $container; + $this->translator = $translator; + } + + public function getFilters() + { + return + [ + new TwigFilter( + 'format_ago', + [ + $this, + 'formatAgo' + ] + ), + new TwigFilter( + 'url_to_markdown', + [ + $this, + 'urlToMarkdown' + ] + ) + ]; + } + + public function formatAgo( + int $time, + ): string + { + $diff = time() - $time; + + if ($diff < 1) + { + return $this->translator->trans('now'); + } + + $values = + [ + 365 * 24 * 60 * 60 => + [ + $this->translator->trans('year ago'), + $this->translator->trans('years ago'), + $this->translator->trans(' years ago') + ], + 30 * 24 * 60 * 60 => + [ + $this->translator->trans('month ago'), + $this->translator->trans('months ago'), + $this->translator->trans(' months ago') + ], + 24 * 60 * 60 => + [ + $this->translator->trans('day ago'), + $this->translator->trans('days ago'), + $this->translator->trans(' days ago') + ], + 60 * 60 => + [ + $this->translator->trans('hour ago'), + $this->translator->trans('hours ago'), + $this->translator->trans(' hours ago') + ], + 60 => + [ + $this->translator->trans('minute ago'), + $this->translator->trans('minutes ago'), + $this->translator->trans(' minutes ago') + ], + 1 => + [ + $this->translator->trans('second ago'), + $this->translator->trans('seconds ago'), + $this->translator->trans(' seconds ago') + ] + ]; + + foreach ($values as $key => $value) + { + $result = $diff / $key; + + if ($result >= 1) + { + $round = round($result); + + return sprintf( + '%s %s', + $round, + $this->plural( + $round, + $value + ) + ); + } + } + } + + public function urlToMarkdown( + string $text + ): string + { + return preg_replace( + '~(https?://(?:www\.)?[^\(\s\)]+)~i', + '[$1]($1)', + $text + ); + } + + private function plural(int $number, array $texts) + { + $cases = [2, 0, 1, 1, 1, 2]; + + return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]]; + } +} \ No newline at end of file diff --git a/templates/default/room/index.html.twig b/templates/default/room/index.html.twig index 739fe41..0b1ab3a 100644 --- a/templates/default/room/index.html.twig +++ b/templates/default/room/index.html.twig @@ -10,11 +10,20 @@ ) ) }} - {% if posts %} + {% if feed %} diff --git a/translations/.gitignore b/translations/.gitignore new file mode 100644 index 0000000..e69de29