]> git.mxchange.org Git - friendica.git/commitdiff
Create events for enotify hooks
authorArt4 <art4@wlabs.de>
Fri, 11 Apr 2025 07:46:48 +0000 (07:46 +0000)
committerArt4 <art4@wlabs.de>
Fri, 11 Apr 2025 07:46:48 +0000 (07:46 +0000)
src/Core/Hooks/HookEventBridge.php
src/Event/ArrayFilterEvent.php
src/Navigation/Notifications/Repository/Notify.php
tests/Unit/Core/Hooks/HookEventBridgeTest.php
tests/Unit/Event/ArrayFilterEventTest.php

index 9b8c2350c4cc23fd0be3970caf49b1960af52cf5..ae7a42c154b819ffae80bc3dd598f71bbadc697b 100644 (file)
@@ -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',
index 591250455fbb490df152d2fbe02ebcb41452414b..1f73eadf62fdc6dbc079ccfd57ec972a8655a251 100644 (file)
@@ -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';
index 1c793faaee1af21b9760fa404fd1af6c081da490..c59eed92aa922f1cfb520a21829fd8cbd26ca1de 100644 (file)
@@ -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);
index fc3752e97783586d239b3ab9c6a09004b32629c4..7b655ee0a109e6c56f8e0d22f1e8ef28b0b31bd7 100644 (file)
@@ -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'],
index 771cd4fed7469ca6b2087a8bf37755bde1447830..aae225cd468f1816cd58a22d5965fcafd79b1bc2 100644 (file)
@@ -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'],