Create event for lockview_content hook

This commit is contained in:
Art4 2025-04-10 11:04:10 +00:00
parent f08aef86f1
commit ff3c4046e0
5 changed files with 76 additions and 7 deletions

View file

@ -43,6 +43,7 @@ final class HookEventBridge
ArrayFilterEvent::NAV_INFO => 'nav_info', ArrayFilterEvent::NAV_INFO => 'nav_info',
ArrayFilterEvent::FEATURE_ENABLED => 'isEnabled', ArrayFilterEvent::FEATURE_ENABLED => 'isEnabled',
ArrayFilterEvent::FEATURE_GET => 'get', ArrayFilterEvent::FEATURE_GET => 'get',
ArrayFilterEvent::PERMISSION_TOOLTIP_CONTENT => 'lockview_content',
ArrayFilterEvent::INSERT_POST_LOCAL_START => 'post_local_start', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'post_local_start',
ArrayFilterEvent::INSERT_POST_LOCAL => 'post_local', ArrayFilterEvent::INSERT_POST_LOCAL => 'post_local',
ArrayFilterEvent::INSERT_POST_LOCAL_END => 'post_local_end', ArrayFilterEvent::INSERT_POST_LOCAL_END => 'post_local_end',
@ -127,6 +128,7 @@ final class HookEventBridge
ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent',
ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent',
ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent',
ArrayFilterEvent::PERMISSION_TOOLTIP_CONTENT => 'onPermissionTooltipContentEvent',
ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent',
ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent', ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent',
ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onInsertPostLocalEndEvent', ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onInsertPostLocalEndEvent',
@ -215,6 +217,20 @@ final class HookEventBridge
); );
} }
/**
* Map the PERMISSION_TOOLTIP_CONTENT event to `lockview_content` hook
*/
public static function onPermissionTooltipContentEvent(ArrayFilterEvent $event): void
{
$data = $event->getArray();
$model = (array) $data['model'] ?? [];
$data['model'] = static::callHook($event->getName(), $model);
$event->setArray($data);
}
/** /**
* Map the INSERT_POST_LOCAL event to `post_local` hook * Map the INSERT_POST_LOCAL event to `post_local` hook
*/ */

View file

@ -24,6 +24,8 @@ final class ArrayFilterEvent extends Event
public const FEATURE_GET = 'friendica.data.feature_get'; public const FEATURE_GET = 'friendica.data.feature_get';
public const PERMISSION_TOOLTIP_CONTENT = 'friendica.data.permission_tooltip_content';
public const INSERT_POST_LOCAL_START = 'friendica.data.insert_post_local_start'; public const INSERT_POST_LOCAL_START = 'friendica.data.insert_post_local_start';
public const INSERT_POST_LOCAL = 'friendica.data.insert_post_local'; public const INSERT_POST_LOCAL = 'friendica.data.insert_post_local';

View file

@ -7,14 +7,16 @@
namespace Friendica\Module\Privacy; namespace Friendica\Module\Privacy;
use Friendica\App; use Friendica\App\Arguments;
use Friendica\App\BaseURL;
use Friendica\BaseModule;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Hook;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Event\ArrayFilterEvent;
use Friendica\Model; use Friendica\Model;
use Friendica\Module\Response; use Friendica\Module\Response;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
@ -23,21 +25,37 @@ use Friendica\Privacy\Entity;
use Friendica\Security\PermissionSet\Repository\PermissionSet; use Friendica\Security\PermissionSet\Repository\PermissionSet;
use Friendica\Util\ACLFormatter; use Friendica\Util\ACLFormatter;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/** /**
* Outputs the permission tooltip HTML content for the provided item, photo or event id. * Outputs the permission tooltip HTML content for the provided item, photo or event id.
*/ */
class PermissionTooltip extends \Friendica\BaseModule class PermissionTooltip extends BaseModule
{ {
private Database $dba; private Database $dba;
private ACLFormatter $aclFormatter; private ACLFormatter $aclFormatter;
private IHandleUserSessions $session; private IHandleUserSessions $session;
private IManageConfigValues $config; private IManageConfigValues $config;
private PermissionSet $permissionSet; private PermissionSet $permissionSet;
private EventDispatcherInterface $eventDispatcher;
public function __construct(PermissionSet $permissionSet, IManageConfigValues $config, IHandleUserSessions $session, ACLFormatter $aclFormatter, Database $dba, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) public function __construct(
{ PermissionSet $permissionSet,
IManageConfigValues $config,
IHandleUserSessions $session,
ACLFormatter $aclFormatter,
Database $dba,
EventDispatcherInterface $eventDispatcher,
L10n $l10n,
BaseURL $baseUrl,
Arguments $args,
LoggerInterface $logger,
Profiler $profiler,
Response $response,
array $server,
array $parameters = [],
) {
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->dba = $dba; $this->dba = $dba;
@ -45,6 +63,7 @@ class PermissionTooltip extends \Friendica\BaseModule
$this->session = $session; $this->session = $session;
$this->config = $config; $this->config = $config;
$this->permissionSet = $permissionSet; $this->permissionSet = $permissionSet;
$this->eventDispatcher = $eventDispatcher;
} }
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
@ -87,8 +106,15 @@ class PermissionTooltip extends \Friendica\BaseModule
throw new HttpException\NotFoundException($this->t('Model not found')); throw new HttpException\NotFoundException($this->t('Model not found'));
} }
// Kept for backwards compatibility $hook_data = [
Hook::callAll('lockview_content', $model); 'model' => $model,
];
$hook_data = $this->eventDispatcher->dispatch(
new ArrayFilterEvent(ArrayFilterEvent::PERMISSION_TOOLTIP_CONTENT, $hook_data),
)->getArray();
$model = $hook_data['model'] ?? $model;
$aclReceivers = new Entity\AclReceivers(); $aclReceivers = new Entity\AclReceivers();
$addressedReceivers = new Entity\AddressedReceivers(); $addressedReceivers = new Entity\AddressedReceivers();

