diff --git a/src/Content/Item.php b/src/Content/Item.php index af26ebd8ed..901a2eac3d 100644 --- a/src/Content/Item.php +++ b/src/Content/Item.php @@ -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) { diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index e5a7dd01ab..82a11f1f55 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -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 */ diff --git a/src/Module/Post/Tag/Add.php b/src/Module/Post/Tag/Add.php index e03bf215a3..a145c36e90 100644 --- a/src/Module/Post/Tag/Add.php +++ b/src/Module/Post/Tag/Add.php @@ -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']); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 17f9e1084d..e6ec58a27c 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -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'],