]> git.mxchange.org Git - friendica.git/commitdiff
create event for event_created and event_updated hooks
authorArt4 <art4@wlabs.de>
Fri, 14 Mar 2025 14:11:58 +0000 (14:11 +0000)
committerArt4 <art4@wlabs.de>
Fri, 14 Mar 2025 14:11:58 +0000 (14:11 +0000)
src/Core/Hook.php
src/Core/Hooks/HookEventBridge.php
src/Event/ArrayFilterEvent.php
src/Model/Event.php
tests/Unit/Core/Hooks/HookEventBridgeTest.php
tests/Unit/Event/ArrayFilterEventTest.php

index 44dfad70bcbd83d20e8b8fa0b525cf2e021f7e54..c783f0a2266a6e66243aa1d31bc55a506780f1e1 100644 (file)
@@ -171,8 +171,8 @@ class Hook
         * Use this function when you want to be able to allow a hook to manipulate
         * the provided data.
         *
-        * @param string        $name of the hook to call
-        * @param string|array &$data to transmit to the callback handler
+        * @param string                $name of the hook to call
+        * @param int|string|array|null $data to transmit to the callback handler
         * @return void
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
index cc2e8be2256006f4522e030551418a5aa67620f3..1567b3e20ab6f46e4b092c4f4c07e8e3c1a7b705 100644 (file)
@@ -69,6 +69,8 @@ final class HookEventBridge
                ArrayFilterEvent::BLOCK_CONTACT                   => 'block',
                ArrayFilterEvent::UNBLOCK_CONTACT                 => 'unblock',
                ArrayFilterEvent::AVATAR_LOOKUP                   => 'avatar_lookup',
+               ArrayFilterEvent::EVENT_CREATED                   => 'event_created',
+               ArrayFilterEvent::EVENT_UPDATED                   => 'event_updated',
                ArrayFilterEvent::ADD_WORKER_TASK                 => 'proc_run',
                ArrayFilterEvent::STORAGE_CONFIG                  => 'storage_config',
                ArrayFilterEvent::STORAGE_INSTANCE                => 'storage_instance',
@@ -123,6 +125,8 @@ final class HookEventBridge
                        ArrayFilterEvent::BLOCK_CONTACT                   => 'onArrayFilterEvent',
                        ArrayFilterEvent::UNBLOCK_CONTACT                 => 'onArrayFilterEvent',
                        ArrayFilterEvent::AVATAR_LOOKUP                   => 'onArrayFilterEvent',
+                       ArrayFilterEvent::EVENT_CREATED                   => 'onEventCreatedEvent',
+                       ArrayFilterEvent::EVENT_UPDATED                   => 'onEventUpdatedEvent',
                        ArrayFilterEvent::ADD_WORKER_TASK                 => 'onArrayFilterEvent',
                        ArrayFilterEvent::STORAGE_CONFIG                  => 'onArrayFilterEvent',
                        ArrayFilterEvent::STORAGE_INSTANCE                => 'onArrayFilterEvent',
@@ -211,6 +215,32 @@ final class HookEventBridge
                $event->setArray($data);
        }
 
+       /**
+        * Map the EVENT_CREATED event to `event_created` hook
+        */
+       public static function onEventCreatedEvent(ArrayFilterEvent $event): void
+       {
+               $data = $event->getArray();
+
+               $id = (int) $data['event']['id'] ?? 0;
+
+               // one-way-event: we don't care about the returned value
+               static::callHook($event->getName(), $id);
+       }
+
+       /**
+        * Map the EVENT_UPDATED event to `event_updated` hook
+        */
+       public static function onEventUpdatedEvent(ArrayFilterEvent $event): void
+       {
+               $data = $event->getArray();
+
+               $id = (int) $data['event']['id'] ?? 0;
+
+               // one-way-event: we don't care about the returned value
+               static::callHook($event->getName(), $id);
+       }
+
        public static function onArrayFilterEvent(ArrayFilterEvent $event): void
        {
                $event->setArray(
@@ -226,9 +256,9 @@ final class HookEventBridge
        }
 
        /**
-        * @param string|array|object $data
+        * @param int|string|array|object $data
         *
-        * @return string|array|object
+        * @return int|string|array|object
         */
        private static function callHook(string $name, $data)
        {
index ac72f3d29a8e0f203fbcfd13a8eddbe27ae4f015..b3be9f70fadf28d188266ad0455ececfae09bd4f 100644 (file)
@@ -76,6 +76,10 @@ final class ArrayFilterEvent extends Event
 
        public const AVATAR_LOOKUP = 'friendica.data.avatar_lookup';
 
+       public const EVENT_CREATED = 'friendica.data.event_created';
+
+       public const EVENT_UPDATED = 'friendica.data.event_updated';
+
        public const ADD_WORKER_TASK = 'friendica.data.add_worker_task';
 
        public const STORAGE_CONFIG = 'friendica.data.storage_config';
index 6cb73f4c37e65fe9330785acd60da852f3f506f6..debeb74d454534804d922ff48a4f59a1f0e429f6 100644 (file)
@@ -14,6 +14,7 @@ use Friendica\Core\Renderer;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\DI;
+use Friendica\Event\ArrayFilterEvent;
 use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Network\HTTPException\NotFoundException;
 use Friendica\Network\HTTPException\UnauthorizedException;
@@ -255,6 +256,7 @@ class Event
                        'finish'    => DateTimeFormat::utc(($arr['finish'] ?? '') ?: DBA::NULL_DATETIME),
                ];
 
+               $eventDispatcher = DI::eventDispatcher();
 
                if ($event['finish'] < DBA::NULL_DATETIME) {
                        $event['finish'] = DBA::NULL_DATETIME;
@@ -295,14 +297,18 @@ class Event
                                Item::update($fields, ['id' => $item['id']]);
                        }
 
-                       Hook::callAll('event_updated', $event['id']);
+                       $eventDispatcher->dispatch(
+                               new ArrayFilterEvent(ArrayFilterEvent::EVENT_UPDATED, ['event' => $event]),
+                       );
                } else {
                        // New event. Store it.
                        DBA::insert('event', $event);
 
                        $event['id'] = DBA::lastInsertId();
 
-                       Hook::callAll("event_created", $event['id']);
+                       $eventDispatcher->dispatch(
+                               new ArrayFilterEvent(ArrayFilterEvent::EVENT_CREATED, ['event' => $event]),
+                       );
                }
 
                return $event['id'];