View file

@ -32,6 +32,7 @@ class HookEventBridgeTest extends TestCase
ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent',
ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent',
ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent',
ArrayFilterEvent::PERMISSION_TOOLTIP_CONTENT => 'onPermissionTooltipContentEvent',
ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent',
ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent', ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent',
ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onInsertPostLocalEndEvent', ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onInsertPostLocalEndEvent',
@ -209,6 +210,28 @@ class HookEventBridgeTest extends TestCase
HookEventBridge::onCollectRoutesEvent($event); HookEventBridge::onCollectRoutesEvent($event);
} }
public function testOnPermissionTooltipContentEventCallsHookWithCorrectValue(): void
{
$event = new ArrayFilterEvent(ArrayFilterEvent::PERMISSION_TOOLTIP_CONTENT, ['model' => ['uid' => -1]]);
$reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, function (string $name, array $data): array {
$this->assertSame('lockview_content', $name);
$this->assertSame(['uid' => -1], $data);
return ['uid' => 123];
});
HookEventBridge::onPermissionTooltipContentEvent($event);
$this->assertSame(
['model' => ['uid' => 123]],
$event->getArray(),
);
}
public function testOnInsertPostLocalEventCallsHookWithCorrectValue(): void public function testOnInsertPostLocalEventCallsHookWithCorrectValue(): void
{ {
$event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, ['item' => ['id' => -1]]); $event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, ['item' => ['id' => -1]]);
@ -230,6 +253,7 @@ class HookEventBridgeTest extends TestCase
$event->getArray(), $event->getArray(),
); );
} }
public function testOnInsertPostLocalEndEventCallsHookWithCorrectValue(): void public function testOnInsertPostLocalEndEventCallsHookWithCorrectValue(): void
{ {
$event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, ['item' => ['id' => -1]]); $event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, ['item' => ['id' => -1]]);

View file

@ -29,6 +29,7 @@ class ArrayFilterEventTest extends TestCase
[ArrayFilterEvent::NAV_INFO, 'friendica.data.nav_info'], [ArrayFilterEvent::NAV_INFO, 'friendica.data.nav_info'],
[ArrayFilterEvent::FEATURE_ENABLED, 'friendica.data.feature_enabled'], [ArrayFilterEvent::FEATURE_ENABLED, 'friendica.data.feature_enabled'],
[ArrayFilterEvent::FEATURE_GET, 'friendica.data.feature_get'], [ArrayFilterEvent::FEATURE_GET, 'friendica.data.feature_get'],
[ArrayFilterEvent::PERMISSION_TOOLTIP_CONTENT, 'friendica.data.permission_tooltip_content'],
[ArrayFilterEvent::INSERT_POST_LOCAL_START, 'friendica.data.insert_post_local_start'], [ArrayFilterEvent::INSERT_POST_LOCAL_START, 'friendica.data.insert_post_local_start'],
[ArrayFilterEvent::INSERT_POST_LOCAL, 'friendica.data.insert_post_local'], [ArrayFilterEvent::INSERT_POST_LOCAL, 'friendica.data.insert_post_local'],
[ArrayFilterEvent::INSERT_POST_LOCAL_END, 'friendica.data.insert_post_local_end'], [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'friendica.data.insert_post_local_end'],