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