Support sending messages when a rule no longer matches and fix dual-use notification sender

This commit is contained in:
2023-01-16 00:10:41 +00:00
parent 75603570ff
commit a2207bbcf4
6 changed files with 173 additions and 63 deletions

View File

@@ -92,8 +92,14 @@ class User(AbstractUser):
plan_list = [plan.name for plan in self.plans.all()]
return plan in plan_list
def get_notification_settings(self):
return NotificationSettings.objects.get_or_create(user=self)[0]
def get_notification_settings(self, check=True):
sets = NotificationSettings.objects.get_or_create(user=self)[0]
if check:
if sets.service == "ntfy" and sets.topic is None:
return None
if sets.service == "webhook" and sets.url is None:
return None
return sets
@property
def allowed_indices(self):
@@ -179,6 +185,7 @@ class NotificationRule(models.Model):
data = models.TextField()
match = models.JSONField(null=True, blank=True)
service = models.CharField(choices=SERVICE_CHOICES, max_length=255, default="ntfy")
send_empty = models.BooleanField(default=False)
def __str__(self):
return f"{self.user} - {self.name}"
@@ -198,12 +205,12 @@ class NotificationRule(models.Model):
if isinstance(self.match, dict):
return f"{sum(list(self.match.values()))}/{len(self.match)}"
def get_notification_settings(self):
def get_notification_settings(self, check=True):
"""
Get the notification settings for this rule.
Notification rule settings take priority.
"""
user_settings = self.user.get_notification_settings()
user_settings = self.user.get_notification_settings(check=False)
user_settings = user_settings.__dict__
if self.priority is not None:
user_settings["priority"] = str(self.priority)
@@ -213,6 +220,14 @@ class NotificationRule(models.Model):
user_settings["url"] = self.url
if self.service is not None:
user_settings["service"] = self.service
if self.send_empty is not None:
user_settings["send_empty"] = self.send_empty
if check:
if user_settings["service"] == "ntfy" and user_settings["topic"] is None:
return None
if user_settings["service"] == "webhook" and user_settings["url"] is None:
return None
return user_settings