From 10e4f4bf36cb601af0c419f0c864fd672dbb8999 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 13 Mar 2025 14:02:35 +0000 Subject: [PATCH] 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'], ]; }