mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-07 20:04:32 +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
|
@ -172,7 +172,7 @@ class Hook
|
|||
* the provided data.
|
||||
*
|
||||
* @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
|
||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
*/
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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'];
|
||||
|
|
|
@ -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'],
|
||||
|
|
|
@ -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'],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue