Replace hooks post_local_start, post_local and post_local_end with EventDispatcher

This commit is contained in:
Art4 2025-02-08 08:17:30 +00:00
parent 649ddf7ab9
commit aabf9a8b01
4 changed files with 48 additions and 22 deletions

View file

@ -24,6 +24,7 @@ use Friendica\Core\System;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Event\ArrayFilterEvent;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\ItemURI; use Friendica\Model\ItemURI;
@ -43,7 +44,11 @@ function item_post()
item_drop($uid, $_REQUEST['dropitems']); item_drop($uid, $_REQUEST['dropitems']);
} }
Hook::callAll('post_local_start', $_REQUEST); $eventDispatcher = DI::eventDispatcher();
$_REQUEST = $eventDispatcher->dispatch(
new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL_START, $_REQUEST)
)->getArray();
$return_path = $_REQUEST['return'] ?? ''; $return_path = $_REQUEST['return'] ?? '';
$preview = intval($_REQUEST['preview'] ?? 0); $preview = intval($_REQUEST['preview'] ?? 0);
@ -275,7 +280,11 @@ function item_process(array $post, array $request, bool $preview, string $return
System::jsonExit(['preview' => $o]); System::jsonExit(['preview' => $o]);
} }
Hook::callAll('post_local', $post); $eventDispatcher = DI::eventDispatcher();
$post = $eventDispatcher->dispatch(
new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL, $post)
)->getArray();
unset($post['edit']); unset($post['edit']);
unset($post['self']); unset($post['self']);

View file

