mirror of
https://github.com/friendica/friendica
synced 2025-03-03 19:48:26 +00:00
Create event for oembed_fetch_url hook
This commit is contained in:
parent
aff117284b
commit
b9a401454d
5 changed files with 51 additions and 3 deletions
|
@ -13,11 +13,11 @@ use DOMXPath;
|
|||
use Exception;
|
||||
use Friendica\Content\Text\BBCode;
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Hook;
|
||||
use Friendica\Core\Renderer;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Event\ArrayFilterEvent;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientRequest;
|
||||
|
@ -180,9 +180,15 @@ class OEmbed
|
|||
$oembed->thumbnail_height = $data['images'][0]['height'];
|
||||
}
|
||||
|
||||
Hook::callAll('oembed_fetch_url', $embedurl);
|
||||
$eventDispatcher = DI::eventDispatcher();
|
||||
|
||||
return $oembed;
|
||||
$oembed_data = ['url' => $embedurl];
|
||||
|
||||
$oembed_data = $eventDispatcher->dispatch(
|
||||
new ArrayFilterEvent(ArrayFilterEvent::OEMBED_FETCH_END, $oembed_data),
|
||||
)->getArray();
|
||||
|
||||
return $oembed_data['url'] ?? $embedurl;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -52,6 +52,7 @@ final class HookEventBridge
|
|||
ArrayFilterEvent::DISPLAY_ITEM => 'display_item',
|
||||
ArrayFilterEvent::RENDER_LOCATION => 'render_location',
|
||||
ArrayFilterEvent::ITEM_PHOTO_MENU => 'item_photo_menu',
|
||||
ArrayFilterEvent::OEMBED_FETCH_END => 'oembed_fetch_url',
|
||||
HtmlFilterEvent::HEAD => 'head',
|
||||
HtmlFilterEvent::FOOTER => 'footer',
|
||||
HtmlFilterEvent::PAGE_HEADER => 'page_header',
|
||||
|
@ -83,6 +84,7 @@ final class HookEventBridge
|
|||
ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent',
|
||||
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
||||
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
||||
HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent',
|
||||
|
@ -109,6 +111,20 @@ final class HookEventBridge
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the OEMBED_FETCH_END event to `oembed_fetch_url` hook
|
||||
*/
|
||||
public static function onOembedFetchEndEvent(ArrayFilterEvent $event): void
|
||||
{
|
||||
$data = $event->getArray();
|
||||
|
||||
$url = (string) $data['url'] ?? '';
|
||||
|
||||
$data['url'] = static::callHook($event->getName(), $url);
|
||||
|
||||
$event->setArray($data);
|
||||
}
|
||||
|
||||
public static function onArrayFilterEvent(ArrayFilterEvent $event): void
|
||||
{
|
||||
$event->setArray(
|
||||
|
|
|
@ -42,6 +42,8 @@ final class ArrayFilterEvent extends Event
|
|||
|
||||
public const ITEM_PHOTO_MENU = 'friendica.data.item_photo_menu';
|
||||
|
||||
public const OEMBED_FETCH_END = 'friendica.data.oembed_fetch_end';
|
||||
|
||||
private array $array;
|
||||
|
||||
public function __construct(string $name, array $array)
|
||||
|
|
|
@ -41,6 +41,7 @@ class HookEventBridgeTest extends TestCase
|
|||
ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent',
|
||||
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
||||
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
||||
HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent',
|
||||
|
@ -156,6 +157,28 @@ class HookEventBridgeTest extends TestCase
|
|||
HookEventBridge::onCollectRoutesEvent($event);
|
||||
}
|
||||
|
||||
public function testOnOembedFetchEndEventCallsHookWithCorrectValue(): void
|
||||
{
|
||||
$event = new ArrayFilterEvent(ArrayFilterEvent::OEMBED_FETCH_END, ['url' => 'original_url']);
|
||||
|
||||
$reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
|
||||
$reflectionProperty->setAccessible(true);
|
||||
|
||||
$reflectionProperty->setValue(null, function (string $name, $data): string {
|
||||
$this->assertSame('oembed_fetch_url', $name);
|
||||
$this->assertSame('original_url', $data);
|
||||
|
||||
return 'changed_url';
|
||||
});
|
||||
|
||||
HookEventBridge::onOembedFetchEndEvent($event);
|
||||
|
||||
$this->assertSame(
|
||||
['url' => 'changed_url'],
|
||||
$event->getArray(),
|
||||
);
|
||||
}
|
||||
|
||||
public static function getArrayFilterEventData(): array
|
||||
{
|
||||
return [
|
||||
|
|
|
@ -38,6 +38,7 @@ class ArrayFilterEventTest extends TestCase
|
|||
[ArrayFilterEvent::DISPLAY_ITEM, 'friendica.data.display_item'],
|
||||
[ArrayFilterEvent::RENDER_LOCATION, 'friendica.data.render_location'],
|
||||
[ArrayFilterEvent::ITEM_PHOTO_MENU, 'friendica.data.item_photo_menu'],
|
||||
[ArrayFilterEvent::OEMBED_FETCH_END, 'friendica.data.oembed_fetch_end'],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue