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.
This commit is contained in:
DJObleezy 2025-04-27 16:58:48 -07:00
parent f7b2e550f6
commit 9337dd49d3
2 changed files with 7 additions and 5 deletions

4
App.py
View File

@ -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({

View File

@ -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)