index 77e0b1ecfca45d489134b9b0fac36e808c38545b..1852248ebbd3b0e5c759ffdac59f796ca246d7bb 100644 (file)
@@ -58,6 +58,8 @@ class HookEventBridgeTest extends TestCase
                        ArrayFilterEvent::BLOCK_CONTACT                   => 'onArrayFilterEvent',
                        ArrayFilterEvent::UNBLOCK_CONTACT                 => 'onArrayFilterEvent',
                        ArrayFilterEvent::AVATAR_LOOKUP                   => 'onArrayFilterEvent',
+                       ArrayFilterEvent::EVENT_CREATED                   => 'onEventCreatedEvent',
+                       ArrayFilterEvent::EVENT_UPDATED                   => 'onEventUpdatedEvent',
                        ArrayFilterEvent::ADD_WORKER_TASK                 => 'onArrayFilterEvent',
                        ArrayFilterEvent::STORAGE_CONFIG                  => 'onArrayFilterEvent',
                        ArrayFilterEvent::STORAGE_INSTANCE                => 'onArrayFilterEvent',
@@ -267,6 +269,40 @@ class HookEventBridgeTest extends TestCase
                );
        }
 
+       public function testOnEventCreatedEventCallsHookWithCorrectValue(): void
+       {
+               $event = new ArrayFilterEvent(ArrayFilterEvent::EVENT_CREATED, ['event' => ['id' => 123]]);
+
+               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
+               $reflectionProperty->setAccessible(true);
+
+               $reflectionProperty->setValue(null, function (string $name, int $data): int {
+                       $this->assertSame('event_created', $name);
+                       $this->assertSame(123, $data);
+
+                       return 123;
+               });
+
+               HookEventBridge::onEventCreatedEvent($event);
+       }
+
+       public function testOnEventUpdatedEventCallsHookWithCorrectValue(): void
+       {
+               $event = new ArrayFilterEvent(ArrayFilterEvent::EVENT_UPDATED, ['event' => ['id' => 123]]);
+
+               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
+               $reflectionProperty->setAccessible(true);
+
+               $reflectionProperty->setValue(null, function (string $name, int $data): int {
+                       $this->assertSame('event_updated', $name);
+                       $this->assertSame(123, $data);
+
+                       return 123;
+               });
+
+               HookEventBridge::onEventUpdatedEvent($event);
+       }
+
        public static function getArrayFilterEventData(): array
        {
                return [
@@ -297,6 +333,8 @@ class HookEventBridgeTest extends TestCase
                        [ArrayFilterEvent::BLOCK_CONTACT, 'block'],
                        [ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'],
                        [ArrayFilterEvent::AVATAR_LOOKUP, 'avatar_lookup'],
+                       [ArrayFilterEvent::EVENT_CREATED, 'event_created'],
+                       [ArrayFilterEvent::EVENT_UPDATED, 'event_updated'],
                        [ArrayFilterEvent::ADD_WORKER_TASK, 'proc_run'],
                        [ArrayFilterEvent::STORAGE_CONFIG, 'storage_config'],
                        [ArrayFilterEvent::STORAGE_INSTANCE, 'storage_instance'],
index 0e0c14721679da6468a3cd8385be9f3301098967..0e4467c1ed086df700bb4334d94a52762f7b97d5 100644 (file)
@@ -55,6 +55,8 @@ class ArrayFilterEventTest extends TestCase
                        [ArrayFilterEvent::BLOCK_CONTACT, 'friendica.data.block_contact'],
                        [ArrayFilterEvent::UNBLOCK_CONTACT, 'friendica.data.unblock_contact'],
                        [ArrayFilterEvent::AVATAR_LOOKUP, 'friendica.data.avatar_lookup'],
+                       [ArrayFilterEvent::EVENT_CREATED, 'friendica.data.event_created'],
+                       [ArrayFilterEvent::EVENT_UPDATED, 'friendica.data.event_updated'],
                        [ArrayFilterEvent::ADD_WORKER_TASK, 'friendica.data.add_worker_task'],
                        [ArrayFilterEvent::STORAGE_CONFIG, 'friendica.data.storage_config'],
                        [ArrayFilterEvent::STORAGE_INSTANCE, 'friendica.data.storage_instance'],