Suppress unread tracking for guests
authorMarcel Werk <burntime@woltlab.com>
Sun, 21 Apr 2024 15:21:43 +0000 (17:21 +0200)
committerMarcel Werk <burntime@woltlab.com>
Sun, 21 Apr 2024 15:21:43 +0000 (17:21 +0200)
Closes #4836

wcfsetup/install/files/lib/system/visitTracker/VisitTracker.class.php

index 8604045d0620f55f75e56ac7a0f1be6a49fc20a0..52971abacc305410d8106d876237c8a0978fdaa3 100644 (file)
@@ -74,32 +74,32 @@ class VisitTracker extends SingletonFactory
      */
     public function getVisitTime($objectType)
     {
+        if (!WCF::getUser()->userID) {
+            return \TIME_NOW;
+        }
+
         $objectTypeID = $this->getObjectTypeID($objectType);
 
         if ($this->userVisits === null) {
-            if (WCF::getUser()->userID) {
-                $data = UserStorageHandler::getInstance()->getField('trackedUserVisits');
+            $data = UserStorageHandler::getInstance()->getField('trackedUserVisits');
 
-                // cache does not exist or is outdated
-                if ($data === null) {
-                    $sql = "SELECT  objectTypeID, visitTime
+            // cache does not exist or is outdated
+            if ($data === null) {
+                $sql = "SELECT  objectTypeID, visitTime
                             FROM    wcf1_tracked_visit_type
                             WHERE   userID = ?";
-                    $statement = WCF::getDB()->prepare($sql);
-                    $statement->execute([WCF::getUser()->userID]);
-                    $this->userVisits = $statement->fetchMap('objectTypeID', 'visitTime');
-
-                    // update storage data
-                    UserStorageHandler::getInstance()->update(
-                        WCF::getUser()->userID,
-                        'trackedUserVisits',
-                        \serialize($this->userVisits)
-                    );
-                } else {
-                    $this->userVisits = @\unserialize($data);
-                }
+                $statement = WCF::getDB()->prepare($sql);
+                $statement->execute([WCF::getUser()->userID]);
+                $this->userVisits = $statement->fetchMap('objectTypeID', 'visitTime');
+
+                // update storage data
+                UserStorageHandler::getInstance()->update(
+                    WCF::getUser()->userID,
+                    'trackedUserVisits',
+                    \serialize($this->userVisits)
+                );
             } else {
-                $this->userVisits = WCF::getSession()->getVar('trackedUserVisits');
+                $this->userVisits = @\unserialize($data);
             }
 
             if (!$this->userVisits) {
@@ -109,10 +109,8 @@ class VisitTracker extends SingletonFactory
 
         $minimum = TIME_NOW - self::LIFETIME;
 
-        if (WCF::getUser()->userID) {
-            // Mark everything before the registration date as read.
-            $minimum = \max($minimum, WCF::getUser()->registrationDate);
-        }
+        // Mark everything before the registration date as read.
+        $minimum = \max($minimum, WCF::getUser()->registrationDate);
 
         return \max($this->userVisits[$objectTypeID] ?? 0, $minimum);
     }
@@ -126,22 +124,20 @@ class VisitTracker extends SingletonFactory
      */
     public function getObjectVisitTime($objectType, $objectID)
     {
-        if (WCF::getUser()->userID) {
-            $sql = "SELECT  visitTime
-                    FROM    wcf1_tracked_visit
-                    WHERE   objectTypeID = ?
-                        AND objectID = ?
-                        AND userID = ?";
-            $statement = WCF::getDB()->prepare($sql);
-            $statement->execute([$this->getObjectTypeID($objectType), $objectID, WCF::getUser()->userID]);
-            $row = $statement->fetchArray();
-            if ($row) {
-                return $row['visitTime'];
-            }
-        } else {
-            if ($visitTime = WCF::getSession()->getVar('trackedUserVisit_' . $this->getObjectTypeID($objectType) . '_' . $objectID)) {
-                return $visitTime;
-            }
+        if (!WCF::getUser()->userID) {
+            return \TIME_NOW;
+        }
+
+        $sql = "SELECT  visitTime
+                FROM    wcf1_tracked_visit
+                WHERE   objectTypeID = ?
+                    AND objectID = ?
+                    AND userID = ?";
+        $statement = WCF::getDB()->prepare($sql);
+        $statement->execute([$this->getObjectTypeID($objectType), $objectID, WCF::getUser()->userID]);
+        $row = $statement->fetchArray();
+        if ($row) {
+            return $row['visitTime'];
         }
 
         return $this->getVisitTime($objectType);
@@ -197,19 +193,15 @@ class VisitTracker extends SingletonFactory
      */
     public function trackObjectVisit($objectType, $objectID, $time = TIME_NOW)
     {
-        if (WCF::getUser()->userID) {
-            // save visit
-            $sql = "REPLACE INTO    wcf1_tracked_visit
-                                    (objectTypeID, objectID, userID, visitTime)
-                    VALUES          (?, ?, ?, ?)";
-            $statement = WCF::getDB()->prepare($sql);
-            $statement->execute([$this->getObjectTypeID($objectType), $objectID, WCF::getUser()->userID, $time]);
-        } elseif (WCF::getSession()->spiderIdentifier === null) {
-            WCF::getSession()->register(
-                'trackedUserVisit_' . $this->getObjectTypeID($objectType) . '_' . $objectID,
-                $time
-            );
+        if (!WCF::getUser()->userID) {
+            return;
         }
+
+        $sql = "REPLACE INTO    wcf1_tracked_visit
+                                (objectTypeID, objectID, userID, visitTime)
+                VALUES          (?, ?, ?, ?)";
+        $statement = WCF::getDB()->prepare($sql);
+        $statement->execute([$this->getObjectTypeID($objectType), $objectID, WCF::getUser()->userID, $time]);
     }
 
     /**
@@ -220,29 +212,27 @@ class VisitTracker extends SingletonFactory
      */
     public function trackTypeVisit($objectType, $time = TIME_NOW)
     {
-        if (WCF::getUser()->userID) {
-            // save visit
-            $sql = "REPLACE INTO    wcf1_tracked_visit_type
-                                    (objectTypeID, userID, visitTime)
-                    VALUES          (?, ?, ?)";
-            $statement = WCF::getDB()->prepare($sql);
-            $statement->execute([$this->getObjectTypeID($objectType), WCF::getUser()->userID, $time]);
-
-            // delete obsolete object visits
-            $sql = "DELETE FROM wcf1_tracked_visit
-                    WHERE       objectTypeID = ?
-                            AND userID = ?
-                            AND visitTime <= ?";
-            $statement = WCF::getDB()->prepare($sql);
-            $statement->execute([$this->getObjectTypeID($objectType), WCF::getUser()->userID, $time]);
-
-            // reset storage
-            UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'trackedUserVisits');
-        } elseif (WCF::getSession()->spiderIdentifier === null) {
-            $this->getVisitTime($objectType);
-            $this->userVisits[$this->getObjectTypeID($objectType)] = $time;
-            WCF::getSession()->register('trackedUserVisits', $this->userVisits);
+        if (!WCF::getUser()->userID) {
+            return;
         }
+
+        // save visit
+        $sql = "REPLACE INTO    wcf1_tracked_visit_type
+                                (objectTypeID, userID, visitTime)
+                VALUES          (?, ?, ?)";
+        $statement = WCF::getDB()->prepare($sql);
+        $statement->execute([$this->getObjectTypeID($objectType), WCF::getUser()->userID, $time]);
+
+        // delete obsolete object visits
+        $sql = "DELETE FROM wcf1_tracked_visit
+                WHERE       objectTypeID = ?
+                        AND userID = ?
+                        AND visitTime <= ?";
+        $statement = WCF::getDB()->prepare($sql);
+        $statement->execute([$this->getObjectTypeID($objectType), WCF::getUser()->userID, $time]);
+
+        // reset storage
+        UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'trackedUserVisits');
     }
 
     /**