Move the notification service into the services directory
This commit is contained in:
parent
387e2fa490
commit
7b6d351282
4 changed files with 39 additions and 43 deletions
89
src/services/notifications.py
Normal file
89
src/services/notifications.py
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import abc
|
||||
from typing import Any
|
||||
|
||||
from src.services.email import EmailService
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
URL = str
|
||||
|
||||
|
||||
@dataclass
|
||||
class NotificationAction:
|
||||
# A unique identifier for the action
|
||||
action: str
|
||||
# The title to show with the action
|
||||
title: str
|
||||
# Where to navigate to when clicked.
|
||||
navigate: URL | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class Notification:
|
||||
"""
|
||||
Details to show in a notification
|
||||
|
||||
Much of the data here is intended to match the arguments to the [showNotification](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification)
|
||||
Web API, but this content is ideally displayable in other contexts (for example in Email or RSS)
|
||||
"""
|
||||
|
||||
# The main title content to show
|
||||
title: str
|
||||
# The content of the notification
|
||||
content: str
|
||||
|
||||
# The UNIX timestamp in milliseconds since the epoch associated with this notification
|
||||
timestamp: int
|
||||
# A list of actions included with this notification
|
||||
actions: list[NotificationAction] = field(default_factory=list)
|
||||
# The location of an icon to show as the icon for this notification
|
||||
icon: URL | None = None
|
||||
|
||||
# The location of an image associated with this notification
|
||||
image: URL | None = None
|
||||
|
||||
|
||||
class NotificationSender(abc.ABC):
|
||||
"""A service that sends notifications to the user"""
|
||||
|
||||
def __init__(self, name: str):
|
||||
self._name = name
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@abc.abstractmethod
|
||||
def notify(self, user: Any, notification: Notification):
|
||||
"""Send a notification"""
|
||||
pass
|
||||
|
||||
|
||||
class NotificationService:
|
||||
def __init__(self):
|
||||
self._senders = []
|
||||
|
||||
def register_sender(self, sender: NotificationSender):
|
||||
self._senders.append(sender)
|
||||
|
||||
def notify_user(self, user: Any, notification: Notification):
|
||||
for s in self._senders:
|
||||
if s.name in user.notification_methods:
|
||||
s.notify(user, notification)
|
||||
|
||||
|
||||
class EmailNotifier(NotificationSender):
|
||||
def __init__(self, email_service: EmailService):
|
||||
super().__init__('email')
|
||||
self._email_service = email_service
|
||||
|
||||
def notify(self, user: Any, notification: Notification):
|
||||
# TODO: Convert notification and user information into an EmailDTO for the email service
|
||||
pass
|
||||
|
||||
|
||||
def get_notification_service(email_service: EmailService):
|
||||
service = NotificationService()
|
||||
service.register_sender(EmailNotifier(email_service))
|
||||
|
||||
return service
|
||||
Loading…
Add table
Add a link
Reference in a new issue