Wrap item in INSERT_POST_LOCAL_END into separate array

This commit is contained in:
Art4 2025-03-18 14:21:31 +00:00
parent 1ddd5674e1
commit 9822dd25d8
4 changed files with 53 additions and 8 deletions

View 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) {

View 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
*/

View 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']);

View 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'],