From 0188b67354a756f0c926edd7b46623ab5b20c12b Mon Sep 17 00:00:00 2001
From: Benjamin Bellamy <ben@podlibre.org>
Date: Fri, 1 Apr 2022 13:29:25 +0000
Subject: [PATCH] feat(analytics-gdpr): update cached personal data to expire
 at midnight

---
 .../Analytics/Helpers/analytics_helper.php    | 20 ++++++-------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/modules/Analytics/Helpers/analytics_helper.php b/modules/Analytics/Helpers/analytics_helper.php
index 5a684c36a8..1c76df0b08 100644
--- a/modules/Analytics/Helpers/analytics_helper.php
+++ b/modules/Analytics/Helpers/analytics_helper.php
@@ -290,17 +290,11 @@ if (! function_exists('podcast_hit')) {
                         'Y-m-d'
                     ) . '_' . $_SERVER['REMOTE_ADDR'] . '_' . $_SERVER['HTTP_USER_AGENT'] . '_' . $episodeId
                 );
-            // Was this episode downloaded in the past 24h:
+            // The cache expires at midnight:
+            $secondsToMidnight = strtotime('tomorrow') - time();
             $downloadedBytes = cache($episodeListenerHashId);
-            // Rolling window is 24 hours (86400 seconds):
-            $rollingTTL = 86400;
-            if ($downloadedBytes) {
-                // In case it was already downloaded, TTL should be adjusted (rolling window is 24h since 1st download):
-                $rollingTTL =
-                    cache()
-                        ->getMetadata($episodeListenerHashId)['expire'] - time();
-            } else {
-                // If it was never downloaded that means that zero byte were downloaded:
+            if ($downloadedBytes === null) {
+                // If it was never downloaded that means that zero bytes were downloaded:
                 $downloadedBytes = 0;
             }
 
@@ -326,7 +320,7 @@ if (! function_exists('podcast_hit')) {
 
                 // We save the number of downloaded bytes for this user and this episode:
                 cache()
-                    ->save($episodeListenerHashId, $downloadedBytes, $rollingTTL);
+                    ->save($episodeListenerHashId, $downloadedBytes, $secondsToMidnight);
 
                 // If more that 1mn was downloaded, that's a hit, we send that to the database:
                 if ($downloadedBytes >= $bytesThreshold) {
@@ -354,11 +348,9 @@ if (! function_exists('podcast_hit')) {
                         $downloadsByUser = 1;
                     }
 
-                    // Listener count is calculated from 00h00 to 23h59:
-                    $midnightTTL = strtotime('tomorrow') - time();
                     // We save the download count for this user until midnight:
                     cache()
-                        ->save($podcastListenerHashId, $downloadsByUser, $midnightTTL);
+                        ->save($podcastListenerHashId, $downloadsByUser, $secondsToMidnight);
 
                     $db->query(
                         "CALL {$procedureName}(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);",
-- 
GitLab