|
@@ -4,12 +4,24 @@ import requests
|
|
|
from django.conf import settings
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
|
|
-class Notification(ABC):
|
|
|
- scrobble: "Scrobble"
|
|
|
+class BasicNtfyNotification(ABC):
|
|
|
ntfy_headers: dict = {}
|
|
|
ntfy_url: str = ""
|
|
|
title: str = ""
|
|
|
|
|
|
+ def __init__(self, profile: "UserProfile"):
|
|
|
+ self.user = profile.user
|
|
|
+ protocol = "http" if settings.DEBUG else "https"
|
|
|
+ domain = Site.objects.get_current().domain
|
|
|
+ self.url_tmpl = f'{protocol}://{domain}' + '{path}'
|
|
|
+
|
|
|
+ @abstractmethod
|
|
|
+ def send(self) -> None:
|
|
|
+ pass
|
|
|
+
|
|
|
+class ScrobbleNotification(BasicNtfyNotification):
|
|
|
+ scrobble: "Scrobble"
|
|
|
+
|
|
|
def __init__(self, scrobble: "Scrobble"):
|
|
|
self.scrobble = scrobble
|
|
|
self.user = scrobble.user
|
|
@@ -19,13 +31,12 @@ class Notification(ABC):
|
|
|
self.url_tmpl = f'{protocol}://{domain}' + '{path}'
|
|
|
|
|
|
|
|
|
-
|
|
|
@abstractmethod
|
|
|
def send(self) -> None:
|
|
|
pass
|
|
|
|
|
|
|
|
|
-class NtfyNotification(Notification):
|
|
|
+class ScrobbleNtfyNotification(ScrobbleNotification):
|
|
|
def __init__(self, scrobble, **kwargs):
|
|
|
super().__init__(scrobble)
|
|
|
self.ntfy_str: str = f"{self.scrobble.media_obj}"
|
|
@@ -55,3 +66,28 @@ class NtfyNotification(Notification):
|
|
|
"Click": self.click_url,
|
|
|
},
|
|
|
)
|
|
|
+
|
|
|
+class MoodNtfyNotification(BasicNtfyNotification):
|
|
|
+ def __init__(self, profile, **kwargs):
|
|
|
+ super().__init__(scrobble)
|
|
|
+ self.profile = profile
|
|
|
+ self.ntfy_str: str = "Would you like to check in about your mood?"
|
|
|
+ self.click_url = self.url_tmpl.format(path=reverse("moods:mood-list"))
|
|
|
+ self.title = "Mood Check-in!"
|
|
|
+
|
|
|
+ def send(self):
|
|
|
+ if (
|
|
|
+ self.profile
|
|
|
+ and self.profile.ntfy_enabled
|
|
|
+ and self.profile.ntfy_url
|
|
|
+ ):
|
|
|
+ requests.post(
|
|
|
+ self.user.profile.ntfy_url,
|
|
|
+ data=self.ntfy_str.encode(encoding="utf-8"),
|
|
|
+ headers={
|
|
|
+ "Title": self.title,
|
|
|
+ "Priority": "high",
|
|
|
+ "Tags": "smiley, check",
|
|
|
+ "Click": self.click_url,
|
|
|
+ },
|
|
|
+ )
|