from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.permissions import AllowAny from django.conf import settings from django.shortcuts import get_object_or_404 from apps.chat.models import ChatMessage from apps.chat.signals import notify_on_new_chat_message class ChatMessageNotifyWebhookView(APIView): permission_classes = [AllowAny] def post(self, request, *args, **kwargs): token = request.headers.get('X-Internal-Secret') or request.query_params.get('secret') if token != getattr(settings, 'INTERNAL_WEBHOOK_SECRET', None): return Response({'error': 'Unauthorized'}, status=403) message_id = request.data.get('message_id') if not message_id: return Response({'error': 'message_id is required'}, status=400) message = get_object_or_404(ChatMessage, id=message_id) # Trigger the notification logic notify_on_new_chat_message(sender=ChatMessage, instance=message, created=True) return Response({'status': 'success'})