Add and use TCommentResponseUserActivityEvent
authorMatthias Schmidt <gravatronics@live.com>
Sun, 17 May 2020 11:49:21 +0000 (13:49 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 17 May 2020 11:49:21 +0000 (13:49 +0200)
Close  #3311

wcfsetup/install/files/lib/system/user/activity/event/ArticleCommentResponseUserActivityEvent.class.php
wcfsetup/install/files/lib/system/user/activity/event/PageCommentResponseUserActivityEvent.class.php
wcfsetup/install/files/lib/system/user/activity/event/ProfileCommentResponseUserActivityEvent.class.php
wcfsetup/install/files/lib/system/user/activity/event/TCommentResponseUserActivityEvent.class.php [new file with mode: 0644]

index e7327424cf2621c4d3d7163298bf26398a54005c..c5f3d3f0ce421127e4ba3f8cf154a97dd0cb8649 100644 (file)
@@ -1,59 +1,32 @@
 <?php
 namespace wcf\system\user\activity\event;
 use wcf\data\article\ViewableArticleList;
-use wcf\data\comment\response\CommentResponseList;
-use wcf\data\comment\CommentList;
-use wcf\data\user\UserList;
 use wcf\system\SingletonFactory;
 use wcf\system\WCF;
 
 /**
  * User activity event implementation for responses to article comments.
- *
+ * 
  * @author     Marcel Werk
- * @copyright  2001-2019 WoltLab GmbH
+ * @copyright  2001-2020 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System\User\Activity\Event
  * @since      3.0
  */
 class ArticleCommentResponseUserActivityEvent extends SingletonFactory implements IUserActivityEvent {
+       use TCommentResponseUserActivityEvent;
+       
        /**
         * @inheritDoc
         */
        public function prepare(array $events) {
-               $responseIDs = [];
-               foreach ($events as $event) {
-                       $responseIDs[] = $event->objectID;
-               }
-               
-               // fetch responses
-               $responseList = new CommentResponseList();
-               $responseList->setObjectIDs($responseIDs);
-               $responseList->readObjects();
-               $responses = $responseList->getObjects();
-               
-               // fetch comments
-               $commentIDs = $comments = [];
-               foreach ($responses as $response) {
-                       $commentIDs[] = $response->commentID;
-               }
-               if (!empty($commentIDs)) {
-                       $commentList = new CommentList();
-                       $commentList->setObjectIDs($commentIDs);
-                       $commentList->readObjects();
-                       $comments = $commentList->getObjects();
-               }
+               $this->readResponseData($events);
                
                // fetch articles
-               $articleContentIDs = [];
-               foreach ($comments as $comment) {
-                       $articleContentIDs[] = $comment->objectID;
-               }
-               
                $articles = $articleContentToArticle = [];
-               if (!empty($articleContentIDs)) {
+               if (!empty($this->commentObjectIDs)) {
                        $articleList = new ViewableArticleList();
-                       $articleList->getConditionBuilder()->add("article.articleID IN (SELECT articleID FROM wcf".WCF_N."_article_content WHERE articleContentID IN (?))", [$articleContentIDs]);
+                       $articleList->getConditionBuilder()->add("article.articleID IN (SELECT articleID FROM wcf".WCF_N."_article_content WHERE articleContentID IN (?))", [$this->commentObjectIDs]);
                        $articleList->readObjects();
                        foreach ($articleList as $article) {
                                $articles[$article->articleID] = $article;
@@ -62,24 +35,12 @@ class ArticleCommentResponseUserActivityEvent extends SingletonFactory implement
                        }
                }
                
-               // fetch users
-               $userIDs = $users = [];
-               foreach ($comments as $comment) {
-                       $userIDs[] = $comment->userID;
-               }
-               if (!empty($userIDs)) {
-                       $userList = new UserList();
-                       $userList->setObjectIDs($userIDs);
-                       $userList->readObjects();
-                       $users = $userList->getObjects();
-               }
-               
                // set message
                foreach ($events as $event) {
-                       if (isset($responses[$event->objectID])) {
-                               $response = $responses[$event->objectID];
-                               $comment = $comments[$response->commentID];
-                               if (isset($articleContentToArticle[$comment->objectID]) && isset($users[$comment->userID])) {
+                       if (isset($this->responses[$event->objectID])) {
+                               $response = $this->responses[$event->objectID];
+                               $comment = $this->comments[$response->commentID];
+                               if (isset($articleContentToArticle[$comment->objectID]) && isset($this->commentAuthors[$comment->userID])) {
                                        $article = $articles[$articleContentToArticle[$comment->objectID]];
                                        
                                        // check permissions
@@ -90,10 +51,10 @@ class ArticleCommentResponseUserActivityEvent extends SingletonFactory implement
                                        
                                        // title
                                        $text = WCF::getLanguage()->getDynamicVariable('wcf.article.recentActivity.articleCommentResponse', [
-                                               'commentAuthor' => $users[$comment->userID],
+                                               'commentAuthor' => $this->commentAuthors[$comment->userID],
                                                'commentID' => $comment->commentID,
                                                'responseID' => $response->responseID,
-                                               'article' => $article
+                                               'article' => $article,
                                        ]);
                                        $event->setTitle($text);
                                        
index 65d71f4221308747627d3c138ae9eab16e896dff..2d76ff634c49e4318a240e5bcd0419df3a0ecf91 100644 (file)
@@ -1,69 +1,33 @@
 <?php
 namespace wcf\system\user\activity\event;
-use wcf\data\comment\response\CommentResponseList;
-use wcf\data\comment\CommentList;
 use wcf\data\page\PageCache;
-use wcf\data\user\UserList;
 use wcf\system\SingletonFactory;
 use wcf\system\WCF;
 
 /**
  * User activity event implementation for responses to page comments.
- *
+ * 
  * @author     Joshua Ruesweg
- * @copyright  2001-2019 WoltLab GmbH
+ * @copyright  2001-2020 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System\User\Activity\Event
  * @since      5.2
  */
 class PageCommentResponseUserActivityEvent extends SingletonFactory implements IUserActivityEvent {
+       use TCommentResponseUserActivityEvent;
+       
        /**
         * @inheritDoc
         */
        public function prepare(array $events) {
-               $responseIDs = [];
-               foreach ($events as $event) {
-                       $responseIDs[] = $event->objectID;
-               }
-               
-               // fetch responses
-               $responseList = new CommentResponseList();
-               $responseList->setObjectIDs($responseIDs);
-               $responseList->readObjects();
-               $responses = $responseList->getObjects();
-               
-               // fetch comments
-               $commentIDs = $comments = [];
-               foreach ($responses as $response) {
-                       $commentIDs[] = $response->commentID;
-               }
-               
-               if (!empty($commentIDs)) {
-                       $commentList = new CommentList();
-                       $commentList->setObjectIDs($commentIDs);
-                       $commentList->readObjects();
-                       $comments = $commentList->getObjects();
-               }
-               
-               // fetch users
-               $userIDs = $users = [];
-               foreach ($comments as $comment) {
-                       $userIDs[] = $comment->userID;
-               }
-               
-               if (!empty($userIDs)) {
-                       $userList = new UserList();
-                       $userList->setObjectIDs($userIDs);
-                       $userList->readObjects();
-                       $users = $userList->getObjects();
-               }
+               $this->readResponseData($events);
                
                // set message
                foreach ($events as $event) {
-                       if (isset($responses[$event->objectID])) {
-                               $response = $responses[$event->objectID];
-                               $comment = $comments[$response->commentID];
-                               if (PageCache::getInstance()->getPage($comment->objectID) && isset($users[$comment->userID])) {
+                       if (isset($this->responses[$event->objectID])) {
+                               $response = $this->responses[$event->objectID];
+                               $comment = $this->comments[$response->commentID];
+                               if (PageCache::getInstance()->getPage($comment->objectID) && isset($this->commentAuthors[$comment->userID])) {
                                        $page = PageCache::getInstance()->getPage($comment->objectID);
                                        
                                        // check permissions
@@ -74,10 +38,10 @@ class PageCommentResponseUserActivityEvent extends SingletonFactory implements I
                                        
                                        // title
                                        $text = WCF::getLanguage()->getDynamicVariable('wcf.page.recentActivity.pageCommentResponse', [
-                                               'commentAuthor' => $users[$comment->userID],
+                                               'commentAuthor' => $this->commentAuthors[$comment->userID],
                                                'commentID' => $comment->commentID,
                                                'responseID' => $response->responseID,
-                                               'page' => $page
+                                               'page' => $page,
                                        ]);
                                        $event->setTitle($text);
                                        
index 9e975638a445640fd75cd99943ef8633b4ab0a4b..7aa05d28e489a089f0b5b67a7b22dba3729b9ffc 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 namespace wcf\system\user\activity\event;
-use wcf\data\comment\response\CommentResponseList;
-use wcf\data\comment\CommentList;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\SingletonFactory;
 use wcf\system\WCF;
@@ -10,11 +8,13 @@ use wcf\system\WCF;
  * User activity event implementation for profile comment responses.
  * 
  * @author     Marcel Werk
- * @copyright  2001-2019 WoltLab GmbH
+ * @copyright  2001-2020 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System\User\Activity\Event
  */
 class ProfileCommentResponseUserActivityEvent extends SingletonFactory implements IUserActivityEvent {
+       use TCommentResponseUserActivityEvent;
+       
        /**
         * @inheritDoc
         */
@@ -23,34 +23,12 @@ class ProfileCommentResponseUserActivityEvent extends SingletonFactory implement
                        return;
                }
                
-               $responseIDs = [];
-               foreach ($events as $event) {
-                       $responseIDs[] = $event->objectID;
-               }
-               
-               // fetch responses
-               $responseList = new CommentResponseList();
-               $responseList->setObjectIDs($responseIDs);
-               $responseList->readObjects();
-               $responses = $responseList->getObjects();
-               
-               // fetch comments
-               $commentIDs = $comments = [];
-               foreach ($responses as $response) {
-                       $commentIDs[] = $response->commentID;
-               }
-               if (!empty($commentIDs)) {
-                       $commentList = new CommentList();
-                       $commentList->setObjectIDs($commentIDs);
-                       $commentList->readObjects();
-                       $comments = $commentList->getObjects();
-               }
+               $this->readResponseData($events);
                
                // fetch users
                $userIDs = $users = [];
-               foreach ($comments as $comment) {
+               foreach ($this->comments as $comment) {
                        $userIDs[] = $comment->objectID;
-                       $userIDs[] = $comment->userID;
                }
                if (!empty($userIDs)) {
                        $users = UserProfileRuntimeCache::getInstance()->getObjects($userIDs);
@@ -58,19 +36,19 @@ class ProfileCommentResponseUserActivityEvent extends SingletonFactory implement
                
                // set message
                foreach ($events as $event) {
-                       if (isset($responses[$event->objectID])) {
-                               $response = $responses[$event->objectID];
-                               $comment = $comments[$response->commentID];
-                               if (isset($users[$comment->objectID]) && isset($users[$comment->userID])) {
+                       if (isset($this->responses[$event->objectID])) {
+                               $response = $this->responses[$event->objectID];
+                               $comment = $this->comments[$response->commentID];
+                               if (isset($users[$comment->objectID]) && isset($this->commentAuthors[$comment->userID])) {
                                        if (!$users[$comment->objectID]->isProtected()) {
                                                $event->setIsAccessible();
                                                
                                                // title
                                                $text = WCF::getLanguage()->getDynamicVariable('wcf.user.profile.recentActivity.profileCommentResponse', [
-                                                       'commentAuthor' => $users[$comment->userID],
+                                                       'commentAuthor' => $this->commentAuthors[$comment->userID],
                                                        'commentID' => $comment->commentID,
                                                        'responseID' => $response->responseID,
-                                                       'user' => $users[$comment->objectID]
+                                                       'user' => $users[$comment->objectID],
                                                ]);
                                                $event->setTitle($text);
                                                
diff --git a/wcfsetup/install/files/lib/system/user/activity/event/TCommentResponseUserActivityEvent.class.php b/wcfsetup/install/files/lib/system/user/activity/event/TCommentResponseUserActivityEvent.class.php
new file mode 100644 (file)
index 0000000..032e6eb
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+namespace wcf\system\user\activity\event;
+use wcf\data\comment\Comment;
+use wcf\data\comment\response\CommentResponse;
+use wcf\data\user\activity\event\ViewableUserActivityEvent;
+use wcf\data\user\User;
+use wcf\system\cache\runtime\CommentResponseRuntimeCache;
+use wcf\system\cache\runtime\CommentRuntimeCache;
+use wcf\system\cache\runtime\UserRuntimeCache;
+
+/**
+ * Provides a method to read the comment response, comment, and user objects related to comment
+ * response user activity events.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2020 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\User\Activity\Event
+ * @since      5.3
+ */
+trait TCommentResponseUserActivityEvent {
+       /**
+        * user objects for the comment authors
+        * @var User[]
+        */
+       protected $commentAuthors = [];
+       
+       /**
+        * ids of the objects the comments belongs to
+        * @var int[] 
+        */
+       protected $commentObjectIDs = [];
+       
+       /**
+        * comment objects the responses belongs to
+        * @var Comment[]
+        */
+       protected $comments = [];
+       
+       /**
+        * comment response the comment response user activity events belong to
+        * @var CommentResponse[]
+        */
+       protected $responses = [];
+       
+       /**
+        * Reads the data of the comment responses the given events belong to.
+        * 
+        * @param       ViewableUserActivityEvent[]     $events
+        */
+       protected function readResponseData(array $events) {
+               $responseIDs = [];
+               foreach ($events as $event) {
+                       $responseIDs[] = $event->objectID;
+               }
+               
+               $this->responses = CommentResponseRuntimeCache::getInstance()->getObjects($responseIDs);
+               
+               $commentIDs = [];
+               foreach ($this->responses as $response) {
+                       $commentIDs[] = $response->commentID;
+               }
+               
+               if (!empty($commentIDs)) {
+                       $this->comments = CommentRuntimeCache::getInstance()->getObjects($commentIDs);
+               }
+               
+               $userIDs = [];
+               foreach ($this->comments as $comment) {
+                       $userIDs[] = $comment->userID;
+                       $this->commentObjectIDs[] = $comment->objectID;
+               }
+               if (!empty($userIDs)) {
+                       $this->commentAuthors = UserRuntimeCache::getInstance()->getObjects($userIDs);
+               }
+       }
+}