mirror of
https://git.sekbaer.de/Friendica/friendica.git
synced 2025-06-07 15:54:26 +02:00
create event for event_created and event_updated hooks
This commit is contained in:
parent
68b604812c
commit
abe35732d8
6 changed files with 86 additions and 6 deletions
|
@ -171,8 +171,8 @@ class Hook
|
||||||
* Use this function when you want to be able to allow a hook to manipulate
|
* Use this function when you want to be able to allow a hook to manipulate
|
||||||
* the provided data.
|
* the provided data.
|
||||||
*
|
*
|
||||||
* @param string $name of the hook to call
|
* @param string $name of the hook to call
|
||||||
* @param string|array &$data to transmit to the callback handler
|
* @param int|string|array|null $data to transmit to the callback handler
|
||||||
* @return void
|
* @return void
|
||||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -69,6 +69,8 @@ final class HookEventBridge
|
||||||
ArrayFilterEvent::BLOCK_CONTACT => 'block',
|
ArrayFilterEvent::BLOCK_CONTACT => 'block',
|
||||||
ArrayFilterEvent::UNBLOCK_CONTACT => 'unblock',
|
ArrayFilterEvent::UNBLOCK_CONTACT => 'unblock',
|
||||||
ArrayFilterEvent::AVATAR_LOOKUP => 'avatar_lookup',
|
ArrayFilterEvent::AVATAR_LOOKUP => 'avatar_lookup',
|
||||||
|
ArrayFilterEvent::EVENT_CREATED => 'event_created',
|
||||||
|
ArrayFilterEvent::EVENT_UPDATED => 'event_updated',
|
||||||
ArrayFilterEvent::ADD_WORKER_TASK => 'proc_run',
|
ArrayFilterEvent::ADD_WORKER_TASK => 'proc_run',
|
||||||
ArrayFilterEvent::STORAGE_CONFIG => 'storage_config',
|
ArrayFilterEvent::STORAGE_CONFIG => 'storage_config',
|
||||||
ArrayFilterEvent::STORAGE_INSTANCE => 'storage_instance',
|
ArrayFilterEvent::STORAGE_INSTANCE => 'storage_instance',
|
||||||
|
@ -123,6 +125,8 @@ final class HookEventBridge
|
||||||
ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent',
|
ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent',
|
||||||
ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent',
|
ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent',
|
||||||
ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent',
|
ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent',
|
||||||
|
ArrayFilterEvent::EVENT_CREATED => 'onEventCreatedEvent',
|
||||||
|
ArrayFilterEvent::EVENT_UPDATED => 'onEventUpdatedEvent',
|
||||||
ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent',
|
ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent',
|
||||||
ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent',
|
ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent',
|
||||||
ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent',
|
ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent',
|
||||||
|
@ -211,6 +215,32 @@ final class HookEventBridge
|
||||||
$event->setArray($data);
|
$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
|
public static function onArrayFilterEvent(ArrayFilterEvent $event): void
|
||||||
{
|
{
|
||||||
$event->setArray(
|
$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)
|
private static function callHook(string $name, $data)
|
||||||
{
|
{
|
||||||
|
|
|
@ -76,6 +76,10 @@ final class ArrayFilterEvent extends Event
|
||||||
|
|
||||||
public const AVATAR_LOOKUP = 'friendica.data.avatar_lookup';
|
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 ADD_WORKER_TASK = 'friendica.data.add_worker_task';
|
||||||
|
|
||||||
public const STORAGE_CONFIG = 'friendica.data.storage_config';
|
public const STORAGE_CONFIG = 'friendica.data.storage_config';
|
||||||
|
|
|
@ -14,6 +14,7 @@ use Friendica\Core\Renderer;
|
||||||
use Friendica\Core\System;
|
use Friendica\Core\System;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
use Friendica\DI;
|
use Friendica\DI;
|
||||||
|
use Friendica\Event\ArrayFilterEvent;
|
||||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||||
use Friendica\Network\HTTPException\NotFoundException;
|
use Friendica\Network\HTTPException\NotFoundException;
|
||||||
use Friendica\Network\HTTPException\UnauthorizedException;
|
use Friendica\Network\HTTPException\UnauthorizedException;
|
||||||
|
@ -255,6 +256,7 @@ class Event
|
||||||
'finish' => DateTimeFormat::utc(($arr['finish'] ?? '') ?: DBA::NULL_DATETIME),
|
'finish' => DateTimeFormat::utc(($arr['finish'] ?? '') ?: DBA::NULL_DATETIME),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$eventDispatcher = DI::eventDispatcher();
|
||||||
|
|
||||||
if ($event['finish'] < DBA::NULL_DATETIME) {
|
if ($event['finish'] < DBA::NULL_DATETIME) {
|
||||||
$event['finish'] = DBA::NULL_DATETIME;
|
$event['finish'] = DBA::NULL_DATETIME;
|
||||||
|
@ -295,14 +297,18 @@ class Event
|
||||||
Item::update($fields, ['id' => $item['id']]);
|
Item::update($fields, ['id' => $item['id']]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Hook::callAll('event_updated', $event['id']);
|
$eventDispatcher->dispatch(
|
||||||
|
new ArrayFilterEvent(ArrayFilterEvent::EVENT_UPDATED, ['event' => $event]),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// New event. Store it.
|
// New event. Store it.
|
||||||
DBA::insert('event', $event);
|
DBA::insert('event', $event);
|
||||||
|
|
||||||
$event['id'] = DBA::lastInsertId();
|
$event['id'] = DBA::lastInsertId();
|
||||||
|
|
||||||
Hook::callAll("event_created", $event['id']);
|
$eventDispatcher->dispatch(
|
||||||
|
new ArrayFilterEvent(ArrayFilterEvent::EVENT_CREATED, ['event' => $event]),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $event['id'];
|
return $event['id'];
|
||||||
|
|
|
@ -58,6 +58,8 @@ class HookEventBridgeTest extends TestCase
|
||||||
ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent',
|
ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent',
|
||||||
ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent',
|
ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent',
|
||||||
ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent',
|
ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent',
|
||||||
|
ArrayFilterEvent::EVENT_CREATED => 'onEventCreatedEvent',
|
||||||
|
ArrayFilterEvent::EVENT_UPDATED => 'onEventUpdatedEvent',
|
||||||
ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent',
|
ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent',
|
||||||
ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent',
|
ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent',
|
||||||
ArrayFilterEvent::STORAGE_INSTANCE => '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
|
public static function getArrayFilterEventData(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -297,6 +333,8 @@ class HookEventBridgeTest extends TestCase
|
||||||
[ArrayFilterEvent::BLOCK_CONTACT, 'block'],
|
[ArrayFilterEvent::BLOCK_CONTACT, 'block'],
|
||||||
[ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'],
|
[ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'],
|
||||||
[ArrayFilterEvent::AVATAR_LOOKUP, 'avatar_lookup'],
|
[ArrayFilterEvent::AVATAR_LOOKUP, 'avatar_lookup'],
|
||||||
|
[ArrayFilterEvent::EVENT_CREATED, 'event_created'],
|
||||||
|
[ArrayFilterEvent::EVENT_UPDATED, 'event_updated'],
|
||||||
[ArrayFilterEvent::ADD_WORKER_TASK, 'proc_run'],
|
[ArrayFilterEvent::ADD_WORKER_TASK, 'proc_run'],
|
||||||
[ArrayFilterEvent::STORAGE_CONFIG, 'storage_config'],
|
[ArrayFilterEvent::STORAGE_CONFIG, 'storage_config'],
|
||||||
[ArrayFilterEvent::STORAGE_INSTANCE, 'storage_instance'],
|
[ArrayFilterEvent::STORAGE_INSTANCE, 'storage_instance'],
|
||||||
|
|
|
@ -55,6 +55,8 @@ class ArrayFilterEventTest extends TestCase
|
||||||
[ArrayFilterEvent::BLOCK_CONTACT, 'friendica.data.block_contact'],
|
[ArrayFilterEvent::BLOCK_CONTACT, 'friendica.data.block_contact'],
|
||||||
[ArrayFilterEvent::UNBLOCK_CONTACT, 'friendica.data.unblock_contact'],
|
[ArrayFilterEvent::UNBLOCK_CONTACT, 'friendica.data.unblock_contact'],
|
||||||
[ArrayFilterEvent::AVATAR_LOOKUP, 'friendica.data.avatar_lookup'],
|
[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::ADD_WORKER_TASK, 'friendica.data.add_worker_task'],
|
||||||
[ArrayFilterEvent::STORAGE_CONFIG, 'friendica.data.storage_config'],
|
[ArrayFilterEvent::STORAGE_CONFIG, 'friendica.data.storage_config'],
|
||||||
[ArrayFilterEvent::STORAGE_INSTANCE, 'friendica.data.storage_instance'],
|
[ArrayFilterEvent::STORAGE_INSTANCE, 'friendica.data.storage_instance'],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue