auto-discovery toggle, auto-prune with customizable retention

This commit is contained in:
lichenblankie 2026-06-05 01:00:47 +00:00
parent 7ebf35b137
commit f8f9cb6337
4 changed files with 115 additions and 4 deletions

View file

@ -1,6 +1,7 @@
import sqlite3
import os
import threading
from datetime import datetime, timedelta
FORUM_DB = "forum.db"
@ -497,3 +498,28 @@ class ForumDB:
).fetchall()
finally:
self.return_db(db)
def prune_old_content(self, retention_days):
"""Delete threads and posts older than retention_days."""
db = self.get_db()
try:
cutoff = (datetime.utcnow() - timedelta(days=retention_days)).strftime("%Y-%m-%dT%H:%M:%S")
# Delete posts in old threads
db.execute(
"DELETE FROM posts WHERE thread_id IN "
"(SELECT id FROM threads WHERE updated_at < ?)",
(cutoff,),
)
# Delete orphaned posts (thread already deleted)
db.execute(
"DELETE FROM posts WHERE thread_id NOT IN (SELECT id FROM threads)"
)
# Delete old threads
db.execute("DELETE FROM threads WHERE updated_at < ?", (cutoff,))
# Clean up orphaned upvotes
db.execute(
"DELETE FROM upvotes WHERE thread_id NOT IN (SELECT id FROM threads)"
)
db.commit()
finally:
self.return_db(db)