Added function to send notifications to all users

This commit is contained in:
florian committed 2026-11-12 18:28:03 +01:00
1 parent 6654e312b6
commit 5c6ee4e80e
3 files changed
+43 -3

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "lib-send-notifications"
version = "0.2.2"
version = "0.3.0"
description = "Shared library to send notifications via the internal backend API"
requires-python = ">=3.10"
authors = [{ name = "Florian Gänsejunge" }]
+4 -2
View File
@@ -6,12 +6,14 @@ from .send_notification import (
NotificationError,
NotificationUnavailableError,
)
from .notify_subscribers import send_to_users
logging.getLogger(__name__).addHandler(logging.NullHandler())
__all__ = [
"InvalidNotificationError",
"NotificationClient",
"NotificationError",
"NotificationUnavailableError",
]
"send_to_users",
]
+38
View File
@@ -0,0 +1,38 @@
import logging
from shared_models import Message
from .send_notification import (
NotificationClient,
NotificationError,
NotificationUnavailableError,
)
logger = logging.getLogger(__name__)
def send_to_users(
client: NotificationClient,
notifications: list[tuple[int, Message]]
):
sent = 0
failed = 0
for index, (user_id, message) in enumerate(notifications):
try:
client.send(
user_id=user_id,
title=message.title,
body=message.body,
link=message.link,
category=message.category,
timestamp=message.timestamp,
)
sent += 1
except NotificationUnavailableError:
skipped = len(notifications) - index
failed += skipped
logger.error("Notification backend unavailable, skipping %s remaining", skipped)
break
except NotificationError:
failed +=1
logger.exception("Failed to notify user_id=%s", user_id)
return sent, failed