Browse Source

complete feed feature

main
ghost 12 months 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. 51
      src/Controller/RoomController.php
  6. 132
      src/Twig/AppExtension.php
  7. 15
      templates/default/room/index.html.twig
  8. 0
      translations/.gitignore

1
composer.json

@ -13,6 +13,7 @@ @@ -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.*"
},

15
config/packages/translation.yaml

@ -0,0 +1,15 @@ @@ -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: @@ -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}

21
public/css/default.css

@ -80,17 +80,34 @@ main > form > input[type="text"] @@ -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

51
src/Controller/RoomController.php

@ -53,26 +53,53 @@ class RoomController extends AbstractController @@ -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
]
);

132
src/Twig/AppExtension.php

@ -0,0 +1,132 @@ @@ -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)]];
}
}

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

@ -10,11 +10,20 @@ @@ -10,11 +10,20 @@
)
)
}}
{% if posts %}
{% if feed %}
<ul>
{% for post in posts %}
{% for post in feed %}
<li>
{{ post.value }}
<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 }}
</p>
</li>
{% endfor %}
</ul>

0
translations/.gitignore vendored

Loading…
Cancel
Save