Delete orphaned notifications automatically
authorMatthias Schmidt <gravatronics@live.com>
Mon, 18 Aug 2014 06:32:20 +0000 (08:32 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Mon, 18 Aug 2014 06:32:20 +0000 (08:32 +0200)
wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php
wcfsetup/install/files/lib/system/user/notification/object/type/AbstractUserNotificationObjectType.class.php

index 4510c058334e67f144a0d3db8dec1307f8f35879..2fafb9bc366fb0ff34c95a9858bd9405995f2ed2 100644 (file)
@@ -375,12 +375,19 @@ class UserNotificationHandler extends SingletonFactory {
                
                // build notification data
                $notifications = array();
+               $deleteNotifications = array();
                foreach ($notificationObjects as $notification) {
+                       $object = $objectTypes[$notification->objectType]['objects'][$notification->objectID];
+                       if ($object->__unknownNotificationObject) {
+                               $deleteNotifications[] = $notification;
+                               continue;
+                       }
+                       
                        $className = $eventObjects[$notification->eventID]->className;
                        $class = new $className($eventObjects[$notification->eventID]);
                        $class->setObject(
                                $notification,
-                               $objectTypes[$notification->objectType]['objects'][$notification->objectID],
+                               $object,
                                (isset($authors[$notification->authorID]) ? $authors[$notification->authorID] : $unknownAuthor),
                                $notification->additionalData
                        );
@@ -414,6 +421,14 @@ class UserNotificationHandler extends SingletonFactory {
                        $notifications[] = $data;
                }
                
+               if (!empty($deleteNotifications)) {
+                       $notificationAction = new UserNotificationAction($deleteNotifications, 'delete');
+                       $notificationAction->executeAction();
+                       
+                       // reset notification counter
+                       UserStorageHandler::getInstance()->reset(array(WCF::getUser()->userID), 'userNotificationCount');
+               }
+               
                // check access
                foreach ($notifications as $index => $notificationData) {
                        if (!$notificationData['event']->checkAccess()) {
index 2d041c01790ea74533de6f5d70d1c50ec4e8888c..3b9a755dd62b08e1bd9ece133e16abd8b8731d32 100644 (file)
@@ -47,7 +47,13 @@ class AbstractUserNotificationObjectType extends AbstractObjectTypeProcessor imp
                foreach ($objectIDs as $objectID) {
                        // append empty objects for unknown ids
                        if (!isset($objects[$objectID])) {
-                               $objects[$objectID] = new static::$decoratorClassName(new static::$objectClassName(null, array($indexName => $objectID)));
+                               // '__unknownNotificationObject' tells the notification API
+                               // that the object does not exist anymore so that the related
+                               // notification can be deleted automatically
+                               $objects[$objectID] = new static::$decoratorClassName(new static::$objectClassName(null, array(
+                                       '__unknownNotificationObject' => true,
+                                       $indexName => $objectID
+                               )));
                        }
                }