Browse Source

add room name twig filter, make optimizations

main
ghost 1 year ago
parent
commit
5af56addfb
  1. 1
      .env
  2. 1
      config/services.yaml
  3. 25
      src/Controller/ModuleController.php
  4. 36
      src/Controller/RoomController.php
  5. 72
      src/Twig/AppExtension.php
  6. 10
      templates/default/module/room.html.twig
  7. 4
      templates/default/room/index.html.twig

1
.env

@ -26,6 +26,7 @@ APP_NAME=KevaChat @@ -26,6 +26,7 @@ APP_NAME=KevaChat
# Connect memcached
APP_MEMCACHED_HOST=127.0.0.1
APP_MEMCACHED_PORT=11211
APP_MEMCACHED_TIMEOUT=3600
# Connect kevacoin

1
config/services.yaml

@ -8,6 +8,7 @@ parameters: @@ -8,6 +8,7 @@ parameters:
app.name: '%env(APP_NAME)%'
app.memcached.host: '%env(APP_MEMCACHED_HOST)%'
app.memcached.port: '%env(APP_MEMCACHED_PORT)%'
app.memcached.timeout: '%env(APP_MEMCACHED_TIMEOUT)%'
app.kevacoin.protocol: '%env(APP_KEVACOIN_PROTOCOL)%'
app.kevacoin.host: '%env(APP_KEVACOIN_HOST)%'
app.kevacoin.port: '%env(APP_KEVACOIN_PORT)%'

25
src/Controller/ModuleController.php

