Make delete delay tweakable

This commit is contained in:
Minecon724 2025-04-02 19:42:09 +02:00
commit 1697d98483
Signed by untrusted user who does not match committer: m724
GPG key ID: A02E6E67AB961189
6 changed files with 29 additions and 9 deletions

View file

@ -33,6 +33,7 @@ AnonChat can be configured using environment variables:
- `BEHIND_PROXY`: Set to "true" when running behind a reverse proxy to properly handle client IP addresses (defaults to "false") - `BEHIND_PROXY`: Set to "true" when running behind a reverse proxy to properly handle client IP addresses (defaults to "false")
- `RATELIMIT_STORAGE_URL`: Storage backend for rate limiting (defaults to memory storage) - `RATELIMIT_STORAGE_URL`: Storage backend for rate limiting (defaults to memory storage)
- `REDIS_URL`: Redis connection URL for session storage (defaults to "redis://localhost:6379/0") - `REDIS_URL`: Redis connection URL for session storage (defaults to "redis://localhost:6379/0")
- `AUTO_DELETE_HOURS`: Number of hours after which closed inquiries are automatically deleted (defaults to 48)
You can set these variables in a `.env` file: You can set these variables in a `.env` file:
@ -43,6 +44,7 @@ FLASK_ENV=development
SITE_TITLE=My Custom Chat SITE_TITLE=My Custom Chat
BEHIND_PROXY=true BEHIND_PROXY=true
REDIS_URL=redis://redis:6379/0 REDIS_URL=redis://redis:6379/0
AUTO_DELETE_HOURS=72
``` ```
## Reverse Proxy Configuration ## Reverse Proxy Configuration

View file

@ -11,11 +11,11 @@ services:
- redis - redis
environment: environment:
- DATABASE_URL=postgresql://anonchat:anonchat@db:5432/anonchat - DATABASE_URL=postgresql://anonchat:anonchat@db:5432/anonchat
- REDIS_URL=redis://redis:6379/0
- SECRET_KEY=change-this-secret-key-in-production - SECRET_KEY=change-this-secret-key-in-production
- SITE_TITLE=Chat
- ADMIN_USERNAME=admin - ADMIN_USERNAME=admin
- ADMIN_PASSWORD=change-this-password-in-production - ADMIN_PASSWORD=change-this-password-in-production
- REDIS_URL=redis://redis:6379/0 - BEHIND_PROXY=false # Set to true if you're running behind a reverse proxy
db: db:
image: postgres:15 image: postgres:15

View file

@ -28,6 +28,8 @@ app.config['WEBHOOK_SECRET'] = os.environ.get('WEBHOOK_SECRET', '')
app.config['ADMIN_USERNAME'] = os.environ.get('ADMIN_USERNAME', 'admin') app.config['ADMIN_USERNAME'] = os.environ.get('ADMIN_USERNAME', 'admin')
app.config['ADMIN_PASSWORD'] = os.environ.get('ADMIN_PASSWORD', None) app.config['ADMIN_PASSWORD'] = os.environ.get('ADMIN_PASSWORD', None)
app.config['ADMIN_FORCE_RESET'] = os.environ.get('ADMIN_FORCE_RESET', 'false').lower() == 'true' app.config['ADMIN_FORCE_RESET'] = os.environ.get('ADMIN_FORCE_RESET', 'false').lower() == 'true'
# Auto-deletion configuration
app.config['AUTO_DELETE_HOURS'] = int(os.environ.get('AUTO_DELETE_HOURS', 48))
# Rate limit configurations # Rate limit configurations
app.config['RATELIMIT_STORAGE_URI'] = os.environ.get('RATELIMIT_STORAGE_URI', os.environ.get('REDIS_URL')) app.config['RATELIMIT_STORAGE_URI'] = os.environ.get('RATELIMIT_STORAGE_URI', os.environ.get('REDIS_URL'))
app.config['RATELIMIT_HEADERS_ENABLED'] = True app.config['RATELIMIT_HEADERS_ENABLED'] = True

View file

@ -1,6 +1,7 @@
from .. import db from .. import db
import secrets import secrets
from datetime import datetime, timedelta from datetime import datetime, timedelta
from flask import current_app
class Inquiry(db.Model): class Inquiry(db.Model):
id = db.Column(db.String(16), primary_key=True, unique=True, default=lambda: secrets.token_hex(8)) id = db.Column(db.String(16), primary_key=True, unique=True, default=lambda: secrets.token_hex(8))
@ -21,9 +22,11 @@ class Inquiry(db.Model):
self.closing_timestamp = None self.closing_timestamp = None
@staticmethod @staticmethod
def get_expired_inquiries(days=2): def get_expired_inquiries(hours=None):
"""Get inquiries that have been closed for more than the specified days""" """Get inquiries that have been closed for more than the specified hours"""
expiry_date = datetime.utcnow() - timedelta(days=days) if hours is None:
hours = current_app.config.get('AUTO_DELETE_HOURS')
expiry_date = datetime.utcnow() - timedelta(hours=hours)
return Inquiry.query.filter( return Inquiry.query.filter(
Inquiry.is_closed == True, Inquiry.is_closed == True,
Inquiry.closing_timestamp <= expiry_date Inquiry.closing_timestamp <= expiry_date
@ -32,5 +35,6 @@ class Inquiry(db.Model):
def get_deletion_date(self): def get_deletion_date(self):
"""Get the deletion date for a closed inquiry""" """Get the deletion date for a closed inquiry"""
if self.is_closed: if self.is_closed:
return self.closing_timestamp + timedelta(days=2) hours = current_app.config.get('AUTO_DELETE_HOURS')
return self.closing_timestamp + timedelta(hours=hours)
return None return None

View file

@ -4,13 +4,14 @@ from .models import Inquiry
from datetime import datetime from datetime import datetime
def check_and_delete_expired_inquiries(): def check_and_delete_expired_inquiries():
"""Check and delete inquiries that have been closed for more than 2 days""" """Check and delete inquiries that have been closed for more than the configured number of hours"""
# Get app context from current_app # Get app context from current_app
app = current_app._get_current_object() app = current_app._get_current_object()
with app.app_context(): with app.app_context():
app.logger.info("Running scheduled task: check_and_delete_expired_inquiries") auto_delete_hours = app.config.get('AUTO_DELETE_HOURS')
expired_inquiries = Inquiry.get_expired_inquiries(days=2) app.logger.info(f"Running scheduled task: check_and_delete_expired_inquiries (delete after {auto_delete_hours} hours)")
expired_inquiries = Inquiry.get_expired_inquiries()
deleted_count = 0 deleted_count = 0
for inquiry in expired_inquiries: for inquiry in expired_inquiries:

View file

@ -30,4 +30,15 @@
<button type="submit">Update Password</button> <button type="submit">Update Password</button>
</form> </form>
</div> </div>
<br>
<div class="settings-info">
<h3>System Configuration</h3>
<p>The following settings are configured through environment variables:</p>
<ul style="margin-left: 1.5rem;">
<li><strong>Auto Delete Delay:</strong> {{ config.AUTO_DELETE_HOURS }} hours (set with AUTO_DELETE_HOURS)</li>
</ul>
<p><small>To change these values, update your environment variables or .env file and restart the application.</small></p>
</div>
{% endblock %} {% endblock %}