Add neutral reaction count on like objects
authorJoshua Rüsweg <josh@bastelstu.be>
Mon, 8 Oct 2018 09:03:29 +0000 (11:03 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Mon, 8 Oct 2018 09:04:38 +0000 (11:04 +0200)
See #2508

wcfsetup/install/files/lib/data/like/object/LikeObject.class.php
wcfsetup/install/files/lib/system/reaction/ReactionHandler.class.php
wcfsetup/install/files/lib/system/worker/LikeRebuildDataWorker.class.php
wcfsetup/setup/db/install.sql

index 9ac2d813b2de6da98e6c80c7e457e75e9e73c604..41a06a7cf4279f179c7ececf232effbc61000145 100644 (file)
@@ -21,6 +21,7 @@ use wcf\util\JSON;
  * @property-read      integer|null    $objectUserID           id of the user who created the liked object or null if user has been deleted or object was created by guest
  * @property-read      integer         $likes                  number of likes of the liked object
  * @property-read      integer         $dislikes               number of dislikes of the liked object
+ * @property-read      integer         $neutralReactions       number of neutral reactions on the liked object
  * @property-read      integer         $cumulativeLikes        cumulative result of likes (counting +1) and dislikes (counting -1)
  * @property-read      string          $cachedUsers            serialized array with the ids and names of the three users who liked (+1) the object last
  * @property-read      string          $cachedReactions        serialized array with the reactionTypeIDs and the count of the reactions
index c7f503ad9a05dea81f890b2b59bcb489b236a2ee..cc3a0490c946e9740dd5597ca5779d692d74f6e7 100644 (file)
@@ -402,6 +402,7 @@ class ReactionHandler extends SingletonFactory {
                if ($likeObject->likeObjectID) {
                        $likes = $likeObject->likes;
                        $dislikes = $likeObject->dislikes;
+                       $neutralReactions = $likeObject->neutralReactions;
                        $cumulativeLikes = $likeObject->cumulativeLikes;
                        
                        if ($likeObject->cachedReactions !== null) {
@@ -424,6 +425,9 @@ class ReactionHandler extends SingletonFactory {
                                        $dislikes--;
                                        $cumulativeLikes++;
                                }
+                               else {
+                                       $neutralReactions--;
+                               }
                                
                                if (isset($cachedReactions[$like->getReactionType()->reactionTypeID])) {
                                        if (--$cachedReactions[$like->getReactionType()->reactionTypeID] == 0) {
@@ -440,6 +444,9 @@ class ReactionHandler extends SingletonFactory {
                                $dislikes++;
                                $cumulativeLikes--;
                        }
+                       else {
+                               $neutralReactions++;
+                       }
                        
                        if (isset($cachedReactions[$reactionType->reactionTypeID])) {
                                $cachedReactions[$reactionType->reactionTypeID]++;
@@ -452,6 +459,7 @@ class ReactionHandler extends SingletonFactory {
                        $updateData = [
                                'likes' => $likes,
                                'dislikes' => $dislikes,
+                               'neutralReactions' => $neutralReactions,
                                'cumulativeLikes' => $cumulativeLikes,
                                'cachedReactions' => serialize($cachedReactions)
                        ];
@@ -473,6 +481,7 @@ class ReactionHandler extends SingletonFactory {
                                'objectUserID' => $likeable->getUserID() ?: null,
                                'likes' => ($reactionType->isPositive()) ? 1 : 0,
                                'dislikes' => ($reactionType->isNegative()) ? 1 : 0,
+                               'neutralReactions' => ($reactionType->isNeutral()) ? 1 : 0,
                                'cumulativeLikes' => $cumulativeLikes,
                                'cachedReactions' => serialize($cachedReactions)
                        ]);
index 66be02aa81afc3b6badb412fa556d625beb44041..de4055b9c4f59f0ab3f054aa937b8e80684d129c 100644 (file)
@@ -72,18 +72,22 @@ class LikeRebuildDataWorker extends AbstractRebuildDataWorker {
                                $likeObjectData[$like->objectTypeID][$like->objectID] = [
                                        'likes' => 0,
                                        'dislikes' => 0,
+                                       'neutralReactions' => 0,
                                        'cumulativeLikes' => 0,
                                        'objectUserID' => $like->objectUserID, 
                                        'cachedReactions' => []
                                ];
                        }
                        
-                       if ($like->likeValue == Like::LIKE) {
+                       if ($like->isLike()) {
                                $likeObjectData[$like->objectTypeID][$like->objectID]['likes']++;
                        }
-                       else {
+                       else if ($like->isDislike()) {
                                $likeObjectData[$like->objectTypeID][$like->objectID]['dislikes']++;
                        }
+                       else {
+                               $likeObjectData[$like->objectTypeID][$like->objectID]['neutralReactions']++;
+                       }
                        $likeObjectData[$like->objectTypeID][$like->objectID]['cumulativeLikes'] += $like->likeValue;
                        
                        if (!isset($likeObjectData[$like->objectTypeID][$like->objectID]['cachedReactions'][$like->getReactionType()->reactionTypeID])) {
@@ -97,10 +101,11 @@ class LikeRebuildDataWorker extends AbstractRebuildDataWorker {
                UserActivityPointHandler::getInstance()->fireEvents('com.woltlab.wcf.like.activityPointEvent.receivedLikes', $itemsToUser, false);
                
                $sql = "INSERT INTO                     wcf".WCF_N."_like_object
-                                                       (objectTypeID, objectID, objectUserID, likes, dislikes, cumulativeLikes, cachedReactions)
-                       VALUES                          (?, ?, ?, ?, ?, ?, ?)
+                                                       (objectTypeID, objectID, objectUserID, likes, dislikes, neutralReactions, cumulativeLikes, cachedReactions)
+                       VALUES                          (?, ?, ?, ?, ?, ?, ?, ?)
                        ON DUPLICATE KEY UPDATE         likes = likes + VALUES(likes),
                                                        dislikes = dislikes + VALUES(dislikes),
+                                                       neutralReactions = neutralReactions + VALUES(neutralReactions),
                                                        cumulativeLikes = cumulativeLikes + VALUES(cumulativeLikes),
                                                        cachedReactions = VALUES(cachedReactions)";
                $statement = WCF::getDB()->prepareStatement($sql);
@@ -114,6 +119,7 @@ class LikeRebuildDataWorker extends AbstractRebuildDataWorker {
                                        $data['objectUserID'],
                                        $data['likes'],
                                        $data['dislikes'],
+                                       $data['neutralReactions'],
                                        $data['cumulativeLikes'], 
                                        serialize($data['cachedReactions'])
                                ]);
index b8b6ed57861d89cc95a3ea127385b876ab4ecdfc..80786e76444e79aa43da77854bab2e0373ef3b2c 100644 (file)
@@ -647,6 +647,7 @@ CREATE TABLE wcf1_like_object (
        objectUserID INT(10),
        likes MEDIUMINT(7) NOT NULL DEFAULT 0,
        dislikes MEDIUMINT(7) NOT NULL DEFAULT 0,
+       neutralReactions MEDIUMINT(7) NOT NULL DEFAULT 0,
        cumulativeLikes MEDIUMINT(7) NOT NULL DEFAULT 0,
        cachedUsers TEXT,
        cachedReactions TEXT,