Pull groups from WhatsApp
This commit is contained in:
@@ -4,6 +4,7 @@ import uuid
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
|
||||
from core.clients import transport
|
||||
@@ -170,6 +171,62 @@ class PersonIdentifier(models.Model):
|
||||
)
|
||||
|
||||
|
||||
class PlatformChatLink(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
person = models.ForeignKey(Person, on_delete=models.CASCADE)
|
||||
person_identifier = models.ForeignKey(
|
||||
PersonIdentifier,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
)
|
||||
service = models.CharField(choices=SERVICE_CHOICES, max_length=255)
|
||||
chat_identifier = models.CharField(max_length=255)
|
||||
chat_jid = models.CharField(max_length=255, blank=True, null=True)
|
||||
chat_name = models.CharField(max_length=255, blank=True, null=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["user", "service", "chat_identifier"],
|
||||
name="unique_platform_chat_link",
|
||||
)
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=["user", "service", "chat_identifier"]),
|
||||
]
|
||||
|
||||
def clean(self):
|
||||
if self.person_id and self.user_id and self.person.user_id != self.user_id:
|
||||
raise ValidationError("Person must belong to the same user.")
|
||||
if self.person_identifier_id:
|
||||
if self.person_identifier.user_id != self.user_id:
|
||||
raise ValidationError(
|
||||
"Person identifier must belong to the same user."
|
||||
)
|
||||
if self.person_identifier.person_id != self.person_id:
|
||||
raise ValidationError(
|
||||
"Person identifier must belong to the selected person."
|
||||
)
|
||||
if self.person_identifier.service != self.service:
|
||||
raise ValidationError(
|
||||
"Chat links cannot be linked across platforms."
|
||||
)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
value = str(self.chat_identifier or "").strip()
|
||||
if "@" in value:
|
||||
value = value.split("@", 1)[0]
|
||||
self.chat_identifier = value
|
||||
self.full_clean()
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.person.name} ({self.service}: {self.chat_identifier})"
|
||||
|
||||
|
||||
class ChatSession(models.Model):
|
||||
"""Represents an ongoing chat session for persisted message history."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user