Clean up reaction counter before processing
authorJoshua Rüsweg <ruesweg@woltlab.com>
Wed, 4 Sep 2019 19:11:02 +0000 (21:11 +0200)
committerJoshua Rüsweg <ruesweg@woltlab.com>
Wed, 4 Sep 2019 19:11:02 +0000 (21:11 +0200)
Fix #3066

wcfsetup/install/files/lib/system/reaction/ReactionHandler.class.php

index 926e6a2cb15edf8471e68e3db70d7736885a26ae..226a4ef8fb3fdf0d3d8a1bc003d54d7bd613e68b 100644 (file)
@@ -429,6 +429,8 @@ class ReactionHandler extends SingletonFactory {
                                $cachedReactions[$reactionType->reactionTypeID] = 1;
                        }
                        
+                       $cachedReactions = self::cleanUpCachedReactions($cachedReactions);
+                       
                        // build update date
                        $updateData = [
                                'likes' => $cumulativeLikes,
@@ -578,6 +580,8 @@ class ReactionHandler extends SingletonFactory {
                                }
                        }
                        
+                       $cachedReactions = self::cleanUpCachedReactions($cachedReactions);
+                       
                        // build update date
                        $updateData = [
                                'likes' => $cumulativeLikes,
@@ -735,6 +739,22 @@ class ReactionHandler extends SingletonFactory {
                return $firstReactionType ? $firstReactionType->reactionTypeID : null;
        }
        
+       /**
+        * Removes deleted reactions from the reaction counter for the like object table. 
+        * 
+        * @param       array   $cachedReactions
+        * @return      array
+        */
+       private function cleanUpCachedReactions(array $cachedReactions) {
+               foreach ($cachedReactions as $reactionTypeID => $count) {
+                       if (self::getReactionTypeByID($reactionTypeID) === null) {
+                               unset($cachedReactions[$reactionTypeID]);
+                       }
+               }
+               
+               return $cachedReactions;
+       }
+       
        /**
         * @param string|null $cachedReactions
         * @return array|null
@@ -744,17 +764,21 @@ class ReactionHandler extends SingletonFactory {
                if ($cachedReactions) {
                        $cachedReactions = @unserialize($cachedReactions);
                        
-                       if (is_array($cachedReactions) && !empty($cachedReactions)) {
-                               $allReactions = array_sum($cachedReactions);
-                               
-                               arsort($cachedReactions, SORT_NUMERIC);
+                       if (is_array($cachedReactions)) {
+                               $cachedReactions = self::cleanUpCachedReactions($cachedReactions);
                                
-                               $count = current($cachedReactions);
-                               return [
-                                       'count' => $count,
-                                       'other' => $allReactions - $count,
-                                       'reaction' => ReactionTypeCache::getInstance()->getReactionTypeByID(key($cachedReactions)),
-                               ];
+                               if (!empty($cachedReactions)) {
+                                       $allReactions = array_sum($cachedReactions);
+                                       
+                                       arsort($cachedReactions, SORT_NUMERIC);
+                                       
+                                       $count = current($cachedReactions);
+                                       return [
+                                               'count' => $count,
+                                               'other' => $allReactions - $count,
+                                               'reaction' => ReactionTypeCache::getInstance()->getReactionTypeByID(key($cachedReactions)),
+                                       ];
+                               }
                        }
                }