agent-zero/api/notification_create.py
Alessandro 764339b5e7 Add ZIP download toast feedback
Allow display_time=0 to create persistent grouped toasts that remain until dismissed or replaced.

Show immediate grouped Preparing download and Downloading feedback for backup ZIPs and file-browser directory/bulk ZIP downloads, with error replacement and focused static coverage.
2026-05-07 18:43:24 +02:00

67 lines
2.3 KiB
Python

from helpers.api import ApiHandler
from flask import Request, Response
from helpers.notification import NotificationManager, NotificationPriority, NotificationType
class NotificationCreate(ApiHandler):
@classmethod
def requires_auth(cls) -> bool:
return True
async def process(self, input: dict, request: Request) -> dict | Response:
# Extract notification data
notification_type = input.get("type", NotificationType.INFO.value)
priority = input.get("priority", NotificationPriority.NORMAL.value)
message = input.get("message", "")
title = input.get("title", "")
detail = input.get("detail", "")
display_time = input.get("display_time", 3) # Default to 3 seconds
group = input.get("group", "") # Group parameter for notification grouping
notification_id = input.get("id", "")
# Validate required fields
if not message:
return {"success": False, "error": "Message is required"}
# Validate display_time
try:
display_time = int(display_time)
if display_time < 0:
display_time = 3 # Reset to default if negative
except (ValueError, TypeError):
display_time = 3 # Reset to default if not numeric
# Validate notification type
try:
if isinstance(notification_type, str):
notification_type = NotificationType(notification_type.lower())
except ValueError:
return {
"success": False,
"error": f"Invalid notification type: {notification_type}",
}
# Create notification using the appropriate helper method
try:
notification = NotificationManager.send_notification(
notification_type,
priority,
message,
title,
detail,
display_time,
group,
notification_id,
)
return {
"success": True,
"notification_id": notification.id,
"message": "Notification created successfully",
}
except Exception as e:
return {
"success": False,
"error": f"Failed to create notification: {str(e)}",
}