]> git.mxchange.org Git - friendica.git/commitdiff
Add exception when message is empty in FormatteNavNotification::createFromNotification
authorHypolite Petovan <hypolite@mrpetovan.com>
Tue, 15 Mar 2022 01:53:33 +0000 (21:53 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 15 Mar 2022 02:56:44 +0000 (22:56 -0400)
- Filter out message less notifications in Ping

src/Module/Notifications/Ping.php
src/Navigation/Notifications/Exception/NoMessageException.php [new file with mode: 0644]
src/Navigation/Notifications/Factory/FormattedNavNotification.php
src/Navigation/Notifications/ValueObject/FormattedNavNotification.php

index 50c823203249ba32dbc15b4b99860b22860f60ca..c2c0c2c67450b0d4f1a4bbd8dd41d11272cc91ef 100644 (file)
@@ -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 (file)
index 0000000..710f25f
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Navigation\Notifications\Exception;
+
+class NoMessageException extends \Exception
+{
+}
index a9498eaa58c2901f94b1c898cb7af246397c6107..2577f53337c7afe925b17d267d563995e70d788e 100644 (file)
@@ -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']);
                }
index 4a11fa616f59fe140bd409e6184478af8d147946..d2fae060aaa93e6fd10fded0442f2b4b39492a41 100644 (file)
 
 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;