from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
from django.forms.models import model_to_dict
from functools import wraps
import json
from old_chat.models import Conversation
from old_chat.forms import SendMessageForm
from .tasks import notify_users

def typing(request, pk):
    conversation = Conversation.objects.get(pk=pk)
    # TODO: validate membership
    member_ids = list(conversation.members.values_list('id', flat=True))
    notify_users.delay(member_ids, {
        'type': 'RECEIVE_TYPING',
        'payload': {
            'conversation_id': str(conversation.pk),
            'user_id': str(request.user.pk),
        },
    })
    return {}


from rest_framework import viewsets, status
from .serializers import ConversationSerializer, ConversationVerboseSerializer, MessageSerializer
from rest_framework.response import Response



class ConversationViewSet(viewsets.ViewSet):

    def list(self, request):
        queryset = request.user.conversations
        serializer = ConversationSerializer(queryset, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk):
        conversation = get_object_or_404(Conversation, pk=pk)
        # TODO: validate membership
        serializer = ConversationVerboseSerializer(conversation)
        return Response(serializer.data)


class MessageViewSet(viewsets.ViewSet):

    def list(self, request, pk):
        conversation = get_object_or_404(Conversation, pk=pk)
        # TODO: validate membership
        queryset = conversation.messages
        serializer = MessageSerializer(queryset, many=True)
        return Response(serializer.data)

    def create(self, request, pk):
        conversation = get_object_or_404(Conversation, pk=pk)
        ## TODO: validate membership
        serializer = MessageSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save(conversation=conversation, author=request.user)

        # TODO: extract this
        member_ids = list(conversation.members.values_list('id', flat=True))
        notify_users.delay(member_ids, {
            'type': 'RECEIVE_MESSAGE',
            'payload': {
                'conversation_id': str(conversation.pk),
                'message': serializer.data,
            },
        })

        return Response(serializer.data, status=status.HTTP_201_CREATED)
        # TODO: add Location header

@@ -25,16 +25,8 @@
 port
- render,
  get
@@ -49,753 +49,132 @@
 rom 
-django.http import JsonResponse%0Afrom django.views.decorators.http import require_http_methods%0Afrom django.forms.models import model_to_dict%0Afrom functools import wraps%0Aimport json%0Afrom old_chat.models import Conversation%0Afrom old_chat.forms import SendMessageForm%0Afrom .tasks import notify_users%0A%0Adef typing(request, pk):%0A    conversation = Conversation.objects.get(pk=pk)%0A    # TODO: validate membership%0A    member_ids = list(conversation.members.values_list('id', flat=True))%0A    notify_users.delay(member_ids, %7B%0A        'type': 'RECEIVE_TYPING',%0A        'payload': %7B%0A            'conversation_id': str(conversation.pk),%0A            'user_id': str(request.user.pk),%0A        %7D,%0A    %7D)%0A    return %7B%7D%0A%0A%0Afrom rest_framework import viewsets, status
+rest_framework import viewsets, status%0Afrom rest_framework.response import Response%0Afrom old_chat.models import Conversation
 %0Afro
@@ -277,47 +277,34 @@
 rom 
-rest_framework.response import Response
+.tasks import notify_users
 %0A%0A%0A%0A
@@ -773,16 +773,592 @@
 .data)%0A%0A
+    def typing(self, request, pk): # TODO: come up with a better name%0A        conversation = get_object_or_404(Conversation, pk=pk)%0A        # TODO: validate membership%0A%0A        # TODO: extract this%0A        member_ids = list(conversation.members.values_list('id', flat=True))%0A        notify_users.delay(member_ids, %7B%0A            'type': 'RECEIVE_TYPING',%0A            'payload': %7B%0A                'conversation_id': str(conversation.pk),%0A                'user_id': str(request.user.pk),%0A            %7D,%0A        %7D)%0A%0A        return Response(None, status=status.HTTP_202_ACCEPTED)%0A%0A
 %0Aclass M

implement typing endpoint