feat: pwa notifications
This commit is contained in:
@@ -40,3 +40,36 @@ class ApiToken(models.Model):
|
||||
def touch(self):
|
||||
self.last_used_at = timezone.now()
|
||||
self.save(update_fields=["last_used_at"])
|
||||
|
||||
|
||||
class PushSubscription(models.Model):
|
||||
"""One installed PWA that has agreed to receive reminders.
|
||||
|
||||
The endpoint is issued by the browser's push service; the two keys are what
|
||||
let us encrypt a payload only that device can read.
|
||||
"""
|
||||
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="push_subscriptions",
|
||||
)
|
||||
endpoint = models.URLField(max_length=500, unique=True)
|
||||
p256dh = models.CharField(max_length=200)
|
||||
auth = models.CharField(max_length=100)
|
||||
user_agent = models.CharField(max_length=300, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
last_success_at = models.DateTimeField(null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-created_at"]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.user} — {self.user_agent[:40] or self.endpoint[:40]}"
|
||||
|
||||
def as_subscription_info(self) -> dict:
|
||||
"""The shape pywebpush expects, i.e. what the browser handed us."""
|
||||
return {
|
||||
"endpoint": self.endpoint,
|
||||
"keys": {"p256dh": self.p256dh, "auth": self.auth},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user