@ -20,6 +20,7 @@ use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Event\ArrayFilterEvent;
use Friendica\Model\Attach; use Friendica\Model\Attach;
use Friendica\Model\Circle; use Friendica\Model\Circle;
use Friendica\Model\Contact; use Friendica\Model\Contact;
@ -43,6 +44,7 @@ use Friendica\Util\Proxy;
use Friendica\Util\XML; use Friendica\Util\XML;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use ImagickException; use ImagickException;
use Psr\EventDispatcher\EventDispatcherInterface;
/** /**
* A content helper class for displaying items * A content helper class for displaying items
@ -69,19 +71,21 @@ class Item
private $emailer; private $emailer;
/** @var AppHelper */ /** @var AppHelper */
private $appHelper; private $appHelper;
private EventDispatcherInterface $eventDispatcher;
public function __construct(Profiler $profiler, Activity $activity, L10n $l10n, IHandleUserSessions $userSession, Video $bbCodeVideo, ACLFormatter $aclFormatter, IManagePersonalConfigValues $pConfig, BaseURL $baseURL, Emailer $emailer, AppHelper $appHelper) public function __construct(Profiler $profiler, Activity $activity, L10n $l10n, IHandleUserSessions $userSession, Video $bbCodeVideo, ACLFormatter $aclFormatter, IManagePersonalConfigValues $pConfig, BaseURL $baseURL, Emailer $emailer, AppHelper $appHelper, EventDispatcherInterface $eventDispatcher)
{ {
$this->profiler = $profiler; $this->profiler = $profiler;
$this->activity = $activity; $this->activity = $activity;
$this->l10n = $l10n; $this->l10n = $l10n;
$this->userSession = $userSession; $this->userSession = $userSession;
$this->bbCodeVideo = $bbCodeVideo; $this->bbCodeVideo = $bbCodeVideo;
$this->aclFormatter = $aclFormatter; $this->aclFormatter = $aclFormatter;
$this->baseURL = $baseURL; $this->baseURL = $baseURL;
$this->pConfig = $pConfig; $this->pConfig = $pConfig;
$this->emailer = $emailer; $this->emailer = $emailer;
$this->appHelper = $appHelper; $this->appHelper = $appHelper;
$this->eventDispatcher = $eventDispatcher;
} }
/** /**
@ -1006,7 +1010,9 @@ class Item
Tag::createImplicitMentions($post['uri-id'], $post['thr-parent-id']); Tag::createImplicitMentions($post['uri-id'], $post['thr-parent-id']);
} }
Hook::callAll('post_local_end', $post); $post = $this->eventDispatcher->dispatch(
new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL_END, $post)
)->getArray();
$author = DBA::selectFirst('contact', ['thumb'], ['uid' => $post['uid'], 'self' => true]); $author = DBA::selectFirst('contact', ['thumb'], ['uid' => $post['uid'], 'self' => true]);

View file

@ -22,6 +22,7 @@ use Friendica\Core\System;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Event\ArrayFilterEvent;
use Friendica\Model\Post\Category; use Friendica\Model\Post\Category;
use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
@ -1119,6 +1120,8 @@ class Item
$item['private'] = self::PRIVATE; $item['private'] = self::PRIVATE;
} }
$eventDispatcher = DI::eventDispatcher();
if ($notify && $post_local) { if ($notify && $post_local) {
$item['edit'] = false; $item['edit'] = false;
$item['parent'] = $parent_id; $item['parent'] = $parent_id;
@ -1137,7 +1140,9 @@ class Item
$dummy_session = false; $dummy_session = false;
} }
Hook::callAll('post_local', $item); $item = $eventDispatcher->dispatch(
new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL, $item)
)->getArray();
if ($dummy_session) { if ($dummy_session) {
unset($_SESSION['authenticated']); unset($_SESSION['authenticated']);
@ -1237,7 +1242,9 @@ class Item
} }
if (empty($item['event-id'])) { if (empty($item['event-id'])) {
unset($item['event-id']); if (array_key_exists('event-id', $item)) {
unset($item['event-id']);
}
$ev = Event::fromBBCode($item['body']); $ev = Event::fromBBCode($item['body']);
if ((!empty($ev['desc']) || !empty($ev['summary'])) && !empty($ev['start'])) { if ((!empty($ev['desc']) || !empty($ev['summary'])) && !empty($ev['start'])) {
@ -3010,9 +3017,6 @@ class Item
* @return bool * @return bool
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
* @hook 'post_local_end'
* array $arr
* 'post_id' => ID of posted item
*/ */
public static function performActivity(int $item_id, string $verb, int $uid, string $allow_cid = null, string $allow_gid = null, string $deny_cid = null, string $deny_gid = null): bool public static function performActivity(int $item_id, string $verb, int $uid, string $allow_cid = null, string $allow_gid = null, string $deny_cid = null, string $deny_gid = null): bool
{ {

View file

@ -13,6 +13,7 @@ use Friendica\Core\L10n;
use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Event\ArrayFilterEvent;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Post; use Friendica\Model\Post;
@ -22,6 +23,7 @@ use Friendica\Protocol\Activity;
use Friendica\Protocol\Delivery; use Friendica\Protocol\Delivery;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use Friendica\Util\XML; use Friendica\Util\XML;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/** /**
@ -31,12 +33,14 @@ class Add extends \Friendica\BaseModule
{ {
/** @var IHandleUserSessions */ /** @var IHandleUserSessions */
private $session; private $session;
private EventDispatcherInterface $eventDispatcher;
public function __construct(IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) public function __construct(IHandleUserSessions $session, EventDispatcherInterface $eventDispatcher, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
{ {
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->session = $session; $this->session = $session;
$this->eventDispatcher = $eventDispatcher;
} }
protected function post(array $request = []) protected function post(array $request = [])
@ -149,7 +153,10 @@ EOT;
Tag::store($item['uri-id'], Tag::HASHTAG, $term); Tag::store($item['uri-id'], Tag::HASHTAG, $term);
$post['id'] = $post_id; $post['id'] = $post_id;
Hook::callAll('post_local_end', $post);
$post = $this->eventDispatcher->dispatch(
new ArrayFilterEvent(ArrayFilterEvent::POST_LOCAL_END, $post)
)->getArray();
$post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]); $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]);