From a7bd8c986c2b4f2a0fe7b4fa9f150cd60f4ca431 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Wed, 8 Nov 2023 13:44:17 +0000
Subject: [PATCH] clean up forum visits and ensure entries are deleted when
 either a user or a...

Closes #2738

Merge request studip/studip!1856
---
 lib/cronjobs/garbage_collector.class.php |  7 +++++++
 lib/models/Course.class.php              |  4 ++++
 lib/models/User.class.php                | 12 ++++++++++++
 3 files changed, 23 insertions(+)

diff --git a/lib/cronjobs/garbage_collector.class.php b/lib/cronjobs/garbage_collector.class.php
index 1d060068705..c56865d7c4b 100644
--- a/lib/cronjobs/garbage_collector.class.php
+++ b/lib/cronjobs/garbage_collector.class.php
@@ -157,6 +157,13 @@ class GarbageCollectorJob extends CronJob
             $statement->execute();
         }
 
+        // Remove outdated entries from forum_visits
+        $query = "DELETE FROM `forum_visits`
+                  WHERE GREATEST(`visitdate`, `last_visitdate`) < UNIX_TIMESTAMP() - :threshold";
+        DBManager::get()->execute($query, [
+            ':threshold' => ForumVisit::LAST_VISIT_MAX,
+        ]);
+
         // clean db cache
         $cache = new StudipDbCache();
         $cache->purge();
diff --git a/lib/models/Course.class.php b/lib/models/Course.class.php
index c449922b65e..ab582196a4d 100644
--- a/lib/models/Course.class.php
+++ b/lib/models/Course.class.php
@@ -278,6 +278,10 @@ class Course extends SimpleORMap implements Range, PrivacyObject, StudipItem, Fe
                 "UPDATE `seminare` SET `parent_course` = NULL WHERE `parent_course` = :course",
                 ['course' => $course->id]
             );
+            DBManager::get()->execute(
+                "DELETE FROM `forum_visits` WHERE `seminar_id` = ?",
+                [$course->id]
+            );
         };
 
         parent::configure($config);
diff --git a/lib/models/User.class.php b/lib/models/User.class.php
index bb23ca36df2..627673d65b6 100644
--- a/lib/models/User.class.php
+++ b/lib/models/User.class.php
@@ -211,6 +211,7 @@ class User extends AuthUserMd5 implements Range, PrivacyObject
         };
 
         $config['registered_callbacks']['after_delete'][] = 'cbRemoveFeedback';
+        $config['registered_callbacks']['after_delete'][] = 'cbRemoveForumVisits';
         $config['registered_callbacks']['before_store'][] = 'cbClearCaches';
         $config['registered_callbacks']['before_store'][] = 'cbStudipLog';
 
@@ -1490,6 +1491,17 @@ class User extends AuthUserMd5 implements Range, PrivacyObject
         FeedbackEntry::deleteBySQL('user_id = ?', [$this->id]);
     }
 
+    /**
+     * This callback is called after deleting a User.
+     * It removes forum visit entries that are associated with the User.
+     */
+    public function cbRemoveForumVisits()
+    {
+        $query = "DELETE FROM `forum_visits`
+                  WHERE `user_id` = ?";
+        DBManager::get()->execute($query, [$this->id]);
+    }
+
     public function cbClearCaches()
     {
         if ($this->isFieldDirty('perms')) {
-- 
GitLab