From 9337dd49d3a1d78b98097fddd830978710655054 Mon Sep 17 00:00:00 2001 From: DJObleezy Date: Sun, 27 Apr 2025 16:58:48 -0700 Subject: [PATCH] Add read_only parameter to notification clearing API Updated the `api_clear_notifications` function to include a `read_only` parameter, allowing conditional clearing of notifications based on their read status. Modified the filtering logic in the `NotificationService` to retain notifications that match the specified category, are newer than a cutoff date, or are unread when `read_only` is true. This enhances the granularity of the notification clearing process. --- App.py | 4 +++- notification_service.py | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/App.py b/App.py index e1206c4..0d5e958 100644 --- a/App.py +++ b/App.py @@ -1158,10 +1158,12 @@ def api_clear_notifications(): """API endpoint to clear notifications.""" category = request.json.get('category') older_than_days = request.json.get('older_than_days') + read_only = request.json.get('read_only', False) # Get the read_only parameter with default False cleared_count = notification_service.clear_notifications( category=category, - older_than_days=older_than_days + older_than_days=older_than_days, + read_only=read_only # Pass the parameter to the method ) return jsonify({ diff --git a/notification_service.py b/notification_service.py index 1d16b5c..93b8bf4 100644 --- a/notification_service.py +++ b/notification_service.py @@ -330,12 +330,12 @@ class NotificationService: if older_than_days: cutoff_date = self._get_current_time() - timedelta(days=older_than_days) - # Apply filters in a single pass + # Apply filters to KEEP notifications that should NOT be cleared self.notifications = [ n for n in self.notifications - if (not category or n.get("category") != category) and - (not cutoff_date or self._parse_timestamp(n.get("timestamp", self._get_current_time().isoformat())) >= cutoff_date) and - (not read_only or n.get("read", False) == False) # Keep unread notifications when read_only=True + if (category and n.get("category") != category) or # Keep if we're filtering by category and this isn't that category + (cutoff_date and self._parse_timestamp(n.get("timestamp", self._get_current_time().isoformat())) >= cutoff_date) or # Keep if newer than cutoff + (read_only and not n.get("read", False)) # Keep if we're only clearing read notifications and this is unread ] cleared_count = original_count - len(self.notifications)