From 4d96875656445ca5204e98b3240c4fadcae90066 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 13 Mar 2025 13:42:54 +0000 Subject: [PATCH 01/45] Add event for unfollow hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Core/Protocol.php | 7 ++++++- src/Event/ArrayFilterEvent.php | 2 ++ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 2f1e51a297..6b652cf4f0 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -62,6 +62,7 @@ final class HookEventBridge ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW => 'support_follow', ArrayFilterEvent::PROTOCOL_SUPPORTS_REVOKE_FOLLOW => 'support_revoke_follow', ArrayFilterEvent::PROTOCOL_SUPPORTS_PROBE => 'support_probe', + ArrayFilterEvent::UNFOLLOW => 'unfollow', HtmlFilterEvent::HEAD => 'head', HtmlFilterEvent::FOOTER => 'footer', HtmlFilterEvent::PAGE_HEADER => 'page_header', @@ -104,6 +105,7 @@ final class HookEventBridge ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW => 'onArrayFilterEvent', ArrayFilterEvent::PROTOCOL_SUPPORTS_REVOKE_FOLLOW => 'onArrayFilterEvent', ArrayFilterEvent::PROTOCOL_SUPPORTS_PROBE => 'onArrayFilterEvent', + ArrayFilterEvent::UNFOLLOW => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', diff --git a/src/Core/Protocol.php b/src/Core/Protocol.php index 1e5b7e7f21..61427d1017 100644 --- a/src/Core/Protocol.php +++ b/src/Core/Protocol.php @@ -183,7 +183,12 @@ class Protocol 'uid' => $owner['uid'], 'result' => null, ]; - Hook::callAll('unfollow', $hook_data); + + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW, $hook_data), + )->getArray(); return $hook_data['result']; } diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index d21c6f8d78..9664071714 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -62,6 +62,8 @@ final class ArrayFilterEvent extends Event public const PROTOCOL_SUPPORTS_PROBE = 'friendica.data.protocol_supports_probe'; + public const UNFOLLOW = 'friendica.data.unfollow'; + private array $array; public function __construct(string $name, array $array) diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index cdd3e0d61b..93d62965c3 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -51,6 +51,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW => 'onArrayFilterEvent', ArrayFilterEvent::PROTOCOL_SUPPORTS_REVOKE_FOLLOW => 'onArrayFilterEvent', ArrayFilterEvent::PROTOCOL_SUPPORTS_PROBE => 'onArrayFilterEvent', + ArrayFilterEvent::UNFOLLOW => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', @@ -278,6 +279,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW, 'support_follow'], [ArrayFilterEvent::PROTOCOL_SUPPORTS_REVOKE_FOLLOW, 'support_revoke_follow'], [ArrayFilterEvent::PROTOCOL_SUPPORTS_PROBE, 'support_probe'], + [ArrayFilterEvent::UNFOLLOW, 'unfollow'], ]; } From 10e4f4bf36cb601af0c419f0c864fd672dbb8999 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 13 Mar 2025 14:02:35 +0000 Subject: [PATCH 02/45] Add events for follow, revoke_follow, block and unblock hooks --- src/Core/Hooks/HookEventBridge.php | 12 ++++++++-- src/Core/Protocol.php | 23 +++++++++++++++---- src/Event/ArrayFilterEvent.php | 10 +++++++- src/Model/Contact.php | 7 +++++- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 12 ++++++++-- tests/Unit/Event/ArrayFilterEventTest.php | 5 ++++ 6 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 6b652cf4f0..39da9a13d0 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -62,7 +62,11 @@ final class HookEventBridge ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW => 'support_follow', ArrayFilterEvent::PROTOCOL_SUPPORTS_REVOKE_FOLLOW => 'support_revoke_follow', ArrayFilterEvent::PROTOCOL_SUPPORTS_PROBE => 'support_probe', - ArrayFilterEvent::UNFOLLOW => 'unfollow', + ArrayFilterEvent::FOLLOW_CONTACT => 'follow', + ArrayFilterEvent::UNFOLLOW_CONTACT => 'unfollow', + ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'revoke_follow', + ArrayFilterEvent::BLOCK_CONTACT => 'block', + ArrayFilterEvent::UNBLOCK_CONTACT => 'unblock', HtmlFilterEvent::HEAD => 'head', HtmlFilterEvent::FOOTER => 'footer', HtmlFilterEvent::PAGE_HEADER => 'page_header', @@ -105,7 +109,11 @@ final class HookEventBridge ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW => 'onArrayFilterEvent', ArrayFilterEvent::PROTOCOL_SUPPORTS_REVOKE_FOLLOW => 'onArrayFilterEvent', ArrayFilterEvent::PROTOCOL_SUPPORTS_PROBE => 'onArrayFilterEvent', - ArrayFilterEvent::UNFOLLOW => 'onArrayFilterEvent', + ArrayFilterEvent::FOLLOW_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::UNFOLLOW_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', diff --git a/src/Core/Protocol.php b/src/Core/Protocol.php index 61427d1017..ef57c22f18 100644 --- a/src/Core/Protocol.php +++ b/src/Core/Protocol.php @@ -187,7 +187,7 @@ class Protocol $eventDispatcher = DI::eventDispatcher(); $hook_data = $eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW, $hook_data), + new ArrayFilterEvent(ArrayFilterEvent::UNFOLLOW_CONTACT, $hook_data), )->getArray(); return $hook_data['result']; @@ -223,7 +223,12 @@ class Protocol 'uid' => $owner['uid'], 'result' => null, ]; - Hook::callAll('revoke_follow', $hook_data); + + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::REVOKE_FOLLOW_CONTACT, $hook_data), + )->getArray(); return $hook_data['result']; } @@ -261,7 +266,12 @@ class Protocol 'uid' => $uid, 'result' => null, ]; - Hook::callAll('block', $hook_data); + + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::BLOCK_CONTACT, $hook_data), + )->getArray(); return $hook_data['result']; } @@ -300,7 +310,12 @@ class Protocol 'uid' => $uid, 'result' => null, ]; - Hook::callAll('unblock', $hook_data); + + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::UNBLOCK_CONTACT, $hook_data), + )->getArray(); return $hook_data['result']; } diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 9664071714..b4ec112272 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -62,7 +62,15 @@ final class ArrayFilterEvent extends Event public const PROTOCOL_SUPPORTS_PROBE = 'friendica.data.protocol_supports_probe'; - public const UNFOLLOW = 'friendica.data.unfollow'; + public const FOLLOW_CONTACT = 'friendica.data.follow_contact'; + + public const UNFOLLOW_CONTACT = 'friendica.data.unfollow_contact'; + + public const REVOKE_FOLLOW_CONTACT = 'friendica.data.revoke_follow_contact'; + + public const BLOCK_CONTACT = 'friendica.data.block_contact'; + + public const UNBLOCK_CONTACT = 'friendica.data.unblock_contact'; private array $array; diff --git a/src/Model/Contact.php b/src/Model/Contact.php index cced0ed626..e94ef46964 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -22,6 +22,7 @@ use Friendica\Core\Worker; 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\HTTPException\NotFoundException; @@ -3144,7 +3145,11 @@ class Contact $arr = ['url' => $url, 'uid' => $uid, 'contact' => []]; - Hook::callAll('follow', $arr); + $eventDispatcher = DI::eventDispatcher(); + + $arr = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::FOLLOW_CONTACT, $arr), + )->getArray(); if (empty($arr)) { $result['message'] = DI::l10n()->t('The contact could not be added. Please check the relevant network credentials in your Settings -> Social Networks page.'); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 93d62965c3..f1b10ed3c7 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -51,7 +51,11 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW => 'onArrayFilterEvent', ArrayFilterEvent::PROTOCOL_SUPPORTS_REVOKE_FOLLOW => 'onArrayFilterEvent', ArrayFilterEvent::PROTOCOL_SUPPORTS_PROBE => 'onArrayFilterEvent', - ArrayFilterEvent::UNFOLLOW => 'onArrayFilterEvent', + ArrayFilterEvent::FOLLOW_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::UNFOLLOW_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', @@ -279,7 +283,11 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW, 'support_follow'], [ArrayFilterEvent::PROTOCOL_SUPPORTS_REVOKE_FOLLOW, 'support_revoke_follow'], [ArrayFilterEvent::PROTOCOL_SUPPORTS_PROBE, 'support_probe'], - [ArrayFilterEvent::UNFOLLOW, 'unfollow'], + [ArrayFilterEvent::FOLLOW_CONTACT, 'follow'], + [ArrayFilterEvent::UNFOLLOW_CONTACT, 'unfollow'], + [ArrayFilterEvent::REVOKE_FOLLOW_CONTACT, 'revoke_follow'], + [ArrayFilterEvent::BLOCK_CONTACT, 'block'], + [ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'], ]; } diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 2d396e6760..be576995c7 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -47,6 +47,11 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW, 'friendica.data.protocol_supports_follow'], [ArrayFilterEvent::PROTOCOL_SUPPORTS_REVOKE_FOLLOW, 'friendica.data.protocol_supports_revoke_follow'], [ArrayFilterEvent::PROTOCOL_SUPPORTS_PROBE, 'friendica.data.protocol_supports_probe'], + [ArrayFilterEvent::FOLLOW_CONTACT, 'friendica.data.follow_contact'], + [ArrayFilterEvent::UNFOLLOW_CONTACT, 'friendica.data.unfollow_contact'], + [ArrayFilterEvent::REVOKE_FOLLOW_CONTACT, 'friendica.data.revoke_follow_contact'], + [ArrayFilterEvent::BLOCK_CONTACT, 'friendica.data.block_contact'], + [ArrayFilterEvent::UNBLOCK_CONTACT, 'friendica.data.unblock_contact'], ]; } From 2a722b16aab9c23288d1f961b7dbd6f6d308854f Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 13 Mar 2025 14:50:29 +0000 Subject: [PATCH 03/45] Add event for proc_run hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Core/Worker.php | 12 +++++++----- src/Event/ArrayFilterEvent.php | 2 ++ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 39da9a13d0..18a41a1730 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -67,6 +67,7 @@ final class HookEventBridge ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'revoke_follow', ArrayFilterEvent::BLOCK_CONTACT => 'block', ArrayFilterEvent::UNBLOCK_CONTACT => 'unblock', + ArrayFilterEvent::ADD_WORKER_TASK => 'proc_run', HtmlFilterEvent::HEAD => 'head', HtmlFilterEvent::FOOTER => 'footer', HtmlFilterEvent::PAGE_HEADER => 'page_header', @@ -114,6 +115,7 @@ final class HookEventBridge ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', diff --git a/src/Core/Worker.php b/src/Core/Worker.php index 995239cde9..0f14807adb 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -13,6 +13,7 @@ use Friendica\Core\Logger\Type\WorkerLogger; use Friendica\Core\Worker\Entity\Process; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Util\DateTimeFormat; /** @@ -1237,10 +1238,6 @@ class Worker * @return int '0' if worker queue entry already existed or there had been an error, otherwise the ID of the worker task * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @note $cmd and string args are surrounded with '' - * - * @hooks 'proc_run' - * array $arr - * */ public static function add(...$args) { @@ -1250,7 +1247,12 @@ class Worker $arr = ['args' => $args, 'run_cmd' => true]; - Hook::callAll('proc_run', $arr); + $eventDispatcher = DI::eventDispatcher(); + + $arr = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ADD_WORKER_TASK, $arr), + )->getArray(); + if (!$arr['run_cmd'] || !count($args)) { return 1; } diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index b4ec112272..6967524f86 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -72,6 +72,8 @@ final class ArrayFilterEvent extends Event public const UNBLOCK_CONTACT = 'friendica.data.unblock_contact'; + public const ADD_WORKER_TASK = 'friendica.data.add_worker_task'; + private array $array; public function __construct(string $name, array $array) diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index f1b10ed3c7..d7df82e4d1 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -56,6 +56,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', @@ -288,6 +289,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::REVOKE_FOLLOW_CONTACT, 'revoke_follow'], [ArrayFilterEvent::BLOCK_CONTACT, 'block'], [ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'], + [ArrayFilterEvent::ADD_WORKER_TASK, 'proc_run'], ]; } diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index be576995c7..2afebba496 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -52,6 +52,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::REVOKE_FOLLOW_CONTACT, 'friendica.data.revoke_follow_contact'], [ArrayFilterEvent::BLOCK_CONTACT, 'friendica.data.block_contact'], [ArrayFilterEvent::UNBLOCK_CONTACT, 'friendica.data.unblock_contact'], + [ArrayFilterEvent::ADD_WORKER_TASK, 'friendica.data.add_worker_task'], ]; } From 2c0c87339ef72c0af7af24d892bacdd8c872869d Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Mar 2025 10:12:56 +0000 Subject: [PATCH 04/45] add events for storage_config and storage_instance hooks --- src/Core/Hooks/HookEventBridge.php | 4 + .../Storage/Repository/StorageManager.php | 18 ++- src/Event/ArrayFilterEvent.php | 4 + tests/Unit/Core/Hooks/HookEventBridgeTest.php | 4 + tests/Unit/Event/ArrayFilterEventTest.php | 2 + tests/Util/FakeEventDispatcher.php | 31 ++++++ .../Storage/Repository/StorageManagerTest.php | 103 +++++++++++++++--- 7 files changed, 149 insertions(+), 17 deletions(-) create mode 100644 tests/Util/FakeEventDispatcher.php diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 18a41a1730..6dc9d01e20 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -68,6 +68,8 @@ final class HookEventBridge ArrayFilterEvent::BLOCK_CONTACT => 'block', ArrayFilterEvent::UNBLOCK_CONTACT => 'unblock', ArrayFilterEvent::ADD_WORKER_TASK => 'proc_run', + ArrayFilterEvent::STORAGE_CONFIG => 'storage_config', + ArrayFilterEvent::STORAGE_INSTANCE => 'storage_instance', HtmlFilterEvent::HEAD => 'head', HtmlFilterEvent::FOOTER => 'footer', HtmlFilterEvent::PAGE_HEADER => 'page_header', @@ -116,6 +118,8 @@ final class HookEventBridge ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', + ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent', + ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', diff --git a/src/Core/Storage/Repository/StorageManager.php b/src/Core/Storage/Repository/StorageManager.php index bbd9e13f14..1de89dbddc 100644 --- a/src/Core/Storage/Repository/StorageManager.php +++ b/src/Core/Storage/Repository/StorageManager.php @@ -20,7 +20,9 @@ use Friendica\Core\Storage\Capability\ICanWriteToStorage; use Friendica\Database\Database; use Friendica\Core\Storage\Type; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Network\HTTPException\InternalServerErrorException; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** @@ -55,6 +57,7 @@ class StorageManager private $config; /** @var LoggerInterface */ private $logger; + private EventDispatcherInterface $eventDispatcher; /** @var L10n */ private $l10n; @@ -71,11 +74,12 @@ class StorageManager * @throws InvalidClassStorageException in case the active backend class is invalid * @throws StorageException in case of unexpected errors during the active backend class loading */ - public function __construct(Database $dba, IManageConfigValues $config, LoggerInterface $logger, L10n $l10n, bool $includeAddon = true) + public function __construct(Database $dba, IManageConfigValues $config, LoggerInterface $logger, EventDispatcherInterface $eventDispatcher, L10n $l10n, bool $includeAddon = true) { $this->dba = $dba; $this->config = $config; $this->logger = $logger; + $this->eventDispatcher = $eventDispatcher; $this->l10n = $l10n; $this->validBackends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS); @@ -146,8 +150,12 @@ class StorageManager 'name' => $name, 'storage_config' => null, ]; + try { - Hook::callAll('storage_config', $data); + $data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::STORAGE_CONFIG, $data), + )->getArray(); + if (!($data['storage_config'] ?? null) instanceof ICanConfigureStorage) { throw new InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name)); } @@ -201,8 +209,12 @@ class StorageManager 'name' => $name, 'storage' => null, ]; + try { - Hook::callAll('storage_instance', $data); + $data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::STORAGE_INSTANCE, $data), + )->getArray(); + if (!($data['storage'] ?? null) instanceof ICanReadFromStorage) { throw new InvalidClassStorageException(sprintf('Backend %s was not found', $name)); } diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 6967524f86..4a08ba4fa3 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -74,6 +74,10 @@ final class ArrayFilterEvent extends Event public const ADD_WORKER_TASK = 'friendica.data.add_worker_task'; + public const STORAGE_CONFIG = 'friendica.data.storage_config'; + + public const STORAGE_INSTANCE = 'friendica.data.storage_instance'; + private array $array; public function __construct(string $name, array $array) diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index d7df82e4d1..8ae803bdab 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -57,6 +57,8 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', + ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent', + ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', @@ -290,6 +292,8 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::BLOCK_CONTACT, 'block'], [ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'], [ArrayFilterEvent::ADD_WORKER_TASK, 'proc_run'], + [ArrayFilterEvent::STORAGE_CONFIG, 'storage_config'], + [ArrayFilterEvent::STORAGE_INSTANCE, 'storage_instance'], ]; } diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 2afebba496..c57b45ceb1 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -53,6 +53,8 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::BLOCK_CONTACT, 'friendica.data.block_contact'], [ArrayFilterEvent::UNBLOCK_CONTACT, 'friendica.data.unblock_contact'], [ArrayFilterEvent::ADD_WORKER_TASK, 'friendica.data.add_worker_task'], + [ArrayFilterEvent::STORAGE_CONFIG, 'friendica.data.storage_config'], + [ArrayFilterEvent::STORAGE_INSTANCE, 'friendica.data.storage_instance'], ]; } diff --git a/tests/Util/FakeEventDispatcher.php b/tests/Util/FakeEventDispatcher.php new file mode 100644 index 0000000000..070e8e77d9 --- /dev/null +++ b/tests/Util/FakeEventDispatcher.php @@ -0,0 +1,31 @@ +at($this->root); - $this->logger = new NullLogger(); $this->database = $this->getDbInstance(); $configFactory = new Config(); @@ -87,7 +85,14 @@ class StorageManagerTest extends DatabaseTestCase */ public function testInstance() { - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + new FakeEventDispatcher(), + $this->l10n, + false + ); self::assertInstanceOf(StorageManager::class, $storageManager); } @@ -149,7 +154,14 @@ class StorageManagerTest extends DatabaseTestCase $this->config->set('storage', 'name', $name); } - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + new FakeEventDispatcher(), + $this->l10n, + false + ); if ($interface === ICanWriteToStorage::class) { $storage = $storageManager->getWritableStorageByName($name); @@ -169,7 +181,14 @@ class StorageManagerTest extends DatabaseTestCase */ public function testIsValidBackend($name, $valid, $interface, $assert, $assertName) { - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + new FakeEventDispatcher(), + $this->l10n, + false + ); // true in every of the backends self::assertEquals(!empty($assertName), $storageManager->isValidBackend($name)); @@ -183,7 +202,14 @@ class StorageManagerTest extends DatabaseTestCase */ public function testListBackends() { - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + new FakeEventDispatcher(), + $this->l10n, + false + ); self::assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends()); } @@ -199,7 +225,14 @@ class StorageManagerTest extends DatabaseTestCase static::markTestSkipped('only works for ICanWriteToStorage'); } - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + new FakeEventDispatcher(), + $this->l10n, + false + ); $selBackend = $storageManager->getWritableStorageByName($name); $storageManager->setBackend($selBackend); @@ -219,7 +252,14 @@ class StorageManagerTest extends DatabaseTestCase $this->expectException(InvalidClassStorageException::class); } - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + new FakeEventDispatcher(), + $this->l10n, + false + ); self::assertInstanceOf($assert, $storageManager->getBackend()); } @@ -240,7 +280,14 @@ class StorageManagerTest extends DatabaseTestCase ->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null]); DI::init($dice); - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + new FakeEventDispatcher(), + $this->l10n, + false + ); self::assertTrue($storageManager->register(SampleStorageBackend::class)); @@ -268,7 +315,21 @@ class StorageManagerTest extends DatabaseTestCase ->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null]); DI::init($dice); - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + /** @var \Friendica\Event\EventDispatcher */ + $eventDispatcher = DI::eventDispatcher(); + + foreach (HookEventBridge::getStaticSubscribedEvents() as $eventName => $methodName) { + $eventDispatcher->addListener($eventName, [HookEventBridge::class, $methodName]); + } + + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + $eventDispatcher, + $this->l10n, + false + ); self::assertTrue($storageManager->register(SampleStorageBackend::class)); @@ -307,7 +368,14 @@ class StorageManagerTest extends DatabaseTestCase $this->loadFixture(__DIR__ . '/../../../../datasets/storage/database.fixture.php', $this->database); - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + new FakeEventDispatcher(), + $this->l10n, + false + ); $storage = $storageManager->getWritableStorageByName($name); $storageManager->move($storage); @@ -331,7 +399,14 @@ class StorageManagerTest extends DatabaseTestCase $this->expectException(InvalidClassStorageException::class); $this->expectExceptionMessage('Backend SystemResource is not valid'); - $storageManager = new StorageManager($this->database, $this->config, $this->logger, $this->l10n, false); + $storageManager = new StorageManager( + $this->database, + $this->config, + new NullLogger(), + new FakeEventDispatcher(), + $this->l10n, + false + ); $storage = $storageManager->getWritableStorageByName(SystemResource::getName()); $storageManager->move($storage); } From 04818781a79d28292f5fff7f487a73225c3d3099 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Mar 2025 10:14:23 +0000 Subject: [PATCH 05/45] Fix code style --- tests/Util/FakeEventDispatcher.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Util/FakeEventDispatcher.php b/tests/Util/FakeEventDispatcher.php index 070e8e77d9..434e9ba5a6 100644 --- a/tests/Util/FakeEventDispatcher.php +++ b/tests/Util/FakeEventDispatcher.php @@ -16,15 +16,15 @@ use Psr\EventDispatcher\EventDispatcherInterface; */ final class FakeEventDispatcher implements EventDispatcherInterface { - /** - * Provide all relevant listeners with an event to process. - * - * @template T of object + /** + * Provide all relevant listeners with an event to process. + * + * @template T of object * @param T $event * * @return T The passed $event MUST be returned - */ - public function dispatch(object $event): object + */ + public function dispatch(object $event): object { return $event; } From 9e3b0b3c40706bddb5f0c96a294c61ee5539fb87 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Mar 2025 12:24:02 +0000 Subject: [PATCH 06/45] Create event for dbstructure_definition hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Database/Definition/DbaDefinition.php | 10 +++++++--- src/Event/ArrayFilterEvent.php | 2 ++ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 2 ++ 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 6dc9d01e20..7c23e333f1 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -70,6 +70,7 @@ final class HookEventBridge ArrayFilterEvent::ADD_WORKER_TASK => 'proc_run', ArrayFilterEvent::STORAGE_CONFIG => 'storage_config', ArrayFilterEvent::STORAGE_INSTANCE => 'storage_instance', + ArrayFilterEvent::DB_STRUCTURE_DEFINITION => 'dbstructure_definition', HtmlFilterEvent::HEAD => 'head', HtmlFilterEvent::FOOTER => 'footer', HtmlFilterEvent::PAGE_HEADER => 'page_header', @@ -120,6 +121,7 @@ final class HookEventBridge ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent', + ArrayFilterEvent::DB_STRUCTURE_DEFINITION => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', diff --git a/src/Database/Definition/DbaDefinition.php b/src/Database/Definition/DbaDefinition.php index 378ba86867..ecc75f6aa3 100644 --- a/src/Database/Definition/DbaDefinition.php +++ b/src/Database/Definition/DbaDefinition.php @@ -8,9 +8,9 @@ namespace Friendica\Database\Definition; use Exception; -use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Hook; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; /** * Stores the whole database definition @@ -109,12 +109,16 @@ class DbaDefinition { $definition = require $this->configFile; - if (!$definition) { + if (!is_array($definition)) { throw new Exception('Corrupted database structure config file static/dbstructure.config.php'); } if ($withAddonStructure) { - Hook::callAll('dbstructure_definition', $definition); + $eventDispatcher = DI::eventDispatcher(); + + $definition = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::DB_STRUCTURE_DEFINITION, $definition), + )->getArray(); } $this->definition = $definition; diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 4a08ba4fa3..f8e19bdad5 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -78,6 +78,8 @@ final class ArrayFilterEvent extends Event public const STORAGE_INSTANCE = 'friendica.data.storage_instance'; + public const DB_STRUCTURE_DEFINITION = 'friendica.data.db_structure_definition'; + private array $array; public function __construct(string $name, array $array) diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 8ae803bdab..ca0cb2c160 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -59,6 +59,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent', + ArrayFilterEvent::DB_STRUCTURE_DEFINITION => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', @@ -294,6 +295,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::ADD_WORKER_TASK, 'proc_run'], [ArrayFilterEvent::STORAGE_CONFIG, 'storage_config'], [ArrayFilterEvent::STORAGE_INSTANCE, 'storage_instance'], + [ArrayFilterEvent::DB_STRUCTURE_DEFINITION, 'dbstructure_definition'], ]; } diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index c57b45ceb1..67b2e8a576 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -42,6 +42,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PAGE_INFO, 'friendica.data.page_info'], [ArrayFilterEvent::SMILEY_LIST, 'friendica.data.smiley_list'], [ArrayFilterEvent::BBCODE_TO_HTML_START, 'friendica.data.bbcode_to_html_start'], + [ArrayFilterEvent::HTML_TO_BBCODE_END, 'friendica.data.html_to_bbcode_end'], [ArrayFilterEvent::BBCODE_TO_MARKDOWN_END, 'friendica.data.bbcode_to_markdown_end'], [ArrayFilterEvent::JOT_NETWORKS, 'friendica.data.jot_networks'], [ArrayFilterEvent::PROTOCOL_SUPPORTS_FOLLOW, 'friendica.data.protocol_supports_follow'], @@ -55,6 +56,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::ADD_WORKER_TASK, 'friendica.data.add_worker_task'], [ArrayFilterEvent::STORAGE_CONFIG, 'friendica.data.storage_config'], [ArrayFilterEvent::STORAGE_INSTANCE, 'friendica.data.storage_instance'], + [ArrayFilterEvent::DB_STRUCTURE_DEFINITION, 'friendica.data.db_structure_definition'], ]; } From 9cf0ce0b99831f66f78c47752944732be61f3055 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Mar 2025 13:02:39 +0000 Subject: [PATCH 07/45] create event for dbview_definition hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Database/Definition/DbaDefinition.php | 1 - src/Database/Definition/ViewDefinition.php | 11 ++++++++--- src/Event/ArrayFilterEvent.php | 2 ++ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 6 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 7c23e333f1..6c477dae36 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -71,6 +71,7 @@ final class HookEventBridge ArrayFilterEvent::STORAGE_CONFIG => 'storage_config', ArrayFilterEvent::STORAGE_INSTANCE => 'storage_instance', ArrayFilterEvent::DB_STRUCTURE_DEFINITION => 'dbstructure_definition', + ArrayFilterEvent::DB_VIEW_DEFINITION => 'dbview_definition', HtmlFilterEvent::HEAD => 'head', HtmlFilterEvent::FOOTER => 'footer', HtmlFilterEvent::PAGE_HEADER => 'page_header', @@ -122,6 +123,7 @@ final class HookEventBridge ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent', ArrayFilterEvent::DB_STRUCTURE_DEFINITION => 'onArrayFilterEvent', + ArrayFilterEvent::DB_VIEW_DEFINITION => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', diff --git a/src/Database/Definition/DbaDefinition.php b/src/Database/Definition/DbaDefinition.php index ecc75f6aa3..c5d77d3a46 100644 --- a/src/Database/Definition/DbaDefinition.php +++ b/src/Database/Definition/DbaDefinition.php @@ -8,7 +8,6 @@ namespace Friendica\Database\Definition; use Exception; -use Friendica\Core\Hook; use Friendica\DI; use Friendica\Event\ArrayFilterEvent; diff --git a/src/Database/Definition/ViewDefinition.php b/src/Database/Definition/ViewDefinition.php index b64e0d0ffc..3c6cb2f43a 100644 --- a/src/Database/Definition/ViewDefinition.php +++ b/src/Database/Definition/ViewDefinition.php @@ -8,7 +8,8 @@ namespace Friendica\Database\Definition; use Exception; -use Friendica\Core\Hook; +use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; /** * Stores the whole View definitions @@ -62,12 +63,16 @@ class ViewDefinition { $definition = require $this->configFile; - if (!$definition) { + if (!is_array($definition)) { throw new Exception('Corrupted database structure config file static/dbstructure.config.php'); } if ($withAddonStructure) { - Hook::callAll('dbview_definition', $definition); + $eventDispatcher = DI::eventDispatcher(); + + $definition = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::DB_VIEW_DEFINITION, $definition), + )->getArray(); } $this->definition = $definition; diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index f8e19bdad5..f62d7f02c6 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -80,6 +80,8 @@ final class ArrayFilterEvent extends Event public const DB_STRUCTURE_DEFINITION = 'friendica.data.db_structure_definition'; + public const DB_VIEW_DEFINITION = 'friendica.data.db_view_definition'; + private array $array; public function __construct(string $name, array $array) diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index ca0cb2c160..f496467e54 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -60,6 +60,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent', ArrayFilterEvent::DB_STRUCTURE_DEFINITION => 'onArrayFilterEvent', + ArrayFilterEvent::DB_VIEW_DEFINITION => 'onArrayFilterEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', @@ -296,6 +297,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::STORAGE_CONFIG, 'storage_config'], [ArrayFilterEvent::STORAGE_INSTANCE, 'storage_instance'], [ArrayFilterEvent::DB_STRUCTURE_DEFINITION, 'dbstructure_definition'], + [ArrayFilterEvent::DB_VIEW_DEFINITION, 'dbview_definition'], ]; } diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 67b2e8a576..e55fa313a6 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -57,6 +57,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::STORAGE_CONFIG, 'friendica.data.storage_config'], [ArrayFilterEvent::STORAGE_INSTANCE, 'friendica.data.storage_instance'], [ArrayFilterEvent::DB_STRUCTURE_DEFINITION, 'friendica.data.db_structure_definition'], + [ArrayFilterEvent::DB_VIEW_DEFINITION, 'friendica.data.db_view_definition'], ]; } From d34861ee9691dd0468e8de532b9513acce26c3f4 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Mar 2025 13:23:21 +0000 Subject: [PATCH 08/45] Create event for contact_photo_menu hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Model/Contact.php | 12 ++++++++++-- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 6c477dae36..6f61c58c72 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -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::CONTACT_PHOTO_MENU => 'contact_photo_menu', ArrayFilterEvent::OEMBED_FETCH_END => 'oembed_fetch_url', ArrayFilterEvent::PAGE_INFO => 'page_info_data', ArrayFilterEvent::SMILEY_LIST => 'smilie', @@ -104,6 +105,7 @@ final class HookEventBridge ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index f62d7f02c6..056cdec470 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -42,6 +42,8 @@ final class ArrayFilterEvent extends Event public const ITEM_PHOTO_MENU = 'friendica.data.item_photo_menu'; + public const CONTACT_PHOTO_MENU = 'friendica.data.contact_photo_menu'; + public const OEMBED_FETCH_END = 'friendica.data.oembed_fetch_end'; public const PAGE_INFO = 'friendica.data.page_info'; diff --git a/src/Model/Contact.php b/src/Model/Contact.php index e94ef46964..845c8be4ee 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1312,9 +1312,17 @@ class Contact } } - $args = ['contact' => $contact, 'menu' => &$menu]; + $args = ['contact' => $contact, 'menu' => $menu]; - Hook::callAll('contact_photo_menu', $args); + $eventDispatcher = DI::eventDispatcher(); + + $args = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::CONTACT_PHOTO_MENU, $args), + )->getArray(); + + if (is_array($args['menu'])) { + $menu = $args['menu']; + } $menucondensed = []; diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index f496467e54..949401f364 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -41,6 +41,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', @@ -282,6 +283,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::DISPLAY_ITEM, 'display_item'], [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], + [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'contact_photo_menu'], [ArrayFilterEvent::PAGE_INFO, 'page_info_data'], [ArrayFilterEvent::SMILEY_LIST, 'smilie'], [ArrayFilterEvent::JOT_NETWORKS, 'jot_networks'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index e55fa313a6..2e539e290e 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -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::CONTACT_PHOTO_MENU, 'friendica.data.contact_photo_menu'], [ArrayFilterEvent::OEMBED_FETCH_END, 'friendica.data.oembed_fetch_end'], [ArrayFilterEvent::PAGE_INFO, 'friendica.data.page_info'], [ArrayFilterEvent::SMILEY_LIST, 'friendica.data.smiley_list'], From 68b604812cc9d2a80ee4ffaf0732c994c44af924 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Mar 2025 13:31:11 +0000 Subject: [PATCH 09/45] create event for avatar_lookup hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Model/Contact.php | 7 +++++-- src/Util/Network.php | 8 ++++++-- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 6f61c58c72..cc2e8be225 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -68,6 +68,7 @@ final class HookEventBridge ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'revoke_follow', ArrayFilterEvent::BLOCK_CONTACT => 'block', ArrayFilterEvent::UNBLOCK_CONTACT => 'unblock', + ArrayFilterEvent::AVATAR_LOOKUP => 'avatar_lookup', ArrayFilterEvent::ADD_WORKER_TASK => 'proc_run', ArrayFilterEvent::STORAGE_CONFIG => 'storage_config', ArrayFilterEvent::STORAGE_INSTANCE => 'storage_instance', @@ -121,6 +122,7 @@ final class HookEventBridge ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent', ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 056cdec470..ac72f3d29a 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -74,6 +74,8 @@ final class ArrayFilterEvent extends Event public const UNBLOCK_CONTACT = 'friendica.data.unblock_contact'; + public const AVATAR_LOOKUP = 'friendica.data.avatar_lookup'; + public const ADD_WORKER_TASK = 'friendica.data.add_worker_task'; public const STORAGE_CONFIG = 'friendica.data.storage_config'; diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 845c8be4ee..4684317e76 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -14,7 +14,6 @@ use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException; use Friendica\Content\Conversation as ConversationContent; use Friendica\Content\Pager; use Friendica\Content\Text\HTML; -use Friendica\Core\Hook; use Friendica\Core\Protocol; use Friendica\Core\Renderer; use Friendica\Core\System; @@ -2205,7 +2204,11 @@ class Contact $avatar['url'] = ''; $avatar['success'] = false; - Hook::callAll('avatar_lookup', $avatar); + $eventDispatcher = DI::eventDispatcher(); + + $avatar = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::AVATAR_LOOKUP, $avatar), + )->getArray(); if ($avatar['success'] && !empty($avatar['url'])) { return $avatar['url']; diff --git a/src/Util/Network.php b/src/Util/Network.php index 1584faea3f..2172c5a177 100644 --- a/src/Util/Network.php +++ b/src/Util/Network.php @@ -7,8 +7,8 @@ namespace Friendica\Util; -use Friendica\Core\Hook; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; @@ -302,7 +302,11 @@ class Network $avatar['url'] = ''; $avatar['success'] = false; - Hook::callAll('avatar_lookup', $avatar); + $eventDispatcher = DI::eventDispatcher(); + + $avatar = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::AVATAR_LOOKUP, $avatar), + )->getArray(); if (! $avatar['success']) { $avatar['url'] = DI::baseUrl() . Contact::DEFAULT_AVATAR_PHOTO; diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 949401f364..77e0b1ecfc 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -57,6 +57,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent', ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_CONFIG => 'onArrayFilterEvent', ArrayFilterEvent::STORAGE_INSTANCE => 'onArrayFilterEvent', @@ -295,6 +296,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::REVOKE_FOLLOW_CONTACT, 'revoke_follow'], [ArrayFilterEvent::BLOCK_CONTACT, 'block'], [ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'], + [ArrayFilterEvent::AVATAR_LOOKUP, 'avatar_lookup'], [ArrayFilterEvent::ADD_WORKER_TASK, 'proc_run'], [ArrayFilterEvent::STORAGE_CONFIG, 'storage_config'], [ArrayFilterEvent::STORAGE_INSTANCE, 'storage_instance'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 2e539e290e..0e0c147216 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -54,6 +54,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::REVOKE_FOLLOW_CONTACT, 'friendica.data.revoke_follow_contact'], [ArrayFilterEvent::BLOCK_CONTACT, 'friendica.data.block_contact'], [ArrayFilterEvent::UNBLOCK_CONTACT, 'friendica.data.unblock_contact'], + [ArrayFilterEvent::AVATAR_LOOKUP, 'friendica.data.avatar_lookup'], [ArrayFilterEvent::ADD_WORKER_TASK, 'friendica.data.add_worker_task'], [ArrayFilterEvent::STORAGE_CONFIG, 'friendica.data.storage_config'], [ArrayFilterEvent::STORAGE_INSTANCE, 'friendica.data.storage_instance'], From abe35732d85f93c80ddf5b91467f645598278783 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Mar 2025 14:11:58 +0000 Subject: [PATCH 10/45] create event for event_created and event_updated hooks --- src/Core/Hook.php | 4 +- src/Core/Hooks/HookEventBridge.php | 34 ++++++++++++++++- src/Event/ArrayFilterEvent.php | 4 ++ src/Model/Event.php | 10 ++++- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 38 +++++++++++++++++++ tests/Unit/Event/ArrayFilterEventTest.php | 2 + 6 files changed, 86 insertions(+), 6 deletions(-) diff --git a/src/Core/Hook.php b/src/Core/Hook.php index 44dfad70bc..c783f0a226 100644 --- a/src/Core/Hook.php +++ b/src/Core/Hook.php @@ -171,8 +171,8 @@ class Hook * 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 */ diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index cc2e8be225..1567b3e20a 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -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) { diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index ac72f3d29a..b3be9f70fa 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -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'; diff --git a/src/Model/Event.php b/src/Model/Event.php index 6cb73f4c37..debeb74d45 100644 --- a/src/Model/Event.php +++ b/src/Model/Event.php @@ -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']; diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 77e0b1ecfc..1852248ebb 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -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'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 0e0c147216..0e4467c1ed 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -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'], From 9aeb68231d98bd4ad5a2b798c74d4d7d8436136b Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Mar 2025 15:35:53 +0000 Subject: [PATCH 11/45] Create events for post_remote and post_remote_end hook --- src/Core/Hooks/HookEventBridge.php | 4 ++++ src/Event/ArrayFilterEvent.php | 4 ++++ src/Model/Event.php | 1 - src/Model/Item.php | 10 ++++++++-- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 4 ++++ tests/Unit/Event/ArrayFilterEventTest.php | 2 ++ 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 1567b3e20a..18a2754e33 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -46,6 +46,8 @@ final class HookEventBridge ArrayFilterEvent::POST_LOCAL_START => 'post_local_start', ArrayFilterEvent::POST_LOCAL => 'post_local', ArrayFilterEvent::POST_LOCAL_END => 'post_local_end', + ArrayFilterEvent::INSERT_POST_REMOTE => 'post_remote', + ArrayFilterEvent::INSERT_POST_REMOTE_END => 'post_remote_end', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', ArrayFilterEvent::NETWORK_TO_NAME => 'network_to_name', ArrayFilterEvent::CONVERSATION_START => 'conversation_start', @@ -102,6 +104,8 @@ final class HookEventBridge ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index b3be9f70fa..0ab06525bd 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -30,6 +30,10 @@ final class ArrayFilterEvent extends Event public const POST_LOCAL_END = 'friendica.data.post_local_end'; + public const INSERT_POST_REMOTE = 'friendica.data.insert_post_remote'; + + public const INSERT_POST_REMOTE_END = 'friendica.data.insert_post_remote_end'; + public const PHOTO_UPLOAD_FORM = 'friendica.data.photo_upload_form'; public const NETWORK_TO_NAME = 'friendica.data.network_to_name'; diff --git a/src/Model/Event.php b/src/Model/Event.php index debeb74d45..125692dfe1 100644 --- a/src/Model/Event.php +++ b/src/Model/Event.php @@ -9,7 +9,6 @@ namespace Friendica\Model; use Friendica\Content\Feature; use Friendica\Content\Text\BBCode; -use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\Core\System; use Friendica\Database\DBA; diff --git a/src/Model/Item.php b/src/Model/Item.php index 31f8a8e22e..99ebba8cae 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -855,7 +855,9 @@ class Item unset($_SESSION['uid']); } } elseif (!$notify) { - Hook::callAll('post_remote', $item); + $item = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_REMOTE, $item) + )->getArray(); } if (!empty($item['cancel'])) { @@ -1112,7 +1114,11 @@ class Item DI::contentItem()->copyPermissions($posted_item['thr-parent-id'], $posted_item['uri-id'], $posted_item['parent-uri-id']); } } else { - Hook::callAll('post_remote_end', $posted_item); + $eventDispatcher = DI::eventDispatcher(); + + $posted_item = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_REMOTE_END, $posted_item) + )->getArray(); } if ($posted_item['gravity'] === self::GRAVITY_PARENT) { diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 1852248ebb..69c49f3513 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -35,6 +35,8 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', @@ -314,6 +316,8 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::POST_LOCAL_START, 'post_local_start'], [ArrayFilterEvent::POST_LOCAL, 'post_local'], [ArrayFilterEvent::POST_LOCAL_END, 'post_local_end'], + [ArrayFilterEvent::INSERT_POST_REMOTE, 'post_remote'], + [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'post_remote_end'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'conversation_start'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 0e4467c1ed..193ccc77d8 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -32,6 +32,8 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::POST_LOCAL_START, 'friendica.data.post_local_start'], [ArrayFilterEvent::POST_LOCAL, 'friendica.data.post_local'], [ArrayFilterEvent::POST_LOCAL_END, 'friendica.data.post_local_end'], + [ArrayFilterEvent::INSERT_POST_REMOTE, 'friendica.data.insert_post_remote'], + [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'friendica.data.insert_post_remote_end'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'friendica.data.photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'friendica.data.network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'friendica.data.conversation_start'], From 9c6f5f222d1daffe1254308d9ca6f1a18b9497d5 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Mar 2025 15:45:36 +0000 Subject: [PATCH 12/45] Rename events to insert local post --- mod/item.php | 4 ++-- src/Content/Item.php | 2 +- src/Core/Hooks/HookEventBridge.php | 12 ++++++------ src/Event/ArrayFilterEvent.php | 6 +++--- src/Model/Item.php | 2 +- src/Module/Post/Tag/Add.php | 2 +- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 12 ++++++------ tests/Unit/Event/ArrayFilterEventTest.php | 6 +++--- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/mod/item.php b/mod/item.php index 46f2a219e3..e6b2cb93f4 100644 --- a/mod/item.php +++ b/mod/item.php @@ -46,7 +46,7 @@ function item_post() $eventDispatcher = DI::eventDispatcher(); $_REQUEST = $eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL_START, $_REQUEST) + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_START, $_REQUEST) )->getArray(); $return_path = $_REQUEST['return'] ?? ''; @@ -282,7 +282,7 @@ function item_process(array $post, array $request, bool $preview, string $return $eventDispatcher = DI::eventDispatcher(); $post = $eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL, $post) + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $post) )->getArray(); unset($post['edit']); diff --git a/src/Content/Item.php b/src/Content/Item.php index 9c9cd10bed..af26ebd8ed 100644 --- a/src/Content/Item.php +++ b/src/Content/Item.php @@ -1012,7 +1012,7 @@ class Item } $post = $this->eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL_END, $post) + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $post) )->getArray(); $author = DBA::selectFirst('contact', ['thumb'], ['uid' => $post['uid'], 'self' => true]); diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 18a2754e33..251c462929 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -43,9 +43,9 @@ final class HookEventBridge ArrayFilterEvent::NAV_INFO => 'nav_info', ArrayFilterEvent::FEATURE_ENABLED => 'isEnabled', ArrayFilterEvent::FEATURE_GET => 'get', - ArrayFilterEvent::POST_LOCAL_START => 'post_local_start', - ArrayFilterEvent::POST_LOCAL => 'post_local', - ArrayFilterEvent::POST_LOCAL_END => 'post_local_end', + ArrayFilterEvent::INSERT_POST_LOCAL_START => 'post_local_start', + ArrayFilterEvent::INSERT_POST_LOCAL => 'post_local', + ArrayFilterEvent::INSERT_POST_LOCAL_END => 'post_local_end', ArrayFilterEvent::INSERT_POST_REMOTE => 'post_remote', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'post_remote_end', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', @@ -101,9 +101,9 @@ final class HookEventBridge ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 0ab06525bd..51719e7de9 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -24,11 +24,11 @@ final class ArrayFilterEvent extends Event public const FEATURE_GET = 'friendica.data.feature_get'; - public const POST_LOCAL_START = 'friendica.data.post_local_start'; + public const INSERT_POST_LOCAL_START = 'friendica.data.insert_post_local_start'; - public const POST_LOCAL = 'friendica.data.post_local'; + public const INSERT_POST_LOCAL = 'friendica.data.insert_post_local'; - public const POST_LOCAL_END = 'friendica.data.post_local_end'; + public const INSERT_POST_LOCAL_END = 'friendica.data.insert_post_local_end'; public const INSERT_POST_REMOTE = 'friendica.data.insert_post_remote'; diff --git a/src/Model/Item.php b/src/Model/Item.php index 99ebba8cae..d2169d3156 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -847,7 +847,7 @@ class Item } $item = $eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL, $item) + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $item) )->getArray(); if ($dummy_session) { diff --git a/src/Module/Post/Tag/Add.php b/src/Module/Post/Tag/Add.php index bf704a50c0..e03bf215a3 100644 --- a/src/Module/Post/Tag/Add.php +++ b/src/Module/Post/Tag/Add.php @@ -154,7 +154,7 @@ EOT; $post['id'] = $post_id; $post = $this->eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL_END, $post) + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $post) )->getArray(); $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 69c49f3513..325aefb742 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -32,9 +32,9 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL_START => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL => 'onArrayFilterEvent', - ArrayFilterEvent::POST_LOCAL_END => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', @@ -313,9 +313,9 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::NAV_INFO, 'nav_info'], [ArrayFilterEvent::FEATURE_ENABLED, 'isEnabled'], [ArrayFilterEvent::FEATURE_GET, 'get'], - [ArrayFilterEvent::POST_LOCAL_START, 'post_local_start'], - [ArrayFilterEvent::POST_LOCAL, 'post_local'], - [ArrayFilterEvent::POST_LOCAL_END, 'post_local_end'], + [ArrayFilterEvent::INSERT_POST_LOCAL_START, 'post_local_start'], + [ArrayFilterEvent::INSERT_POST_LOCAL, 'post_local'], + [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'post_local_end'], [ArrayFilterEvent::INSERT_POST_REMOTE, 'post_remote'], [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'post_remote_end'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 193ccc77d8..7c124f0b33 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -29,9 +29,9 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::NAV_INFO, 'friendica.data.nav_info'], [ArrayFilterEvent::FEATURE_ENABLED, 'friendica.data.feature_enabled'], [ArrayFilterEvent::FEATURE_GET, 'friendica.data.feature_get'], - [ArrayFilterEvent::POST_LOCAL_START, 'friendica.data.post_local_start'], - [ArrayFilterEvent::POST_LOCAL, 'friendica.data.post_local'], - [ArrayFilterEvent::POST_LOCAL_END, 'friendica.data.post_local_end'], + [ArrayFilterEvent::INSERT_POST_LOCAL_START, 'friendica.data.insert_post_local_start'], + [ArrayFilterEvent::INSERT_POST_LOCAL, 'friendica.data.insert_post_local'], + [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'friendica.data.insert_post_local_end'], [ArrayFilterEvent::INSERT_POST_REMOTE, 'friendica.data.insert_post_remote'], [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'friendica.data.insert_post_remote_end'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'friendica.data.photo_upload_form'], From d48e491144e8a5fd59a7d811923e9ac2774c6b1d Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 17 Mar 2025 08:26:45 +0000 Subject: [PATCH 13/45] Create events for prepare_body hooks --- src/Core/Hooks/HookEventBridge.php | 22 +++++++++ src/Event/ArrayFilterEvent.php | 20 +++++++++ src/Model/Item.php | 45 ++++++++++++++----- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 29 ++++++++++++ tests/Unit/Event/ArrayFilterEventTest.php | 4 ++ 5 files changed, 108 insertions(+), 12 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 251c462929..8d53df417f 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -48,6 +48,10 @@ final class HookEventBridge ArrayFilterEvent::INSERT_POST_LOCAL_END => 'post_local_end', ArrayFilterEvent::INSERT_POST_REMOTE => 'post_remote', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'post_remote_end', + ArrayFilterEvent::PREPARE_POST_START => 'prepare_body_init', + ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT => 'prepare_body_content_filter', + ArrayFilterEvent::PREPARE_POST => 'prepare_body', + ArrayFilterEvent::PREPARE_POST_END => 'prepare_body_final', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', ArrayFilterEvent::NETWORK_TO_NAME => 'network_to_name', ArrayFilterEvent::CONVERSATION_START => 'conversation_start', @@ -106,6 +110,10 @@ final class HookEventBridge ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', + ArrayFilterEvent::PREPARE_POST_START => 'onPreparePostStartEvent', + ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT => 'onArrayFilterEvent', + ArrayFilterEvent::PREPARE_POST => 'onArrayFilterEvent', + ArrayFilterEvent::PREPARE_POST_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', @@ -163,6 +171,20 @@ final class HookEventBridge ); } + /** + * Map the PREPARE_POST_START event to `prepare_body_init` hook + */ + public static function onPreparePostStartEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $item = (array) $data['item'] ?? []; + + $data['item'] = static::callHook($event->getName(), $item); + + $event->setArray($data); + } + /** * Map the OEMBED_FETCH_END event to `oembed_fetch_url` hook */ diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 51719e7de9..f29acad952 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -34,6 +34,26 @@ final class ArrayFilterEvent extends Event public const INSERT_POST_REMOTE_END = 'friendica.data.insert_post_remote_end'; + /** + * item array before any work + */ + public const PREPARE_POST_START = 'friendica.data.prepare_post_start'; + + /** + * before first bbcode to html + */ + public const PREPARE_POST_FILTER_CONTENT = 'friendica.data.prepare_post_filter_content'; + + /** + * after first bbcode to html + */ + public const PREPARE_POST = 'friendica.data.prepare_post'; + + /** + * after attach icons and blockquote special case handling (spoiler, author) + */ + public const PREPARE_POST_END = 'friendica.data.prepare_post_end'; + public const PHOTO_UPLOAD_FORM = 'friendica.data.photo_upload_form'; public const NETWORK_TO_NAME = 'friendica.data.network_to_name'; diff --git a/src/Model/Item.php b/src/Model/Item.php index d2169d3156..6c8be7528e 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3058,16 +3058,22 @@ class Item * @return string item body html * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException - * @hook prepare_body_init item array before any work - * @hook prepare_body_content_filter ('item'=>item array, 'filter_reasons'=>string array) before first bbcode to html - * @hook prepare_body ('item'=>item array, 'html'=>body string, 'is_preview'=>boolean, 'filter_reasons'=>string array) after first bbcode to html - * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author) */ public static function prepareBody(array &$item, bool $attach = false, bool $is_preview = false, bool $only_cache = false): string { - $appHelper = DI::appHelper(); - $uid = DI::userSession()->getLocalUserId(); - Hook::callAll('prepare_body_init', $item); + $appHelper = DI::appHelper(); + $uid = DI::userSession()->getLocalUserId(); + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = [ + 'item' => $item, + ]; + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_START, $hook_data), + )->getArray(); + + $item = $hook_data['item'] ?? $item; // In order to provide theme developers more possibilities, event items // are treated differently. @@ -3186,7 +3192,11 @@ class Item 'item' => $item, 'filter_reasons' => $filter_reasons ]; - Hook::callAll('prepare_body_content_filter', $hook_data); + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT, $hook_data), + )->getArray(); + $filter_reasons = $hook_data['filter_reasons']; unset($hook_data); } @@ -3205,7 +3215,11 @@ class Item 'preview' => $is_preview, 'filter_reasons' => $filter_reasons ]; - Hook::callAll('prepare_body', $hook_data); + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST, $hook_data), + )->getArray(); + $s = $hook_data['html']; unset($hook_data); @@ -3257,9 +3271,16 @@ class Item $s = HTML::applyContentFilter($s, $filter_reasons); - $hook_data = ['item' => $item, 'html' => $s]; - Hook::callAll('prepare_body_final', $hook_data); - return $hook_data['html']; + $hook_data = [ + 'item' => $item, + 'html' => $s, + ]; + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_END, $hook_data), + )->getArray(); + + return (string) $hook_data['html'] ?? $s; } /** diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 325aefb742..17291539a7 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -37,6 +37,10 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', + ArrayFilterEvent::PREPARE_POST_START => 'onPreparePostStartEvent', + ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT => 'onArrayFilterEvent', + ArrayFilterEvent::PREPARE_POST => 'onArrayFilterEvent', + ArrayFilterEvent::PREPARE_POST_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', @@ -183,6 +187,28 @@ class HookEventBridgeTest extends TestCase HookEventBridge::onCollectRoutesEvent($event); } + public function testOnPreparePostStartEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_START, ['item' => ['id' => -1]]); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, array $data): array { + $this->assertSame('prepare_body_init', $name); + $this->assertSame(['id' => -1], $data); + + return ['id' => 123]; + }); + + HookEventBridge::onPreparePostStartEvent($event); + + $this->assertSame( + ['item' => ['id' => 123]], + $event->getArray(), + ); + } + public function testOnOembedFetchEndEventCallsHookWithCorrectValue(): void { $event = new ArrayFilterEvent(ArrayFilterEvent::OEMBED_FETCH_END, ['url' => 'original_url']); @@ -318,6 +344,9 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'post_local_end'], [ArrayFilterEvent::INSERT_POST_REMOTE, 'post_remote'], [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'post_remote_end'], + [ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT, 'prepare_body_content_filter'], + [ArrayFilterEvent::PREPARE_POST, 'prepare_body'], + [ArrayFilterEvent::PREPARE_POST_END, 'prepare_body_final'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'conversation_start'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 7c124f0b33..5af49e9fb8 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -34,6 +34,10 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'friendica.data.insert_post_local_end'], [ArrayFilterEvent::INSERT_POST_REMOTE, 'friendica.data.insert_post_remote'], [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'friendica.data.insert_post_remote_end'], + [ArrayFilterEvent::PREPARE_POST_START, 'friendica.data.prepare_post_start'], + [ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT, 'friendica.data.prepare_post_filter_content'], + [ArrayFilterEvent::PREPARE_POST, 'friendica.data.prepare_post'], + [ArrayFilterEvent::PREPARE_POST_END, 'friendica.data.prepare_post_end'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'friendica.data.photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'friendica.data.network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'friendica.data.conversation_start'], From 84148663e265ac2400b6dd6af23c5ed0fca5412f Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 17 Mar 2025 09:17:13 +0000 Subject: [PATCH 14/45] Create event for detect_languages hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Model/Item.php | 11 ++++++++--- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 8d53df417f..2406fb7037 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -56,6 +56,7 @@ final class HookEventBridge ArrayFilterEvent::NETWORK_TO_NAME => 'network_to_name', ArrayFilterEvent::CONVERSATION_START => 'conversation_start', ArrayFilterEvent::DISPLAY_ITEM => 'display_item', + ArrayFilterEvent::DETECT_LANGUAGES => 'detect_languages', ArrayFilterEvent::RENDER_LOCATION => 'render_location', ArrayFilterEvent::ITEM_PHOTO_MENU => 'item_photo_menu', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'contact_photo_menu', @@ -118,6 +119,7 @@ final class HookEventBridge ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', + ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index f29acad952..44aa403b18 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -62,6 +62,8 @@ final class ArrayFilterEvent extends Event public const DISPLAY_ITEM = 'friendica.data.display_item'; + public const DETECT_LANGUAGES = 'friendica.data.detect_languages'; + public const RENDER_LOCATION = 'friendica.data.render_location'; public const ITEM_PHOTO_MENU = 'friendica.data.item_photo_menu'; diff --git a/src/Model/Item.php b/src/Model/Item.php index 6c8be7528e..954c0ce11c 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1903,18 +1903,23 @@ class Item $result = []; + $eventDispatcher = DI::eventDispatcher(); + foreach (self::splitByBlocks($searchtext) as $block) { $languages = $ld->detect($block)->close() ?: []; - $data = [ + $hook_data = [ 'text' => $block, 'detected' => $languages, 'uri-id' => $uri_id, 'author-id' => $author_id, ]; - Hook::callAll('detect_languages', $data); - foreach ($data['detected'] as $language => $quality) { + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::DETECT_LANGUAGES, $hook_data), + )->getArray(); + + foreach ($hook_data['detected'] as $language => $quality) { $result[$language] = max($result[$language] ?? 0, $quality * (strlen($block) / strlen($searchtext))); } } diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 17291539a7..e0e9a3eb8e 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -45,6 +45,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', + ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', @@ -351,6 +352,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'conversation_start'], [ArrayFilterEvent::DISPLAY_ITEM, 'display_item'], + [ArrayFilterEvent::DETECT_LANGUAGES, 'detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'contact_photo_menu'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 5af49e9fb8..d079cacdec 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -42,6 +42,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::NETWORK_TO_NAME, 'friendica.data.network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'friendica.data.conversation_start'], [ArrayFilterEvent::DISPLAY_ITEM, 'friendica.data.display_item'], + [ArrayFilterEvent::DETECT_LANGUAGES, 'friendica.data.detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'friendica.data.render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'friendica.data.item_photo_menu'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'friendica.data.contact_photo_menu'], From 4a1a1d3ca3eef458fd6f9db6ce93f26859e45268 Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 17 Mar 2025 09:29:49 +0000 Subject: [PATCH 15/45] create event for tagged hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Model/Item.php | 11 +++++++++-- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 2406fb7037..95ecad13bf 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -55,6 +55,7 @@ final class HookEventBridge ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', ArrayFilterEvent::NETWORK_TO_NAME => 'network_to_name', ArrayFilterEvent::CONVERSATION_START => 'conversation_start', + ArrayFilterEvent::ITEM_TAGGED => 'tagged', ArrayFilterEvent::DISPLAY_ITEM => 'display_item', ArrayFilterEvent::DETECT_LANGUAGES => 'detect_languages', ArrayFilterEvent::RENDER_LOCATION => 'render_location', @@ -118,6 +119,7 @@ final class HookEventBridge ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', + ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 44aa403b18..84c1d4cff6 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -60,6 +60,8 @@ final class ArrayFilterEvent extends Event public const CONVERSATION_START = 'friendica.data.conversation_start'; + public const ITEM_TAGGED = 'friendica.data.item_tagged'; + public const DISPLAY_ITEM = 'friendica.data.display_item'; public const DETECT_LANGUAGES = 'friendica.data.detect_languages'; diff --git a/src/Model/Item.php b/src/Model/Item.php index 954c0ce11c..9be9f2b27a 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2285,9 +2285,16 @@ class Item return true; } - $arr = ['item' => $item, 'user' => $owner]; + $eventDispatcher = DI::eventDispatcher(); - Hook::callAll('tagged', $arr); + $arr = [ + 'item' => $item, + 'user' => $owner, + ]; + + $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ITEM_TAGGED, $arr), + ); } else { if (Tag::isMentioned($item['parent-uri-id'], $owner['url'])) { DI::logger()->info('Mention found in parent tag.', ['uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index e0e9a3eb8e..ac33ba4280 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -44,6 +44,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', + ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', @@ -351,6 +352,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'conversation_start'], + [ArrayFilterEvent::ITEM_TAGGED, 'tagged'], [ArrayFilterEvent::DISPLAY_ITEM, 'display_item'], [ArrayFilterEvent::DETECT_LANGUAGES, 'detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index d079cacdec..806e8ad014 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -41,6 +41,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'friendica.data.photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'friendica.data.network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'friendica.data.conversation_start'], + [ArrayFilterEvent::ITEM_TAGGED, 'friendica.data.item_tagged'], [ArrayFilterEvent::DISPLAY_ITEM, 'friendica.data.display_item'], [ArrayFilterEvent::DETECT_LANGUAGES, 'friendica.data.detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'friendica.data.render_location'], From 2276bdcd7e7c32c09db3c6b5003c93acf071789b Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 17 Mar 2025 09:40:27 +0000 Subject: [PATCH 16/45] Create event for put_item_in_cache hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Model/Item.php | 14 ++++++++++++-- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 95ecad13bf..f59a267230 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -57,6 +57,7 @@ final class HookEventBridge ArrayFilterEvent::CONVERSATION_START => 'conversation_start', ArrayFilterEvent::ITEM_TAGGED => 'tagged', ArrayFilterEvent::DISPLAY_ITEM => 'display_item', + ArrayFilterEvent::CACHE_ITEM => 'put_item_in_cache', ArrayFilterEvent::DETECT_LANGUAGES => 'detect_languages', ArrayFilterEvent::RENDER_LOCATION => 'render_location', ArrayFilterEvent::ITEM_PHOTO_MENU => 'item_photo_menu', @@ -121,6 +122,7 @@ final class HookEventBridge ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', + ArrayFilterEvent::CACHE_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 84c1d4cff6..db57babfda 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -64,6 +64,8 @@ final class ArrayFilterEvent extends Event public const DISPLAY_ITEM = 'friendica.data.display_item'; + public const CACHE_ITEM = 'friendica.data.cache_item'; + public const DETECT_LANGUAGES = 'friendica.data.detect_languages'; public const RENDER_LOCATION = 'friendica.data.render_location'; diff --git a/src/Model/Item.php b/src/Model/Item.php index 9be9f2b27a..d93ab1768d 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3038,8 +3038,18 @@ class Item $item['rendered-html'] = BBCode::convertForUriId($item['uri-id'], $item['body']); $item['rendered-hash'] = hash('md5', BBCode::VERSION . '::' . $body); - $hook_data = ['item' => $item, 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']]; - Hook::callAll('put_item_in_cache', $hook_data); + $hook_data = [ + 'rendered-html' => $item['rendered-html'], + 'rendered-hash' => $item['rendered-hash'], + 'item' => $item, + ]; + + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::CACHE_ITEM, $hook_data), + )->getArray(); + $item['rendered-html'] = $hook_data['rendered-html']; $item['rendered-hash'] = $hook_data['rendered-hash']; unset($hook_data); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index ac33ba4280..0e4afb2448 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -46,6 +46,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', + ArrayFilterEvent::CACHE_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', @@ -354,6 +355,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::CONVERSATION_START, 'conversation_start'], [ArrayFilterEvent::ITEM_TAGGED, 'tagged'], [ArrayFilterEvent::DISPLAY_ITEM, 'display_item'], + [ArrayFilterEvent::CACHE_ITEM, 'put_item_in_cache'], [ArrayFilterEvent::DETECT_LANGUAGES, 'detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 806e8ad014..e087856178 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -43,6 +43,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::CONVERSATION_START, 'friendica.data.conversation_start'], [ArrayFilterEvent::ITEM_TAGGED, 'friendica.data.item_tagged'], [ArrayFilterEvent::DISPLAY_ITEM, 'friendica.data.display_item'], + [ArrayFilterEvent::CACHE_ITEM, 'friendica.data.cache_item'], [ArrayFilterEvent::DETECT_LANGUAGES, 'friendica.data.detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'friendica.data.render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'friendica.data.item_photo_menu'], From ac8ca35b2b078ead4143170df8220a258d1c5b48 Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 17 Mar 2025 10:52:37 +0000 Subject: [PATCH 17/45] Create event for item_by_link hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Model/Item.php | 15 +++++++++------ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index f59a267230..83a075c7d5 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -55,6 +55,7 @@ final class HookEventBridge ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', ArrayFilterEvent::NETWORK_TO_NAME => 'network_to_name', ArrayFilterEvent::CONVERSATION_START => 'conversation_start', + ArrayFilterEvent::FETCH_ITEM_BY_LINK => 'item_by_link', ArrayFilterEvent::ITEM_TAGGED => 'tagged', ArrayFilterEvent::DISPLAY_ITEM => 'display_item', ArrayFilterEvent::CACHE_ITEM => 'put_item_in_cache', @@ -120,6 +121,7 @@ final class HookEventBridge ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', + ArrayFilterEvent::FETCH_ITEM_BY_LINK => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CACHE_ITEM => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index db57babfda..ee36b2d983 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -60,6 +60,8 @@ final class ArrayFilterEvent extends Event public const CONVERSATION_START = 'friendica.data.conversation_start'; + public const FETCH_ITEM_BY_LINK = 'friendica.data.fetch_item_by_link'; + public const ITEM_TAGGED = 'friendica.data.item_tagged'; public const DISPLAY_ITEM = 'friendica.data.display_item'; diff --git a/src/Model/Item.php b/src/Model/Item.php index d93ab1768d..3edd6950e7 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -14,7 +14,6 @@ use Friendica\Content\Post\Collection\PostMedias; use Friendica\Content\Post\Entity\PostMedia; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Protocol; use Friendica\Core\Renderer; @@ -3951,17 +3950,21 @@ class Item return 0; } - $hookData = [ + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = [ 'uri' => $uri, 'uid' => $uid, 'item_id' => null, ]; - Hook::callAll('item_by_link', $hookData); + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::FETCH_ITEM_BY_LINK, $hook_data) + )->getArray(); - if (isset($hookData['item_id'])) { - DI::logger()->info('Hook link fetched', ['uid' => $uid, 'uri' => $uri, 'id' => $hookData['item_id']]); - return is_numeric($hookData['item_id']) ? $hookData['item_id'] : 0; + if (isset($hook_data['item_id'])) { + DI::logger()->info('Hook link fetched', ['uid' => $uid, 'uri' => $uri, 'id' => $hook_data['item_id']]); + return is_numeric($hook_data['item_id']) ? $hook_data['item_id'] : 0; } if (!$mimetype) { diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 0e4afb2448..c6b7ac7a5c 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -44,6 +44,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', + ArrayFilterEvent::FETCH_ITEM_BY_LINK => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CACHE_ITEM => 'onArrayFilterEvent', @@ -353,6 +354,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'conversation_start'], + [ArrayFilterEvent::FETCH_ITEM_BY_LINK, 'item_by_link'], [ArrayFilterEvent::ITEM_TAGGED, 'tagged'], [ArrayFilterEvent::DISPLAY_ITEM, 'display_item'], [ArrayFilterEvent::CACHE_ITEM, 'put_item_in_cache'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index e087856178..7a72faf758 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -41,6 +41,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'friendica.data.photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'friendica.data.network_to_name'], [ArrayFilterEvent::CONVERSATION_START, 'friendica.data.conversation_start'], + [ArrayFilterEvent::FETCH_ITEM_BY_LINK, 'friendica.data.fetch_item_by_link'], [ArrayFilterEvent::ITEM_TAGGED, 'friendica.data.item_tagged'], [ArrayFilterEvent::DISPLAY_ITEM, 'friendica.data.display_item'], [ArrayFilterEvent::CACHE_ITEM, 'friendica.data.cache_item'], From 59359f7d9d4341732844668f8c942c1e4d3da951 Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 17 Mar 2025 12:20:25 +0000 Subject: [PATCH 18/45] Create hook for profile_sidebar hooks --- src/Core/Hooks/HookEventBridge.php | 18 +++++++++++ src/Event/ArrayFilterEvent.php | 4 +++ src/Model/Profile.php | 30 ++++++++++++++----- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 26 ++++++++++++++++ tests/Unit/Event/ArrayFilterEventTest.php | 2 ++ 5 files changed, 72 insertions(+), 8 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 83a075c7d5..e287cdd767 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -63,6 +63,8 @@ final class HookEventBridge ArrayFilterEvent::RENDER_LOCATION => 'render_location', ArrayFilterEvent::ITEM_PHOTO_MENU => 'item_photo_menu', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'contact_photo_menu', + ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'profile_sidebar_enter', + ArrayFilterEvent::PROFILE_SIDEBAR => 'profile_sidebar', ArrayFilterEvent::OEMBED_FETCH_END => 'oembed_fetch_url', ArrayFilterEvent::PAGE_INFO => 'page_info_data', ArrayFilterEvent::SMILEY_LIST => 'smilie', @@ -129,6 +131,8 @@ final class HookEventBridge ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', + ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', @@ -193,6 +197,20 @@ final class HookEventBridge $event->setArray($data); } + /** + * Map the PROFILE_SIDEBAR_ENTRY event to `profile_sidebar_enter` hook + */ + public static function onProfileSidebarEntryEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $profile = (array) $data['profile'] ?? []; + + $data['profile'] = static::callHook($event->getName(), $profile); + + $event->setArray($data); + } + /** * Map the OEMBED_FETCH_END event to `oembed_fetch_url` hook */ diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index ee36b2d983..12105decc9 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -76,6 +76,10 @@ final class ArrayFilterEvent extends Event public const CONTACT_PHOTO_MENU = 'friendica.data.contact_photo_menu'; + public const PROFILE_SIDEBAR_ENTRY = 'friendica.data.profile_sidebar_entry'; + + public const PROFILE_SIDEBAR = 'friendica.data.profile_sidebar'; + public const OEMBED_FETCH_END = 'friendica.data.oembed_fetch_end'; public const PAGE_INFO = 'friendica.data.page_info'; diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 8217b2a96a..7a900b9bc4 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -19,6 +19,7 @@ use Friendica\Core\Search; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; @@ -258,11 +259,6 @@ class Profile * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException * @note Returns empty string if passed $profile is wrong type or not populated - * - * @hooks 'profile_sidebar_enter' - * array $profile - profile data - * @hooks 'profile_sidebar' - * array $arr */ public static function getVCardHtml(array $profile, bool $block, bool $show_contacts): string { @@ -282,7 +278,17 @@ class Profile $profile['network_link'] = ''; - Hook::callAll('profile_sidebar_enter', $profile); + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = [ + 'profile' => $profile, + ]; + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, $hook_data), + )->getArray(); + + $profile = $hook_data['profile'] ?? $profile; $profile_url = $profile['url']; @@ -473,9 +479,17 @@ class Profile '$network_url' => $network_url, ]); - $arr = ['profile' => &$profile, 'entry' => &$o]; + $hook_data = [ + 'profile' => &$profile, + 'entry' => &$o, + ]; - Hook::callAll('profile_sidebar', $arr); + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SIDEBAR, $hook_data), + )->getArray(); + + $profile = $hook_data['profile'] ?? $profile; + $o = $hook_data['entry'] ?? $o; return $o; } diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index c6b7ac7a5c..1a28e09161 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -52,6 +52,8 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', + ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', @@ -213,6 +215,28 @@ class HookEventBridgeTest extends TestCase ); } + public function testOnProfileSidebarEntryEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, ['profile' => ['uid' => 0, 'name' => 'original']]); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, array $data): array { + $this->assertSame('profile_sidebar_enter', $name); + $this->assertSame(['uid' => 0, 'name' => 'original'], $data); + + return ['uid' => 0, 'name' => 'changed']; + }); + + HookEventBridge::onProfileSidebarEntryEvent($event); + + $this->assertSame( + ['profile' => ['uid' => 0, 'name' => 'changed']], + $event->getArray(), + ); + } + public function testOnOembedFetchEndEventCallsHookWithCorrectValue(): void { $event = new ArrayFilterEvent(ArrayFilterEvent::OEMBED_FETCH_END, ['url' => 'original_url']); @@ -362,6 +386,8 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'contact_photo_menu'], + [ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, 'profile_sidebar_enter'], + [ArrayFilterEvent::PROFILE_SIDEBAR, 'profile_sidebar'], [ArrayFilterEvent::PAGE_INFO, 'page_info_data'], [ArrayFilterEvent::SMILEY_LIST, 'smilie'], [ArrayFilterEvent::JOT_NETWORKS, 'jot_networks'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 7a72faf758..85d90ba132 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -49,6 +49,8 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::RENDER_LOCATION, 'friendica.data.render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'friendica.data.item_photo_menu'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'friendica.data.contact_photo_menu'], + [ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, 'friendica.data.profile_sidebar_entry'], + [ArrayFilterEvent::PROFILE_SIDEBAR, 'friendica.data.profile_sidebar'], [ArrayFilterEvent::OEMBED_FETCH_END, 'friendica.data.oembed_fetch_end'], [ArrayFilterEvent::PAGE_INFO, 'friendica.data.page_info'], [ArrayFilterEvent::SMILEY_LIST, 'friendica.data.smiley_list'], From 1ddd5674e1993b1dff129aac10fe936f7f23280c Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 17 Mar 2025 13:08:20 +0000 Subject: [PATCH 19/45] Wrap item in INSERT_POST_LOCAL in separate array --- mod/item.php | 10 ++++++-- src/Core/Hooks/HookEventBridge.php | 16 ++++++++++++- src/Model/Item.php | 10 ++++++-- src/Model/Profile.php | 1 - tests/Unit/Core/Hooks/HookEventBridgeTest.php | 24 ++++++++++++++++++- 5 files changed, 54 insertions(+), 7 deletions(-) diff --git a/mod/item.php b/mod/item.php index e6b2cb93f4..e416126a4a 100644 --- a/mod/item.php +++ b/mod/item.php @@ -281,10 +281,16 @@ function item_process(array $post, array $request, bool $preview, string $return $eventDispatcher = DI::eventDispatcher(); - $post = $eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $post) + $hook_data = [ + 'item' => $post, + ]; + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $hook_data) )->getArray(); + $post = $hook_data['item'] ?? $post; + unset($post['edit']); unset($post['self']); unset($post['api_source']); diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index e287cdd767..e5a7dd01ab 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -112,7 +112,7 @@ final class HookEventBridge ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', - ArrayFilterEvent::INSERT_POST_LOCAL => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent', ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', @@ -183,6 +183,20 @@ final class HookEventBridge ); } + /** + * Map the INSERT_POST_LOCAL event to `post_local` hook + */ + public static function onInsertPostLocalEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $item = (array) $data['item'] ?? []; + + $data['item'] = static::callHook($event->getName(), $item); + + $event->setArray($data); + } + /** * Map the PREPARE_POST_START event to `prepare_body_init` hook */ diff --git a/src/Model/Item.php b/src/Model/Item.php index 3edd6950e7..651b812f3d 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -845,10 +845,16 @@ class Item $dummy_session = false; } - $item = $eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $item) + $hook_data = [ + 'item' => $item, + ]; + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, $hook_data) )->getArray(); + $item = $hook_data['item'] ?? $item; + if ($dummy_session) { unset($_SESSION['authenticated']); unset($_SESSION['uid']); diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 7a900b9bc4..a647a58c78 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -12,7 +12,6 @@ use Friendica\AppHelper; use Friendica\Content\Text\BBCode; use Friendica\Content\Widget\ContactBlock; use Friendica\Core\Cache\Enum\Duration; -use Friendica\Core\Hook; use Friendica\Core\Protocol; use Friendica\Core\Renderer; use Friendica\Core\Search; diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 1a28e09161..17f9e1084d 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -33,7 +33,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', - ArrayFilterEvent::INSERT_POST_LOCAL => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent', ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', @@ -193,6 +193,28 @@ class HookEventBridgeTest extends TestCase HookEventBridge::onCollectRoutesEvent($event); } + public function testOnInsertPostLocalEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, ['item' => ['id' => -1]]); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, array $data): array { + $this->assertSame('post_local', $name); + $this->assertSame(['id' => -1], $data); + + return ['id' => 123]; + }); + + HookEventBridge::onInsertPostLocalEvent($event); + + $this->assertSame( + ['item' => ['id' => 123]], + $event->getArray(), + ); + } + public function testOnPreparePostStartEventCallsHookWithCorrectValue(): void { $event = new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_START, ['item' => ['id' => -1]]); From 9822dd25d84198ea8cfff3127b0ad9dcb2c03341 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 18 Mar 2025 14:21:31 +0000 Subject: [PATCH 20/45] Wrap item in INSERT_POST_LOCAL_END into separate array --- src/Content/Item.php | 10 ++++++-- src/Core/Hooks/HookEventBridge.php | 16 +++++++++++- src/Module/Post/Tag/Add.php | 10 ++++++-- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 25 ++++++++++++++++--- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/Content/Item.php b/src/Content/Item.php index af26ebd8ed..901a2eac3d 100644 --- a/src/Content/Item.php +++ b/src/Content/Item.php @@ -1011,10 +1011,16 @@ class Item Tag::createImplicitMentions($post['uri-id'], $post['thr-parent-id']); } - $post = $this->eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $post) + $hook_data = [ + 'item' => $post, + ]; + + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $hook_data) )->getArray(); + $post = $hook_data['item'] ?? $post; + $author = DBA::selectFirst('contact', ['thumb'], ['uid' => $post['uid'], 'self' => true]); foreach ($recipients as $recipient) { diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index e5a7dd01ab..82a11f1f55 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -113,7 +113,7 @@ final class HookEventBridge ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent', - ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onInsertPostLocalEndEvent', ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', ArrayFilterEvent::PREPARE_POST_START => 'onPreparePostStartEvent', @@ -197,6 +197,20 @@ final class HookEventBridge $event->setArray($data); } + /** + * Map the INSERT_POST_LOCAL_END event to `post_local_end` hook + */ + public static function onInsertPostLocalEndEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $item = (array) $data['item'] ?? []; + + $data['item'] = static::callHook($event->getName(), $item); + + $event->setArray($data); + } + /** * Map the PREPARE_POST_START event to `prepare_body_init` hook */ diff --git a/src/Module/Post/Tag/Add.php b/src/Module/Post/Tag/Add.php index e03bf215a3..a145c36e90 100644 --- a/src/Module/Post/Tag/Add.php +++ b/src/Module/Post/Tag/Add.php @@ -153,10 +153,16 @@ EOT; $post['id'] = $post_id; - $post = $this->eventDispatcher->dispatch( - new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $post) + $hook_data = [ + 'item' => $post, + ]; + + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, $hook_data) )->getArray(); + $post = $hook_data['item'] ?? $post; + $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]); Worker::add(Worker::PRIORITY_HIGH, 'Notifier', Delivery::POST, $post['uri-id'], $post['uid']); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 17f9e1084d..e6ec58a27c 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -34,7 +34,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent', - ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onArrayFilterEvent', + ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onInsertPostLocalEndEvent', ArrayFilterEvent::INSERT_POST_REMOTE => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_REMOTE_END => 'onArrayFilterEvent', ArrayFilterEvent::PREPARE_POST_START => 'onPreparePostStartEvent', @@ -214,6 +214,27 @@ class HookEventBridgeTest extends TestCase $event->getArray(), ); } + public function testOnInsertPostLocalEndEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, ['item' => ['id' => -1]]); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, array $data): array { + $this->assertSame('post_local_end', $name); + $this->assertSame(['id' => -1], $data); + + return ['id' => 123]; + }); + + HookEventBridge::onInsertPostLocalEndEvent($event); + + $this->assertSame( + ['item' => ['id' => 123]], + $event->getArray(), + ); + } public function testOnPreparePostStartEventCallsHookWithCorrectValue(): void { @@ -390,8 +411,6 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::FEATURE_ENABLED, 'isEnabled'], [ArrayFilterEvent::FEATURE_GET, 'get'], [ArrayFilterEvent::INSERT_POST_LOCAL_START, 'post_local_start'], - [ArrayFilterEvent::INSERT_POST_LOCAL, 'post_local'], - [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'post_local_end'], [ArrayFilterEvent::INSERT_POST_REMOTE, 'post_remote'], [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'post_remote_end'], [ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT, 'prepare_body_content_filter'], From 90db7d2e65f449d5bbdf633f94a14128da032911 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 20 Mar 2025 14:59:47 +0000 Subject: [PATCH 21/45] Create events for authenticate, register_account and remove_user hooks --- src/Core/Hooks/HookEventBridge.php | 34 +++++++++++++++ src/Event/ArrayFilterEvent.php | 6 +++ src/Model/User.php | 33 ++++++++++++--- src/Security/BasicAuth.php | 17 +++++--- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 41 ++++++++++++++++++- tests/Unit/Event/ArrayFilterEventTest.php | 3 ++ 6 files changed, 122 insertions(+), 12 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 82a11f1f55..bc2262df55 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -81,6 +81,9 @@ final class HookEventBridge ArrayFilterEvent::BLOCK_CONTACT => 'block', ArrayFilterEvent::UNBLOCK_CONTACT => 'unblock', ArrayFilterEvent::AVATAR_LOOKUP => 'avatar_lookup', + ArrayFilterEvent::ACCOUNT_AUTHENTICATE => 'authenticate', + ArrayFilterEvent::ACCOUNT_REGISTER => 'register_account', + ArrayFilterEvent::ACCOUNT_REMOVE => 'remove_user', ArrayFilterEvent::EVENT_CREATED => 'event_created', ArrayFilterEvent::EVENT_UPDATED => 'event_updated', ArrayFilterEvent::ADD_WORKER_TASK => 'proc_run', @@ -149,6 +152,9 @@ final class HookEventBridge ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent', + ArrayFilterEvent::ACCOUNT_AUTHENTICATE => 'onArrayFilterEvent', + ArrayFilterEvent::ACCOUNT_REGISTER => 'onAccountRegisterEvent', + ArrayFilterEvent::ACCOUNT_REMOVE => 'onAccountRemoveEvent', ArrayFilterEvent::EVENT_CREATED => 'onEventCreatedEvent', ArrayFilterEvent::EVENT_UPDATED => 'onEventUpdatedEvent', ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', @@ -295,6 +301,34 @@ final class HookEventBridge $event->setArray($data); } + /** + * Map the ACCOUNT_REGISTER event to `register_account` hook + */ + public static function onAccountRegisterEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $uid = (int) $data['uid'] ?? 0; + + $data['uid'] = static::callHook($event->getName(), $uid); + + $event->setArray($data); + } + + /** + * Map the ACCOUNT_REMOVE event to `remove_account` hook + */ + public static function onAccountRemoveEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $user = (array) $data['user'] ?? []; + + $data['user'] = static::callHook($event->getName(), $user); + + $event->setArray($data); + } + /** * Map the EVENT_CREATED event to `event_created` hook */ diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 12105decc9..02a516bf61 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -112,6 +112,12 @@ final class ArrayFilterEvent extends Event public const AVATAR_LOOKUP = 'friendica.data.avatar_lookup'; + public const ACCOUNT_AUTHENTICATE = 'friendica.data.account_authenticate'; + + public const ACCOUNT_REGISTER = 'friendica.data.account_register'; + + public const ACCOUNT_REMOVE = 'friendica.data.account_remove'; + public const EVENT_CREATED = 'friendica.data.event_created'; public const EVENT_UPDATED = 'friendica.data.event_updated'; diff --git a/src/Model/User.php b/src/Model/User.php index 927a1a82bd..5195fb77a3 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -13,7 +13,6 @@ use ErrorException; use Exception; use Friendica\App; use Friendica\Content\Pager; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Protocol; use Friendica\Core\Search; @@ -21,6 +20,7 @@ use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Module; use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; @@ -758,12 +758,16 @@ class User 'user_record' => null ]; - /* + $eventDispatcher = DI::eventDispatcher(); + + /** * An addon indicates successful login by setting 'authenticated' to non-zero value and returning a user record * Addons should never set 'authenticated' except to indicate success - as hooks may be chained * and later addons should not interfere with an earlier one that succeeded. */ - Hook::callAll('authenticate', $addon_auth); + $addon_auth = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ACCOUNT_AUTHENTICATE, $addon_auth), + )->getArray(); if ($addon_auth['authenticated'] && $addon_auth['user_record']) { return $addon_auth['user_record']['uid']; @@ -1460,11 +1464,20 @@ class User Contact::updateSelfFromUserID($uid, true); } - Hook::callAll('register_account', $uid); + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = [ + 'uid' => $uid, + ]; + + $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ACCOUNT_REGISTER, $hook_data), + ); self::setRegisterMethodByUserCount(); $return['user'] = $user; + return $return; } @@ -1787,7 +1800,17 @@ class User throw new \RuntimeException(DI::l10n()->t("User with delegates can't be removed, please remove delegate users first")); } - Hook::callAll('remove_user', $user); + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = [ + 'user' => $user, + ]; + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ACCOUNT_REMOVE, $hook_data), + )->getArray(); + + $user = $hook_data['user'] ?? $user; // save username (actually the nickname as it is guaranteed // unique), so it cannot be re-registered in the future. diff --git a/src/Security/BasicAuth.php b/src/Security/BasicAuth.php index fe89bf452e..52a0f75026 100644 --- a/src/Security/BasicAuth.php +++ b/src/Security/BasicAuth.php @@ -11,6 +11,7 @@ use Exception; use Friendica\Core\Hook; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\User; use Friendica\Network\HTTPException\UnauthorizedException; @@ -136,12 +137,16 @@ class BasicAuth 'user_record' => null, ]; - /* - * An addon indicates successful login by setting 'authenticated' to non-zero value and returning a user record - * Addons should never set 'authenticated' except to indicate success - as hooks may be chained - * and later addons should not interfere with an earlier one that succeeded. - */ - Hook::callAll('authenticate', $addon_auth); + $eventDispatcher = DI::eventDispatcher(); + + /** + * An addon indicates successful login by setting 'authenticated' to non-zero value and returning a user record + * Addons should never set 'authenticated' except to indicate success - as hooks may be chained + * and later addons should not interfere with an earlier one that succeeded. + */ + $addon_auth = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ACCOUNT_AUTHENTICATE, $addon_auth), + )->getArray(); if ($addon_auth['authenticated'] && !empty($addon_auth['user_record'])) { $record = $addon_auth['user_record']; diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index e6ec58a27c..d47ea1cbf7 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -70,6 +70,9 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent', + ArrayFilterEvent::ACCOUNT_AUTHENTICATE => 'onArrayFilterEvent', + ArrayFilterEvent::ACCOUNT_REGISTER => 'onAccountRegisterEvent', + ArrayFilterEvent::ACCOUNT_REMOVE => 'onAccountRemoveEvent', ArrayFilterEvent::EVENT_CREATED => 'onEventCreatedEvent', ArrayFilterEvent::EVENT_UPDATED => 'onEventUpdatedEvent', ArrayFilterEvent::ADD_WORKER_TASK => 'onArrayFilterEvent', @@ -385,6 +388,40 @@ class HookEventBridgeTest extends TestCase HookEventBridge::onEventCreatedEvent($event); } + public function testOnAccountRegisterEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::ACCOUNT_REGISTER, ['uid' => 123]); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, int $data): int { + $this->assertSame('register_account', $name); + $this->assertSame(123, $data); + + return $data; + }); + + HookEventBridge::onAccountRegisterEvent($event); + } + + public function testOnAccountRemoveEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::ACCOUNT_REMOVE, ['user' => ['uid' => 123]]); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, array $data): array { + $this->assertSame('remove_user', $name); + $this->assertSame(['uid' => 123], $data); + + return $data; + }); + + HookEventBridge::onAccountRemoveEvent($event); + } + public function testOnEventUpdatedEventCallsHookWithCorrectValue(): void { $event = new ArrayFilterEvent(ArrayFilterEvent::EVENT_UPDATED, ['event' => ['id' => 123]]); @@ -427,7 +464,6 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'contact_photo_menu'], - [ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, 'profile_sidebar_enter'], [ArrayFilterEvent::PROFILE_SIDEBAR, 'profile_sidebar'], [ArrayFilterEvent::PAGE_INFO, 'page_info_data'], [ArrayFilterEvent::SMILEY_LIST, 'smilie'], @@ -441,6 +477,9 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::BLOCK_CONTACT, 'block'], [ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'], [ArrayFilterEvent::AVATAR_LOOKUP, 'avatar_lookup'], + [ArrayFilterEvent::ACCOUNT_AUTHENTICATE, 'authenticate'], + [ArrayFilterEvent::ACCOUNT_REGISTER, 'register_account'], + [ArrayFilterEvent::ACCOUNT_REMOVE, 'remove_user'], [ArrayFilterEvent::EVENT_CREATED, 'event_created'], [ArrayFilterEvent::EVENT_UPDATED, 'event_updated'], [ArrayFilterEvent::ADD_WORKER_TASK, 'proc_run'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 85d90ba132..5bc71084c3 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -67,6 +67,9 @@ 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::ACCOUNT_AUTHENTICATE, 'friendica.data.account_authenticate'], + [ArrayFilterEvent::ACCOUNT_REGISTER, 'friendica.data.account_register'], + [ArrayFilterEvent::ACCOUNT_REMOVE, 'friendica.data.account_remove'], [ArrayFilterEvent::EVENT_CREATED, 'friendica.data.event_created'], [ArrayFilterEvent::EVENT_UPDATED, 'friendica.data.event_updated'], [ArrayFilterEvent::ADD_WORKER_TASK, 'friendica.data.add_worker_task'], From 76c3eeb96e47000f5dfa1e4d88d1e2c6f4b56b2b Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 24 Mar 2025 08:54:38 +0000 Subject: [PATCH 22/45] Register hooks as event listener in API tests --- tests/ApiTestCase.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/ApiTestCase.php b/tests/ApiTestCase.php index 6124bf9f32..45a84c280a 100644 --- a/tests/ApiTestCase.php +++ b/tests/ApiTestCase.php @@ -11,6 +11,7 @@ use Friendica\Capabilities\ICanCreateResponses; use Friendica\Core\Addon\AddonHelper; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Hook; +use Friendica\Core\Hooks\HookEventBridge; use Friendica\DI; use Friendica\Module\Special\HTTPException; use Friendica\Security\Authentication; @@ -159,6 +160,13 @@ abstract class ApiTestCase extends FixtureTestCase ; DI::init($this->dice); + /** @var \Friendica\Event\EventDispatcher */ + $eventDispatcher = DI::eventDispatcher(); + + foreach (HookEventBridge::getStaticSubscribedEvents() as $eventName => $methodName) { + $eventDispatcher->addListener($eventName, [HookEventBridge::class, $methodName]); + } + $this->httpExceptionMock = $this->dice->create(HTTPException::class); AuthTestConfig::$authenticated = true; From f0273a618c5d9db7ddd845f22a0603c333190ab1 Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 24 Mar 2025 12:28:22 +0000 Subject: [PATCH 23/45] Create event for check_item_notification hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Model/Post/UserNotification.php | 9 +++++++-- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index bc2262df55..356dfe7e9c 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -59,6 +59,7 @@ final class HookEventBridge ArrayFilterEvent::ITEM_TAGGED => 'tagged', ArrayFilterEvent::DISPLAY_ITEM => 'display_item', ArrayFilterEvent::CACHE_ITEM => 'put_item_in_cache', + ArrayFilterEvent::CHECK_ITEM_NOTIFICATION => 'check_item_notification', ArrayFilterEvent::DETECT_LANGUAGES => 'detect_languages', ArrayFilterEvent::RENDER_LOCATION => 'render_location', ArrayFilterEvent::ITEM_PHOTO_MENU => 'item_photo_menu', @@ -130,6 +131,7 @@ final class HookEventBridge ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CACHE_ITEM => 'onArrayFilterEvent', + ArrayFilterEvent::CHECK_ITEM_NOTIFICATION => 'onArrayFilterEvent', ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 02a516bf61..0313520060 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -68,6 +68,8 @@ final class ArrayFilterEvent extends Event public const CACHE_ITEM = 'friendica.data.cache_item'; + public const CHECK_ITEM_NOTIFICATION = 'friendica.data.check_item_notification'; + public const DETECT_LANGUAGES = 'friendica.data.detect_languages'; public const RENDER_LOCATION = 'friendica.data.render_location'; diff --git a/src/Model/Post/UserNotification.php b/src/Model/Post/UserNotification.php index 95c578f486..d212b07b2f 100644 --- a/src/Model/Post/UserNotification.php +++ b/src/Model/Post/UserNotification.php @@ -9,10 +9,10 @@ namespace Friendica\Model\Post; use BadMethodCallException; use Exception; -use Friendica\Core\Hook; use Friendica\Database\Database; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\Post; @@ -396,7 +396,12 @@ class UserNotification $profiles = [$owner['nurl']]; $notification_data = ['uid' => $uid, 'profiles' => []]; - Hook::callAll('check_item_notification', $notification_data); + + $eventDispatcher = DI::eventDispatcher(); + + $notification_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::CHECK_ITEM_NOTIFICATION, $notification_data), + )->getArray(); // Normalize the connector profiles foreach ($notification_data['profiles'] as $profile) { diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index d47ea1cbf7..f00e803ba1 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -48,6 +48,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CACHE_ITEM => 'onArrayFilterEvent', + ArrayFilterEvent::CHECK_ITEM_NOTIFICATION => 'onArrayFilterEvent', ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', @@ -460,6 +461,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::ITEM_TAGGED, 'tagged'], [ArrayFilterEvent::DISPLAY_ITEM, 'display_item'], [ArrayFilterEvent::CACHE_ITEM, 'put_item_in_cache'], + [ArrayFilterEvent::CHECK_ITEM_NOTIFICATION, 'check_item_notification'], [ArrayFilterEvent::DETECT_LANGUAGES, 'detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 5bc71084c3..91d611a33b 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -45,6 +45,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::ITEM_TAGGED, 'friendica.data.item_tagged'], [ArrayFilterEvent::DISPLAY_ITEM, 'friendica.data.display_item'], [ArrayFilterEvent::CACHE_ITEM, 'friendica.data.cache_item'], + [ArrayFilterEvent::CHECK_ITEM_NOTIFICATION, 'friendica.data.check_item_notification'], [ArrayFilterEvent::DETECT_LANGUAGES, 'friendica.data.detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'friendica.data.render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'friendica.data.item_photo_menu'], From 20a1d2262019f5a906a92fcd87c898a76a1cc16f Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 24 Mar 2025 12:41:40 +0000 Subject: [PATCH 24/45] Create event for profile_tabs hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Module/BaseProfile.php | 12 ++++++++---- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 356dfe7e9c..3d2d6d62f5 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -66,6 +66,7 @@ final class HookEventBridge ArrayFilterEvent::CONTACT_PHOTO_MENU => 'contact_photo_menu', ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'profile_sidebar_enter', ArrayFilterEvent::PROFILE_SIDEBAR => 'profile_sidebar', + ArrayFilterEvent::PROFILE_TABS => 'profile_tabs', ArrayFilterEvent::OEMBED_FETCH_END => 'oembed_fetch_url', ArrayFilterEvent::PAGE_INFO => 'page_info_data', ArrayFilterEvent::SMILEY_LIST => 'smilie', @@ -138,6 +139,7 @@ final class HookEventBridge ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_TABS => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 0313520060..06ada45a22 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -82,6 +82,8 @@ final class ArrayFilterEvent extends Event public const PROFILE_SIDEBAR = 'friendica.data.profile_sidebar'; + public const PROFILE_TABS = 'friendica.data.profile_tabs'; + public const OEMBED_FETCH_END = 'friendica.data.oembed_fetch_end'; public const PAGE_INFO = 'friendica.data.page_info'; diff --git a/src/Module/BaseProfile.php b/src/Module/BaseProfile.php index 4a73408bc9..ce5d276861 100644 --- a/src/Module/BaseProfile.php +++ b/src/Module/BaseProfile.php @@ -9,9 +9,9 @@ namespace Friendica\Module; use Friendica\BaseModule; use Friendica\Content\Feature; -use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\User; class BaseProfile extends BaseModule @@ -128,12 +128,16 @@ class BaseProfile extends BaseModule ]; } - $arr = ['is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $current, 'tabs' => $tabs]; + $hook_data = ['is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $current, 'tabs' => $tabs]; - Hook::callAll('profile_tabs', $arr); + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PROFILE_TABS, $hook_data), + )->getArray(); $tpl = Renderer::getMarkupTemplate('common_tabs.tpl'); - return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs'], '$more' => DI::l10n()->t('More')]); + return Renderer::replaceMacros($tpl, ['$tabs' => $hook_data['tabs'], '$more' => DI::l10n()->t('More')]); } } diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index f00e803ba1..874fc9b25e 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -55,6 +55,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_TABS => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', @@ -467,6 +468,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'contact_photo_menu'], [ArrayFilterEvent::PROFILE_SIDEBAR, 'profile_sidebar'], + [ArrayFilterEvent::PROFILE_TABS, 'profile_tabs'], [ArrayFilterEvent::PAGE_INFO, 'page_info_data'], [ArrayFilterEvent::SMILEY_LIST, 'smilie'], [ArrayFilterEvent::JOT_NETWORKS, 'jot_networks'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 91d611a33b..ea495110c3 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -52,6 +52,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'friendica.data.contact_photo_menu'], [ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, 'friendica.data.profile_sidebar_entry'], [ArrayFilterEvent::PROFILE_SIDEBAR, 'friendica.data.profile_sidebar'], + [ArrayFilterEvent::PROFILE_TABS, 'friendica.data.profile_tabs'], [ArrayFilterEvent::OEMBED_FETCH_END, 'friendica.data.oembed_fetch_end'], [ArrayFilterEvent::PAGE_INFO, 'friendica.data.page_info'], [ArrayFilterEvent::SMILEY_LIST, 'friendica.data.smiley_list'], From 88929bb307423337b3c8f37784e466c4c0174d10 Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 24 Mar 2025 13:31:53 +0000 Subject: [PATCH 25/45] Create event for directory_item hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Module/Directory.php | 16 +++++++++++++--- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 3d2d6d62f5..e208901a82 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -63,6 +63,7 @@ final class HookEventBridge ArrayFilterEvent::DETECT_LANGUAGES => 'detect_languages', ArrayFilterEvent::RENDER_LOCATION => 'render_location', ArrayFilterEvent::ITEM_PHOTO_MENU => 'item_photo_menu', + ArrayFilterEvent::DIRECTORY_ITEM => 'directory_item', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'contact_photo_menu', ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'profile_sidebar_enter', ArrayFilterEvent::PROFILE_SIDEBAR => 'profile_sidebar', @@ -136,6 +137,7 @@ final class HookEventBridge ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::DIRECTORY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 06ada45a22..de92599ee3 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -76,6 +76,8 @@ final class ArrayFilterEvent extends Event public const ITEM_PHOTO_MENU = 'friendica.data.item_photo_menu'; + public const DIRECTORY_ITEM = 'friendica.data.directory_item'; + public const CONTACT_PHOTO_MENU = 'friendica.data.contact_photo_menu'; public const PROFILE_SIDEBAR_ENTRY = 'friendica.data.profile_sidebar_entry'; diff --git a/src/Module/Directory.php b/src/Module/Directory.php index ad95b49524..d63bbc13e3 100644 --- a/src/Module/Directory.php +++ b/src/Module/Directory.php @@ -15,6 +15,7 @@ use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\Core\Search; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model; use Friendica\Model\Profile; use Friendica\Network\HTTPException; @@ -161,13 +162,22 @@ class Directory extends BaseModule ]; - $hook = ['contact' => $contact, 'entry' => $entry]; + $eventDispatcher = DI::eventDispatcher(); - Hook::callAll('directory_item', $hook); + $hook_data = [ + 'contact' => $contact, + 'entry' => $entry, + ]; + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::DIRECTORY_ITEM, $hook_data), + )->getArray(); + + $entry = $hook_data['entry'] ?? $entry; unset($profile); unset($location); - return $hook['entry']; + return $entry; } } diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 874fc9b25e..69e37c0b9c 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -52,6 +52,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::DIRECTORY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', @@ -466,6 +467,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::DETECT_LANGUAGES, 'detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], + [ArrayFilterEvent::DIRECTORY_ITEM, 'directory_item'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'contact_photo_menu'], [ArrayFilterEvent::PROFILE_SIDEBAR, 'profile_sidebar'], [ArrayFilterEvent::PROFILE_TABS, 'profile_tabs'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index ea495110c3..881deacd43 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -49,6 +49,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::DETECT_LANGUAGES, 'friendica.data.detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'friendica.data.render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'friendica.data.item_photo_menu'], + [ArrayFilterEvent::DIRECTORY_ITEM, 'friendica.data.directory_item'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'friendica.data.contact_photo_menu'], [ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, 'friendica.data.profile_sidebar_entry'], [ArrayFilterEvent::PROFILE_SIDEBAR, 'friendica.data.profile_sidebar'], From a9f02bfef5453e0005a63f490a7f81a677883e4c Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 25 Mar 2025 12:26:40 +0000 Subject: [PATCH 26/45] Create event for about_hook hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/HtmlFilterEvent.php | 2 ++ src/Module/Friendica.php | 26 ++++++++++++++++--- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/HtmlFilterEventTest.php | 4 +++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index e208901a82..24d15606ea 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -99,6 +99,7 @@ final class HookEventBridge HtmlFilterEvent::PAGE_HEADER => 'page_header', HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top', HtmlFilterEvent::PAGE_END => 'page_end', + HtmlFilterEvent::ABOUT_CONTENT => 'about_hook', HtmlFilterEvent::JOT_TOOL => 'jot_tool', HtmlFilterEvent::CONTACT_BLOCK_END => 'contact_block_end', ]; @@ -173,6 +174,7 @@ final class HookEventBridge HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', + HtmlFilterEvent::ABOUT_CONTENT => 'onHtmlFilterEvent', HtmlFilterEvent::JOT_TOOL => 'onHtmlFilterEvent', HtmlFilterEvent::CONTACT_BLOCK_END => 'onHtmlFilterEvent', ]; diff --git a/src/Event/HtmlFilterEvent.php b/src/Event/HtmlFilterEvent.php index cb8a3992df..2f68d864bf 100644 --- a/src/Event/HtmlFilterEvent.php +++ b/src/Event/HtmlFilterEvent.php @@ -26,6 +26,8 @@ final class HtmlFilterEvent extends Event public const PAGE_END = 'friendica.html.page_end'; + public const ABOUT_CONTENT = 'friendica.html.about_content'; + public const JOT_TOOL = 'friendica.html.jot_tool'; public const CONTACT_BLOCK_END = 'friendica.html.contact_block_end'; diff --git a/src/Module/Friendica.php b/src/Module/Friendica.php index 1a741b890c..0ace0707dd 100644 --- a/src/Module/Friendica.php +++ b/src/Module/Friendica.php @@ -13,16 +13,17 @@ use Friendica\App\BaseURL; use Friendica\BaseModule; use Friendica\Core\Addon\AddonHelper; use Friendica\Core\Config\Capability\IManageConfigValues; -use Friendica\Core\Hook; use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\PostUpdate; +use Friendica\Event\HtmlFilterEvent; use Friendica\Model\User; use Friendica\Network\HTTPException; use Friendica\Protocol\ActivityPub; use Friendica\Util\Profiler; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** @@ -32,6 +33,7 @@ use Psr\Log\LoggerInterface; class Friendica extends BaseModule { private AddonHelper $addonHelper; + private EventDispatcherInterface $eventDispatcher; /** @var IManageConfigValues */ private $config; /** @var IManageKeyValuePairs */ @@ -39,13 +41,27 @@ class Friendica extends BaseModule /** @var IHandleUserSessions */ private $session; - public function __construct(AddonHelper $addonHelper, IHandleUserSessions $session, IManageKeyValuePairs $keyValue, IManageConfigValues $config, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) - { + public function __construct( + AddonHelper $addonHelper, + EventDispatcherInterface $eventDispatcher, + IHandleUserSessions $session, + IManageKeyValuePairs $keyValue, + IManageConfigValues $config, + 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); $this->config = $config; $this->keyValue = $keyValue; $this->session = $session; + $this->eventDispatcher = $eventDispatcher; $this->addonHelper = $addonHelper; } @@ -99,7 +115,9 @@ class Friendica extends BaseModule $hooked = ''; - Hook::callAll('about_hook', $hooked); + $hooked = $this->eventDispatcher->dispatch( + new HtmlFilterEvent(HtmlFilterEvent::ABOUT_CONTENT, $hooked), + )->getHtml(); $tpl = Renderer::getMarkupTemplate('friendica.tpl'); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 69e37c0b9c..ab6f5e2cf0 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -88,6 +88,7 @@ class HookEventBridgeTest extends TestCase HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', + HtmlFilterEvent::ABOUT_CONTENT => 'onHtmlFilterEvent', HtmlFilterEvent::JOT_TOOL => 'onHtmlFilterEvent', HtmlFilterEvent::CONTACT_BLOCK_END => 'onHtmlFilterEvent', ]; @@ -525,6 +526,7 @@ class HookEventBridgeTest extends TestCase [HtmlFilterEvent::PAGE_HEADER, 'page_header'], [HtmlFilterEvent::PAGE_CONTENT_TOP, 'page_content_top'], [HtmlFilterEvent::PAGE_END, 'page_end'], + [HtmlFilterEvent::ABOUT_CONTENT, 'about_hook'], [HtmlFilterEvent::JOT_TOOL, 'jot_tool'], [HtmlFilterEvent::CONTACT_BLOCK_END, 'contact_block_end'], ]; diff --git a/tests/Unit/Event/HtmlFilterEventTest.php b/tests/Unit/Event/HtmlFilterEventTest.php index ae1d27a5ad..9d38cf097a 100644 --- a/tests/Unit/Event/HtmlFilterEventTest.php +++ b/tests/Unit/Event/HtmlFilterEventTest.php @@ -27,8 +27,12 @@ class HtmlFilterEventTest extends TestCase return [ [HtmlFilterEvent::HEAD, 'friendica.html.head'], [HtmlFilterEvent::FOOTER, 'friendica.html.footer'], + [HtmlFilterEvent::PAGE_HEADER, 'friendica.html.page_header'], [HtmlFilterEvent::PAGE_CONTENT_TOP, 'friendica.html.page_content_top'], [HtmlFilterEvent::PAGE_END, 'friendica.html.page_end'], + [HtmlFilterEvent::ABOUT_CONTENT, 'friendica.html.about_content'], + [HtmlFilterEvent::JOT_TOOL, 'friendica.html.jot_tool'], + [HtmlFilterEvent::CONTACT_BLOCK_END, 'friendica.html.contact_block_end'], ]; } From a9b36f55c95d94d408f7cf37aa5fec631d2b419b Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 25 Mar 2025 15:57:26 +0000 Subject: [PATCH 27/45] Create event for home_content hook --- src/Core/Hooks/HookEventBridge.php | 6 ++++-- src/Event/HtmlFilterEvent.php | 4 +++- src/Module/Friendica.php | 2 +- src/Module/Home.php | 7 +++++-- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 6 ++++-- tests/Unit/Event/HtmlFilterEventTest.php | 3 ++- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 24d15606ea..84835dc780 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -99,7 +99,8 @@ final class HookEventBridge HtmlFilterEvent::PAGE_HEADER => 'page_header', HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top', HtmlFilterEvent::PAGE_END => 'page_end', - HtmlFilterEvent::ABOUT_CONTENT => 'about_hook', + HtmlFilterEvent::MOD_HOME_CONTENT => 'home_content', + HtmlFilterEvent::MOD_ABOUT_CONTENT => 'about_hook', HtmlFilterEvent::JOT_TOOL => 'jot_tool', HtmlFilterEvent::CONTACT_BLOCK_END => 'contact_block_end', ]; @@ -174,7 +175,8 @@ final class HookEventBridge HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', - HtmlFilterEvent::ABOUT_CONTENT => 'onHtmlFilterEvent', + HtmlFilterEvent::MOD_HOME_CONTENT => 'onHtmlFilterEvent', + HtmlFilterEvent::MOD_ABOUT_CONTENT => 'onHtmlFilterEvent', HtmlFilterEvent::JOT_TOOL => 'onHtmlFilterEvent', HtmlFilterEvent::CONTACT_BLOCK_END => 'onHtmlFilterEvent', ]; diff --git a/src/Event/HtmlFilterEvent.php b/src/Event/HtmlFilterEvent.php index 2f68d864bf..9e30d6d656 100644 --- a/src/Event/HtmlFilterEvent.php +++ b/src/Event/HtmlFilterEvent.php @@ -26,7 +26,9 @@ final class HtmlFilterEvent extends Event public const PAGE_END = 'friendica.html.page_end'; - public const ABOUT_CONTENT = 'friendica.html.about_content'; + public const MOD_HOME_CONTENT = 'friendica.html.mod_home_content'; + + public const MOD_ABOUT_CONTENT = 'friendica.html.mod_about_content'; public const JOT_TOOL = 'friendica.html.jot_tool'; diff --git a/src/Module/Friendica.php b/src/Module/Friendica.php index 0ace0707dd..314152abca 100644 --- a/src/Module/Friendica.php +++ b/src/Module/Friendica.php @@ -116,7 +116,7 @@ class Friendica extends BaseModule $hooked = ''; $hooked = $this->eventDispatcher->dispatch( - new HtmlFilterEvent(HtmlFilterEvent::ABOUT_CONTENT, $hooked), + new HtmlFilterEvent(HtmlFilterEvent::MOD_ABOUT_CONTENT, $hooked), )->getHtml(); $tpl = Renderer::getMarkupTemplate('friendica.tpl'); diff --git a/src/Module/Home.php b/src/Module/Home.php index 7e7d59d760..9a6a4a4730 100644 --- a/src/Module/Home.php +++ b/src/Module/Home.php @@ -8,10 +8,10 @@ namespace Friendica\Module; use Friendica\BaseModule; -use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\DI; use Friendica\Event\Event; +use Friendica\Event\HtmlFilterEvent; use Friendica\Model\User; use Friendica\Module\Security\Login; use Friendica\Protocol\ActivityPub; @@ -66,7 +66,10 @@ class Home extends BaseModule $login = Login::form(DI::args()->getQueryString(), Register::getPolicy() !== Register::CLOSED); $content = ''; - Hook::callAll('home_content', $content); + + $content = $eventDispatcher->dispatch( + new HtmlFilterEvent(HtmlFilterEvent::MOD_HOME_CONTENT, $content), + )->getHtml(); $tpl = Renderer::getMarkupTemplate('home.tpl'); return Renderer::replaceMacros($tpl, [ diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index ab6f5e2cf0..075630da1c 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -88,7 +88,8 @@ class HookEventBridgeTest extends TestCase HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', - HtmlFilterEvent::ABOUT_CONTENT => 'onHtmlFilterEvent', + HtmlFilterEvent::MOD_HOME_CONTENT => 'onHtmlFilterEvent', + HtmlFilterEvent::MOD_ABOUT_CONTENT => 'onHtmlFilterEvent', HtmlFilterEvent::JOT_TOOL => 'onHtmlFilterEvent', HtmlFilterEvent::CONTACT_BLOCK_END => 'onHtmlFilterEvent', ]; @@ -526,7 +527,8 @@ class HookEventBridgeTest extends TestCase [HtmlFilterEvent::PAGE_HEADER, 'page_header'], [HtmlFilterEvent::PAGE_CONTENT_TOP, 'page_content_top'], [HtmlFilterEvent::PAGE_END, 'page_end'], - [HtmlFilterEvent::ABOUT_CONTENT, 'about_hook'], + [HtmlFilterEvent::MOD_HOME_CONTENT, 'home_content'], + [HtmlFilterEvent::MOD_ABOUT_CONTENT, 'about_hook'], [HtmlFilterEvent::JOT_TOOL, 'jot_tool'], [HtmlFilterEvent::CONTACT_BLOCK_END, 'contact_block_end'], ]; diff --git a/tests/Unit/Event/HtmlFilterEventTest.php b/tests/Unit/Event/HtmlFilterEventTest.php index 9d38cf097a..47a2d2596d 100644 --- a/tests/Unit/Event/HtmlFilterEventTest.php +++ b/tests/Unit/Event/HtmlFilterEventTest.php @@ -30,7 +30,8 @@ class HtmlFilterEventTest extends TestCase [HtmlFilterEvent::PAGE_HEADER, 'friendica.html.page_header'], [HtmlFilterEvent::PAGE_CONTENT_TOP, 'friendica.html.page_content_top'], [HtmlFilterEvent::PAGE_END, 'friendica.html.page_end'], - [HtmlFilterEvent::ABOUT_CONTENT, 'friendica.html.about_content'], + [HtmlFilterEvent::MOD_HOME_CONTENT, 'friendica.html.mod_home_content'], + [HtmlFilterEvent::MOD_ABOUT_CONTENT, 'friendica.html.mod_about_content'], [HtmlFilterEvent::JOT_TOOL, 'friendica.html.jot_tool'], [HtmlFilterEvent::CONTACT_BLOCK_END, 'friendica.html.contact_block_end'], ]; From 27e474c83c3b626b220b0f815ef5d64d35b20c98 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 26 Mar 2025 08:30:20 +0000 Subject: [PATCH 28/45] create event for parse_link hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Module/ParseUrl.php | 24 +++++++++++++------ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 84835dc780..0beeec8667 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -54,6 +54,7 @@ final class HookEventBridge ArrayFilterEvent::PREPARE_POST_END => 'prepare_body_final', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', ArrayFilterEvent::NETWORK_TO_NAME => 'network_to_name', + ArrayFilterEvent::PARSE_LINK => 'parse_link', ArrayFilterEvent::CONVERSATION_START => 'conversation_start', ArrayFilterEvent::FETCH_ITEM_BY_LINK => 'item_by_link', ArrayFilterEvent::ITEM_TAGGED => 'tagged', @@ -130,6 +131,7 @@ final class HookEventBridge ArrayFilterEvent::PREPARE_POST_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', + ArrayFilterEvent::PARSE_LINK => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', ArrayFilterEvent::FETCH_ITEM_BY_LINK => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index de92599ee3..c9ae0fabe1 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -58,6 +58,8 @@ final class ArrayFilterEvent extends Event public const NETWORK_TO_NAME = 'friendica.data.network_to_name'; + public const PARSE_LINK = 'friendica.data.parse_link'; + public const CONVERSATION_START = 'friendica.data.conversation_start'; public const FETCH_ITEM_BY_LINK = 'friendica.data.fetch_item_by_link'; diff --git a/src/Module/ParseUrl.php b/src/Module/ParseUrl.php index 8da72c742c..508e5d987d 100644 --- a/src/Module/ParseUrl.php +++ b/src/Module/ParseUrl.php @@ -11,12 +11,13 @@ use Friendica\App\Arguments; use Friendica\App\BaseURL; use Friendica\BaseModule; use Friendica\Content\Text\BBCode; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Session\Capability\IHandleUserSessions; +use Friendica\Event\ArrayFilterEvent; use Friendica\Network\HTTPException\BadRequestException; use Friendica\Util; use Friendica\Util\Profiler; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; class ParseUrl extends BaseModule @@ -24,11 +25,14 @@ class ParseUrl extends BaseModule /** @var IHandleUserSessions */ protected $userSession; - public function __construct(L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, $server, array $parameters = []) + private EventDispatcherInterface $eventDispatcher; + + public function __construct(L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, EventDispatcherInterface $eventDispatcher, $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->userSession = $userSession; + $this->eventDispatcher = $eventDispatcher; } protected function rawContent(array $request = []) @@ -80,15 +84,21 @@ class ParseUrl extends BaseModule } } - $arr = ['url' => $url, 'format' => $format, 'text' => null]; + $hook_data = [ + 'url' => $url, + 'format' => $format, + 'text' => null, + ]; - Hook::callAll('parse_link', $arr); + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PARSE_LINK, $hook_data), + )->getArray(); - if ($arr['text']) { + if ($hook_data['text']) { if ($format == 'json') { - $this->jsonExit($arr['text']); + $this->jsonExit($hook_data['text']); } else { - $this->httpExit($arr['text']); + $this->httpExit($hook_data['text']); } } diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 075630da1c..bdfe5d5508 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -43,6 +43,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PREPARE_POST_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', + ArrayFilterEvent::PARSE_LINK => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', ArrayFilterEvent::FETCH_ITEM_BY_LINK => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_TAGGED => 'onArrayFilterEvent', @@ -460,6 +461,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::PREPARE_POST_END, 'prepare_body_final'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'], + [ArrayFilterEvent::PARSE_LINK, 'parse_link'], [ArrayFilterEvent::CONVERSATION_START, 'conversation_start'], [ArrayFilterEvent::FETCH_ITEM_BY_LINK, 'item_by_link'], [ArrayFilterEvent::ITEM_TAGGED, 'tagged'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 881deacd43..fafd1a66e0 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -40,6 +40,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PREPARE_POST_END, 'friendica.data.prepare_post_end'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'friendica.data.photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'friendica.data.network_to_name'], + [ArrayFilterEvent::PARSE_LINK, 'friendica.data.parse_link'], [ArrayFilterEvent::CONVERSATION_START, 'friendica.data.conversation_start'], [ArrayFilterEvent::FETCH_ITEM_BY_LINK, 'friendica.data.fetch_item_by_link'], [ArrayFilterEvent::ITEM_TAGGED, 'friendica.data.item_tagged'], From e4cb372760d04b9d346148010cac40d0af7da984 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 26 Mar 2025 08:58:23 +0000 Subject: [PATCH 29/45] Create events for register_form and register_post hook --- src/Core/Hooks/HookEventBridge.php | 4 +++ src/Event/ArrayFilterEvent.php | 4 +++ src/Module/Register.php | 27 ++++++++++++++----- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 4 +++ tests/Unit/Event/ArrayFilterEventTest.php | 2 ++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 0beeec8667..8b6c69c2c2 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -86,6 +86,8 @@ final class HookEventBridge ArrayFilterEvent::UNBLOCK_CONTACT => 'unblock', ArrayFilterEvent::AVATAR_LOOKUP => 'avatar_lookup', ArrayFilterEvent::ACCOUNT_AUTHENTICATE => 'authenticate', + ArrayFilterEvent::ACCOUNT_REGISTER_FORM => 'register_form', + ArrayFilterEvent::ACCOUNT_REGISTER_POST => 'register_post', ArrayFilterEvent::ACCOUNT_REGISTER => 'register_account', ArrayFilterEvent::ACCOUNT_REMOVE => 'remove_user', ArrayFilterEvent::EVENT_CREATED => 'event_created', @@ -163,6 +165,8 @@ final class HookEventBridge ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent', ArrayFilterEvent::ACCOUNT_AUTHENTICATE => 'onArrayFilterEvent', + ArrayFilterEvent::ACCOUNT_REGISTER_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::ACCOUNT_REGISTER_POST => 'onArrayFilterEvent', ArrayFilterEvent::ACCOUNT_REGISTER => 'onAccountRegisterEvent', ArrayFilterEvent::ACCOUNT_REMOVE => 'onAccountRemoveEvent', ArrayFilterEvent::EVENT_CREATED => 'onEventCreatedEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index c9ae0fabe1..40433328a3 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -122,6 +122,10 @@ final class ArrayFilterEvent extends Event public const ACCOUNT_AUTHENTICATE = 'friendica.data.account_authenticate'; + public const ACCOUNT_REGISTER_FORM = 'friendica.data.account_register_form'; + + public const ACCOUNT_REGISTER_POST = 'friendica.data.account_register_post'; + public const ACCOUNT_REGISTER = 'friendica.data.account_register'; public const ACCOUNT_REMOVE = 'friendica.data.account_remove'; diff --git a/src/Module/Register.php b/src/Module/Register.php index 1cea2ba9a4..6f641355e3 100644 --- a/src/Module/Register.php +++ b/src/Module/Register.php @@ -12,18 +12,19 @@ use Friendica\App\BaseURL; use Friendica\BaseModule; use Friendica\Content\Text\BBCode; use Friendica\Core\Config\Capability\IManageConfigValues; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model; use Friendica\Model\User; use Friendica\Util\DateTimeFormat; use Friendica\Util\Profiler; use Friendica\Util\Proxy; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** @@ -41,13 +42,16 @@ class Register extends BaseModule /** @var IHandleUserSessions */ private $session; - public function __construct(IHandleUserSessions $session, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManageConfigValues $config, array $server, array $parameters = []) + private EventDispatcherInterface $eventDispatcher; + + public function __construct(IHandleUserSessions $session, EventDispatcherInterface $eventDispatcher, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManageConfigValues $config, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->tos = new Tos($l10n, $baseUrl, $args, $logger, $profiler, $response, $config, $server, $parameters); $this->session = $session; + $this->eventDispatcher = $eventDispatcher; } /** @@ -129,11 +133,15 @@ class Register extends BaseModule $tpl = Renderer::getMarkupTemplate('register.tpl'); - $arr = ['template' => $tpl]; + $hook_data = [ + 'template' => $tpl, + ]; - Hook::callAll('register_form', $arr); + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ACCOUNT_REGISTER_FORM, $hook_data), + )->getArray(); - $tpl = $arr['template']; + $tpl = $hook_data['template'] ?? $tpl; $o = Renderer::replaceMacros($tpl, [ '$invitations' => DI::config()->get('system', 'invitation_only'), @@ -190,8 +198,13 @@ class Register extends BaseModule { BaseModule::checkFormSecurityTokenRedirectOnError('/register', 'register'); - $arr = ['post' => $_POST]; - Hook::callAll('register_post', $arr); + $arr = [ + 'post' => $_POST, + ]; + + $arr = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ACCOUNT_REGISTER_POST, $arr), + )->getArray(); $additional_account = false; diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index bdfe5d5508..7660a9d2a1 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -75,6 +75,8 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent', ArrayFilterEvent::ACCOUNT_AUTHENTICATE => 'onArrayFilterEvent', + ArrayFilterEvent::ACCOUNT_REGISTER_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::ACCOUNT_REGISTER_POST => 'onArrayFilterEvent', ArrayFilterEvent::ACCOUNT_REGISTER => 'onAccountRegisterEvent', ArrayFilterEvent::ACCOUNT_REMOVE => 'onAccountRemoveEvent', ArrayFilterEvent::EVENT_CREATED => 'onEventCreatedEvent', @@ -488,6 +490,8 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'], [ArrayFilterEvent::AVATAR_LOOKUP, 'avatar_lookup'], [ArrayFilterEvent::ACCOUNT_AUTHENTICATE, 'authenticate'], + [ArrayFilterEvent::ACCOUNT_REGISTER_FORM, 'register_form'], + [ArrayFilterEvent::ACCOUNT_REGISTER_POST, 'register_post'], [ArrayFilterEvent::ACCOUNT_REGISTER, 'register_account'], [ArrayFilterEvent::ACCOUNT_REMOVE, 'remove_user'], [ArrayFilterEvent::EVENT_CREATED, 'event_created'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index fafd1a66e0..b0c40e5129 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -72,6 +72,8 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::UNBLOCK_CONTACT, 'friendica.data.unblock_contact'], [ArrayFilterEvent::AVATAR_LOOKUP, 'friendica.data.avatar_lookup'], [ArrayFilterEvent::ACCOUNT_AUTHENTICATE, 'friendica.data.account_authenticate'], + [ArrayFilterEvent::ACCOUNT_REGISTER_FORM, 'friendica.data.account_register_form'], + [ArrayFilterEvent::ACCOUNT_REGISTER_POST, 'friendica.data.account_register_post'], [ArrayFilterEvent::ACCOUNT_REGISTER, 'friendica.data.account_register'], [ArrayFilterEvent::ACCOUNT_REMOVE, 'friendica.data.account_remove'], [ArrayFilterEvent::EVENT_CREATED, 'friendica.data.event_created'], From 584e7e05b028f0a31b007d2b34d5519feaffdaf0 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 28 Mar 2025 08:31:18 +0000 Subject: [PATCH 30/45] Create events for contact_edit and contact_edit_post hooks --- src/Core/Hooks/HookEventBridge.php | 4 + src/Event/ArrayFilterEvent.php | 4 + src/Module/Contact/Profile.php | 159 +++++++++++------- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 4 + tests/Unit/Event/ArrayFilterEventTest.php | 2 + 5 files changed, 109 insertions(+), 64 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 8b6c69c2c2..c74b0a8df2 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -84,6 +84,8 @@ final class HookEventBridge ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'revoke_follow', ArrayFilterEvent::BLOCK_CONTACT => 'block', ArrayFilterEvent::UNBLOCK_CONTACT => 'unblock', + ArrayFilterEvent::EDIT_CONTACT_FORM => 'contact_edit', + ArrayFilterEvent::EDIT_CONTACT_POST => 'contact_edit_post', ArrayFilterEvent::AVATAR_LOOKUP => 'avatar_lookup', ArrayFilterEvent::ACCOUNT_AUTHENTICATE => 'authenticate', ArrayFilterEvent::ACCOUNT_REGISTER_FORM => 'register_form', @@ -163,6 +165,8 @@ final class HookEventBridge ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::EDIT_CONTACT_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::EDIT_CONTACT_POST => 'onArrayFilterEvent', ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent', ArrayFilterEvent::ACCOUNT_AUTHENTICATE => 'onArrayFilterEvent', ArrayFilterEvent::ACCOUNT_REGISTER_FORM => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 40433328a3..9435ab37b6 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -118,6 +118,10 @@ final class ArrayFilterEvent extends Event public const UNBLOCK_CONTACT = 'friendica.data.unblock_contact'; + public const EDIT_CONTACT_FORM = 'friendica.data.edit_contact_form'; + + public const EDIT_CONTACT_POST = 'friendica.data.edit_contact_post'; + public const AVATAR_LOOKUP = 'friendica.data.avatar_lookup'; public const ACCOUNT_AUTHENTICATE = 'friendica.data.account_authenticate'; diff --git a/src/Module/Contact/Profile.php b/src/Module/Contact/Profile.php index a176a20684..39029a25a8 100644 --- a/src/Module/Contact/Profile.php +++ b/src/Module/Contact/Profile.php @@ -7,31 +7,37 @@ namespace Friendica\Module\Contact; -use Friendica\App; +use Friendica\App\Arguments; +use Friendica\App\BaseURL; +use Friendica\App\Page; use Friendica\BaseModule; -use Friendica\Contact\LocalRelationship; use Friendica\Contact\LocalRelationship\Entity\LocalRelationship as LocalRelationshipEntity; +use Friendica\Contact\LocalRelationship\Repository\LocalRelationship as LocalRelationshipRepository; use Friendica\Content\ContactSelector; use Friendica\Content\Nav; use Friendica\Content\Text\BBCode; use Friendica\Content\Widget; use Friendica\Core\Config\Capability\IManageConfigValues; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Protocol; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Circle; -use Friendica\Model\Contact; -use Friendica\Module; +use Friendica\Model\Contact as ContactModel; +use Friendica\Model\Contact\User as UserContact; +use Friendica\Module\Contact as ContactModule; use Friendica\Module\Response; +use Friendica\Module\Security\Login; use Friendica\Navigation\SystemMessages; -use Friendica\Network\HTTPException; -use Friendica\User\Settings; +use Friendica\Network\HTTPException\InternalServerErrorException; +use Friendica\Network\HTTPException\NotFoundException; +use Friendica\User\Settings\Repository\UserGServer as UserGServerRepository; use Friendica\Util\DateTimeFormat; use Friendica\Util\Profiler; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** @@ -39,9 +45,9 @@ use Psr\Log\LoggerInterface; */ class Profile extends BaseModule { - /** @var LocalRelationship\Repository\LocalRelationship */ + /** @var LocalRelationshipRepository */ private $localRelationship; - /** @var App\Page */ + /** @var Page */ private $page; /** @var IManageConfigValues */ private $config; @@ -51,11 +57,28 @@ class Profile extends BaseModule private $systemMessages; /** @var Database */ private $db; - /** @var Settings\Repository\UserGServer */ + /** @var UserGServerRepository */ private $userGServer; + private EventDispatcherInterface $eventDispatcher; - public function __construct(Settings\Repository\UserGServer $userGServer, Database $db, SystemMessages $systemMessages, IHandleUserSessions $session, L10n $l10n, LocalRelationship\Repository\LocalRelationship $localRelationship, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, App\Page $page, IManageConfigValues $config, array $server, array $parameters = []) - { + public function __construct( + UserGServerRepository $userGServer, + EventDispatcherInterface $eventDispatcher, + Database $db, + SystemMessages $systemMessages, + IHandleUserSessions $session, + L10n $l10n, + LocalRelationshipRepository $localRelationship, + BaseURL $baseUrl, + Arguments $args, + LoggerInterface $logger, + Profiler $profiler, + Response $response, + Page $page, + IManageConfigValues $config, + array $server, + array $parameters = [], + ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->localRelationship = $localRelationship; @@ -65,6 +88,7 @@ class Profile extends BaseModule $this->systemMessages = $systemMessages; $this->db = $db; $this->userGServer = $userGServer; + $this->eventDispatcher = $eventDispatcher; } protected function post(array $request = []) @@ -77,12 +101,14 @@ class Profile extends BaseModule // Backward compatibility: The update still needs a user-specific contact ID // Change to user-contact table check by version 2022.03 - $ucid = Contact::getUserContactId($contact_id, $this->session->getLocalUserId()); + $ucid = ContactModel::getUserContactId($contact_id, $this->session->getLocalUserId()); if (!$ucid || !$this->db->exists('contact', ['id' => $ucid, 'deleted' => false])) { return; } - Hook::callAll('contact_edit_post', $request); + $request = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::EDIT_CONTACT_POST, $request), + )->getArray(); $fields = []; @@ -120,14 +146,14 @@ class Profile extends BaseModule } if (isset($request['channel_frequency'])) { - Contact\User::setChannelFrequency($ucid, $this->session->getLocalUserId(), $request['channel_frequency']); + UserContact::setChannelFrequency($ucid, $this->session->getLocalUserId(), $request['channel_frequency']); } if (isset($request['channel_only'])) { - Contact\User::setChannelOnly($ucid, $this->session->getLocalUserId(), $request['channel_only']); + UserContact::setChannelOnly($ucid, $this->session->getLocalUserId(), $request['channel_only']); } - if (!Contact::update($fields, ['id' => $ucid, 'uid' => $this->session->getLocalUserId()])) { + if (!ContactModel::update($fields, ['id' => $ucid, 'uid' => $this->session->getLocalUserId()])) { $this->systemMessages->addNotice($this->t('Failed to update contact record.')); } $this->baseUrl->redirect('contact/' . $contact_id); @@ -136,43 +162,43 @@ class Profile extends BaseModule protected function content(array $request = []): string { if (!$this->session->getLocalUserId()) { - return Module\Security\Login::form($_SERVER['REQUEST_URI']); + return Login::form($_SERVER['REQUEST_URI']); } // Backward compatibility: Ensure to use the public contact when the user contact is provided // Remove by version 2022.03 - $data = Contact::getPublicAndUserContactID(intval($this->parameters['id']), $this->session->getLocalUserId()); + $data = ContactModel::getPublicAndUserContactID(intval($this->parameters['id']), $this->session->getLocalUserId()); if (empty($data)) { - throw new HTTPException\NotFoundException($this->t('Contact not found.')); + throw new NotFoundException($this->t('Contact not found.')); } - $contact = Contact::getById($data['public']); + $contact = ContactModel::getById($data['public']); if (!$this->db->isResult($contact)) { - throw new HTTPException\NotFoundException($this->t('Contact not found.')); + throw new NotFoundException($this->t('Contact not found.')); } // Fetch the protocol from the user's contact. if ($data['user']) { - $usercontact = Contact::getById($data['user'], ['network', 'protocol']); + $usercontact = ContactModel::getById($data['user'], ['network', 'protocol']); if ($this->db->isResult($usercontact)) { $contact['network'] = $usercontact['network']; $contact['protocol'] = $usercontact['protocol']; } } - if (empty($contact['network']) && Contact::isLocal($contact['url']) ) { + if (empty($contact['network']) && ContactModel::isLocal($contact['url']) ) { $contact['network'] = Protocol::DFRN; $contact['protocol'] = Protocol::ACTIVITYPUB; } // Don't display contacts that are about to be deleted if ($contact['deleted'] || $contact['network'] == Protocol::PHANTOM) { - throw new HTTPException\NotFoundException($this->t('Contact not found.')); + throw new NotFoundException($this->t('Contact not found.')); } $localRelationship = $this->localRelationship->getForUserContact($this->session->getLocalUserId(), $contact['id']); - if ($localRelationship->rel === Contact::SELF) { + if ($localRelationship->rel === ContactModel::SELF) { $this->baseUrl->redirect('profile/' . $contact['nick'] . '/profile'); } @@ -180,8 +206,8 @@ class Profile extends BaseModule self::checkFormSecurityTokenRedirectOnError('contact/' . $contact['id'], 'contact_action', 't'); $cmd = $this->parameters['action']; - if ($cmd === 'update' && $localRelationship->rel !== Contact::NOTHING) { - Module\Contact::updateContactFromPoll($contact['id']); + if ($cmd === 'update' && $localRelationship->rel !== ContactModel::NOTHING) { + ContactModule::updateContactFromPoll($contact['id']); } if ($cmd === 'updateprofile') { @@ -191,12 +217,12 @@ class Profile extends BaseModule if ($cmd === 'block') { if ($localRelationship->blocked) { // @TODO Backward compatibility, replace with $localRelationship->unblock() - Contact\User::setBlocked($contact['id'], $this->session->getLocalUserId(), false); + UserContact::setBlocked($contact['id'], $this->session->getLocalUserId(), false); $message = $this->t('Contact has been unblocked'); } else { // @TODO Backward compatibility, replace with $localRelationship->block() - Contact\User::setBlocked($contact['id'], $this->session->getLocalUserId(), true); + UserContact::setBlocked($contact['id'], $this->session->getLocalUserId(), true); $message = $this->t('Contact has been blocked'); } @@ -207,12 +233,12 @@ class Profile extends BaseModule if ($cmd === 'ignore') { if ($localRelationship->ignored) { // @TODO Backward compatibility, replace with $localRelationship->unblock() - Contact\User::setIgnored($contact['id'], $this->session->getLocalUserId(), false); + UserContact::setIgnored($contact['id'], $this->session->getLocalUserId(), false); $message = $this->t('Contact has been unignored'); } else { // @TODO Backward compatibility, replace with $localRelationship->block() - Contact\User::setIgnored($contact['id'], $this->session->getLocalUserId(), true); + UserContact::setIgnored($contact['id'], $this->session->getLocalUserId(), true); $message = $this->t('Contact has been ignored'); } @@ -223,12 +249,12 @@ class Profile extends BaseModule if ($cmd === 'collapse') { if ($localRelationship->collapsed) { // @TODO Backward compatibility, replace with $localRelationship->unblock() - Contact\User::setCollapsed($contact['id'], $this->session->getLocalUserId(), false); + UserContact::setCollapsed($contact['id'], $this->session->getLocalUserId(), false); $message = $this->t('Contact has been uncollapsed'); } else { // @TODO Backward compatibility, replace with $localRelationship->block() - Contact\User::setCollapsed($contact['id'], $this->session->getLocalUserId(), true); + UserContact::setCollapsed($contact['id'], $this->session->getLocalUserId(), true); $message = $this->t('Contact has been collapsed'); } @@ -242,7 +268,7 @@ class Profile extends BaseModule $vcard_widget = Widget\VCard::getHTML($contact); $circles_widget = ''; - if (!in_array($localRelationship->rel, [Contact::NOTHING, Contact::SELF])) { + if (!in_array($localRelationship->rel, [ContactModel::NOTHING, ContactModel::SELF])) { $circles_widget = Circle::sidebarWidget('contact', 'circle', 'full', 'everyone', $data['user']); } @@ -257,9 +283,9 @@ class Profile extends BaseModule ]); switch ($localRelationship->rel) { - case Contact::FRIEND: $relation_text = $this->t('You are mutual friends with %s', $contact['name']); break; - case Contact::FOLLOWER: $relation_text = $this->t('You are sharing with %s', $contact['name']); break; - case Contact::SHARING: $relation_text = $this->t('%s is sharing with you', $contact['name']); break; + case ContactModel::FRIEND: $relation_text = $this->t('You are mutual friends with %s', $contact['name']); break; + case ContactModel::FOLLOWER: $relation_text = $this->t('You are sharing with %s', $contact['name']); break; + case ContactModel::SHARING: $relation_text = $this->t('%s is sharing with you', $contact['name']); break; default: $relation_text = ''; } @@ -268,7 +294,7 @@ class Profile extends BaseModule $relation_text = ''; } - $url = Contact::magicLinkByContact($contact); + $url = ContactModel::magicLinkByContact($contact); if (strpos($url, 'contact/redir/') === 0) { $sparkle = ' class="sparkle" '; } else { @@ -300,7 +326,7 @@ class Profile extends BaseModule $nettype = $this->t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['protocol'], $contact['gsid'])); // tabs - $tab_str = Module\Contact::getTabsHTML($contact, Module\Contact::TAB_PROFILE); + $tab_str = ContactModule::getTabsHTML($contact, ContactModule::TAB_PROFILE); $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? $this->t('Communications lost with this contact!') : ''); @@ -312,10 +338,10 @@ class Profile extends BaseModule $localRelationship->fetchFurtherInformation, $this->t('Fetch information like preview pictures, title and teaser from the feed item. You can activate this if the feed doesn\'t contain much text. Keywords are taken from the meta header in the feed item and are posted as hash tags.'), [ - LocalRelationship\Entity\LocalRelationship::FFI_NONE => $this->t('Disabled'), - LocalRelationship\Entity\LocalRelationship::FFI_INFORMATION => $this->t('Fetch information'), - LocalRelationship\Entity\LocalRelationship::FFI_KEYWORD => $this->t('Fetch keywords'), - LocalRelationship\Entity\LocalRelationship::FFI_BOTH => $this->t('Fetch information and keywords') + LocalRelationshipEntity::FFI_NONE => $this->t('Disabled'), + LocalRelationshipEntity::FFI_INFORMATION => $this->t('Fetch information'), + LocalRelationshipEntity::FFI_KEYWORD => $this->t('Fetch keywords'), + LocalRelationshipEntity::FFI_BOTH => $this->t('Fetch information and keywords') ] ]; } @@ -346,8 +372,8 @@ class Profile extends BaseModule ]; } - $channel_frequency = Contact\User::getChannelFrequency($contact['id'], $this->session->getLocalUserId()); - $channel_only = Contact\User::getChannelOnly($contact['id'], $this->session->getLocalUserId()); + $channel_frequency = UserContact::getChannelFrequency($contact['id'], $this->session->getLocalUserId()); + $channel_only = UserContact::getChannelOnly($contact['id'], $this->session->getLocalUserId()); $poll_interval = null; if ((($contact['network'] == Protocol::FEED) && !$this->config->get('system', 'adjust_poll_frequency')) || ($contact['network'] == Protocol::MAIL)) { @@ -356,12 +382,12 @@ class Profile extends BaseModule $contact_actions = $this->getContactActions($contact, $localRelationship); - if (Contact\User::isIsBlocked($contact['id'], $this->session->getLocalUserId())) { + if (UserContact::isIsBlocked($contact['id'], $this->session->getLocalUserId())) { $relation_text = $this->t('%s has blocked you', $contact['name'] ?: $contact['nick']); unset($contact_actions['follow']); } - if ($localRelationship->rel !== Contact::NOTHING) { + if ($localRelationship->rel !== ContactModel::NOTHING) { $lbl_info1 = $this->t('Contact Information / Notes'); $contact_settings_label = $this->t('Contact Settings'); } else { @@ -407,13 +433,13 @@ class Profile extends BaseModule '$notify_new_posts' => ['notify_new_posts', $this->t('Notification for new posts'), ($localRelationship->notifyNewPosts), $this->t('Send a notification of every new post of this contact')], '$fetch_further_information' => $fetch_further_information, '$ffi_keyword_denylist' => ['ffi_keyword_denylist', $this->t('Keyword Deny List'), $localRelationship->ffiKeywordDenylist, $this->t('Comma separated list of keywords that should not be converted to hashtags, when "Fetch information and keywords" is selected')], - '$photo' => Contact::getPhoto($contact), + '$photo' => ContactModel::getPhoto($contact), '$name' => $contact['name'], '$sparkle' => $sparkle, '$url' => $url, '$profileurllabel' => $this->t('Profile URL'), '$profileurl' => $contact['url'], - '$account_type' => Contact::getAccountType($contact['contact-type']), + '$account_type' => ContactModel::getAccountType($contact['contact-type']), '$location' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['location']), '$location_label' => $this->t('Location:'), '$xmpp' => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['xmpp']), @@ -440,18 +466,23 @@ class Profile extends BaseModule '$channel_settings_label' => $this->t('Channel Settings'), '$frequency_label' => $this->t('Frequency of this contact in relevant channels'), '$frequency_description' => $this->t("Depending on the type of the channel not all posts from this contact are displayed. By default, posts need to have a minimum amount of interactions (comments, likes) to show in your channels. On the other hand there can be contacts who flood the channel, so you might want to see only some of their posts. Or you don't want to see their content at all, but you don't want to block or hide the contact completely."), - '$frequency_default' => ['channel_frequency', $this->t('Default frequency'), Contact\User::FREQUENCY_DEFAULT, $this->t('Posts by this contact are displayed in the "for you" channel if you interact often with this contact or if a post reached some level of interaction.'), $channel_frequency == Contact\User::FREQUENCY_DEFAULT], - '$frequency_always' => ['channel_frequency', $this->t('Display all posts of this contact'), Contact\User::FREQUENCY_ALWAYS, $this->t('All posts from this contact will appear on the "for you" channel'), $channel_frequency == Contact\User::FREQUENCY_ALWAYS], - '$frequency_reduced' => ['channel_frequency', $this->t('Display only few posts'), Contact\User::FREQUENCY_REDUCED, $this->t('When a contact creates a lot of posts in a short period, this setting reduces the number of displayed posts in every channel.'), $channel_frequency == Contact\User::FREQUENCY_REDUCED], - '$frequency_never' => ['channel_frequency', $this->t('Never display posts'), Contact\User::FREQUENCY_NEVER, $this->t('Posts from this contact will never be displayed in any channel'), $channel_frequency == Contact\User::FREQUENCY_NEVER], + '$frequency_default' => ['channel_frequency', $this->t('Default frequency'), UserContact::FREQUENCY_DEFAULT, $this->t('Posts by this contact are displayed in the "for you" channel if you interact often with this contact or if a post reached some level of interaction.'), $channel_frequency == UserContact::FREQUENCY_DEFAULT], + '$frequency_always' => ['channel_frequency', $this->t('Display all posts of this contact'), UserContact::FREQUENCY_ALWAYS, $this->t('All posts from this contact will appear on the "for you" channel'), $channel_frequency == UserContact::FREQUENCY_ALWAYS], + '$frequency_reduced' => ['channel_frequency', $this->t('Display only few posts'), UserContact::FREQUENCY_REDUCED, $this->t('When a contact creates a lot of posts in a short period, this setting reduces the number of displayed posts in every channel.'), $channel_frequency == UserContact::FREQUENCY_REDUCED], + '$frequency_never' => ['channel_frequency', $this->t('Never display posts'), UserContact::FREQUENCY_NEVER, $this->t('Posts from this contact will never be displayed in any channel'), $channel_frequency == UserContact::FREQUENCY_NEVER], '$channel_only' => ['channel_only', $this->t('Channel Only'), $channel_only, $this->t('If enabled, posts from this contact will only appear in channels and network streams in circles, but not in the general network stream.')], ]); - $arr = ['contact' => $contact, 'output' => $o]; + $hook_data = [ + 'contact' => $contact, + 'output' => $o, + ]; - Hook::callAll('contact_edit', $arr); + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::EDIT_CONTACT_FORM, $hook_data), + )->getArray(); - return $arr['output']; + return $hook_data['output'] ?? $o; } /** @@ -460,18 +491,18 @@ class Profile extends BaseModule * This includes actions like e.g. 'block', 'hide', 'delete' and others * * @param array $contact Public contact row - * @param LocalRelationship\Entity\LocalRelationship $localRelationship + * * @return array with contact related actions - * @throws HTTPException\InternalServerErrorException + * @throws InternalServerErrorException */ - private function getContactActions(array $contact, LocalRelationship\Entity\LocalRelationship $localRelationship): array + private function getContactActions(array $contact, LocalRelationshipEntity $localRelationship): array { $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::FEED, Protocol::MAIL]); $contact_actions = []; $formSecurityToken = self::getFormSecurityToken('contact_action'); - if ($localRelationship->rel & Contact::SHARING) { + if ($localRelationship->rel & ContactModel::SHARING) { $contact_actions['unfollow'] = [ 'label' => $this->t('Unfollow'), 'url' => 'contact/unfollow?url=' . urlencode($contact['url']) . '&auto=1', @@ -544,7 +575,7 @@ class Profile extends BaseModule 'id' => 'toggle-collapse', ]; - if (Protocol::supportsRevokeFollow($contact['network']) && in_array($localRelationship->rel, [Contact::FOLLOWER, Contact::FRIEND])) { + if (Protocol::supportsRevokeFollow($contact['network']) && in_array($localRelationship->rel, [ContactModel::FOLLOWER, ContactModel::FRIEND])) { $contact_actions['revoke_follow'] = [ 'label' => $this->t('Revoke Follow'), 'url' => 'contact/' . $contact['id'] . '/revoke', @@ -562,7 +593,7 @@ class Profile extends BaseModule * * @param int $contact_id Id of the contact with uid != 0 * @return void - * @throws HTTPException\InternalServerErrorException + * @throws InternalServerErrorException * @throws \ImagickException */ private function updateContactFromProbe(int $contact_id) @@ -572,6 +603,6 @@ class Profile extends BaseModule } // Update the entry in the contact table - Contact::updateFromProbe($contact_id); + ContactModel::updateFromProbe($contact_id); } } diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 7660a9d2a1..348124b389 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -73,6 +73,8 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::REVOKE_FOLLOW_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::BLOCK_CONTACT => 'onArrayFilterEvent', ArrayFilterEvent::UNBLOCK_CONTACT => 'onArrayFilterEvent', + ArrayFilterEvent::EDIT_CONTACT_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::EDIT_CONTACT_POST => 'onArrayFilterEvent', ArrayFilterEvent::AVATAR_LOOKUP => 'onArrayFilterEvent', ArrayFilterEvent::ACCOUNT_AUTHENTICATE => 'onArrayFilterEvent', ArrayFilterEvent::ACCOUNT_REGISTER_FORM => 'onArrayFilterEvent', @@ -488,6 +490,8 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::REVOKE_FOLLOW_CONTACT, 'revoke_follow'], [ArrayFilterEvent::BLOCK_CONTACT, 'block'], [ArrayFilterEvent::UNBLOCK_CONTACT, 'unblock'], + [ArrayFilterEvent::EDIT_CONTACT_FORM, 'contact_edit'], + [ArrayFilterEvent::EDIT_CONTACT_POST, 'contact_edit_post'], [ArrayFilterEvent::AVATAR_LOOKUP, 'avatar_lookup'], [ArrayFilterEvent::ACCOUNT_AUTHENTICATE, 'authenticate'], [ArrayFilterEvent::ACCOUNT_REGISTER_FORM, 'register_form'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index b0c40e5129..a64080198e 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -70,6 +70,8 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::REVOKE_FOLLOW_CONTACT, 'friendica.data.revoke_follow_contact'], [ArrayFilterEvent::BLOCK_CONTACT, 'friendica.data.block_contact'], [ArrayFilterEvent::UNBLOCK_CONTACT, 'friendica.data.unblock_contact'], + [ArrayFilterEvent::EDIT_CONTACT_FORM, 'friendica.data.edit_contact_form'], + [ArrayFilterEvent::EDIT_CONTACT_POST, 'friendica.data.edit_contact_post'], [ArrayFilterEvent::AVATAR_LOOKUP, 'friendica.data.avatar_lookup'], [ArrayFilterEvent::ACCOUNT_AUTHENTICATE, 'friendica.data.account_authenticate'], [ArrayFilterEvent::ACCOUNT_REGISTER_FORM, 'friendica.data.account_register_form'], From a34a93c0b98c9e3c4890c21b25a3d68bd007206a Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 2 Apr 2025 12:41:22 +0000 Subject: [PATCH 31/45] Create events for networt_tabs and network_content_init hooks --- src/Core/Hooks/HookEventBridge.php | 4 + src/Event/ArrayFilterEvent.php | 4 + src/Module/Conversation/Network.php | 73 ++++++++++++++++--- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 4 + tests/Unit/Event/ArrayFilterEventTest.php | 2 + 5 files changed, 78 insertions(+), 9 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index c74b0a8df2..72204ab11e 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -54,6 +54,8 @@ final class HookEventBridge ArrayFilterEvent::PREPARE_POST_END => 'prepare_body_final', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', ArrayFilterEvent::NETWORK_TO_NAME => 'network_to_name', + ArrayFilterEvent::NETWORK_CONTENT_START => 'network_content_init', + ArrayFilterEvent::NETWORK_CONTENT_TABS => 'network_tabs', ArrayFilterEvent::PARSE_LINK => 'parse_link', ArrayFilterEvent::CONVERSATION_START => 'conversation_start', ArrayFilterEvent::FETCH_ITEM_BY_LINK => 'item_by_link', @@ -135,6 +137,8 @@ final class HookEventBridge ArrayFilterEvent::PREPARE_POST_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', + ArrayFilterEvent::NETWORK_CONTENT_START => 'onArrayFilterEvent', + ArrayFilterEvent::NETWORK_CONTENT_TABS => 'onArrayFilterEvent', ArrayFilterEvent::PARSE_LINK => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', ArrayFilterEvent::FETCH_ITEM_BY_LINK => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 9435ab37b6..01a9a90675 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -58,6 +58,10 @@ final class ArrayFilterEvent extends Event public const NETWORK_TO_NAME = 'friendica.data.network_to_name'; + public const NETWORK_CONTENT_START = 'friendica.data.network_content_start'; + + public const NETWORK_CONTENT_TABS = 'friendica.data.network_content_tabs'; + public const PARSE_LINK = 'friendica.data.parse_link'; public const CONVERSATION_START = 'friendica.data.conversation_start'; diff --git a/src/Module/Conversation/Network.php b/src/Module/Conversation/Network.php index 86c2fcf736..e50d2f4d61 100644 --- a/src/Module/Conversation/Network.php +++ b/src/Module/Conversation/Network.php @@ -39,6 +39,7 @@ use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\Database\Database; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Circle; use Friendica\Model\Post; @@ -49,6 +50,7 @@ use Friendica\Network\HTTPException; use Friendica\Navigation\SystemMessages; use Friendica\Util\DateTimeFormat; use Friendica\Util\Profiler; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; class Network extends Timeline @@ -90,12 +92,55 @@ class Network extends Timeline protected $community; /** @var NetworkFactory */ protected $networkFactory; + private EventDispatcherInterface $eventDispatcher; - public function __construct(UserDefinedChannelFactory $userDefinedChannel, NetworkFactory $network, CommunityFactory $community, ChannelFactory $channelFactory, UserDefinedChannel $channel, AppHelper $appHelper, TimelineFactory $timeline, SystemMessages $systemMessages, Mode $mode, Conversation $conversation, Page $page, IHandleUserSessions $session, Database $database, IManagePersonalConfigValues $pConfig, IManageConfigValues $config, ICanCache $cache, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) - { - parent::__construct($channel, $mode, $session, $database, $pConfig, $config, $cache, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + public function __construct( + UserDefinedChannelFactory $userDefinedChannel, + NetworkFactory $network, + CommunityFactory $community, + ChannelFactory $channelFactory, + UserDefinedChannel $channel, + AppHelper $appHelper, + EventDispatcherInterface $eventDispatcher, + TimelineFactory $timeline, + SystemMessages $systemMessages, + Mode $mode, + Conversation $conversation, + Page $page, + IHandleUserSessions $session, + Database $database, + IManagePersonalConfigValues $pConfig, + IManageConfigValues $config, + ICanCache $cache, + L10n $l10n, + BaseURL $baseUrl, + Arguments $args, + LoggerInterface $logger, + Profiler $profiler, + Response $response, + array $server, + array $parameters = [] + ) { + parent::__construct( + $channel, + $mode, + $session, + $database, + $pConfig, + $config, + $cache, + $l10n, + $baseUrl, + $args, + $logger, + $profiler, + $response, + $server, + $parameters, + ); $this->appHelper = $appHelper; + $this->eventDispatcher = $eventDispatcher; $this->timeline = $timeline; $this->systemMessages = $systemMessages; $this->conversation = $conversation; @@ -116,8 +161,13 @@ class Network extends Timeline $module = 'network'; - $arr = ['query' => $this->args->getQueryString()]; - Hook::callAll('network_content_init', $arr); + $hook_data = [ + 'query' => $this->args->getQueryString(), + ]; + + $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::NETWORK_CONTENT_START, $hook_data) + ); $o = ''; @@ -275,19 +325,24 @@ class Network extends Timeline $tabs = array_merge($tabs, $this->getTabArray($this->community->getTimelines(true), 'network', 'channel')); } - $arr = ['tabs' => $tabs]; - Hook::callAll('network_tabs', $arr); + $hook_data = [ + 'tabs' => $tabs, + ]; + + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::NETWORK_CONTENT_TABS, $hook_data) + )->getArray(); if (!empty($network_timelines)) { $tabs = []; - foreach ($arr['tabs'] as $tab) { + foreach ($hook_data['tabs'] as $tab) { if (in_array($tab['code'], $network_timelines)) { $tabs[] = $tab; } } } else { - $tabs = $arr['tabs']; + $tabs = $hook_data['tabs']; } $tpl = Renderer::getMarkupTemplate('common_tabs.tpl'); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 348124b389..7b97e76a02 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -43,6 +43,8 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PREPARE_POST_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', + ArrayFilterEvent::NETWORK_CONTENT_START => 'onArrayFilterEvent', + ArrayFilterEvent::NETWORK_CONTENT_TABS => 'onArrayFilterEvent', ArrayFilterEvent::PARSE_LINK => 'onArrayFilterEvent', ArrayFilterEvent::CONVERSATION_START => 'onArrayFilterEvent', ArrayFilterEvent::FETCH_ITEM_BY_LINK => 'onArrayFilterEvent', @@ -465,6 +467,8 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::PREPARE_POST_END, 'prepare_body_final'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'], + [ArrayFilterEvent::NETWORK_CONTENT_START, 'network_content_init'], + [ArrayFilterEvent::NETWORK_CONTENT_TABS, 'network_tabs'], [ArrayFilterEvent::PARSE_LINK, 'parse_link'], [ArrayFilterEvent::CONVERSATION_START, 'conversation_start'], [ArrayFilterEvent::FETCH_ITEM_BY_LINK, 'item_by_link'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index a64080198e..4e1a800435 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -40,6 +40,8 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PREPARE_POST_END, 'friendica.data.prepare_post_end'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'friendica.data.photo_upload_form'], [ArrayFilterEvent::NETWORK_TO_NAME, 'friendica.data.network_to_name'], + [ArrayFilterEvent::NETWORK_CONTENT_START, 'friendica.data.network_content_start'], + [ArrayFilterEvent::NETWORK_CONTENT_TABS, 'friendica.data.network_content_tabs'], [ArrayFilterEvent::PARSE_LINK, 'friendica.data.parse_link'], [ArrayFilterEvent::CONVERSATION_START, 'friendica.data.conversation_start'], [ArrayFilterEvent::FETCH_ITEM_BY_LINK, 'friendica.data.fetch_item_by_link'], From 2a7ebc860c5640aaea8b9d8e52affad3f85ce858 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 9 Apr 2025 14:25:00 +0000 Subject: [PATCH 32/45] Create event for moderation_users_tabs hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/ArrayFilterEvent.php | 2 ++ src/Module/Moderation/BaseUsers.php | 20 ++++++++++++++++--- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 72204ab11e..6dc6aeb667 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -71,6 +71,7 @@ final class HookEventBridge ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'profile_sidebar_enter', ArrayFilterEvent::PROFILE_SIDEBAR => 'profile_sidebar', ArrayFilterEvent::PROFILE_TABS => 'profile_tabs', + ArrayFilterEvent::MODERATION_USERS_TABS => 'moderation_users_tabs', ArrayFilterEvent::OEMBED_FETCH_END => 'oembed_fetch_url', ArrayFilterEvent::PAGE_INFO => 'page_info_data', ArrayFilterEvent::SMILEY_LIST => 'smilie', @@ -154,6 +155,7 @@ final class HookEventBridge ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_TABS => 'onArrayFilterEvent', + ArrayFilterEvent::MODERATION_USERS_TABS => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 01a9a90675..cb14052bb2 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -92,6 +92,8 @@ final class ArrayFilterEvent extends Event public const PROFILE_TABS = 'friendica.data.profile_tabs'; + public const MODERATION_USERS_TABS = 'friendica.data.moderation_users_tabs'; + public const OEMBED_FETCH_END = 'friendica.data.oembed_fetch_end'; public const PAGE_INFO = 'friendica.data.page_info'; diff --git a/src/Module/Moderation/BaseUsers.php b/src/Module/Moderation/BaseUsers.php index fcf4b4aa16..6f9e4a800d 100644 --- a/src/Module/Moderation/BaseUsers.php +++ b/src/Module/Moderation/BaseUsers.php @@ -16,6 +16,8 @@ use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\Database; +use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Register; use Friendica\Model\User; use Friendica\Module\BaseModeration; @@ -95,11 +97,23 @@ abstract class BaseUsers extends BaseModeration 'accesskey' => 'd', ], ]; - $tabs_arr = ['tabs' => $tabs, 'selectedTab' => $selectedTab]; - Hook::callAll('moderation_users_tabs', $tabs_arr); + + $hook_data = [ + 'tabs' => $tabs, + 'selectedTab' => $selectedTab, + ]; + + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::MODERATION_USERS_TABS, $hook_data), + )->getArray(); + + $tabs = $hook_data['tabs'] ?? $tabs; $tpl = Renderer::getMarkupTemplate('common_tabs.tpl'); - return Renderer::replaceMacros($tpl, ['$tabs' => $tabs_arr['tabs'], '$more' => $this->t('More')]); + + return Renderer::replaceMacros($tpl, ['$tabs' => $tabs, '$more' => $this->t('More')]); } protected function setupUserCallback(): \Closure diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 7b97e76a02..ae98a2fc0e 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -60,6 +60,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_TABS => 'onArrayFilterEvent', + ArrayFilterEvent::MODERATION_USERS_TABS => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', @@ -483,6 +484,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'contact_photo_menu'], [ArrayFilterEvent::PROFILE_SIDEBAR, 'profile_sidebar'], [ArrayFilterEvent::PROFILE_TABS, 'profile_tabs'], + [ArrayFilterEvent::MODERATION_USERS_TABS, 'moderation_users_tabs'], [ArrayFilterEvent::PAGE_INFO, 'page_info_data'], [ArrayFilterEvent::SMILEY_LIST, 'smilie'], [ArrayFilterEvent::JOT_NETWORKS, 'jot_networks'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 4e1a800435..4f20c37543 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -57,6 +57,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, 'friendica.data.profile_sidebar_entry'], [ArrayFilterEvent::PROFILE_SIDEBAR, 'friendica.data.profile_sidebar'], [ArrayFilterEvent::PROFILE_TABS, 'friendica.data.profile_tabs'], + [ArrayFilterEvent::MODERATION_USERS_TABS, 'friendica.data.moderation_users_tabs'], [ArrayFilterEvent::OEMBED_FETCH_END, 'friendica.data.oembed_fetch_end'], [ArrayFilterEvent::PAGE_INFO, 'friendica.data.page_info'], [ArrayFilterEvent::SMILEY_LIST, 'friendica.data.smiley_list'], From f08aef86f181c2cfa1a7571ef68ee463c1101347 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 9 Apr 2025 14:38:54 +0000 Subject: [PATCH 33/45] set EventDispatcher via constructor injection --- src/Module/Moderation/BaseUsers.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Module/Moderation/BaseUsers.php b/src/Module/Moderation/BaseUsers.php index 6f9e4a800d..283fa74ec2 100644 --- a/src/Module/Moderation/BaseUsers.php +++ b/src/Module/Moderation/BaseUsers.php @@ -11,12 +11,10 @@ use Friendica\App\Arguments; use Friendica\App\BaseURL; use Friendica\App\Page; use Friendica\AppHelper; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\Database; -use Friendica\DI; use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Register; use Friendica\Model\User; @@ -26,6 +24,7 @@ use Friendica\Navigation\SystemMessages; use Friendica\Network\HTTPException\ServiceUnavailableException; use Friendica\Util\Profiler; use Friendica\Util\Temporal; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; abstract class BaseUsers extends BaseModeration @@ -33,11 +32,28 @@ abstract class BaseUsers extends BaseModeration /** @var Database */ protected $database; - public function __construct(Database $database, Page $page, AppHelper $appHelper, SystemMessages $systemMessages, IHandleUserSessions $session, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) - { + private EventDispatcherInterface $eventDispatcher; + + public function __construct( + Database $database, + EventDispatcherInterface $eventDispatcher, + Page $page, + AppHelper $appHelper, + SystemMessages $systemMessages, + IHandleUserSessions $session, + L10n $l10n, + BaseURL $baseUrl, + Arguments $args, + LoggerInterface $logger, + Profiler $profiler, + Response $response, + array $server, + array $parameters = [], + ) { parent::__construct($page, $appHelper, $systemMessages, $session, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->database = $database; + $this->eventDispatcher = $eventDispatcher; } /** @@ -103,9 +119,7 @@ abstract class BaseUsers extends BaseModeration 'selectedTab' => $selectedTab, ]; - $eventDispatcher = DI::eventDispatcher(); - - $hook_data = $eventDispatcher->dispatch( + $hook_data = $this->eventDispatcher->dispatch( new ArrayFilterEvent(ArrayFilterEvent::MODERATION_USERS_TABS, $hook_data), )->getArray(); From ff3c4046e0e4b58a30ce1f3c78e22947329e99cc Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 10 Apr 2025 11:04:10 +0000 Subject: [PATCH 34/45] Create event for lockview_content hook --- src/Core/Hooks/HookEventBridge.php | 16 ++++++++ src/Event/ArrayFilterEvent.php | 2 + src/Module/Privacy/PermissionTooltip.php | 40 +++++++++++++++---- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 24 +++++++++++ tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 6dc6aeb667..03f8ced58c 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -43,6 +43,7 @@ final class HookEventBridge ArrayFilterEvent::NAV_INFO => 'nav_info', ArrayFilterEvent::FEATURE_ENABLED => 'isEnabled', ArrayFilterEvent::FEATURE_GET => 'get', + ArrayFilterEvent::PERMISSION_TOOLTIP_CONTENT => 'lockview_content', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'post_local_start', ArrayFilterEvent::INSERT_POST_LOCAL => 'post_local', ArrayFilterEvent::INSERT_POST_LOCAL_END => 'post_local_end', @@ -127,6 +128,7 @@ final class HookEventBridge ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', + ArrayFilterEvent::PERMISSION_TOOLTIP_CONTENT => 'onPermissionTooltipContentEvent', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent', 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 */ diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index cb14052bb2..f1ba9887d9 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -24,6 +24,8 @@ final class ArrayFilterEvent extends Event 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 = 'friendica.data.insert_post_local'; diff --git a/src/Module/Privacy/PermissionTooltip.php b/src/Module/Privacy/PermissionTooltip.php index ef4ba9f7bd..6f77daaec1 100644 --- a/src/Module/Privacy/PermissionTooltip.php +++ b/src/Module/Privacy/PermissionTooltip.php @@ -7,14 +7,16 @@ 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\Hook; use Friendica\Core\L10n; use Friendica\Core\Protocol; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\Database; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model; use Friendica\Module\Response; use Friendica\Network\HTTPException; @@ -23,21 +25,37 @@ use Friendica\Privacy\Entity; use Friendica\Security\PermissionSet\Repository\PermissionSet; use Friendica\Util\ACLFormatter; use Friendica\Util\Profiler; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** * 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 ACLFormatter $aclFormatter; private IHandleUserSessions $session; private IManageConfigValues $config; 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); $this->dba = $dba; @@ -45,6 +63,7 @@ class PermissionTooltip extends \Friendica\BaseModule $this->session = $session; $this->config = $config; $this->permissionSet = $permissionSet; + $this->eventDispatcher = $eventDispatcher; } protected function rawContent(array $request = []) @@ -87,8 +106,15 @@ class PermissionTooltip extends \Friendica\BaseModule throw new HttpException\NotFoundException($this->t('Model not found')); } - // Kept for backwards compatibility - Hook::callAll('lockview_content', $model); + $hook_data = [ + '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(); $addressedReceivers = new Entity\AddressedReceivers(); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index ae98a2fc0e..a07907e427 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -32,6 +32,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent', ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent', + ArrayFilterEvent::PERMISSION_TOOLTIP_CONTENT => 'onPermissionTooltipContentEvent', ArrayFilterEvent::INSERT_POST_LOCAL_START => 'onArrayFilterEvent', ArrayFilterEvent::INSERT_POST_LOCAL => 'onInsertPostLocalEvent', ArrayFilterEvent::INSERT_POST_LOCAL_END => 'onInsertPostLocalEndEvent', @@ -209,6 +210,28 @@ class HookEventBridgeTest extends TestCase 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 { $event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL, ['item' => ['id' => -1]]); @@ -230,6 +253,7 @@ class HookEventBridgeTest extends TestCase $event->getArray(), ); } + public function testOnInsertPostLocalEndEventCallsHookWithCorrectValue(): void { $event = new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_LOCAL_END, ['item' => ['id' => -1]]); diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 4f20c37543..69a8d6829d 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -29,6 +29,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::NAV_INFO, 'friendica.data.nav_info'], [ArrayFilterEvent::FEATURE_ENABLED, 'friendica.data.feature_enabled'], [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, 'friendica.data.insert_post_local'], [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'friendica.data.insert_post_local_end'], From 39439fe26b3bf299344d3079af9ef70c1d4da6bb Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 10 Apr 2025 11:13:43 +0000 Subject: [PATCH 35/45] Fix missing dependency for EventDispatcher --- src/Module/Ping/Network.php | 59 +++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Module/Ping/Network.php b/src/Module/Ping/Network.php index f8c41a2e1c..03c6b8eeac 100644 --- a/src/Module/Ping/Network.php +++ b/src/Module/Ping/Network.php @@ -31,6 +31,7 @@ use Friendica\Module\Conversation\Network as NetworkModule; use Friendica\Module\Response; use Friendica\Navigation\SystemMessages; use Friendica\Util\Profiler; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; class Network extends NetworkModule @@ -40,9 +41,61 @@ class Network extends NetworkModule */ private $lock; - public function __construct(ICanLock $lock, UserDefinedChannelFactory $userDefinedChannel, NetworkFactory $network, CommunityFactory $community, ChannelFactory $channelFactory, UserDefinedChannel $channel, AppHelper $appHelper, TimelineFactory $timeline, SystemMessages $systemMessages, Mode $mode, Conversation $conversation, Page $page, IHandleUserSessions $session, Database $database, IManagePersonalConfigValues $pConfig, IManageConfigValues $config, ICanCache $cache, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) - { - parent::__construct($userDefinedChannel, $network, $community, $channelFactory, $channel, $appHelper, $timeline, $systemMessages, $mode, $conversation, $page, $session, $database, $pConfig, $config, $cache, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + public function __construct( + ICanLock $lock, + UserDefinedChannelFactory $userDefinedChannel, + NetworkFactory $network, + CommunityFactory $community, + ChannelFactory $channelFactory, + UserDefinedChannel $channel, + AppHelper $appHelper, + EventDispatcherInterface $eventDispatcher, + TimelineFactory $timeline, + SystemMessages $systemMessages, + Mode $mode, + Conversation $conversation, + Page $page, + IHandleUserSessions $session, + Database $database, + IManagePersonalConfigValues $pConfig, + IManageConfigValues $config, + ICanCache $cache, + L10n $l10n, + BaseURL $baseUrl, + Arguments $args, + LoggerInterface $logger, + Profiler $profiler, + Response $response, + array $server, + array $parameters = [], + ) { + parent::__construct( + $userDefinedChannel, + $network, + $community, + $channelFactory, + $channel, + $appHelper, + $eventDispatcher, + $timeline, + $systemMessages, + $mode, + $conversation, + $page, + $session, + $database, + $pConfig, + $config, + $cache, + $l10n, + $baseUrl, + $args, + $logger, + $profiler, + $response, + $server, + $parameters + ); $this->lock = $lock; } From 86ebbecff5240319cf8cc8945bcfcdb1696720bf Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 10 Apr 2025 14:17:20 +0000 Subject: [PATCH 36/45] Create event for photo upload hooks --- src/Core/Hooks/HookEventBridge.php | 33 +++++++ src/Event/ArrayFilterEvent.php | 6 ++ src/Module/Profile/Photos.php | 93 ++++++++++++++----- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 43 +++++++++ tests/Unit/Event/ArrayFilterEventTest.php | 3 + 5 files changed, 155 insertions(+), 23 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 03f8ced58c..df2a17322e 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -54,6 +54,9 @@ final class HookEventBridge ArrayFilterEvent::PREPARE_POST => 'prepare_body', ArrayFilterEvent::PREPARE_POST_END => 'prepare_body_final', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'photo_upload_form', + ArrayFilterEvent::PHOTO_UPLOAD_START => 'photo_post_init', + ArrayFilterEvent::PHOTO_UPLOAD => 'photo_post_file', + ArrayFilterEvent::PHOTO_UPLOAD_END => 'photo_post_end', ArrayFilterEvent::NETWORK_TO_NAME => 'network_to_name', ArrayFilterEvent::NETWORK_CONTENT_START => 'network_content_init', ArrayFilterEvent::NETWORK_CONTENT_TABS => 'network_tabs', @@ -139,6 +142,9 @@ final class HookEventBridge ArrayFilterEvent::PREPARE_POST => 'onArrayFilterEvent', ArrayFilterEvent::PREPARE_POST_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::PHOTO_UPLOAD_START => 'onPhotoUploadStartEvent', + ArrayFilterEvent::PHOTO_UPLOAD => 'onArrayFilterEvent', + ArrayFilterEvent::PHOTO_UPLOAD_END => 'onPhotoUploadEndEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_CONTENT_START => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_CONTENT_TABS => 'onArrayFilterEvent', @@ -273,6 +279,33 @@ final class HookEventBridge $event->setArray($data); } + /** + * Map the PHOTO_UPLOAD_START event to `photo_post_init` hook + */ + public static function onPhotoUploadStartEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $request = (array) $data['request'] ?? []; + + $data['request'] = static::callHook($event->getName(), $request); + + $event->setArray($data); + } + + /** + * Map the PHOTO_UPLOAD_END event to `photo_post_end` hook + */ + public static function onPhotoUploadEndEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $id = (int) $data['id'] ?? 0; + + // one-way-event: we don't care about the returned value + static::callHook($event->getName(), $id); + } + /** * Map the PROFILE_SIDEBAR_ENTRY event to `profile_sidebar_enter` hook */ diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index f1ba9887d9..7c772f4132 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -58,6 +58,12 @@ final class ArrayFilterEvent extends Event public const PHOTO_UPLOAD_FORM = 'friendica.data.photo_upload_form'; + public const PHOTO_UPLOAD_START = 'friendica.data.photo_upload_start'; + + public const PHOTO_UPLOAD = 'friendica.data.photo_upload'; + + public const PHOTO_UPLOAD_END = 'friendica.data.photo_upload_end'; + public const NETWORK_TO_NAME = 'friendica.data.network_to_name'; public const NETWORK_CONTENT_START = 'friendica.data.network_content_start'; diff --git a/src/Module/Profile/Photos.php b/src/Module/Profile/Photos.php index a377cd6dc3..29ece42ef9 100644 --- a/src/Module/Profile/Photos.php +++ b/src/Module/Profile/Photos.php @@ -14,12 +14,12 @@ use Friendica\AppHelper; use Friendica\Content\Feature; use Friendica\Content\Pager; use Friendica\Core\Config\Capability\IManageConfigValues; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; use Friendica\Database\Database; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\Photo; @@ -34,6 +34,7 @@ use Friendica\Util\DateTimeFormat; use Friendica\Util\Images; use Friendica\Util\Profiler; use Friendica\Util\Strings; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; class Photos extends \Friendica\Module\BaseProfile @@ -52,11 +53,28 @@ class Photos extends \Friendica\Module\BaseProfile private $systemMessages; /** @var ACLFormatter */ private $aclFormatter; + private EventDispatcherInterface $eventDispatcher; /** @var array owner-view record */ private $owner; - public function __construct(ACLFormatter $aclFormatter, SystemMessages $systemMessages, Database $database, AppHelper $appHelper, IManageConfigValues $config, Page $page, IHandleUserSessions $session, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) - { + public function __construct( + ACLFormatter $aclFormatter, + SystemMessages $systemMessages, + Database $database, + AppHelper $appHelper, + IManageConfigValues $config, + Page $page, + IHandleUserSessions $session, + 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); $this->session = $session; @@ -66,6 +84,7 @@ class Photos extends \Friendica\Module\BaseProfile $this->database = $database; $this->systemMessages = $systemMessages; $this->aclFormatter = $aclFormatter; + $this->eventDispatcher = $eventDispatcher; $owner = Profile::load($this->appHelper, $this->parameters['nickname'] ?? '', false); if (!$owner || $owner['account_removed'] || $owner['account_expired']) { @@ -97,8 +116,16 @@ class Photos extends \Friendica\Module\BaseProfile $str_contact_allow .= $this->aclFormatter->toString(Contact::getPublicIdByUserId($this->owner['uid'])); } + $hook_data = [ + 'request' => $request, + ]; + // default post action - upload a photo - Hook::callAll('photo_post_init', $request); + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD_START, $hook_data), + )->getArray(); + + $request = $hook_data['request'] ?? $request; // Determine the album to use $album = trim($request['album'] ?? ''); @@ -127,19 +154,27 @@ class Photos extends \Friendica\Module\BaseProfile $visible = 0; } - $ret = ['src' => '', 'filename' => '', 'filesize' => 0, 'type' => '']; + $hook_data = [ + 'src' => '', + 'filename' => '', + 'filesize' => 0, + 'type' => '', + ]; + + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD, $hook_data), + )->getArray(); + $src = null; $filename = ''; $filesize = 0; $type = ''; - Hook::callAll('photo_post_file', $ret); - - if (!empty($ret['src']) && !empty($ret['filesize'])) { - $src = $ret['src']; - $filename = $ret['filename']; - $filesize = $ret['filesize']; - $type = $ret['type']; + if (!empty($hook_data['src']) && !empty($hook_data['filesize'])) { + $src = $hook_data['src']; + $filename = $hook_data['filename']; + $filesize = $hook_data['filesize']; + $type = $hook_data['type']; $error = UPLOAD_ERR_OK; } elseif (!empty($_FILES['userfile'])) { $src = $_FILES['userfile']['tmp_name']; @@ -176,8 +211,10 @@ class Photos extends \Friendica\Module\BaseProfile @unlink($src); } - $foo = 0; - Hook::callAll('photo_post_end', $foo); + $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD_END, ['id' => 0]), + ); + return; } @@ -188,16 +225,22 @@ class Photos extends \Friendica\Module\BaseProfile if ($maximagesize && ($filesize > $maximagesize)) { $this->systemMessages->addNotice($this->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize))); @unlink($src); - $foo = 0; - Hook::callAll('photo_post_end', $foo); + + $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD_END, ['id' => 0]), + ); + return; } if (!$filesize) { $this->systemMessages->addNotice($this->t('Image file is empty.')); @unlink($src); - $foo = 0; - Hook::callAll('photo_post_end', $foo); + + $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD_END, ['id' => 0]), + ); + return; } @@ -211,8 +254,11 @@ class Photos extends \Friendica\Module\BaseProfile $this->logger->notice('unable to process image'); $this->systemMessages->addNotice($this->t('Unable to process image.')); @unlink($src); - $foo = 0; - Hook::callAll('photo_post_end',$foo); + + $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD_END, ['id' => 0]), + ); + return; } @@ -274,10 +320,11 @@ class Photos extends \Friendica\Module\BaseProfile // Update the photo albums cache Photo::clearAlbumCache($this->owner['uid']); - Hook::callAll('photo_post_end', $item_id); - - // addon uploaders should call "exit()" within the photo_post_end hook + // addon uploaders should call "exit()" within the PHOTO_UPLOAD_END event // if they do not wish to be redirected + $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD_END, ['id' => $item_id]), + ); $this->baseUrl->redirect($this->session->get('photo_return') ?? 'profile/' . $this->owner['nickname'] . '/photos'); } diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index a07907e427..f30a1c2fef 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -43,6 +43,9 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PREPARE_POST => 'onArrayFilterEvent', ArrayFilterEvent::PREPARE_POST_END => 'onArrayFilterEvent', ArrayFilterEvent::PHOTO_UPLOAD_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::PHOTO_UPLOAD_START => 'onPhotoUploadStartEvent', + ArrayFilterEvent::PHOTO_UPLOAD => 'onArrayFilterEvent', + ArrayFilterEvent::PHOTO_UPLOAD_END => 'onPhotoUploadEndEvent', ArrayFilterEvent::NETWORK_TO_NAME => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_CONTENT_START => 'onArrayFilterEvent', ArrayFilterEvent::NETWORK_CONTENT_TABS => 'onArrayFilterEvent', @@ -298,6 +301,45 @@ class HookEventBridgeTest extends TestCase ); } + public function testOnPhotoUploadStartEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD_START, ['request' => ['album' => -1]]); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, array $data): array { + $this->assertSame('photo_post_init', $name); + $this->assertSame(['album' => -1], $data); + + return ['album' => 123]; + }); + + HookEventBridge::onPhotoUploadStartEvent($event); + + $this->assertSame( + ['request' => ['album' => 123]], + $event->getArray(), + ); + } + + public function testOnPhotoUploadEndEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::PHOTO_UPLOAD_END, ['id' => -1]); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, int $data): int { + $this->assertSame('photo_post_end', $name); + $this->assertSame(-1, $data); + + return 123; + }); + + HookEventBridge::onPhotoUploadEndEvent($event); + } + public function testOnProfileSidebarEntryEventCallsHookWithCorrectValue(): void { $event = new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, ['profile' => ['uid' => 0, 'name' => 'original']]); @@ -491,6 +533,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::PREPARE_POST, 'prepare_body'], [ArrayFilterEvent::PREPARE_POST_END, 'prepare_body_final'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'], + [ArrayFilterEvent::PHOTO_UPLOAD, 'photo_post_file'], [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'], [ArrayFilterEvent::NETWORK_CONTENT_START, 'network_content_init'], [ArrayFilterEvent::NETWORK_CONTENT_TABS, 'network_tabs'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 69a8d6829d..9ecdb6feee 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -40,6 +40,9 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PREPARE_POST, 'friendica.data.prepare_post'], [ArrayFilterEvent::PREPARE_POST_END, 'friendica.data.prepare_post_end'], [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'friendica.data.photo_upload_form'], + [ArrayFilterEvent::PHOTO_UPLOAD_START, 'friendica.data.photo_upload_start'], + [ArrayFilterEvent::PHOTO_UPLOAD, 'friendica.data.photo_upload'], + [ArrayFilterEvent::PHOTO_UPLOAD_END, 'friendica.data.photo_upload_end'], [ArrayFilterEvent::NETWORK_TO_NAME, 'friendica.data.network_to_name'], [ArrayFilterEvent::NETWORK_CONTENT_START, 'friendica.data.network_content_start'], [ArrayFilterEvent::NETWORK_CONTENT_TABS, 'friendica.data.network_content_tabs'], From 3ea20e9eee97e1aa35d3dfa4bf3eeced5d114418 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 10 Apr 2025 14:36:57 +0000 Subject: [PATCH 37/45] create event for profile_advanced hook --- src/Core/Hooks/HookEventBridge.php | 2 ++ src/Event/HtmlFilterEvent.php | 2 ++ src/Module/Profile/Profile.php | 28 ++++++++++++++++--- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 ++ tests/Unit/Event/HtmlFilterEventTest.php | 1 + 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index df2a17322e..9d20a36e89 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -113,6 +113,7 @@ final class HookEventBridge HtmlFilterEvent::PAGE_END => 'page_end', HtmlFilterEvent::MOD_HOME_CONTENT => 'home_content', HtmlFilterEvent::MOD_ABOUT_CONTENT => 'about_hook', + HtmlFilterEvent::MOD_PROFILE_CONTENT => 'profile_advanced', HtmlFilterEvent::JOT_TOOL => 'jot_tool', HtmlFilterEvent::CONTACT_BLOCK_END => 'contact_block_end', ]; @@ -201,6 +202,7 @@ final class HookEventBridge HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', HtmlFilterEvent::MOD_HOME_CONTENT => 'onHtmlFilterEvent', HtmlFilterEvent::MOD_ABOUT_CONTENT => 'onHtmlFilterEvent', + HtmlFilterEvent::MOD_PROFILE_CONTENT => 'onHtmlFilterEvent', HtmlFilterEvent::JOT_TOOL => 'onHtmlFilterEvent', HtmlFilterEvent::CONTACT_BLOCK_END => 'onHtmlFilterEvent', ]; diff --git a/src/Event/HtmlFilterEvent.php b/src/Event/HtmlFilterEvent.php index 9e30d6d656..1275e0791d 100644 --- a/src/Event/HtmlFilterEvent.php +++ b/src/Event/HtmlFilterEvent.php @@ -30,6 +30,8 @@ final class HtmlFilterEvent extends Event public const MOD_ABOUT_CONTENT = 'friendica.html.mod_about_content'; + public const MOD_PROFILE_CONTENT = 'friendica.html.mod_profile_content'; + public const JOT_TOOL = 'friendica.html.jot_tool'; public const CONTACT_BLOCK_END = 'friendica.html.contact_block_end'; diff --git a/src/Module/Profile/Profile.php b/src/Module/Profile/Profile.php index e84003e4dc..b3f15f5f44 100644 --- a/src/Module/Profile/Profile.php +++ b/src/Module/Profile/Profile.php @@ -17,13 +17,13 @@ use Friendica\Content\Nav; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; use Friendica\Core\Config\Capability\IManageConfigValues; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Protocol; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\Event\HtmlFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Profile as ProfileModel; use Friendica\Model\Tag; @@ -40,6 +40,7 @@ use Friendica\Util\Network; use Friendica\Util\Profiler; use Friendica\Util\Temporal; use GuzzleHttp\Psr7\Uri; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; class Profile extends BaseProfile @@ -56,9 +57,25 @@ class Profile extends BaseProfile private $page; /** @var ProfileField */ private $profileField; + private EventDispatcherInterface $eventDispatcher; - public function __construct(ProfileField $profileField, Page $page, IManageConfigValues $config, IHandleUserSessions $session, AppHelper $appHelper, Database $database, L10n $l10n, BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) - { + public function __construct( + ProfileField $profileField, + Page $page, + IManageConfigValues $config, + IHandleUserSessions $session, + AppHelper $appHelper, + Database $database, + 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); $this->database = $database; @@ -67,6 +84,7 @@ class Profile extends BaseProfile $this->config = $config; $this->page = $page; $this->profileField = $profileField; + $this->eventDispatcher = $eventDispatcher; } protected function rawContent(array $request = []) @@ -282,7 +300,9 @@ class Profile extends BaseProfile ], ]); - Hook::callAll('profile_advanced', $o); + $o = $this->eventDispatcher->dispatch( + new HTmlFilterEvent(HtmlFilterEvent::MOD_PROFILE_CONTENT, $o), + )->getHtml(); return $o; } diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index f30a1c2fef..6c5c5fecf8 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -102,6 +102,7 @@ class HookEventBridgeTest extends TestCase HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent', HtmlFilterEvent::MOD_HOME_CONTENT => 'onHtmlFilterEvent', HtmlFilterEvent::MOD_ABOUT_CONTENT => 'onHtmlFilterEvent', + HtmlFilterEvent::MOD_PROFILE_CONTENT => 'onHtmlFilterEvent', HtmlFilterEvent::JOT_TOOL => 'onHtmlFilterEvent', HtmlFilterEvent::CONTACT_BLOCK_END => 'onHtmlFilterEvent', ]; @@ -612,6 +613,7 @@ class HookEventBridgeTest extends TestCase [HtmlFilterEvent::PAGE_END, 'page_end'], [HtmlFilterEvent::MOD_HOME_CONTENT, 'home_content'], [HtmlFilterEvent::MOD_ABOUT_CONTENT, 'about_hook'], + [HtmlFilterEvent::MOD_PROFILE_CONTENT, 'profile_advanced'], [HtmlFilterEvent::JOT_TOOL, 'jot_tool'], [HtmlFilterEvent::CONTACT_BLOCK_END, 'contact_block_end'], ]; diff --git a/tests/Unit/Event/HtmlFilterEventTest.php b/tests/Unit/Event/HtmlFilterEventTest.php index 47a2d2596d..bae1669ca0 100644 --- a/tests/Unit/Event/HtmlFilterEventTest.php +++ b/tests/Unit/Event/HtmlFilterEventTest.php @@ -32,6 +32,7 @@ class HtmlFilterEventTest extends TestCase [HtmlFilterEvent::PAGE_END, 'friendica.html.page_end'], [HtmlFilterEvent::MOD_HOME_CONTENT, 'friendica.html.mod_home_content'], [HtmlFilterEvent::MOD_ABOUT_CONTENT, 'friendica.html.mod_about_content'], + [HtmlFilterEvent::MOD_PROFILE_CONTENT, 'friendica.html.mod_profile_content'], [HtmlFilterEvent::JOT_TOOL, 'friendica.html.jot_tool'], [HtmlFilterEvent::CONTACT_BLOCK_END, 'friendica.html.contact_block_end'], ]; From 7531a2fb36019c423b740855b61bc540afd9123d Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 11 Apr 2025 06:55:51 +0000 Subject: [PATCH 38/45] Create event for acl_lookup_end hook --- src/Core/Hooks/HookEventBridge.php | 2 + src/Event/ArrayFilterEvent.php | 2 + src/Module/Search/Acl.php | 42 +++++++++++++------ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 2 + tests/Unit/Event/ArrayFilterEventTest.php | 1 + 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 9d20a36e89..9b8c2350c4 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -76,6 +76,7 @@ final class HookEventBridge ArrayFilterEvent::PROFILE_SIDEBAR => 'profile_sidebar', ArrayFilterEvent::PROFILE_TABS => 'profile_tabs', ArrayFilterEvent::MODERATION_USERS_TABS => 'moderation_users_tabs', + ArrayFilterEvent::ACL_LOOKUP_END => 'acl_lookup_end', ArrayFilterEvent::OEMBED_FETCH_END => 'oembed_fetch_url', ArrayFilterEvent::PAGE_INFO => 'page_info_data', ArrayFilterEvent::SMILEY_LIST => 'smilie', @@ -165,6 +166,7 @@ final class HookEventBridge ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_TABS => 'onArrayFilterEvent', ArrayFilterEvent::MODERATION_USERS_TABS => 'onArrayFilterEvent', + ArrayFilterEvent::ACL_LOOKUP_END => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 7c772f4132..591250455f 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -102,6 +102,8 @@ final class ArrayFilterEvent extends Event public const MODERATION_USERS_TABS = 'friendica.data.moderation_users_tabs'; + public const ACL_LOOKUP_END = 'friendica.data.acl_lookup_end'; + public const OEMBED_FETCH_END = 'friendica.data.oembed_fetch_end'; public const PAGE_INFO = 'friendica.data.page_info'; diff --git a/src/Module/Search/Acl.php b/src/Module/Search/Acl.php index ad1dc180be..83e5a5dfff 100644 --- a/src/Module/Search/Acl.php +++ b/src/Module/Search/Acl.php @@ -7,22 +7,23 @@ namespace Friendica\Module\Search; -use Friendica\App; +use Friendica\App\Arguments; +use Friendica\App\BaseURL; use Friendica\BaseModule; use Friendica\Content\Widget; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Protocol; use Friendica\Core\Search; use Friendica\Core\Session\Capability\IHandleUserSessions; -use Friendica\Core\System; use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Post; use Friendica\Module\Response; -use Friendica\Network\HTTPException; +use Friendica\Network\HTTPException\UnauthorizedException; use Friendica\Util\Profiler; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** @@ -44,13 +45,26 @@ class Acl extends BaseModule private $session; /** @var Database */ private $database; + private EventDispatcherInterface $eventDispatcher; - public function __construct(Database $database, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) - { + public function __construct( + Database $database, + IHandleUserSessions $session, + 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); $this->session = $session; $this->database = $database; + $this->eventDispatcher = $eventDispatcher; } protected function post(array $request = []) @@ -61,7 +75,7 @@ class Acl extends BaseModule protected function rawContent(array $request = []) { if (!$this->session->getLocalUserId()) { - throw new HTTPException\UnauthorizedException($this->t('You must be logged in to use this module.')); + throw new UnauthorizedException($this->t('You must be logged in to use this module.')); } $type = $request['type'] ?? self::TYPE_MENTION_CONTACT_CIRCLE; @@ -280,7 +294,7 @@ class Acl extends BaseModule $resultTotal += count($unknown_contacts); } - $results = [ + $hook_data = [ 'tot' => $resultTotal, 'start' => $start, 'count' => $count, @@ -291,13 +305,15 @@ class Acl extends BaseModule 'search' => $search, ]; - Hook::callAll('acl_lookup_end', $results); + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ACL_LOOKUP_END, $hook_data), + )->getArray(); $o = [ - 'tot' => $results['tot'], - 'start' => $results['start'], - 'count' => $results['count'], - 'items' => $results['items'], + 'tot' => $hook_data['tot'], + 'start' => $hook_data['start'], + 'count' => $hook_data['count'], + 'items' => $hook_data['items'], ]; $this->logger->info('ACL {action} - {subaction} - done', ['module' => 'acl', 'action' => 'content', 'subaction' => 'search', 'search' => $search, 'type' => $type, 'conversation' => $conv_id]); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 6c5c5fecf8..fc3752e977 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -65,6 +65,7 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_TABS => 'onArrayFilterEvent', ArrayFilterEvent::MODERATION_USERS_TABS => 'onArrayFilterEvent', + ArrayFilterEvent::ACL_LOOKUP_END => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', @@ -553,6 +554,7 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::PROFILE_SIDEBAR, 'profile_sidebar'], [ArrayFilterEvent::PROFILE_TABS, 'profile_tabs'], [ArrayFilterEvent::MODERATION_USERS_TABS, 'moderation_users_tabs'], + [ArrayFilterEvent::ACL_LOOKUP_END, 'acl_lookup_end'], [ArrayFilterEvent::PAGE_INFO, 'page_info_data'], [ArrayFilterEvent::SMILEY_LIST, 'smilie'], [ArrayFilterEvent::JOT_NETWORKS, 'jot_networks'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 9ecdb6feee..771cd4fed7 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -62,6 +62,7 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PROFILE_SIDEBAR, 'friendica.data.profile_sidebar'], [ArrayFilterEvent::PROFILE_TABS, 'friendica.data.profile_tabs'], [ArrayFilterEvent::MODERATION_USERS_TABS, 'friendica.data.moderation_users_tabs'], + [ArrayFilterEvent::ACL_LOOKUP_END, 'friendica.data.acl_lookup_end'], [ArrayFilterEvent::OEMBED_FETCH_END, 'friendica.data.oembed_fetch_end'], [ArrayFilterEvent::PAGE_INFO, 'friendica.data.page_info'], [ArrayFilterEvent::SMILEY_LIST, 'friendica.data.smiley_list'], From c436f5249b80428163cb06aeee118145714129fc Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 11 Apr 2025 07:46:48 +0000 Subject: [PATCH 39/45] Create events for enotify hooks --- src/Core/Hooks/HookEventBridge.php | 6 ++ src/Event/ArrayFilterEvent.php | 6 ++ .../Notifications/Repository/Notify.php | 70 ++++++++++++------- tests/Unit/Core/Hooks/HookEventBridgeTest.php | 6 ++ tests/Unit/Event/ArrayFilterEventTest.php | 3 + 5 files changed, 67 insertions(+), 24 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 9b8c2350c4..ae7a42c154 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -67,6 +67,9 @@ final class HookEventBridge ArrayFilterEvent::DISPLAY_ITEM => 'display_item', ArrayFilterEvent::CACHE_ITEM => 'put_item_in_cache', ArrayFilterEvent::CHECK_ITEM_NOTIFICATION => 'check_item_notification', + ArrayFilterEvent::ENOTIFY => 'enotify', + ArrayFilterEvent::ENOTIFY_STORE => 'enotify_store', + ArrayFilterEvent::ENOTIFY_MAIL => 'enotify_mail', ArrayFilterEvent::DETECT_LANGUAGES => 'detect_languages', ArrayFilterEvent::RENDER_LOCATION => 'render_location', ArrayFilterEvent::ITEM_PHOTO_MENU => 'item_photo_menu', @@ -157,6 +160,9 @@ final class HookEventBridge ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CACHE_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CHECK_ITEM_NOTIFICATION => 'onArrayFilterEvent', + ArrayFilterEvent::ENOTIFY => 'onArrayFilterEvent', + ArrayFilterEvent::ENOTIFY_STORE => 'onArrayFilterEvent', + ArrayFilterEvent::ENOTIFY_MAIL => 'onArrayFilterEvent', ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 591250455f..1f73eadf62 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -84,6 +84,12 @@ final class ArrayFilterEvent extends Event public const CHECK_ITEM_NOTIFICATION = 'friendica.data.check_item_notification'; + public const ENOTIFY = 'friendica.data.enotify'; + + public const ENOTIFY_STORE = 'friendica.data.enotify_store'; + + public const ENOTIFY_MAIL = 'friendica.data.enotify_mail'; + public const DETECT_LANGUAGES = 'friendica.data.detect_languages'; public const RENDER_LOCATION = 'friendica.data.render_location'; diff --git a/src/Navigation/Notifications/Repository/Notify.php b/src/Navigation/Notifications/Repository/Notify.php index 1c793faaee..c59eed92aa 100644 --- a/src/Navigation/Notifications/Repository/Notify.php +++ b/src/Navigation/Notifications/Repository/Notify.php @@ -17,6 +17,7 @@ use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\Event\ArrayFilterEvent; use Friendica\Factory\Api\Mastodon\Notification as NotificationFactory; use Friendica\Model; use Friendica\Navigation\Notifications\Collection; @@ -28,6 +29,7 @@ use Friendica\Object\Api\Mastodon\Notification; use Friendica\Protocol\Activity; use Friendica\Util\DateTimeFormat; use Friendica\Util\Emailer; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; /** @@ -56,16 +58,29 @@ class Notify extends BaseRepository /** @var Factory\Notification */ protected $notification; + private EventDispatcherInterface $eventDispatcher; + protected static $table_name = 'notify'; - public function __construct(Database $database, LoggerInterface $logger, L10n $l10n, BaseURL $baseUrl, IManageConfigValues $config, IManagePersonalConfigValues $pConfig, Emailer $emailer, Factory\Notification $notification, Factory\Notify $factory = null) - { + public function __construct( + Database $database, + LoggerInterface $logger, + L10n $l10n, + BaseURL $baseUrl, + IManageConfigValues $config, + IManagePersonalConfigValues $pConfig, + Emailer $emailer, + Factory\Notification $notification, + EventDispatcherInterface $eventDispatcher, + Factory\Notify $factory = null, + ) { $this->l10n = $l10n; $this->baseUrl = $baseUrl; $this->config = $config; $this->pConfig = $pConfig; $this->emailer = $emailer; $this->notification = $notification; + $this->eventDispatcher = $eventDispatcher; parent::__construct($database, $logger, $factory ?? new Factory\Notify($logger)); } @@ -171,7 +186,10 @@ class Notify extends BaseRepository $this->db->update(self::$table_name, $fields, ['id' => $Notify->id]); } else { $fields['date'] = DateTimeFormat::utcNow(); - Hook::callAll('enotify_store', $fields); + + $fields = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ENOTIFY_STORE, $fields), + )->getArray(); $this->db->insert(self::$table_name, $fields); @@ -549,7 +567,7 @@ class Notify extends BaseRepository $subject .= " (".$nickname."@".$hostname.")"; - $h = [ + $hook_data = [ 'params' => $params, 'subject' => $subject, 'preamble' => $preamble, @@ -561,18 +579,20 @@ class Notify extends BaseRepository 'itemlink' => $itemlink ]; - Hook::callAll('enotify', $h); + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ENOTIFY, $hook_data), + )->getArray(); - $subject = $h['subject']; + $subject = $hook_data['subject']; - $preamble = $h['preamble']; - $epreamble = $h['epreamble']; + $preamble = $hook_data['preamble']; + $epreamble = $hook_data['epreamble']; - $body = $h['body']; + $body = $hook_data['body']; - $tsitelink = $h['tsitelink']; - $hsitelink = $h['hsitelink']; - $itemlink = $h['itemlink']; + $tsitelink = $hook_data['tsitelink']; + $hsitelink = $hook_data['hsitelink']; + $itemlink = $hook_data['itemlink']; $notify_id = 0; @@ -620,7 +640,7 @@ class Notify extends BaseRepository } } - $datarray = [ + $hook_data = [ 'preamble' => $preamble, 'type' => $params['type'], 'parent' => $parent_id, @@ -637,31 +657,33 @@ class Notify extends BaseRepository 'headers' => $emailBuilder->getHeaders(), ]; - Hook::callAll('enotify_mail', $datarray); + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::ENOTIFY_MAIL, $hook_data), + )->getArray(); $emailBuilder - ->withHeaders($datarray['headers']) + ->withHeaders($hook_data['headers']) ->withRecipient($params['to_email']) ->forUser([ - 'uid' => $datarray['uid'], + 'uid' => $hook_data['uid'], 'language' => $params['language'], ]) - ->withNotification($datarray['subject'], $datarray['preamble'], $datarray['title'], $datarray['body']) - ->withSiteLink($datarray['tsitelink'], $datarray['hsitelink']) - ->withItemLink($datarray['itemlink']); + ->withNotification($hook_data['subject'], $hook_data['preamble'], $hook_data['title'], $hook_data['body']) + ->withSiteLink($hook_data['tsitelink'], $hook_data['hsitelink']) + ->withItemLink($hook_data['itemlink']); // If a photo is present, add it to the email - if (!empty($datarray['source_photo'])) { + if (!empty($hook_data['source_photo'])) { $emailBuilder->withPhoto( - $datarray['source_photo'], - $datarray['source_link'] ?? $sitelink, - $datarray['source_name'] ?? $sitename + $hook_data['source_photo'], + $hook_data['source_link'] ?? $sitelink, + $hook_data['source_name'] ?? $sitename ); } $email = $emailBuilder->build(); - $this->logger->debug('Send mail', $datarray); + $this->logger->debug('Send mail', $hook_data); // use the Emailer class to send the message return $this->emailer->send($email); diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index fc3752e977..7b655ee0a1 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -56,6 +56,9 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::DISPLAY_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CACHE_ITEM => 'onArrayFilterEvent', ArrayFilterEvent::CHECK_ITEM_NOTIFICATION => 'onArrayFilterEvent', + ArrayFilterEvent::ENOTIFY => 'onArrayFilterEvent', + ArrayFilterEvent::ENOTIFY_STORE => 'onArrayFilterEvent', + ArrayFilterEvent::ENOTIFY_MAIL => 'onArrayFilterEvent', ArrayFilterEvent::DETECT_LANGUAGES => 'onArrayFilterEvent', ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', @@ -546,6 +549,9 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::DISPLAY_ITEM, 'display_item'], [ArrayFilterEvent::CACHE_ITEM, 'put_item_in_cache'], [ArrayFilterEvent::CHECK_ITEM_NOTIFICATION, 'check_item_notification'], + [ArrayFilterEvent::ENOTIFY, 'enotify'], + [ArrayFilterEvent::ENOTIFY_STORE, 'enotify_store'], + [ArrayFilterEvent::ENOTIFY_MAIL, 'enotify_mail'], [ArrayFilterEvent::DETECT_LANGUAGES, 'detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 771cd4fed7..aae225cd46 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -53,6 +53,9 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::DISPLAY_ITEM, 'friendica.data.display_item'], [ArrayFilterEvent::CACHE_ITEM, 'friendica.data.cache_item'], [ArrayFilterEvent::CHECK_ITEM_NOTIFICATION, 'friendica.data.check_item_notification'], + [ArrayFilterEvent::ENOTIFY, 'friendica.data.enotify'], + [ArrayFilterEvent::ENOTIFY_STORE, 'friendica.data.enotify_store'], + [ArrayFilterEvent::ENOTIFY_MAIL, 'friendica.data.enotify_mail'], [ArrayFilterEvent::DETECT_LANGUAGES, 'friendica.data.detect_languages'], [ArrayFilterEvent::RENDER_LOCATION, 'friendica.data.render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'friendica.data.item_photo_menu'], From 2a7d329c524c67b0d1f24ba1e377249fe20f305c Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 11 Apr 2025 08:21:29 +0000 Subject: [PATCH 40/45] Create event for profile_edit and profile_post hooks --- src/Core/Hooks/HookEventBridge.php | 4 ++ src/Event/ArrayFilterEvent.php | 4 ++ src/Module/Settings/Profile/Index.php | 52 +++++++++++++++---- .../Notifications/Repository/Notify.php | 1 - tests/Unit/Core/Hooks/HookEventBridgeTest.php | 4 ++ tests/Unit/Event/ArrayFilterEventTest.php | 2 + 6 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index ae7a42c154..06f918ea6d 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -78,6 +78,8 @@ final class HookEventBridge ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'profile_sidebar_enter', ArrayFilterEvent::PROFILE_SIDEBAR => 'profile_sidebar', ArrayFilterEvent::PROFILE_TABS => 'profile_tabs', + ArrayFilterEvent::PROFILE_SETTINGS_FORM => 'profile_edit', + ArrayFilterEvent::PROFILE_SETTINGS_POST => 'profile_post', ArrayFilterEvent::MODERATION_USERS_TABS => 'moderation_users_tabs', ArrayFilterEvent::ACL_LOOKUP_END => 'acl_lookup_end', ArrayFilterEvent::OEMBED_FETCH_END => 'oembed_fetch_url', @@ -171,6 +173,8 @@ final class HookEventBridge ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_TABS => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_SETTINGS_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_SETTINGS_POST => 'onArrayFilterEvent', ArrayFilterEvent::MODERATION_USERS_TABS => 'onArrayFilterEvent', ArrayFilterEvent::ACL_LOOKUP_END => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index 1f73eadf62..864f269cdb 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -106,6 +106,10 @@ final class ArrayFilterEvent extends Event public const PROFILE_TABS = 'friendica.data.profile_tabs'; + public const PROFILE_SETTINGS_FORM = 'friendica.data.profile_settings_form'; + + public const PROFILE_SETTINGS_POST = 'friendica.data.profile_settings_post'; + public const MODERATION_USERS_TABS = 'friendica.data.moderation_users_tabs'; public const ACL_LOOKUP_END = 'friendica.data.acl_lookup_end'; diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index f63ae3b135..bf865667e4 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -7,30 +7,33 @@ namespace Friendica\Module\Settings\Profile; -use Friendica\App; +use Friendica\App\Arguments; +use Friendica\App\BaseURL; +use Friendica\App\Page; use Friendica\Core\ACL; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Protocol; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Theme; +use Friendica\Core\Worker; use Friendica\Database\DBA; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Profile; use Friendica\Module\Response; -use Friendica\Navigation\SystemMessages; -use Friendica\Profile\ProfileField; use Friendica\Model\User; use Friendica\Module\BaseSettings; use Friendica\Module\Security\Login; +use Friendica\Navigation\SystemMessages; use Friendica\Network\HTTPException; +use Friendica\Profile\ProfileField; use Friendica\Security\PermissionSet; use Friendica\Util\ACLFormatter; use Friendica\Util\DateTimeFormat; use Friendica\Util\Profiler; use Friendica\Util\Temporal; -use Friendica\Core\Worker; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; class Index extends BaseSettings @@ -47,9 +50,27 @@ class Index extends BaseSettings private $permissionSetFactory; /** @var ACLFormatter */ private $aclFormatter; + private EventDispatcherInterface $eventDispatcher; - public function __construct(ACLFormatter $aclFormatter, PermissionSet\Factory\PermissionSet $permissionSetFactory, PermissionSet\Repository\PermissionSet $permissionSetRepo, SystemMessages $systemMessages, ProfileField\Factory\ProfileField $profileFieldFactory, ProfileField\Repository\ProfileField $profileFieldRepo, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) - { + public function __construct( + ACLFormatter $aclFormatter, + PermissionSet\Factory\PermissionSet $permissionSetFactory, + PermissionSet\Repository\PermissionSet $permissionSetRepo, + SystemMessages $systemMessages, + ProfileField\Factory\ProfileField $profileFieldFactory, + ProfileField\Repository\ProfileField $profileFieldRepo, + EventDispatcherInterface $eventDispatcher, + IHandleUserSessions $session, + Page $page, + L10n $l10n, + BaseURL $baseUrl, + Arguments $args, + LoggerInterface $logger, + Profiler $profiler, + Response $response, + array $server, + array $parameters = [], + ) { parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->profileFieldRepo = $profileFieldRepo; @@ -58,6 +79,7 @@ class Index extends BaseSettings $this->permissionSetRepo = $permissionSetRepo; $this->permissionSetFactory = $permissionSetFactory; $this->aclFormatter = $aclFormatter; + $this->eventDispatcher = $eventDispatcher; } protected function post(array $request = []) @@ -73,7 +95,9 @@ class Index extends BaseSettings self::checkFormSecurityTokenRedirectOnError('/settings/profile', 'settings_profile'); - Hook::callAll('profile_post', $request); + $request = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SETTINGS_POST, $request), + )->getArray(); $dob = trim($request['dob'] ?? ''); @@ -288,8 +312,16 @@ class Index extends BaseSettings '$custom_fields' => $custom_fields, ]); - $arr = ['profile' => $owner, 'entry' => $o]; - Hook::callAll('profile_edit', $arr); + $hook_data = [ + 'profile' => $owner, + 'entry' => $o, + ]; + + $hook_data = $this->eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SETTINGS_FORM, $hook_data), + )->getArray(); + + $o = $hook_data['entry'] ?? $o; return $o; } diff --git a/src/Navigation/Notifications/Repository/Notify.php b/src/Navigation/Notifications/Repository/Notify.php index c59eed92aa..7f55fd1a48 100644 --- a/src/Navigation/Notifications/Repository/Notify.php +++ b/src/Navigation/Notifications/Repository/Notify.php @@ -13,7 +13,6 @@ use Friendica\Content\Text\BBCode; use Friendica\Content\Text\Plaintext; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Database\Database; use Friendica\Database\DBA; diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 7b655ee0a1..436e1435bc 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -67,6 +67,8 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::PROFILE_TABS => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_SETTINGS_FORM => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_SETTINGS_POST => 'onArrayFilterEvent', ArrayFilterEvent::MODERATION_USERS_TABS => 'onArrayFilterEvent', ArrayFilterEvent::ACL_LOOKUP_END => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', @@ -559,6 +561,8 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'contact_photo_menu'], [ArrayFilterEvent::PROFILE_SIDEBAR, 'profile_sidebar'], [ArrayFilterEvent::PROFILE_TABS, 'profile_tabs'], + [ArrayFilterEvent::PROFILE_SETTINGS_FORM, 'profile_edit'], + [ArrayFilterEvent::PROFILE_SETTINGS_POST, 'profile_post'], [ArrayFilterEvent::MODERATION_USERS_TABS, 'moderation_users_tabs'], [ArrayFilterEvent::ACL_LOOKUP_END, 'acl_lookup_end'], [ArrayFilterEvent::PAGE_INFO, 'page_info_data'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index aae225cd46..85f9307a0d 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -64,6 +64,8 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, 'friendica.data.profile_sidebar_entry'], [ArrayFilterEvent::PROFILE_SIDEBAR, 'friendica.data.profile_sidebar'], [ArrayFilterEvent::PROFILE_TABS, 'friendica.data.profile_tabs'], + [ArrayFilterEvent::PROFILE_SETTINGS_FORM, 'friendica.data.profile_settings_form'], + [ArrayFilterEvent::PROFILE_SETTINGS_POST, 'friendica.data.profile_settings_post'], [ArrayFilterEvent::MODERATION_USERS_TABS, 'friendica.data.moderation_users_tabs'], [ArrayFilterEvent::ACL_LOOKUP_END, 'friendica.data.acl_lookup_end'], [ArrayFilterEvent::OEMBED_FETCH_END, 'friendica.data.oembed_fetch_end'], From f1abd5735682c4d0df9c25b8c84a5cc142b8680a Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 11 Apr 2025 08:32:05 +0000 Subject: [PATCH 41/45] Fix compat with PHP 7.4 --- src/Navigation/Notifications/Repository/Notify.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Navigation/Notifications/Repository/Notify.php b/src/Navigation/Notifications/Repository/Notify.php index 7f55fd1a48..0e572e9c61 100644 --- a/src/Navigation/Notifications/Repository/Notify.php +++ b/src/Navigation/Notifications/Repository/Notify.php @@ -71,7 +71,7 @@ class Notify extends BaseRepository Emailer $emailer, Factory\Notification $notification, EventDispatcherInterface $eventDispatcher, - Factory\Notify $factory = null, + Factory\Notify $factory = null ) { $this->l10n = $l10n; $this->baseUrl = $baseUrl; From 2e672414b3813dc062b8c7c7de126e4737469d44 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 11 Apr 2025 08:38:30 +0000 Subject: [PATCH 42/45] Fix compat with PHP 7.4 --- src/Module/Contact/Profile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Module/Contact/Profile.php b/src/Module/Contact/Profile.php index 39029a25a8..83ea318390 100644 --- a/src/Module/Contact/Profile.php +++ b/src/Module/Contact/Profile.php @@ -77,7 +77,7 @@ class Profile extends BaseModule Page $page, IManageConfigValues $config, array $server, - array $parameters = [], + array $parameters = [] ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); From fa58d8b11ab5661aa7de5ced8f283a6c3d94d703 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 15 Apr 2025 06:11:17 +0000 Subject: [PATCH 43/45] Fix compat with PHP 7.4 --- src/Module/Friendica.php | 2 +- src/Module/Moderation/BaseUsers.php | 2 +- src/Module/Ping/Network.php | 2 +- src/Module/Privacy/PermissionTooltip.php | 2 +- src/Module/Profile/Photos.php | 2 +- src/Module/Profile/Profile.php | 2 +- src/Module/Search/Acl.php | 2 +- src/Module/Settings/Profile/Index.php | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Module/Friendica.php b/src/Module/Friendica.php index 314152abca..42a46d8278 100644 --- a/src/Module/Friendica.php +++ b/src/Module/Friendica.php @@ -54,7 +54,7 @@ class Friendica extends BaseModule Profiler $profiler, Response $response, array $server, - array $parameters = [], + array $parameters = [] ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); diff --git a/src/Module/Moderation/BaseUsers.php b/src/Module/Moderation/BaseUsers.php index 283fa74ec2..698f4256a5 100644 --- a/src/Module/Moderation/BaseUsers.php +++ b/src/Module/Moderation/BaseUsers.php @@ -48,7 +48,7 @@ abstract class BaseUsers extends BaseModeration Profiler $profiler, Response $response, array $server, - array $parameters = [], + array $parameters = [] ) { parent::__construct($page, $appHelper, $systemMessages, $session, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); diff --git a/src/Module/Ping/Network.php b/src/Module/Ping/Network.php index 03c6b8eeac..520a5e66a6 100644 --- a/src/Module/Ping/Network.php +++ b/src/Module/Ping/Network.php @@ -67,7 +67,7 @@ class Network extends NetworkModule Profiler $profiler, Response $response, array $server, - array $parameters = [], + array $parameters = [] ) { parent::__construct( $userDefinedChannel, diff --git a/src/Module/Privacy/PermissionTooltip.php b/src/Module/Privacy/PermissionTooltip.php index 6f77daaec1..5541a49bd6 100644 --- a/src/Module/Privacy/PermissionTooltip.php +++ b/src/Module/Privacy/PermissionTooltip.php @@ -54,7 +54,7 @@ class PermissionTooltip extends BaseModule Profiler $profiler, Response $response, array $server, - array $parameters = [], + array $parameters = [] ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); diff --git a/src/Module/Profile/Photos.php b/src/Module/Profile/Photos.php index 29ece42ef9..2c543f9449 100644 --- a/src/Module/Profile/Photos.php +++ b/src/Module/Profile/Photos.php @@ -73,7 +73,7 @@ class Photos extends \Friendica\Module\BaseProfile Profiler $profiler, Response $response, array $server, - array $parameters = [], + array $parameters = [] ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); diff --git a/src/Module/Profile/Profile.php b/src/Module/Profile/Profile.php index b3f15f5f44..cb75b2fecc 100644 --- a/src/Module/Profile/Profile.php +++ b/src/Module/Profile/Profile.php @@ -74,7 +74,7 @@ class Profile extends BaseProfile Profiler $profiler, Response $response, array $server, - array $parameters = [], + array $parameters = [] ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); diff --git a/src/Module/Search/Acl.php b/src/Module/Search/Acl.php index 83e5a5dfff..3419672b19 100644 --- a/src/Module/Search/Acl.php +++ b/src/Module/Search/Acl.php @@ -58,7 +58,7 @@ class Acl extends BaseModule Profiler $profiler, Response $response, array $server, - array $parameters = [], + array $parameters = [] ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index bf865667e4..7b05aceaa6 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -69,7 +69,7 @@ class Index extends BaseSettings Profiler $profiler, Response $response, array $server, - array $parameters = [], + array $parameters = [] ) { parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); From b2bbcc496531194c0afe21d73353d3be4145693b Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 13 May 2025 06:12:16 +0000 Subject: [PATCH 44/45] Fix PHPStan errors --- src/Model/Item.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Model/Item.php b/src/Model/Item.php index 62e6aff96b..397b774fa6 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -864,6 +864,7 @@ class Item unset($_SESSION['uid']); } } elseif (!$notify) { + /** @var array */ $item = $eventDispatcher->dispatch( new ArrayFilterEvent(ArrayFilterEvent::INSERT_POST_REMOTE, $item) )->getArray(); From 02fae146e6007a36da9825d0e138e061df453075 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 13 May 2025 06:18:41 +0000 Subject: [PATCH 45/45] Fix code style --- .../Storage/Repository/StorageManager.php | 10 +-- src/Model/Item.php | 2 +- src/Model/Profile.php | 4 +- src/Module/Contact/Profile.php | 19 +++-- src/Module/Conversation/Network.php | 1 - src/Module/Directory.php | 7 +- src/Module/Friendica.php | 8 +-- src/Module/Moderation/BaseUsers.php | 4 +- src/Module/ParseUrl.php | 20 +++--- src/Module/Privacy/PermissionTooltip.php | 69 ++++++++++--------- src/Module/Profile/Photos.php | 26 +++---- src/Module/Profile/Profile.php | 18 ++--- src/Module/Register.php | 2 +- src/Module/Search/Acl.php | 37 ++++++---- src/Module/Settings/Profile/Index.php | 9 +-- .../Notifications/Repository/Notify.php | 12 ++-- .../Storage/Repository/StorageManagerTest.php | 4 +- 17 files changed, 134 insertions(+), 118 deletions(-) diff --git a/src/Core/Storage/Repository/StorageManager.php b/src/Core/Storage/Repository/StorageManager.php index 1de89dbddc..36efa3a1bf 100644 --- a/src/Core/Storage/Repository/StorageManager.php +++ b/src/Core/Storage/Repository/StorageManager.php @@ -76,12 +76,12 @@ class StorageManager */ public function __construct(Database $dba, IManageConfigValues $config, LoggerInterface $logger, EventDispatcherInterface $eventDispatcher, L10n $l10n, bool $includeAddon = true) { - $this->dba = $dba; - $this->config = $config; - $this->logger = $logger; + $this->dba = $dba; + $this->config = $config; + $this->logger = $logger; $this->eventDispatcher = $eventDispatcher; - $this->l10n = $l10n; - $this->validBackends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS); + $this->l10n = $l10n; + $this->validBackends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS); $currentName = $this->config->get('storage', 'name'); diff --git a/src/Model/Item.php b/src/Model/Item.php index 397b774fa6..3eb2b3b90b 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3051,7 +3051,7 @@ class Item $hook_data = [ 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash'], - 'item' => $item, + 'item' => $item, ]; $eventDispatcher = DI::eventDispatcher(); diff --git a/src/Model/Profile.php b/src/Model/Profile.php index a647a58c78..731a66cb2b 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -480,7 +480,7 @@ class Profile $hook_data = [ 'profile' => &$profile, - 'entry' => &$o, + 'entry' => &$o, ]; $hook_data = $eventDispatcher->dispatch( @@ -488,7 +488,7 @@ class Profile )->getArray(); $profile = $hook_data['profile'] ?? $profile; - $o = $hook_data['entry'] ?? $o; + $o = $hook_data['entry'] ?? $o; return $o; } diff --git a/src/Module/Contact/Profile.php b/src/Module/Contact/Profile.php index 83ea318390..dd7288e58c 100644 --- a/src/Module/Contact/Profile.php +++ b/src/Module/Contact/Profile.php @@ -265,7 +265,7 @@ class Profile extends BaseModule $this->baseUrl->redirect('contact/' . $contact['id']); } - $vcard_widget = Widget\VCard::getHTML($contact); + $vcard_widget = Widget\VCard::getHTML($contact); $circles_widget = ''; if (!in_array($localRelationship->rel, [ContactModel::NOTHING, ContactModel::SELF])) { @@ -283,9 +283,15 @@ class Profile extends BaseModule ]); switch ($localRelationship->rel) { - case ContactModel::FRIEND: $relation_text = $this->t('You are mutual friends with %s', $contact['name']); break; - case ContactModel::FOLLOWER: $relation_text = $this->t('You are sharing with %s', $contact['name']); break; - case ContactModel::SHARING: $relation_text = $this->t('%s is sharing with you', $contact['name']); break; + case ContactModel::FRIEND: + $relation_text = $this->t('You are mutual friends with %s', $contact['name']); + break; + case ContactModel::FOLLOWER: + $relation_text = $this->t('You are sharing with %s', $contact['name']); + break; + case ContactModel::SHARING: + $relation_text = $this->t('%s is sharing with you', $contact['name']); + break; default: $relation_text = ''; } @@ -308,8 +314,7 @@ class Profile extends BaseModule $this->logger->notice('Empty gsid for contact', ['contact' => $contact]); } - $serverIgnored = - $contact['gsid'] && + $serverIgnored = $contact['gsid'] && $this->userGServer->isIgnoredByUser($this->session->getLocalUserId(), $contact['gsid']) ? $this->t('This contact is on a server you ignored.') : ''; @@ -475,7 +480,7 @@ class Profile extends BaseModule $hook_data = [ 'contact' => $contact, - 'output' => $o, + 'output' => $o, ]; $hook_data = $this->eventDispatcher->dispatch( diff --git a/src/Module/Conversation/Network.php b/src/Module/Conversation/Network.php index f38c0d4b3f..ce4b8d9dd3 100644 --- a/src/Module/Conversation/Network.php +++ b/src/Module/Conversation/Network.php @@ -31,7 +31,6 @@ use Friendica\Content\Widget\TrendingTags; use Friendica\Core\ACL; use Friendica\Core\Cache\Capability\ICanCache; use Friendica\Core\Config\Capability\IManageConfigValues; -use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\Renderer; diff --git a/src/Module/Directory.php b/src/Module/Directory.php index d63bbc13e3..786c92dcd0 100644 --- a/src/Module/Directory.php +++ b/src/Module/Directory.php @@ -11,7 +11,6 @@ use Friendica\BaseModule; use Friendica\Content\Nav; use Friendica\Content\Pager; use Friendica\Content\Widget; -use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\Core\Search; use Friendica\DI; @@ -40,7 +39,7 @@ class Directory extends BaseModule DI::page()['aside'] .= Widget::follow(); } - $output = ''; + $output = ''; $entries = []; Nav::setSelected('directory'); @@ -48,7 +47,7 @@ class Directory extends BaseModule $search = trim(rawurldecode($_REQUEST['search'] ?? '')); $gDirPath = ''; - $dirURL = Search::getGlobalDirectory(); + $dirURL = Search::getGlobalDirectory(); if (strlen($dirURL)) { $gDirPath = OpenWebAuth::getZrlUrl($dirURL, true); } @@ -166,7 +165,7 @@ class Directory extends BaseModule $hook_data = [ 'contact' => $contact, - 'entry' => $entry, + 'entry' => $entry, ]; $hook_data = $eventDispatcher->dispatch( diff --git a/src/Module/Friendica.php b/src/Module/Friendica.php index 42a46d8278..64064423d7 100644 --- a/src/Module/Friendica.php +++ b/src/Module/Friendica.php @@ -58,11 +58,11 @@ class Friendica extends BaseModule ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->config = $config; - $this->keyValue = $keyValue; - $this->session = $session; + $this->config = $config; + $this->keyValue = $keyValue; + $this->session = $session; $this->eventDispatcher = $eventDispatcher; - $this->addonHelper = $addonHelper; + $this->addonHelper = $addonHelper; } protected function content(array $request = []): string diff --git a/src/Module/Moderation/BaseUsers.php b/src/Module/Moderation/BaseUsers.php index 698f4256a5..8a12d2b0a1 100644 --- a/src/Module/Moderation/BaseUsers.php +++ b/src/Module/Moderation/BaseUsers.php @@ -52,7 +52,7 @@ abstract class BaseUsers extends BaseModeration ) { parent::__construct($page, $appHelper, $systemMessages, $session, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->database = $database; + $this->database = $database; $this->eventDispatcher = $eventDispatcher; } @@ -115,7 +115,7 @@ abstract class BaseUsers extends BaseModeration ]; $hook_data = [ - 'tabs' => $tabs, + 'tabs' => $tabs, 'selectedTab' => $selectedTab, ]; diff --git a/src/Module/ParseUrl.php b/src/Module/ParseUrl.php index 508e5d987d..d023368f10 100644 --- a/src/Module/ParseUrl.php +++ b/src/Module/ParseUrl.php @@ -31,7 +31,7 @@ class ParseUrl extends BaseModule { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->userSession = $userSession; + $this->userSession = $userSession; $this->eventDispatcher = $eventDispatcher; } @@ -41,10 +41,10 @@ class ParseUrl extends BaseModule throw new \Friendica\Network\HTTPException\ForbiddenException(); } - $format = ''; - $title = ''; + $format = ''; + $title = ''; $description = ''; - $ret = ['success' => false, 'contentType' => '']; + $ret = ['success' => false, 'contentType' => '']; if (!empty($_GET['binurl']) && Util\Strings::isHex($_GET['binurl'])) { $url = trim(hex2bin($_GET['binurl'])); @@ -85,9 +85,9 @@ class ParseUrl extends BaseModule } $hook_data = [ - 'url' => $url, + 'url' => $url, 'format' => $format, - 'text' => null, + 'text' => null, ]; $hook_data = $this->eventDispatcher->dispatch( @@ -119,14 +119,14 @@ class ParseUrl extends BaseModule } $ret['contentType'] = $content_type; - $ret['data'] = ['url' => $url]; - $ret['success'] = true; + $ret['data'] = ['url' => $url]; + $ret['success'] = true; } else { unset($siteinfo['keywords']); - $ret['data'] = $siteinfo; + $ret['data'] = $siteinfo; $ret['contentType'] = 'attachment'; - $ret['success'] = true; + $ret['success'] = true; } $this->jsonExit($ret); diff --git a/src/Module/Privacy/PermissionTooltip.php b/src/Module/Privacy/PermissionTooltip.php index 5541a49bd6..e6656b95e9 100644 --- a/src/Module/Privacy/PermissionTooltip.php +++ b/src/Module/Privacy/PermissionTooltip.php @@ -58,17 +58,17 @@ class PermissionTooltip extends BaseModule ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->dba = $dba; - $this->aclFormatter = $aclFormatter; - $this->session = $session; - $this->config = $config; - $this->permissionSet = $permissionSet; + $this->dba = $dba; + $this->aclFormatter = $aclFormatter; + $this->session = $session; + $this->config = $config; + $this->permissionSet = $permissionSet; $this->eventDispatcher = $eventDispatcher; } protected function rawContent(array $request = []) { - $type = $this->parameters['type']; + $type = $this->parameters['type']; $referenceId = $this->parameters['id']; $expectedTypes = ['item', 'photo', 'event']; @@ -79,10 +79,10 @@ class PermissionTooltip extends BaseModule $condition = ['id' => $referenceId, 'uid' => [0, $this->session->getLocalUserId()]]; if ($type == 'item') { $fields = ['uid', 'psid', 'private', 'uri-id', 'origin', 'network']; - $model = Model\Post::selectFirst($fields, $condition, ['order' => ['uid' => true]]); + $model = Model\Post::selectFirst($fields, $condition, ['order' => ['uid' => true]]); if ($model['origin'] || ($model['network'] != Protocol::ACTIVITYPUB)) { - $permissionSet = $this->permissionSet->selectOneById($model['psid'], $model['uid']); + $permissionSet = $this->permissionSet->selectOneById($model['psid'], $model['uid']); $model['allow_cid'] = $permissionSet->allow_cid; $model['allow_gid'] = $permissionSet->allow_gid; $model['deny_cid'] = $permissionSet->deny_cid; @@ -94,8 +94,8 @@ class PermissionTooltip extends BaseModule $model['deny_gid'] = []; } } else { - $fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']; - $model = $this->dba->selectFirst($type, $fields, $condition); + $fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']; + $model = $this->dba->selectFirst($type, $fields, $condition); $model['allow_cid'] = $this->aclFormatter->expand($model['allow_cid']); $model['allow_gid'] = $this->aclFormatter->expand($model['allow_gid']); $model['deny_cid'] = $this->aclFormatter->expand($model['deny_cid']); @@ -116,7 +116,7 @@ class PermissionTooltip extends BaseModule $model = $hook_data['model'] ?? $model; - $aclReceivers = new Entity\AclReceivers(); + $aclReceivers = new Entity\AclReceivers(); $addressedReceivers = new Entity\AddressedReceivers(); if (!empty($model['allow_cid']) || !empty($model['allow_gid']) || !empty($model['deny_cid']) || !empty($model['deny_gid'])) { $aclReceivers = $this->fetchReceiversFromACL($model); @@ -126,30 +126,35 @@ class PermissionTooltip extends BaseModule $privacy = ''; switch ($model['private'] ?? null) { - case Model\Item::PUBLIC: $privacy = $this->t('Public'); break; - case Model\Item::UNLISTED: $privacy = $this->t('Unlisted'); break; - case Model\Item::PRIVATE: $privacy = $this->t('Limited/Private'); break; + case Model\Item::PUBLIC: + $privacy = $this->t('Public'); + break; + case Model\Item::UNLISTED: + $privacy = $this->t('Unlisted'); + break; + case Model\Item::PRIVATE: + $privacy = $this->t('Limited/Private'); + break; } - if ($aclReceivers->isEmpty() && $addressedReceivers->isEmpty() && empty($privacy)) - { + if ($aclReceivers->isEmpty() && $addressedReceivers->isEmpty() && empty($privacy)) { echo $this->t('Remote privacy information not available.'); exit; } - $tpl = Renderer::getMarkupTemplate('privacy/permission_tooltip.tpl'); + $tpl = Renderer::getMarkupTemplate('privacy/permission_tooltip.tpl'); $output = Renderer::replaceMacros($tpl, [ '$l10n' => [ 'visible_to' => $this->t('Visible to:'), - 'to' => $this->t('To:'), - 'cc' => $this->t('CC:'), - 'bcc' => $this->t('BCC:'), - 'audience' => $this->t('Audience:'), + 'to' => $this->t('To:'), + 'cc' => $this->t('CC:'), + 'bcc' => $this->t('BCC:'), + 'audience' => $this->t('Audience:'), 'attributed' => $this->t('Attributed To:'), ], - '$aclReceivers' => $aclReceivers, + '$aclReceivers' => $aclReceivers, '$addressedReceivers' => $addressedReceivers, - '$privacy' => $privacy, + '$privacy' => $privacy, ]); $this->httpExit($output); @@ -223,7 +228,7 @@ class PermissionTooltip extends BaseModule private function fetchAddressedReceivers(int $uriId): Entity\AddressedReceivers { $own_url = ''; - $uid = $this->session->getLocalUserId(); + $uid = $this->session->getLocalUserId(); if ($uid) { $owner = Model\User::getOwnerDataById($uid); if (!empty($owner['url'])) { @@ -246,11 +251,11 @@ class PermissionTooltip extends BaseModule $receivers[$receiver['type']][] = $this->t('Collection (%s)', $receiver['name']); break; case Model\Tag::FOLLOWER_COLLECTION: - $apcontact = $this->dba->selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]); + $apcontact = $this->dba->selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]); $receivers[$receiver['type']][] = $this->t('Followers (%s)', $apcontact['name'] ?? $receiver['name']); break; case Model\Tag::ACCOUNT: - $apcontact = Model\APContact::getByURL($receiver['url'], false); + $apcontact = Model\APContact::getByURL($receiver['url'], false); $receivers[$receiver['type']][] = $apcontact['name'] ?? $receiver['name']; break; default: @@ -260,19 +265,19 @@ class PermissionTooltip extends BaseModule } foreach ($receivers as $type => $receiver) { - $max = $this->config->get('system', 'max_receivers'); + $max = $this->config->get('system', 'max_receivers'); $total = count($receiver); if ($total > $max) { - $receivers[$type] = array_slice($receiver, 0, $max); + $receivers[$type] = array_slice($receiver, 0, $max); $receivers[$type][] = $this->t('%d more', $total - $max); } } return new Entity\AddressedReceivers( - $receivers[Model\Tag::TO] ?? [], - $receivers[Model\Tag::CC] ?? [], - $receivers[Model\Tag::BCC] ?? [], - $receivers[Model\Tag::AUDIENCE] ?? [], + $receivers[Model\Tag::TO] ?? [], + $receivers[Model\Tag::CC] ?? [], + $receivers[Model\Tag::BCC] ?? [], + $receivers[Model\Tag::AUDIENCE] ?? [], $receivers[Model\Tag::ATTRIBUTED] ?? [], ); } diff --git a/src/Module/Profile/Photos.php b/src/Module/Profile/Photos.php index 2c543f9449..6d9bca26e3 100644 --- a/src/Module/Profile/Photos.php +++ b/src/Module/Profile/Photos.php @@ -77,13 +77,13 @@ class Photos extends \Friendica\Module\BaseProfile ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->session = $session; - $this->page = $page; - $this->config = $config; - $this->appHelper = $appHelper; - $this->database = $database; - $this->systemMessages = $systemMessages; - $this->aclFormatter = $aclFormatter; + $this->session = $session; + $this->page = $page; + $this->config = $config; + $this->appHelper = $appHelper; + $this->database = $database; + $this->systemMessages = $systemMessages; + $this->aclFormatter = $aclFormatter; $this->eventDispatcher = $eventDispatcher; $owner = Profile::load($this->appHelper, $this->parameters['nickname'] ?? '', false); @@ -109,7 +109,7 @@ class Photos extends \Friendica\Module\BaseProfile if ($visibility === 'public') { // The ACL selector introduced in version 2019.12 sends ACL input data even when the Public visibility is selected $str_contact_allow = $str_circle_allow = $str_contact_deny = $str_circle_deny = ''; - } else if ($visibility === 'custom') { + } elseif ($visibility === 'custom') { // Since we know from the visibility parameter the item should be private, we have to prevent the empty ACL // case that would make it public. So we always append the author's contact id to the allowed contacts. // See https://github.com/friendica/friendica/issues/9672 @@ -155,10 +155,10 @@ class Photos extends \Friendica\Module\BaseProfile } $hook_data = [ - 'src' => '', + 'src' => '', 'filename' => '', 'filesize' => 0, - 'type' => '', + 'type' => '', ]; $hook_data = $this->eventDispatcher->dispatch( @@ -183,7 +183,7 @@ class Photos extends \Friendica\Module\BaseProfile $type = $_FILES['userfile']['type']; $error = $_FILES['userfile']['error']; } else { - $error = UPLOAD_ERR_NO_FILE; + $error = UPLOAD_ERR_NO_FILE; } if ($error !== UPLOAD_ERR_OK) { @@ -314,7 +314,7 @@ class Photos extends \Friendica\Module\BaseProfile $arr['visible'] = $visible; $arr['origin'] = 1; - $arr['body'] = Images::getBBCodeByResource($resource_id, $this->owner['nickname'], $preview, $image->getExt()); + $arr['body'] = Images::getBBCodeByResource($resource_id, $this->owner['nickname'], $preview, $image->getExt()); $item_id = Item::insert($arr); // Update the photo albums cache @@ -383,7 +383,7 @@ class Photos extends \Friendica\Module\BaseProfile $pager->getItemsPerPage() )); - $photos = array_map(function ($photo){ + $photos = array_map(function ($photo) { return [ 'id' => $photo['id'], 'link' => 'photos/' . $this->owner['nickname'] . '/image/' . $photo['resource-id'], diff --git a/src/Module/Profile/Profile.php b/src/Module/Profile/Profile.php index cb75b2fecc..58f829907e 100644 --- a/src/Module/Profile/Profile.php +++ b/src/Module/Profile/Profile.php @@ -78,12 +78,12 @@ class Profile extends BaseProfile ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->database = $database; - $this->appHelper = $appHelper; - $this->session = $session; - $this->config = $config; - $this->page = $page; - $this->profileField = $profileField; + $this->database = $database; + $this->appHelper = $appHelper; + $this->session = $session; + $this->config = $config; + $this->page = $page; + $this->profileField = $profileField; $this->eventDispatcher = $eventDispatcher; } @@ -273,7 +273,7 @@ class Profile extends BaseProfile } $tpl = Renderer::getMarkupTemplate('profile/profile.tpl'); - $o .= Renderer::replaceMacros($tpl, [ + $o .= Renderer::replaceMacros($tpl, [ '$title' => $this->t('Profile'), '$yourself' => $this->t('Yourself'), '$view_as_contacts' => $view_as_contacts, @@ -293,7 +293,7 @@ class Profile extends BaseProfile 'title' => '', 'label' => $this->t('Edit profile') ], - '$viewas_link' => [ + '$viewas_link' => [ 'url' => $this->args->getQueryString() . '#viewas', 'title' => '', 'label' => $this->t('View as') @@ -362,7 +362,7 @@ class Profile extends BaseProfile $htmlhead .= '' . "\n"; $htmlhead .= '' . "\n"; $htmlhead .= '' . "\n"; - $uri = urlencode('acct:' . $profile['nickname'] . '@' . $this->baseUrl->getHost() . ($this->baseUrl->getPath() ? '/' . $this->baseUrl->getPath() : '')); + $uri = urlencode('acct:' . $profile['nickname'] . '@' . $this->baseUrl->getHost() . ($this->baseUrl->getPath() ? '/' . $this->baseUrl->getPath() : '')); $htmlhead .= '' . "\n"; header('Link: <' . $this->baseUrl . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false); diff --git a/src/Module/Register.php b/src/Module/Register.php index 6f641355e3..39ec42cd84 100644 --- a/src/Module/Register.php +++ b/src/Module/Register.php @@ -50,7 +50,7 @@ class Register extends BaseModule $this->tos = new Tos($l10n, $baseUrl, $args, $logger, $profiler, $response, $config, $server, $parameters); - $this->session = $session; + $this->session = $session; $this->eventDispatcher = $eventDispatcher; } diff --git a/src/Module/Search/Acl.php b/src/Module/Search/Acl.php index 3419672b19..410bae9a5e 100644 --- a/src/Module/Search/Acl.php +++ b/src/Module/Search/Acl.php @@ -62,8 +62,8 @@ class Acl extends BaseModule ) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->session = $session; - $this->database = $database; + $this->session = $session; + $this->database = $database; $this->eventDispatcher = $eventDispatcher; } @@ -118,9 +118,9 @@ class Acl extends BaseModule private function regularContactSearch(array $request, string $type): array { - $start = $request['start'] ?? 0; - $count = $request['count'] ?? 100; - $search = $request['search'] ?? ''; + $start = $request['start'] ?? 0; + $count = $request['count'] ?? 100; + $search = $request['search'] ?? ''; $conv_id = $request['conversation'] ?? null; // For use with jquery.textcomplete for private mail completion @@ -138,9 +138,9 @@ class Acl extends BaseModule $condition_circle = ["`uid` = ? AND NOT `deleted`", $this->session->getLocalUserId()]; if ($search != '') { - $sql_extra = "AND `name` LIKE '%%" . $this->database->escape($search) . "%%'"; - $condition = DBA::mergeConditions($condition, ["(`attag` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", - '%' . $search . '%', '%' . $search . '%', '%' . $search . '%']); + $sql_extra = "AND `name` LIKE '%%" . $this->database->escape($search) . "%%'"; + $condition = DBA::mergeConditions($condition, ["(`attag` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", + '%' . $search . '%', '%' . $search . '%', '%' . $search . '%']); $condition_circle = DBA::mergeConditions($condition_circle, ["`name` LIKE ?", '%' . $search . '%']); } @@ -156,21 +156,27 @@ class Acl extends BaseModule switch ($type) { case self::TYPE_MENTION_CONTACT_CIRCLE: case self::TYPE_MENTION_CONTACT: - $condition = DBA::mergeConditions($condition, + $condition = DBA::mergeConditions( + $condition, ["NOT `self` AND NOT `blocked`", - ]); + ] + ); break; case self::TYPE_MENTION_GROUP: - $condition = DBA::mergeConditions($condition, + $condition = DBA::mergeConditions( + $condition, ["NOT `self` AND NOT `blocked` AND (NOT `ap-posting-restricted` OR `ap-posting-restricted` IS NULL) AND `contact-type` = ?", Contact::TYPE_COMMUNITY - ]); + ] + ); break; case self::TYPE_PRIVATE_MESSAGE: - $condition = DBA::mergeConditions($condition, + $condition = DBA::mergeConditions( + $condition, ["NOT `self` AND NOT `blocked` AND `network` IN (?, ?, ?)", Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA - ]); + ] + ); break; } @@ -184,7 +190,8 @@ class Acl extends BaseModule if ($type == self::TYPE_MENTION_CONTACT_CIRCLE || $type == self::TYPE_MENTION_CIRCLE) { /// @todo We should cache this query. // This can be done when we can delete cache entries via wildcard - $circles = $this->database->toArray($this->database->p("SELECT `circle`.`id`, `circle`.`name`, GROUP_CONCAT(DISTINCT `circle_member`.`contact-id` SEPARATOR ',') AS uids + $circles = $this->database->toArray($this->database->p( + "SELECT `circle`.`id`, `circle`.`name`, GROUP_CONCAT(DISTINCT `circle_member`.`contact-id` SEPARATOR ',') AS uids FROM `group` AS `circle` INNER JOIN `group_member` AS `circle_member` ON `circle_member`.`gid` = `circle`.`id` WHERE NOT `circle`.`deleted` AND `circle`.`uid` = ? diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index 7b05aceaa6..6f7a3a6204 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -111,7 +111,7 @@ class Index extends BaseSettings if (strpos($dob, '0000-') === 0 || strpos($dob, '0001-') === 0) { $ignore_year = true; - $dob = substr($dob, 5); + $dob = substr($dob, 5); } if ($ignore_year) { @@ -245,7 +245,7 @@ class Index extends BaseSettings $this->session->getLocalUserId(), false, ['allow_cid' => []], - ['network' => Protocol::DFRN], + ['network' => Protocol::DFRN], 'profile_field[new]' ), ], @@ -278,7 +278,8 @@ class Index extends BaseSettings 'miscellaneous_section' => $this->t('Miscellaneous'), 'custom_fields_section' => $this->t('Custom Profile Fields'), 'profile_photo' => $this->t('Upload Profile Photo'), - 'custom_fields_description' => $this->t('

Custom fields appear on your profile page.

+ 'custom_fields_description' => $this->t( + '

Custom fields appear on your profile page.

You can use BBCodes in the field values.

Reorder by dragging the field title.

Empty the label field to remove a custom field.

@@ -314,7 +315,7 @@ class Index extends BaseSettings $hook_data = [ 'profile' => $owner, - 'entry' => $o, + 'entry' => $o, ]; $hook_data = $this->eventDispatcher->dispatch( diff --git a/src/Navigation/Notifications/Repository/Notify.php b/src/Navigation/Notifications/Repository/Notify.php index 17d3e81c44..40caa29437 100644 --- a/src/Navigation/Notifications/Repository/Notify.php +++ b/src/Navigation/Notifications/Repository/Notify.php @@ -74,12 +74,12 @@ class Notify extends BaseRepository EventDispatcherInterface $eventDispatcher, Factory\Notify $factory = null ) { - $this->l10n = $l10n; - $this->baseUrl = $baseUrl; - $this->config = $config; - $this->pConfig = $pConfig; - $this->emailer = $emailer; - $this->notification = $notification; + $this->l10n = $l10n; + $this->baseUrl = $baseUrl; + $this->config = $config; + $this->pConfig = $pConfig; + $this->emailer = $emailer; + $this->notification = $notification; $this->eventDispatcher = $eventDispatcher; parent::__construct($database, $logger, $factory ?? new Factory\Notify($logger)); diff --git a/tests/src/Core/Storage/Repository/StorageManagerTest.php b/tests/src/Core/Storage/Repository/StorageManagerTest.php index 14aa41f5a4..8af22c4b78 100644 --- a/tests/src/Core/Storage/Repository/StorageManagerTest.php +++ b/tests/src/Core/Storage/Repository/StorageManagerTest.php @@ -376,7 +376,7 @@ class StorageManagerTest extends DatabaseTestCase $this->l10n, false ); - $storage = $storageManager->getWritableStorageByName($name); + $storage = $storageManager->getWritableStorageByName($name); $storageManager->move($storage); $photos = $this->database->select('photo', ['backend-ref', 'backend-class', 'id', 'data']); @@ -407,7 +407,7 @@ class StorageManagerTest extends DatabaseTestCase $this->l10n, false ); - $storage = $storageManager->getWritableStorageByName(SystemResource::getName()); + $storage = $storageManager->getWritableStorageByName(SystemResource::getName()); $storageManager->move($storage); } }