@ -56,33 +56,10 @@ class ModuleController extends AbstractController @@ -56,33 +56,10 @@ class ModuleController extends AbstractController
$this->getParameter('app.kevacoin.password')
);
$list = [];
// Get configured rooms list
foreach ((array) explode('|', $this->getParameter('app.kevacoin.room.namespaces')) as $namespace)
{
$list[$namespace] = [
'name' => $namespace,
'namespace' => $namespace,
'active' => $namespace === $request->get('namespace')
];
}
// Find related room names
foreach ((array) $client->kevaListNamespaces() as $value)
{
if (isset($list[$value['namespaceId']]))
{
$list[$value['namespaceId']]['name'] = $value['displayName'];
}
}
return $this->render(
'default/module/room.html.twig',
[
'list' => array_values(
$list
),
'list' => (array) explode('|', $this->getParameter('app.kevacoin.room.namespaces')),
'form' =>
[
'namespace' =>

36
src/Controller/RoomController.php

@ -61,20 +61,6 @@ class RoomController extends AbstractController @@ -61,20 +61,6 @@ class RoomController extends AbstractController
$this->getParameter('app.kevacoin.password')
);
// Set title
$name = $request->get('namespace');
foreach ((array) $client->kevaListNamespaces() as $namespace)
{
// Get current room namespace (could be third-party)
if ($namespace['namespaceId'] == $request->get('namespace'))
{
$name = $namespace['displayName'];
break;
}
}
// Check for external rooms reading allowed in config
if (
!in_array(
@ -178,7 +164,6 @@ class RoomController extends AbstractController @@ -178,7 +164,6 @@ class RoomController extends AbstractController
return $this->render(
'default/room/index.rss.twig',
[
'name' => $name,
'feed' => $feed,
'request' => $request
],
@ -190,7 +175,6 @@ class RoomController extends AbstractController @@ -190,7 +175,6 @@ class RoomController extends AbstractController
return $this->render(
'default/room/index.html.twig',
[
'name' => $name,
'feed' => $feed,
'request' => $request
]
@ -221,15 +205,13 @@ class RoomController extends AbstractController @@ -221,15 +205,13 @@ class RoomController extends AbstractController
$this->getParameter('app.memcached.port')
);
$memory = [
'app.add.post.remote.ip.delay' => md5(
sprintf(
'kevachat.app.add.post.remote.ip.delay:%s.%s',
$this->getParameter('app.name'),
$request->getClientIp(),
),
$memory = md5(
sprintf(
'%s.RoomController::post:add.post.remote.ip.delay:%s',
__DIR__,
$request->getClientIp(),
),
];
);
// Connect kevacoin
$client = new \Kevachat\Kevacoin\Client(
@ -322,7 +304,7 @@ class RoomController extends AbstractController @@ -322,7 +304,7 @@ class RoomController extends AbstractController
}
/// Validate remote IP limits
if ($delay = (int) $memcached->get($memory['app.add.post.remote.ip.delay']))
if ($delay = (int) $memcached->get($memory))
{
// Error
return $this->redirectToRoute(
@ -369,9 +351,9 @@ class RoomController extends AbstractController @@ -369,9 +351,9 @@ class RoomController extends AbstractController
{
// Register event time
$memcached->set(
$memory['app.add.post.remote.ip.delay'],
$memory,
time(),
(int) $this->getParameter('app.add.post.remote.ip.delay')
(int) $this->getParameter('app.add.post.remote.ip.delay') // auto remove on cache expire
);
// Redirect back to room

72
src/Twig/AppExtension.php

@ -37,6 +37,13 @@ class AppExtension extends AbstractExtension @@ -37,6 +37,13 @@ class AppExtension extends AbstractExtension
$this,
'urlToMarkdown'
]
),
new TwigFilter(
'keva_namespace_value',
[
$this,
'kevaNamespaceValue'
]
)
];
}
@ -129,4 +136,69 @@ class AppExtension extends AbstractExtension @@ -129,4 +136,69 @@ class AppExtension extends AbstractExtension
return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]];
}
// @TODO
// this is fix of keva magic with Kevachat\Kevacoin\Client::kevaListNamespaces results (on pending transactions exist in the namespaces)
// let's extract it from latest_KEVA_NS_ value and temporarily store results in memory cache
public function kevaNamespaceValue(
string $namespace
): string
{
// Connect kevacoin
$client = new \Kevachat\Kevacoin\Client(
$this->container->getParameter('app.kevacoin.protocol'),
$this->container->getParameter('app.kevacoin.host'),
$this->container->getParameter('app.kevacoin.port'),
$this->container->getParameter('app.kevacoin.username'),
$this->container->getParameter('app.kevacoin.password')
);
// Connect memcached
$memcached = new \Memcached();
$memcached->addServer(
$this->container->getParameter('app.memcached.host'),
$this->container->getParameter('app.memcached.port')
);
// Generate unique cache key
$key = md5(
sprintf(
'%s.AppExtension::kevaNamespaceValue:%s',
__DIR__,
$namespace
)
);
// Return cached value if exists
if ($value = $memcached->get($key))
{
return $value;
}
// Find related room names
$_keva_ns = [];
foreach ((array) $client->kevaFilter($namespace) as $data)
{
if ($data['key'] == '_KEVA_NS_')
{
$_keva_ns[$data['height']] = $data['value'];
}
}
// Get last by it block height
if (!empty($_keva_ns))
{
$value = reset(
$_keva_ns
);
if ($memcached->set($key, $value, $this->container->getParameter('app.memcached.timeout')))
{
return (string) $value;
}
}
// Return original hash
return $namespace;
}
}

10
templates/default/module/room.html.twig

@ -2,17 +2,17 @@ @@ -2,17 +2,17 @@
<input type="text" name="namespace" value="{{ form.namespace.value }}" placeholder="{{ 'join room by kevacoin namespace...' | trans }}" />
</form>
{% if list %}
{% for i, room in list %}
{% for i, namespace in list %}
{% if i %}&bull;{% endif %}
<h2>
{% if room.active %}
{{ room.name }}
{% if namespace == form.namespace.value %}
{{ namespace | keva_namespace_value }}
{% else %}
<a href="{{ path('room_namespace', { namespace : room.namespace }) }}">{{ room.name }}</a>
<a href="{{ path('room_namespace', { namespace : namespace }) }}">{{ namespace | keva_namespace_value }}</a>
{% endif %}
</h2>
<sup>
<a href="{{ path('room_namespace', { namespace : room.namespace, feed : 'rss' }) }}" title="{{ 'RSS' | trans }}" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 16 16">
<a href="{{ path('room_namespace', { namespace : namespace, feed : 'rss' }) }}" title="{{ 'RSS' | trans }}" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 16 16">
<path d="M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zm1.5 2.5c5.523 0 10 4.477 10 10a1 1 0 1 1-2 0 8 8 0 0 0-8-8 1 1 0 0 1 0-2m0 4a6 6 0 0 1 6 6 1 1 0 1 1-2 0 4 4 0 0 0-4-4 1 1 0 0 1 0-2m.5 7a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3"/>
</svg></a>
</sup>

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
{% extends 'default/layout.html.twig' %}
{% block head_title_content %}{{ name }} - {{ 'KevaChat' | trans }}{% endblock %}
{% block head_title_content %}{{ request.get('namespace') | keva_namespace_value }} - {{ 'KevaChat' | trans }}{% endblock %}
{% block main_content %}
{{
render(
@ -28,7 +28,7 @@ @@ -28,7 +28,7 @@
&bull;
<a href="{{ path('room_namespace', { namespace : request.get('namespace'), txid : post.id }) }}#{{ post.id }}">{{ 'reply' | trans }}</a>
{% if post.pending %}
<span title="{{ 'pending for new block...' | trans }}">
<span title="{{ 'pending in pool' | trans }}">
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 16 16">
<path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0M8 3.5a.5.5 0 0 0-1 0V9a.5.5 0 0 0 .252.434l3.5 2a.5.5 0 0 0 .496-.868L8 8.71z"/>
</svg>

Loading…
Cancel
Save