from django.db import transaction
from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import SupportMessage
from .notifications import prepare_ticket_message_notification


@receiver(post_save, sender=SupportMessage)
def notify_support_message(sender, instance, created, **kwargs):
    if not created or instance.is_internal:
        return
    transaction.on_commit(lambda: prepare_ticket_message_notification(instance))
