|
|
|
@ -5,6 +5,7 @@ from .serializers import *
|
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
from django.db.models import Q
|
|
|
|
|
from django.utils.dateparse import parse_date
|
|
|
|
|
from django.db.models import Count
|
|
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
|
|
|
|
def get_chat_rooms(request):
|
|
|
|
@ -85,6 +86,20 @@ def start_conversation(request):
|
|
|
|
|
|
|
|
|
|
start_with_id = request.data.get('start_with')
|
|
|
|
|
start_with = get_object_or_404(User, id=start_with_id)
|
|
|
|
|
|
|
|
|
|
existing_room = ChatRoom.objects.filter(
|
|
|
|
|
chatmember__member=user
|
|
|
|
|
).annotate(
|
|
|
|
|
member_count=Count('chatmember')
|
|
|
|
|
).filter(
|
|
|
|
|
member_count=2,
|
|
|
|
|
chatmember__member=start_with
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
if existing_room:
|
|
|
|
|
return successRes({'room_id': existing_room.id})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chat_title = (
|
|
|
|
|
f"Chat created by {user.first_name} {user.last_name} "
|
|
|
|
|
f"with {start_with.first_name} {start_with.last_name} "
|
|
|
|
@ -96,6 +111,7 @@ def start_conversation(request):
|
|
|
|
|
created_by=user,
|
|
|
|
|
date_created=timezone.now(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
ChatMember.objects.create(
|
|
|
|
|
member=user,
|
|
|
|
|
room=room,
|
|
|
|
@ -106,10 +122,10 @@ def start_conversation(request):
|
|
|
|
|
room=room,
|
|
|
|
|
date_joined=timezone.now()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return successRes({'room_id': room.id})
|
|
|
|
|
|
|
|
|
|
except TokenError as terr:
|
|
|
|
|
return errorRes(msg=str(terr), status=450)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return errorRes(str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|