Mark comment (response) notifications as read when loaded via AJAX
authorMatthias Schmidt <gravatronics@live.com>
Sun, 27 Jan 2019 11:26:10 +0000 (12:26 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 27 Jan 2019 11:26:10 +0000 (12:26 +0100)
Close #2835

wcfsetup/install/files/lib/data/comment/CommentAction.class.php
wcfsetup/install/files/lib/data/comment/response/CommentResponseAction.class.php
wcfsetup/install/files/lib/system/comment/CommentHandler.class.php

index a759a66f01a8da61e0bf4729ea50e6d43ed5d66b..bff123c305fb56495e83a82bf2728b9355b9f495 100644 (file)
@@ -198,6 +198,12 @@ class CommentAction extends AbstractDatabaseObjectAction implements IMessageInli
                $commentList->getConditionBuilder()->add("comment.time < ?", [$this->parameters['data']['lastCommentTime']]);
                $commentList->readObjects();
                
+               // mark notifications for loaded comments as read
+               CommentHandler::getInstance()->markNotificationsAsConfirmedForComments(
+                       CommentHandler::getInstance()->getObjectType($this->parameters['data']['objectTypeID'])->objectType,
+                       $commentList->getObjects()
+               );
+               
                WCF::getTPL()->assign([
                        'commentList' => $commentList,
                        'likeData' => MODULE_LIKE ? $commentList->getLikeData() : []
@@ -252,6 +258,21 @@ class CommentAction extends AbstractDatabaseObjectAction implements IMessageInli
                        return ['template' => ''];
                }
                
+               // mark notifications for loaded comment/response as read
+               $objectType = CommentHandler::getInstance()->getObjectType($this->parameters['data']['objectTypeID'])->objectType;
+               if ($this->response === null) {
+                       CommentHandler::getInstance()->markNotificationsAsConfirmedForComments(
+                               $objectType,
+                               [new StructuredComment($this->comment)]
+                       );
+               }
+               else {
+                       CommentHandler::getInstance()->markNotificationsAsConfirmedForResponses(
+                               $objectType,
+                               [$this->response]
+                       );
+               }
+               
                $returnValues = $this->renderComment($this->comment, $this->response);
                return (is_array($returnValues)) ? $returnValues : ['template' => $returnValues];
        }
index 0077a00ba343e525ab5b26c066347d7e0dabacd6..0e63c61bc3ad5f4030dc378a773b0323a1accf20 100644 (file)
@@ -207,6 +207,12 @@ class CommentResponseAction extends AbstractDatabaseObjectAction {
                        $lastResponseTime = max($lastResponseTime, $response->time);
                }
                
+               // mark notifications for loaded responses as read
+               CommentHandler::getInstance()->markNotificationsAsConfirmedForResponses(
+                       CommentHandler::getInstance()->getObjectType($this->comment->objectTypeID)->objectType,
+                       $responseList->getObjects()
+               );
+               
                WCF::getTPL()->assign([
                        'commentCanModerate' => $commentCanModerate,
                        'likeData' => MODULE_LIKE ? $responseList->getLikeData() : [],
index 7fd43c0b69efec1995f50f263918b814c03df0f9..07f1313d59a0b38b6676547e19237ed65049b989 100644 (file)
@@ -1,9 +1,10 @@
 <?php
 namespace wcf\system\comment;
+use wcf\data\comment\response\CommentResponse;
 use wcf\data\comment\response\CommentResponseList;
 use wcf\data\comment\CommentEditor;
 use wcf\data\comment\CommentList;
-use wcf\data\comment\response\StructuredCommentResponseList;
+use wcf\data\comment\StructuredComment;
 use wcf\data\comment\StructuredCommentList;
 use wcf\data\object\type\ObjectType;
 use wcf\data\object\type\ObjectTypeCache;
@@ -442,19 +443,24 @@ class CommentHandler extends SingletonFactory {
         * given comment list as confirmed for the active user.
         * 
         * @param       string                  $objectType     comment object type name
-        * @param       StructuredCommentList   $commentList    comments whose notifications will be marked as read
+        * @param       StructuredComment[]     $comments       comments whose notifications will be marked as read
         * @throws      \InvalidArgumentException               if invalid comment object type name is given
         * @since       5.2
         */
-       public function markNotificationsAsConfirmedForCommentList($objectType, StructuredCommentList $commentList) {
+       public function markNotificationsAsConfirmedForComments($objectType, array $comments) {
                if ($this->getObjectTypeID($objectType) === null) {
                        throw new \InvalidArgumentException("Unknown comment object type '{$objectType}'.");
                }
                
-               if (count($commentList) === 0) {
+               if (count($comments) === 0) {
                        return;
                }
                
+               $commentIDs = [];
+               foreach ($comments as $comment) {
+                       $commentIDs[] = $comment->commentID;
+               }
+               
                // 1. comments
                
                // mark comment notifications as confirmed
@@ -474,7 +480,7 @@ class CommentHandler extends SingletonFactory {
                                        $eventData['eventName'],
                                        $eventData['objectType'],
                                        [WCF::getUser()->userID],
-                                       $commentList->getObjectIDs()
+                                       $commentIDs
                                );
                        }
                }
@@ -496,7 +502,7 @@ class CommentHandler extends SingletonFactory {
                        $notificationList = new UserNotificationList();
                        $notificationList->getConditionBuilder()->add('user_notification.eventID IN (?)', [array_keys($reactionCommentEvents)]);
                        $notificationList->getConditionBuilder()->add('user_notification.userID = ?', [WCF::getUser()->userID]);
-                       $notificationList->getConditionBuilder()->add('user_notification.baseObjectID IN (?)', [$commentList->getObjectIDs()]);
+                       $notificationList->getConditionBuilder()->add('user_notification.baseObjectID IN (?)', [$commentIDs]);
                        $notificationList->readObjects();
                        
                        $objectIDs = [];
@@ -523,7 +529,7 @@ class CommentHandler extends SingletonFactory {
                // 2. responses
                
                $responseIDs = [];
-               foreach ($commentList as $comment) {
+               foreach ($comments as $comment) {
                        // as we do not know whether `Comment::getUnfilteredResponseIDs()`
                        // or `Comment::getResponseIDs()` has been used, collect response
                        // ids manually
@@ -602,21 +608,26 @@ class CommentHandler extends SingletonFactory {
         * Marks all comment response-related notifications for objects of the given object type in
         * the given comment response list as confirmed for the active user.
         * 
-        * @param       string                  $objectType             comment object type name
-        * @param       StructuredCommentResponseList $responseList     comment responses whose notifications will be marked as read
+        * @param       string                  $objectType     comment object type name
+        * @param       CommentResponse[]       $responses      comment responses whose notifications will be marked as read
         * 
         * @throws      \InvalidArgumentException               if invalid comment object type name is given
         * @since       5.2
         */
-       public function markNotificationsAsConfirmedForResponseList($objectType, StructuredCommentResponseList $responseList) {
+       public function markNotificationsAsConfirmedForResponses($objectType, array $responses) {
                if ($this->getObjectTypeID($objectType) === null) {
                        throw new \InvalidArgumentException("Unknown comment object type '{$objectType}'.");
                }
                
-               if (count($responseList) === 0) {
+               if (count($responses) === 0) {
                        return;
                }
                
+               $responseIDs = [];
+               foreach ($responses as $response) {
+                       $responseIDs[] = $response->responseID;
+               }
+               
                $responseEvents = [];
                if (UserNotificationHandler::getInstance()->getObjectTypeID($objectType.'.response.notification')) {
                        foreach (UserNotificationHandler::getInstance()->getEvents($objectType . '.response.notification') as $event) {
@@ -633,7 +644,7 @@ class CommentHandler extends SingletonFactory {
                                        $eventData['eventName'],
                                        $eventData['objectType'],
                                        [WCF::getUser()->userID],
-                                       $responseList->getObjectIDs()
+                                       $responseIDs
                                );
                        }
                }
@@ -655,7 +666,7 @@ class CommentHandler extends SingletonFactory {
                        $notificationList = new UserNotificationList();
                        $notificationList->getConditionBuilder()->add('user_notification.eventID IN (?)', [array_keys($reactionResponseEvents)]);
                        $notificationList->getConditionBuilder()->add('user_notification.userID = ?', [WCF::getUser()->userID]);
-                       $notificationList->getConditionBuilder()->add('user_notification.baseObjectID IN (?)', [$responseList->getObjectIDs()]);
+                       $notificationList->getConditionBuilder()->add('user_notification.baseObjectID IN (?)', [$responseIDs]);
                        $notificationList->readObjects();
                        
                        $objectIDs = [];