diff --git a/README.md b/README.md index 25fa04e..23fb0eb 100644 --- a/README.md +++ b/README.md @@ -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") - `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") +- `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: @@ -43,6 +44,7 @@ FLASK_ENV=development SITE_TITLE=My Custom Chat BEHIND_PROXY=true REDIS_URL=redis://redis:6379/0 +AUTO_DELETE_HOURS=72 ``` ## Reverse Proxy Configuration diff --git a/docker-compose.yml b/docker-compose.yml index ac2a1b3..8b4ba61 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,11 +11,11 @@ services: - redis environment: - DATABASE_URL=postgresql://anonchat:anonchat@db:5432/anonchat + - REDIS_URL=redis://redis:6379/0 - SECRET_KEY=change-this-secret-key-in-production - - SITE_TITLE=Chat - ADMIN_USERNAME=admin - 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: image: postgres:15 diff --git a/src/anonchat/__init__.py b/src/anonchat/__init__.py index 21b1223..82eff47 100644 --- a/src/anonchat/__init__.py +++ b/src/anonchat/__init__.py @@ -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_PASSWORD'] = os.environ.get('ADMIN_PASSWORD', None) 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 app.config['RATELIMIT_STORAGE_URI'] = os.environ.get('RATELIMIT_STORAGE_URI', os.environ.get('REDIS_URL')) app.config['RATELIMIT_HEADERS_ENABLED'] = True diff --git a/src/anonchat/models/inquiry.py b/src/anonchat/models/inquiry.py index 0a0caea..a96fb2d 100644 --- a/src/anonchat/models/inquiry.py +++ b/src/anonchat/models/inquiry.py @@ -1,6 +1,7 @@ from .. import db import secrets from datetime import datetime, timedelta +from flask import current_app class Inquiry(db.Model): 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 @staticmethod - def get_expired_inquiries(days=2): - """Get inquiries that have been closed for more than the specified days""" - expiry_date = datetime.utcnow() - timedelta(days=days) + def get_expired_inquiries(hours=None): + """Get inquiries that have been closed for more than the specified hours""" + if hours is None: + hours = current_app.config.get('AUTO_DELETE_HOURS') + expiry_date = datetime.utcnow() - timedelta(hours=hours) return Inquiry.query.filter( Inquiry.is_closed == True, Inquiry.closing_timestamp <= expiry_date @@ -32,5 +35,6 @@ class Inquiry(db.Model): def get_deletion_date(self): """Get the deletion date for a closed inquiry""" 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 \ No newline at end of file diff --git a/src/anonchat/tasks.py b/src/anonchat/tasks.py index 8325b3e..f381388 100644 --- a/src/anonchat/tasks.py +++ b/src/anonchat/tasks.py @@ -4,13 +4,14 @@ from .models import Inquiry from datetime import datetime 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 app = current_app._get_current_object() with app.app_context(): - app.logger.info("Running scheduled task: check_and_delete_expired_inquiries") - expired_inquiries = Inquiry.get_expired_inquiries(days=2) + auto_delete_hours = app.config.get('AUTO_DELETE_HOURS') + 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 for inquiry in expired_inquiries: diff --git a/src/anonchat/templates/admin_settings.html b/src/anonchat/templates/admin_settings.html index 3e2bf4e..985d61e 100644 --- a/src/anonchat/templates/admin_settings.html +++ b/src/anonchat/templates/admin_settings.html @@ -30,4 +30,15 @@ + +
+ +
+

System Configuration

+

The following settings are configured through environment variables:

+ +

To change these values, update your environment variables or .env file and restart the application.

+
{% endblock %} \ No newline at end of file