Refactor and implement queueing messages
This commit is contained in:
@@ -4,6 +4,7 @@ import uuid
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
from core.lib.notify import raw_sendmsg
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -49,6 +50,19 @@ class User(AbstractUser):
|
||||
def get_notification_settings(self):
|
||||
return NotificationSettings.objects.get_or_create(user=self)[0]
|
||||
|
||||
def sendmsg(self, *args, **kwargs):
|
||||
notification_settings = self.get_notification_settings()
|
||||
|
||||
if notification_settings.ntfy_topic is None:
|
||||
# No topic set, so don't send
|
||||
return
|
||||
else:
|
||||
topic = notification_settings.ntfy_topic
|
||||
|
||||
raw_sendmsg(*args, **kwargs, url=notification_settings.ntfy_url, topic=topic)
|
||||
|
||||
|
||||
|
||||
|
||||
class NotificationSettings(models.Model):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
@@ -115,6 +129,21 @@ class ChatSession(models.Model):
|
||||
def __str__(self):
|
||||
return f"{self.identifier.person.name} ({self.identifier.service})"
|
||||
|
||||
class QueuedMessage(models.Model):
|
||||
"""Stores individual messages linked to a ChatSession."""
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
session = models.ForeignKey(ChatSession, on_delete=models.CASCADE)
|
||||
manipulation = models.ForeignKey("core.Manipulation", on_delete=models.CASCADE)
|
||||
ts = models.BigIntegerField() # Use Unix timestamp
|
||||
sender_uuid = models.CharField(max_length=255, blank=True, null=True) # Signal UUID
|
||||
text = models.TextField(blank=True, null=True)
|
||||
|
||||
custom_author = models.CharField(max_length=255, blank=True, null=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["ts"]
|
||||
|
||||
class Message(models.Model):
|
||||
"""Stores individual messages linked to a ChatSession."""
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
@@ -203,7 +232,17 @@ class Manipulation(models.Model):
|
||||
ai = models.ForeignKey(AI, on_delete=models.CASCADE)
|
||||
persona = models.ForeignKey(Persona, on_delete=models.CASCADE)
|
||||
enabled = models.BooleanField(default=False)
|
||||
send_enabled = models.BooleanField(default=False)
|
||||
mode = models.CharField(
|
||||
max_length=50,
|
||||
choices=[
|
||||
("active", "Send replies to messages"),
|
||||
("instant", "Click link to send reply"),
|
||||
("prospective", "Click link to open page"),
|
||||
("notify", "Send notification of ideal reply only"),
|
||||
("silent", "Do not generate or send replies"),
|
||||
],
|
||||
blank=True, null=True
|
||||
)
|
||||
|
||||
|
||||
# class Perms(models.Model):
|
||||
|
||||
Reference in New Issue
Block a user