* 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
*/
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',
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',
$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(
}
/**
- * @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)
{
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';
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;
'finish' => DateTimeFormat::utc(($arr['finish'] ?? '') ?: DBA::NULL_DATETIME),
];
+ $eventDispatcher = DI::eventDispatcher();
if ($event['finish'] < DBA::NULL_DATETIME) {
$event['finish'] = DBA::NULL_DATETIME;
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'];
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',
);
}
+ 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 [
[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'],
[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'],