]> git.mxchange.org Git - friendica.git/commitdiff
Wrap item in INSERT_POST_LOCAL in separate array
authorArt4 <art4@wlabs.de>
Mon, 17 Mar 2025 13:08:20 +0000 (13:08 +0000)
committerArt4 <art4@wlabs.de>
Mon, 17 Mar 2025 13:08:20 +0000 (13:08 +0000)
mod/item.php
src/Core/Hooks/HookEventBridge.php
src/Model/Item.php
src/Model/Profile.php
tests/Unit/Core/Hooks/HookEventBridgeTest.php

index e6b2cb93f4f947a5b5356f9561468f061f9a31a1..e416126a4a6a1f1018ea6660634c89fa0a64aa59 100644 (file)
@@ -281,10 +281,16 @@ function item_process(array $post, array $request, bool $preview, string $return
 
        $eventDispatcher = DI::eventDispatcher();
 
-       $post = $eventDispatcher->dispatch(
-               new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $post)
+       $hook_data = [
+               'item' => $post,
+       ];
+
+       $hook_data = $eventDispatcher->dispatch(
+               new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $hook_data)
        )->getArray();
 
+       $post = $hook_data['item'] ?? $post;
+
        unset($post['edit']);
        unset($post['self']);
        unset($post['api_source']);
index e287cdd76768c5758491ae50205d293ed8f503bb..e5a7dd01ab07059ed8d223a7ea6fd5446b090085 100644 (file)
@@ -112,7 +112,7 @@ final class HookEventBridge
                        ArrayFilterEvent::FEATURE_ENABLED                 => 'onArrayFilterEvent',
                        ArrayFilterEvent::FEATURE_GET                     => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_LOCAL_START         => 'onArrayFilterEvent',
-                       ArrayFilterEvent::INSERT_POST_LOCAL               => 'onArrayFilterEvent',
+                       ArrayFilterEvent::INSERT_POST_LOCAL               => 'onInsertPostLocalEvent',
                        ArrayFilterEvent::INSERT_POST_LOCAL_END           => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE              => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE_END          => 'onArrayFilterEvent',
@@ -183,6 +183,20 @@ final class HookEventBridge
                );
        }
 
+       /**
+        * Map the INSERT_POST_LOCAL event to `post_local` hook
+        */
+       public static function onInsertPostLocalEvent(ArrayFilterEvent $event): void
+       {
+               $data = $event->getArray();
+
+               $item = (array) $data['item'] ?? [];
+
+               $data['item'] = static::callHook($event->getName(), $item);
+
+               $event->setArray($data);
+       }
+
        /**
         * Map the PREPARE_POST_START event to `prepare_body_init` hook
         */
index 3edd6950e7f0828c7e5384db46657b05afa50047..651b812f3dac3c8f25b1e7582f1568bd8d522836 100644 (file)
@@ -845,10 +845,16 @@ class Item
                                $dummy_session = false;
                        }
 
-                       $item = $eventDispatcher->dispatch(
-                               new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $item)
+                       $hook_data = [
+                               'item' => $item,
+                       ];
+
+                       $hook_data = $eventDispatcher->dispatch(
+                               new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $hook_data)
                        )->getArray();
 
+                       $item = $hook_data['item'] ?? $item;
+
                        if ($dummy_session) {
                                unset($_SESSION['authenticated']);
                                unset($_SESSION['uid']);
index 7a900b9bc471691f29c4a2ff75d96d15dec3352d..a647a58c786bb64815c441ecd56e4a868a085b9b 100644 (file)
@@ -12,7 +12,6 @@ use Friendica\AppHelper;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Widget\ContactBlock;
 use Friendica\Core\Cache\Enum\Duration;
-use Friendica\Core\Hook;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
 use Friendica\Core\Search;
index 1a28e09161409b4c6ce66bc77bd53b20cf39fe2e..17f9e1084d6f3a0b9ab4c497f89632d1b876ba70 100644 (file)
@@ -33,7 +33,7 @@ class HookEventBridgeTest extends TestCase
                        ArrayFilterEvent::FEATURE_ENABLED                 => 'onArrayFilterEvent',
                        ArrayFilterEvent::FEATURE_GET                     => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_LOCAL_START         => 'onArrayFilterEvent',
-                       ArrayFilterEvent::INSERT_POST_LOCAL               => 'onArrayFilterEvent',
+                       ArrayFilterEvent::INSERT_POST_LOCAL               => 'onInsertPostLocalEvent',
                        ArrayFilterEvent::INSERT_POST_LOCAL_END           => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE              => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE_END          => 'onArrayFilterEvent',
@@ -193,6 +193,28 @@ class HookEventBridgeTest extends TestCase
                HookEventBridge::onCollectRoutesEvent($event);
        }
 
+       public function testOnInsertPostLocalEventCallsHookWithCorrectValue(): void
+       {
+               $event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, ['item' => ['id' => -1]]);
+
+               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
+               $reflectionProperty->setAccessible(true);
+
+               $reflectionProperty->setValue(null, function (string $name, array $data): array {
+                       $this->assertSame('post_local', $name);
+                       $this->assertSame(['id' => -1], $data);
+
+                       return ['id' => 123];
+               });
+
+               HookEventBridge::onInsertPostLocalEvent($event);
+
+               $this->assertSame(
+                       ['item' => ['id' => 123]],
+                       $event->getArray(),
+               );
+       }
+
        public function testOnPreparePostStartEventCallsHookWithCorrectValue(): void
        {
                $event = new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_START, ['item' => ['id' => -1]]);