]> git.mxchange.org Git - friendica.git/commitdiff
Wrap item in INSERT_POST_LOCAL_END into separate array
authorArt4 <art4@wlabs.de>
Tue, 18 Mar 2025 14:21:31 +0000 (14:21 +0000)
committerArt4 <art4@wlabs.de>
Tue, 18 Mar 2025 14:21:31 +0000 (14:21 +0000)
src/Content/Item.php
src/Core/Hooks/HookEventBridge.php
src/Module/Post/Tag/Add.php
tests/Unit/Core/Hooks/HookEventBridgeTest.php

index af26ebd8ed7eb3cb9d7e12fd50e339bb2b7e9026..901a2eac3dd35befb99356ea5d91f46b3f3be03b 100644 (file)
@@ -1011,10 +1011,16 @@ class Item
                        Tag::createImplicitMentions($post['uri-id'], $post['thr-parent-id']);
                }
 
-               $post = $this->eventDispatcher->dispatch(
-                       new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $post)
+               $hook_data = [
+                       'item' => $post,
+               ];
+
+               $hook_data = $this->eventDispatcher->dispatch(
+                       new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $hook_data)
                )->getArray();
 
+               $post = $hook_data['item'] ?? $post;
+
                $author = DBA::selectFirst('contact', ['thumb'], ['uid' => $post['uid'], 'self' => true]);
 
                foreach ($recipients as $recipient) {
index e5a7dd01ab07059ed8d223a7ea6fd5446b090085..82a11f1f55b02ad14b3766388041375a0a57f75d 100644 (file)
@@ -113,7 +113,7 @@ final class HookEventBridge
                        ArrayFilterEvent::FEATURE_GET                     => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_LOCAL_START         => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_LOCAL               => 'onInsertPostLocalEvent',
-                       ArrayFilterEvent::INSERT_POST_LOCAL_END           => 'onArrayFilterEvent',
+                       ArrayFilterEvent::INSERT_POST_LOCAL_END           => 'onInsertPostLocalEndEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE              => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE_END          => 'onArrayFilterEvent',
                        ArrayFilterEvent::PREPARE_POST_START              => 'onPreparePostStartEvent',
@@ -197,6 +197,20 @@ final class HookEventBridge
                $event->setArray($data);
        }
 
+       /**
+        * Map the INSERT_POST_LOCAL_END event to `post_local_end` hook
+        */
+       public static function onInsertPostLocalEndEvent(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 e03bf215a3f72d1b25eed920894f682d44296120..a145c36e90b867d52c118969587b41237e36a487 100644 (file)
@@ -153,10 +153,16 @@ EOT;
 
                $post['id'] = $post_id;
 
-               $post = $this->eventDispatcher->dispatch(
-                       new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $post)
+               $hook_data = [
+                       'item' => $post,
+               ];
+
+               $hook_data = $this->eventDispatcher->dispatch(
+                       new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $hook_data)
                )->getArray();
 
+               $post = $hook_data['item'] ?? $post;
+
                $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]);
 
                Worker::add(Worker::PRIORITY_HIGH, 'Notifier', Delivery::POST, $post['uri-id'], $post['uid']);
index 17f9e1084d6f3a0b9ab4c497f89632d1b876ba70..e6ec58a27cf10bf8a01de1a843ed98afcedfa4e8 100644 (file)
@@ -34,7 +34,7 @@ class HookEventBridgeTest extends TestCase
                        ArrayFilterEvent::FEATURE_GET                     => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_LOCAL_START         => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_LOCAL               => 'onInsertPostLocalEvent',
-                       ArrayFilterEvent::INSERT_POST_LOCAL_END           => 'onArrayFilterEvent',
+                       ArrayFilterEvent::INSERT_POST_LOCAL_END           => 'onInsertPostLocalEndEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE              => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE_END          => 'onArrayFilterEvent',
                        ArrayFilterEvent::PREPARE_POST_START              => 'onPreparePostStartEvent',
@@ -214,6 +214,27 @@ class HookEventBridgeTest extends TestCase
                        $event->getArray(),
                );
        }
+       public function testOnInsertPostLocalEndEventCallsHookWithCorrectValue(): void
+       {
+               $event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, ['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_end', $name);
+                       $this->assertSame(['id' => -1], $data);
+
+                       return ['id' => 123];
+               });
+
+               HookEventBridge::onInsertPostLocalEndEvent($event);
+
+               $this->assertSame(
+                       ['item' => ['id' => 123]],
+                       $event->getArray(),
+               );
+       }
 
        public function testOnPreparePostStartEventCallsHookWithCorrectValue(): void
        {
@@ -390,8 +411,6 @@ class HookEventBridgeTest extends TestCase
                        [ArrayFilterEvent::FEATURE_ENABLED, 'isEnabled'],
                        [ArrayFilterEvent::FEATURE_GET, 'get'],
                        [ArrayFilterEvent::INSERT_POST_LOCAL_START, 'post_local_start'],
-                       [ArrayFilterEvent::INSERT_POST_LOCAL, 'post_local'],
-                       [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'post_local_end'],
                        [ArrayFilterEvent::INSERT_POST_REMOTE, 'post_remote'],
                        [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'post_remote_end'],
                        [ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT, 'prepare_body_content_filter'],