Browse Source

add room name twig filter, make optimizations

main
ghost 12 months ago
parent
commit
5af56addfb
  1. 1
      .env
  2. 1
      config/services.yaml
  3. 25
      src/Controller/ModuleController.php
  4. 32
      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
# Connect memcached # Connect memcached
APP_MEMCACHED_HOST=127.0.0.1 APP_MEMCACHED_HOST=127.0.0.1
APP_MEMCACHED_PORT=11211 APP_MEMCACHED_PORT=11211
APP_MEMCACHED_TIMEOUT=3600
# Connect kevacoin # Connect kevacoin

1
config/services.yaml

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

25
src/Controller/ModuleController.php

@ -56,33 +56,10 @@ class ModuleController extends AbstractController
$this->getParameter('app.kevacoin.password') $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( return $this->render(
'default/module/room.html.twig', 'default/module/room.html.twig',
[ [
'list' => array_values( 'list' => (array) explode('|', $this->getParameter('app.kevacoin.room.namespaces')),
$list
),
'form' => 'form' =>
[ [
'namespace' => 'namespace' =>

32
src/Controller/RoomController.php

@ -61,20 +61,6 @@ class RoomController extends AbstractController
$this->getParameter('app.kevacoin.password') $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 // Check for external rooms reading allowed in config
if ( if (
!in_array( !in_array(
@ -178,7 +164,6 @@ class RoomController extends AbstractController
return $this->render( return $this->render(
'default/room/index.rss.twig', 'default/room/index.rss.twig',
[ [
'name' => $name,
'feed' => $feed, 'feed' => $feed,
'request' => $request 'request' => $request
], ],
@ -190,7 +175,6 @@ class RoomController extends AbstractController
return $this->render( return $this->render(
'default/room/index.html.twig', 'default/room/index.html.twig',
[ [
'name' => $name,
'feed' => $feed, 'feed' => $feed,
'request' => $request 'request' => $request
] ]
@ -221,15 +205,13 @@ class RoomController extends AbstractController
$this->getParameter('app.memcached.port') $this->getParameter('app.memcached.port')
); );
$memory = [ $memory = md5(
'app.add.post.remote.ip.delay' => md5(
sprintf( sprintf(
'kevachat.app.add.post.remote.ip.delay:%s.%s', '%s.RoomController::post:add.post.remote.ip.delay:%s',
$this->getParameter('app.name'), __DIR__,
$request->getClientIp(), $request->getClientIp(),
), ),
), );
];
// Connect kevacoin // Connect kevacoin
$client = new \Kevachat\Kevacoin\Client( $client = new \Kevachat\Kevacoin\Client(
@ -322,7 +304,7 @@ class RoomController extends AbstractController
} }
/// Validate remote IP limits /// Validate remote IP limits
if ($delay = (int) $memcached->get($memory['app.add.post.remote.ip.delay'])) if ($delay = (int) $memcached->get($memory))
{ {
// Error // Error
return $this->redirectToRoute( return $this->redirectToRoute(
@ -369,9 +351,9 @@ class RoomController extends AbstractController
{ {
// Register event time // Register event time
$memcached->set( $memcached->set(
$memory['app.add.post.remote.ip.delay'], $memory,
time(), 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 // Redirect back to room

72
src/Twig/AppExtension.php

@ -37,6 +37,13 @@ class AppExtension extends AbstractExtension
$this, $this,
'urlToMarkdown' 'urlToMarkdown'
] ]
),
new TwigFilter(
'keva_namespace_value',
[
$this,
'kevaNamespaceValue'
]
) )
]; ];
} }
@ -129,4 +136,69 @@ class AppExtension extends AbstractExtension
return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]]; 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 @@
<input type="text" name="namespace" value="{{ form.namespace.value }}" placeholder="{{ 'join room by kevacoin namespace...' | trans }}" /> <input type="text" name="namespace" value="{{ form.namespace.value }}" placeholder="{{ 'join room by kevacoin namespace...' | trans }}" />
</form> </form>
{% if list %} {% if list %}
{% for i, room in list %} {% for i, namespace in list %}
{% if i %}&bull;{% endif %} {% if i %}&bull;{% endif %}
<h2> <h2>
{% if room.active %} {% if namespace == form.namespace.value %}
{{ room.name }} {{ namespace | keva_namespace_value }}
{% else %} {% 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 %} {% endif %}
</h2> </h2>
<sup> <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"/> <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> </svg></a>
</sup> </sup>

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

@ -1,5 +1,5 @@
{% extends 'default/layout.html.twig' %} {% 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 %} {% block main_content %}
{{ {{
render( render(
@ -28,7 +28,7 @@
&bull; &bull;
<a href="{{ path('room_namespace', { namespace : request.get('namespace'), txid : post.id }) }}#{{ post.id }}">{{ 'reply' | trans }}</a> <a href="{{ path('room_namespace', { namespace : request.get('namespace'), txid : post.id }) }}#{{ post.id }}">{{ 'reply' | trans }}</a>
{% if post.pending %} {% 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"> <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"/> <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> </svg>

Loading…
Cancel
Save