From d09f940f4b05327bd48acd82e0d608bf14931c3b Mon Sep 17 00:00:00 2001 From: ghost Date: Thu, 25 Jan 2024 22:27:45 +0200 Subject: [PATCH] replace mention txid with it original message --- src/controller/room.php | 55 +++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/src/controller/room.php b/src/controller/room.php index bd9057f..a95b4de 100644 --- a/src/controller/room.php +++ b/src/controller/room.php @@ -48,7 +48,7 @@ class Room foreach ((array) $this->_kevacoin->kevaFilter($namespace['namespaceId']) as $record) { // Is protocol compatible post - if ($this->post($namespace['namespaceId'], $record['key'], 'txid')) + if ($this->post($namespace['namespaceId'], $record['key'], [], 'txid')) { $total++; } @@ -136,33 +136,35 @@ class Room public function posts(string $namespace): ?string { - // Get subject - $subject = null; - - foreach ((array) $this->_kevacoin->kevaListNamespaces() as $record) + // Get namespace records + if (!$records = (array) $this->_kevacoin->kevaFilter($namespace)) { - if ($record['namespaceId'] == $namespace) - { - $subject = $record['displayName']; - } + return null; } // Get posts $posts = []; - foreach ((array) $this->_kevacoin->kevaFilter($namespace) as $record) + foreach ($records as $record) { - if (empty($record['key'])) + if ($post = $this->post($namespace, $record['key'], $records)) { - continue; + $posts[] = $post; } + } - if ($post = $this->post($namespace, $record['key'])) + // Get subject + $subject = null; + + foreach ((array) $this->_kevacoin->kevaListNamespaces() as $record) + { + if ($record['namespaceId'] == $namespace) { - $posts[] = $post; + $subject = $record['displayName']; } } + // Get template return str_replace( [ '{logo}', @@ -185,7 +187,7 @@ class Room ); } - public function post(string $namespace, string $key, ?string $field = null): ?string + public function post(string $namespace, string $key, array $posts = [], ?string $field = null): ?string { // Check record exists if (!$record = (array) $this->_kevacoin->kevaGet($namespace, $key)) @@ -239,9 +241,30 @@ class Room $quote = null; if (preg_match('/^@([A-z0-9]{64})/', $record['value'], $mention)) { + // Message starts with mention if (!empty($mention[1])) { - $quote = '@' . $mention[1]; // @TODO replace to post message by txid + // Use original mention as quote by default + $quote = $mention[1]; + + // Try to replace with post message by txid + foreach ($posts as $post) + { + if ($post['txid'] == $quote) + { + // Strip folding + $quote = + trim( + preg_replace( + '/^@([A-z0-9]{64})/', + null, + $post['value'] + ) + ); + + break; + } + } // Remove mention from message $record['value'] = preg_replace('/^@([A-z0-9]{64})/', null, $record['value']);