From 71d84f81e950654c73ba5125de747fa154355359 Mon Sep 17 00:00:00 2001 From: joshuaruesweg Date: Tue, 21 Sep 2021 11:23:51 +0200 Subject: [PATCH] Add method to get all subscribers of an object --- .../category/ArticleCategory.class.php | 15 +++--- .../watch/UserObjectWatchHandler.class.php | 53 ++++++++++++------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php b/wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php index fee8d6160e..bfc0dffbf8 100644 --- a/wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php +++ b/wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php @@ -204,15 +204,12 @@ class ArticleCategory extends AbstractDecoratedCategory implements IAccessibleOb */ public function getSubscribedUserIDs(): array { - $objectTypeID = UserObjectWatchHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.article.category'); - $sql = "SELECT userID - FROM wcf" . WCF_N . "_user_object_watch - WHERE objectTypeID = ? - AND objectID = ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute([$objectTypeID, $this->categoryID]); - - return $statement->fetchAll(\PDO::FETCH_COLUMN); + $subscribers = UserObjectWatchHandler::getInstance()->getSubscribers( + 'com.woltlab.wcf.article.category', + $this->categoryID + ); + + return \array_keys($subscribers); } /** diff --git a/wcfsetup/install/files/lib/system/user/object/watch/UserObjectWatchHandler.class.php b/wcfsetup/install/files/lib/system/user/object/watch/UserObjectWatchHandler.class.php index 48c332c0c3..cbf3bcacf8 100644 --- a/wcfsetup/install/files/lib/system/user/object/watch/UserObjectWatchHandler.class.php +++ b/wcfsetup/install/files/lib/system/user/object/watch/UserObjectWatchHandler.class.php @@ -120,28 +120,21 @@ class UserObjectWatchHandler extends SingletonFactory IUserNotificationObject $notificationObject, array $additionalData = [] ) { - // get object type id $objectTypeObj = ObjectTypeCache::getInstance() ->getObjectTypeByName('com.woltlab.wcf.user.objectWatch', $objectType); - - // get subscriber - $userIDs = $recipientIDs = []; - $sql = "SELECT userID, notification - FROM wcf" . WCF_N . "_user_object_watch - WHERE objectTypeID = ? - AND objectID = ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute([$objectTypeObj->objectTypeID, $objectID]); - while ($row = $statement->fetchArray()) { - $userIDs[] = $row['userID']; - if ($row['notification'] && $notificationObject->getAuthorID() != $row['userID']) { - $recipientIDs[] = $row['userID']; - } - } + $userIDs = $this->getSubscribers($objectType, $objectID); if (!empty($userIDs)) { // reset user storage - $objectTypeObj->getProcessor()->resetUserStorage($userIDs); + $objectTypeObj->getProcessor()->resetUserStorage(\array_keys($userIDs)); + + $recipientIDs = \array_filter( + $userIDs, + static function ($notification, $userID) use ($notificationObject) { + return $notification && $userID != $notificationObject->getAuthorID(); + }, + \ARRAY_FILTER_USE_BOTH + ); if (!empty($recipientIDs)) { // create notifications @@ -149,10 +142,34 @@ class UserObjectWatchHandler extends SingletonFactory $notificationEventName, $notificationObjectType, $notificationObject, - $recipientIDs, + \array_keys($recipientIDs), $additionalData ); } } } + + /** + * Returns the subscribers for a specific object as an array. + * The array key indicates the userID and the array value indicates + * the notification status (`1` = should get a notification, + * `0` = should not get a notification). + * + * @since 5.5 + */ + public function getSubscribers(string $objectType, int $objectID): array + { + $objectTypeObj = ObjectTypeCache::getInstance() + ->getObjectTypeByName('com.woltlab.wcf.user.objectWatch', $objectType); + + $userIDs = []; + $sql = "SELECT userID, notification + FROM wcf" . WCF_N . "_user_object_watch + WHERE objectTypeID = ? + AND objectID = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([$objectTypeObj->objectTypeID, $objectID]); + + return $statement->fetchMap('userID', 'notification'); + } } -- 2.20.1