Rebuild reaction stats on user rebuilding
authorJoshua Rüsweg <josh@bastelstu.be>
Wed, 1 Aug 2018 11:08:24 +0000 (13:08 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Wed, 1 Aug 2018 11:08:24 +0000 (13:08 +0200)
See #2508

wcfsetup/install/files/lib/system/worker/UserRebuildDataWorker.class.php

index 4c8f58066b36896b9c04dc2b598075267f867bab..de4c281876967979fbfd499e17d030eea7461036 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\system\worker;
-use wcf\data\like\Like;
+use wcf\data\reaction\type\ReactionType;
+use wcf\data\reaction\type\ReactionTypeCache;
 use wcf\data\user\avatar\UserAvatar;
 use wcf\data\user\avatar\UserAvatarEditor;
 use wcf\data\user\avatar\UserAvatarList;
@@ -85,14 +86,69 @@ class UserRebuildDataWorker extends AbstractRebuildDataWorker {
                        
                        // update like counter
                        if (MODULE_LIKE) {
-                               $sql = "UPDATE  wcf".WCF_N."_user user_table
-                                       SET     likesReceived = (
+                               $positiveReactionTypes = $negativeReactionTypes = $neutralReactionTypes = [];
+                               
+                               foreach (ReactionTypeCache::getInstance()->getEnabledReactionTypes() as $reactionType) {
+                                       switch ($reactionType->type) {
+                                               case ReactionType::REACTION_TYPE_POSITIVE: 
+                                                       $positiveReactionTypes[] = $reactionType->reactionTypeID;
+                                                       break;
+                                               
+                                               case ReactionType::REACTION_TYPE_NEGATIVE: 
+                                                       $negativeReactionTypes[] = $reactionType->reactionTypeID;
+                                                       break;
+                                                       
+                                               case ReactionType::REACTION_TYPE_NEUTRAL:
+                                                       $neutralReactionTypes[] = $reactionType->reactionTypeID;
+                                                       break;
+                                                       
+                                               default: 
+                                                       throw new \LogicException('Unreachable');
+                                       }
+                               }
+                               
+                               
+                               $sql = "UPDATE  wcf".WCF_N."_user user_table SET";
+                                       
+                               if (!empty($positiveReactionTypes)) {
+                                       $sql .= " likesReceived = (
                                                        SELECT  COUNT(*)
                                                        FROM    wcf".WCF_N."_like
                                                        WHERE   objectUserID = user_table.userID
-                                                               AND likeValue = ".Like::LIKE."
-                                               )
-                                       ".$conditionBuilder;
+                                                               AND reactionTypeID IN (". implode(',', $positiveReactionTypes) .")
+                                               ), positiveReactionsReceived = (
+                                                       SELECT  COUNT(*)
+                                                       FROM    wcf".WCF_N."_like
+                                                       WHERE   objectUserID = user_table.userID
+                                                               AND reactionTypeID IN (". implode(',', $positiveReactionTypes) ."))";
+                               }
+                               else {
+                                       $sql .= " likesReceived = 0, positiveReactionsReceived = 0";
+                               }
+                               
+                               if (!empty($negativeReactionTypes)) {
+                                       $sql .= ", negativeReactionsReceived = (
+                                                       SELECT  COUNT(*)
+                                                       FROM    wcf".WCF_N."_like
+                                                       WHERE   objectUserID = user_table.userID
+                                                               AND reactionTypeID IN (". implode(',', $negativeReactionTypes) ."))";
+                               }
+                               else {
+                                       $sql .= ", negativeReactionsReceived = 0";
+                               }
+                                       
+                               if (!empty($neutralReactionTypes)) {
+                                       $sql .= ", neutralReactionsReceived = (
+                                                       SELECT  COUNT(*)
+                                                       FROM    wcf".WCF_N."_like
+                                                       WHERE   objectUserID = user_table.userID
+                                                               AND reactionTypeID IN (". implode(',', $neutralReactionTypes) ."))";
+                               }
+                               else {
+                                       $sql .= ", neutralReactionsReceived = 0";
+                               }
+                                       
+                               $sql .= " ".$conditionBuilder;
                                $statement = WCF::getDB()->prepareStatement($sql);
                                $statement->execute($conditionBuilder->getParameters());
                        }