4 changed files with 88 additions and 2 deletions
-
56apps/account/tests/notifications/test_communication_notifications.py
-
6apps/chat/urls.py
-
27apps/chat/views.py
-
1config/urls.py
@ -0,0 +1,6 @@ |
|||
from django.urls import path |
|||
from .views import ChatMessageNotifyWebhookView |
|||
|
|||
urlpatterns = [ |
|||
path('notify-message/', ChatMessageNotifyWebhookView.as_view(), name='chat-notify-message'), |
|||
] |
|||
@ -1,3 +1,26 @@ |
|||
from django.shortcuts import render |
|||
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 |
|||
|
|||
# Create your views here. |
|||
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'}) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue