From a987ef81b23bb7fbd8c06d14915bddea74988135 Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Wed, 17 Jun 2026 15:06:05 +0200 Subject: [PATCH] add format_title helper for notification titles --- .../app/notifications/service/messages/text.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/surfsense_backend/app/notifications/service/messages/text.py b/surfsense_backend/app/notifications/service/messages/text.py index 98d5284cb..344c9eb4e 100644 --- a/surfsense_backend/app/notifications/service/messages/text.py +++ b/surfsense_backend/app/notifications/service/messages/text.py @@ -2,7 +2,21 @@ from __future__ import annotations +from app.notifications.constants import TITLE_MAX_LENGTH + def truncate(text: str, limit: int) -> str: """Return ``text`` capped at ``limit`` chars, appending an ellipsis if cut.""" return text[:limit] + "..." if len(text) > limit else text + + +def format_title(prefix: str, text: str, *, max_length: int = TITLE_MAX_LENGTH) -> str: + """Build a notification title that fits ``max_length`` including ``prefix``.""" + budget = max_length - len(prefix) + if budget <= 0: + return prefix[:max_length] + if len(text) <= budget: + return f"{prefix}{text}" + if budget <= 3: + return f"{prefix}{text[:budget]}" + return f"{prefix}{text[: budget - 3]}..."