Browse Source

complete feed feature

main
ghost 1 year ago
parent
commit
92ce322ed3
  1. 1
      composer.json
  2. 15
      config/packages/translation.yaml
  3. 6
      config/services.yaml
  4. 21
      public/css/default.css
  5. 47
      src/Controller/RoomController.php
  6. 132
      src/Twig/AppExtension.php
  7. 13
      templates/default/room/index.html.twig
  8. 0
      translations/.gitignore

1
composer.json

@ -13,6 +13,7 @@
"symfony/flex": "^2", "symfony/flex": "^2",
"symfony/framework-bundle": "7.0.*", "symfony/framework-bundle": "7.0.*",
"symfony/runtime": "7.0.*", "symfony/runtime": "7.0.*",
"symfony/translation": "7.0.*",
"symfony/twig-bundle": "7.0.*", "symfony/twig-bundle": "7.0.*",
"symfony/yaml": "7.0.*" "symfony/yaml": "7.0.*"
}, },

15
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)%'

6
config/services.yaml

@ -33,3 +33,9 @@ services:
# add more service definitions when explicit configuration is needed # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones
App\Twig\AppExtension:
arguments:
- '@service_container'
tags:
- { name: twig.extension}

21
public/css/default.css

@ -80,17 +80,34 @@ main > form > input[type="text"]
width: 100%; width: 100%;
} }
main > ul main ul
{
list-style: none;
}
main ul ul
{ {
margin-bottom: 16px; margin-bottom: 16px;
margin-left: 16px; margin-left: 16px;
margin-top: 16px; margin-top: 16px;
} }
main > ul > li main ul > li
{ {
margin: 8px 0; margin: 8px 0;
padding: 8px 6px;
word-wrap: break-word; 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 footer

47
src/Controller/RoomController.php

@ -53,26 +53,53 @@ class RoomController extends AbstractController
$this->getParameter('app.kevacoin.password') $this->getParameter('app.kevacoin.password')
); );
// Get room posts // Get room feed
$posts = []; $feed = [];
foreach ((array) $client->kevaFilter($request->get('namespace')) as $message) foreach ((array) $client->kevaFilter($request->get('namespace')) as $post)
{ {
$posts[] = // 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' =>
[ [
'key' => $message['key'], 'time' => date('c', $transaction['time']),
'value' => $message['value'], 'timestamp' => $transaction['time'],
'height' => $message['height'], 'confirmations' => $transaction['confirmations'],
'vout' => $message['vout'], ],
'txid' => $message['txid'], 'sort' => $transaction['time'] // sort order field
]; ];
} }
}
// Sort posts by newest on top
array_multisort(
array_column(
$feed,
'sort'
),
SORT_DESC,
$feed
);
// Return result // Return result
return $this->render( return $this->render(
'default/room/index.html.twig', 'default/room/index.html.twig',
[ [
'posts' => $posts, 'feed' => $feed,
'request' => $request 'request' => $request
] ]
); );

132
src/Twig/AppExtension.php

@ -0,0 +1,132 @@
<?php
namespace App\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
protected TranslatorInterface $translator;
public function __construct(
ContainerInterface $container,
TranslatorInterface $translator
)
{
$this->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)]];
}
}

13
templates/default/room/index.html.twig

@ -10,11 +10,20 @@
) )
) )
}} }}
{% if posts %} {% if feed %}
<ul> <ul>
{% for post in posts %} {% for post in feed %}
<li> <li>
<a name="{{ post.txid }}"></a>
<strong>
@{{ post.key }}
</strong>
<a href="#{{ post.txid }}" title="{{ 'time:' | trans }} {{ post.transaction.time }} / {{ 'block:' | trans }} {{ post.height }}{# / {{ 'confirmations:' | trans }} {{ post.transaction.confirmations }}#}">
{{ post.transaction.timestamp | format_ago }}
</a>
<p>
{{ post.value }} {{ post.value }}
</p>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>

0
translations/.gitignore vendored

Loading…
Cancel
Save