From 372d24ab1d25bc364b6ca5dc92dc5819a41c3e51 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Mon, 18 Aug 2014 08:32:20 +0200 Subject: [PATCH] Delete orphaned notifications automatically --- .../UserNotificationHandler.class.php | 17 ++++++++++++++++- ...AbstractUserNotificationObjectType.class.php | 8 +++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php b/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php index 4510c05833..2fafb9bc36 100644 --- a/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php +++ b/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php @@ -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()) { diff --git a/wcfsetup/install/files/lib/system/user/notification/object/type/AbstractUserNotificationObjectType.class.php b/wcfsetup/install/files/lib/system/user/notification/object/type/AbstractUserNotificationObjectType.class.php index 2d041c0179..3b9a755dd6 100644 --- a/wcfsetup/install/files/lib/system/user/notification/object/type/AbstractUserNotificationObjectType.class.php +++ b/wcfsetup/install/files/lib/system/user/notification/object/type/AbstractUserNotificationObjectType.class.php @@ -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 + ))); } } -- 2.20.1