From 73c7b88fb2b25ddc1a5b4c04f2d70b86fefe2019 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 14 Mar 2022 21:53:33 -0400 Subject: [PATCH] Add exception when message is empty in FormatteNavNotification::createFromNotification - Filter out message less notifications in Ping --- src/Module/Notifications/Ping.php | 16 +++++++++--- .../Exception/NoMessageException.php | 26 +++++++++++++++++++ .../Factory/FormattedNavNotification.php | 13 ++++++++++ .../ValueObject/FormattedNavNotification.php | 4 +-- 4 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/Navigation/Notifications/Exception/NoMessageException.php diff --git a/src/Module/Notifications/Ping.php b/src/Module/Notifications/Ping.php index 50c823203..c2c0c2c67 100644 --- a/src/Module/Notifications/Ping.php +++ b/src/Module/Notifications/Ping.php @@ -37,6 +37,7 @@ use Friendica\Model\Verb; use Friendica\Module\Register; use Friendica\Module\Response; use Friendica\Navigation\Notifications\Entity; +use Friendica\Navigation\Notifications\Exception\NoMessageException; use Friendica\Navigation\Notifications\Factory; use Friendica\Navigation\Notifications\Repository; use Friendica\Navigation\Notifications\ValueObject; @@ -182,11 +183,20 @@ class Ping extends BaseModule } } - $sysnotify_count = $notifications->countUnseen(); - + // Temporary workaround for notifications without messages like with the following verb: + // - \Friendica\Protocol\Activity::ANNOUNCE $navNotifications = array_map(function (Entity\Notification $notification) { - return $this->formattedNavNotification->createFromNotification($notification); + try { + return $this->formattedNavNotification->createFromNotification($notification); + } catch (NoMessageException $e) { + return null; + } }, $notifications->getArrayCopy()); + $navNotifications = array_filter($navNotifications); + + $sysnotify_count = array_reduce($navNotifications, function (int $carry, ValueObject\FormattedNavNotification $navNotification) { + return $carry + ($navNotification->seen ? 0 : 1); + }, 0); // merge all notification types in one array foreach ($intros as $intro) { diff --git a/src/Navigation/Notifications/Exception/NoMessageException.php b/src/Navigation/Notifications/Exception/NoMessageException.php new file mode 100644 index 000000000..710f25fa3 --- /dev/null +++ b/src/Navigation/Notifications/Exception/NoMessageException.php @@ -0,0 +1,26 @@ +. + * + */ + +namespace Friendica\Navigation\Notifications\Exception; + +class NoMessageException extends \Exception +{ +} diff --git a/src/Navigation/Notifications/Factory/FormattedNavNotification.php b/src/Navigation/Notifications/Factory/FormattedNavNotification.php index a9498eaa5..2577f5333 100644 --- a/src/Navigation/Notifications/Factory/FormattedNavNotification.php +++ b/src/Navigation/Notifications/Factory/FormattedNavNotification.php @@ -25,6 +25,7 @@ use Friendica\BaseFactory; use Friendica\Core\Renderer; use Friendica\Model\Contact; use Friendica\Navigation\Notifications\Entity; +use Friendica\Navigation\Notifications\Exception\NoMessageException; use Friendica\Navigation\Notifications\ValueObject; use Friendica\Util\DateTimeFormat; use Friendica\Util\Proxy; @@ -94,10 +95,22 @@ class FormattedNavNotification extends BaseFactory ); } + /** + * @param Entity\Notification $notification + * @return ValueObject\FormattedNavNotification + * @throws NoMessageException + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \Friendica\Network\HTTPException\NotFoundException + * @throws \Friendica\Network\HTTPException\ServiceUnavailableException + */ public function createFromNotification(Entity\Notification $notification): ValueObject\FormattedNavNotification { $message = $this->notification->getMessageFromNotification($notification); + if (empty($message)) { + throw new NoMessageException(); + } + if (!isset(self::$contacts[$notification->actorId])) { self::$contacts[$notification->actorId] = Contact::getById($notification->actorId, ['name', 'url']); } diff --git a/src/Navigation/Notifications/ValueObject/FormattedNavNotification.php b/src/Navigation/Notifications/ValueObject/FormattedNavNotification.php index 4a11fa616..d2fae060a 100644 --- a/src/Navigation/Notifications/ValueObject/FormattedNavNotification.php +++ b/src/Navigation/Notifications/ValueObject/FormattedNavNotification.php @@ -21,12 +21,12 @@ namespace Friendica\Navigation\Notifications\ValueObject; -use Friendica\BaseDataTransferObject; +use Friendica\BaseEntity; /** * A view-only object for printing item notifications to the frontend */ -class FormattedNavNotification extends BaseDataTransferObject +class FormattedNavNotification extends BaseEntity { /** @var array */ protected $contact;