Create events for enotify hooks

This commit is contained in:
Art4 2025-04-11 07:46:48 +00:00
parent 7531a2fb36
commit c436f5249b
5 changed files with 67 additions and 24 deletions

View 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',

View 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';

View 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);

View 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'],

View 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'],