2022-10-17 00:11:48 +03:00
|
|
|
from datetime import timedelta
|
2022-01-13 14:07:20 +03:00
|
|
|
|
2022-10-17 00:11:48 +03:00
|
|
|
from asgiref.sync import async_to_sync
|
|
|
|
from channels.layers import get_channel_layer
|
2023-05-01 13:30:53 +03:00
|
|
|
from django.contrib.auth.models import User
|
2022-10-25 21:04:12 +03:00
|
|
|
from django.utils import timezone
|
2023-11-28 20:21:27 +03:00
|
|
|
from drf_spectacular.utils import OpenApiParameter, extend_schema
|
2022-10-25 21:04:12 +03:00
|
|
|
from rest_framework import status, viewsets
|
2023-05-05 13:12:38 +03:00
|
|
|
from rest_framework.authentication import TokenAuthentication
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2022-10-25 21:04:12 +03:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2023-05-01 13:30:53 +03:00
|
|
|
from api.models import Order
|
2023-03-27 12:37:36 +03:00
|
|
|
from api.tasks import send_notification
|
2022-10-25 21:04:12 +03:00
|
|
|
from chat.models import ChatRoom, Message
|
2023-11-28 20:21:27 +03:00
|
|
|
from chat.serializers import ChatSerializer, InMessageSerializer, PostMessageSerializer
|
2022-10-17 00:11:48 +03:00
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-10-17 00:11:48 +03:00
|
|
|
class ChatView(viewsets.ViewSet):
|
2023-11-28 20:21:27 +03:00
|
|
|
serializer_class = ChatSerializer
|
2023-08-14 17:21:12 +03:00
|
|
|
authentication_classes = [TokenAuthentication]
|
2023-05-05 13:12:38 +03:00
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
lookup_url_kwarg = ["order_id", "offset"]
|
2022-10-17 00:11:48 +03:00
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
queryset = Message.objects.filter(
|
|
|
|
order__status__in=[Order.Status.CHA, Order.Status.FSE]
|
|
|
|
)
|
2022-10-17 00:11:48 +03:00
|
|
|
|
2023-11-28 20:21:27 +03:00
|
|
|
@extend_schema(
|
|
|
|
request=ChatSerializer,
|
|
|
|
parameters=[
|
|
|
|
OpenApiParameter(
|
|
|
|
name="order_id", location=OpenApiParameter.QUERY, type=int
|
|
|
|
),
|
|
|
|
OpenApiParameter(name="offset", location=OpenApiParameter.QUERY, type=int),
|
|
|
|
],
|
|
|
|
)
|
2022-10-17 00:11:48 +03:00
|
|
|
def get(self, request, format=None):
|
|
|
|
"""
|
|
|
|
Returns chat messages for an order with an index higher than `offset`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
order_id = request.GET.get("order_id", None)
|
|
|
|
offset = request.GET.get("offset", 0)
|
|
|
|
|
|
|
|
if order_id is None:
|
|
|
|
return Response(
|
2022-10-20 12:56:10 +03:00
|
|
|
{"bad_request": "Order ID does not exist"},
|
2022-10-17 00:11:48 +03:00
|
|
|
status.HTTP_400_BAD_REQUEST,
|
2022-10-20 12:56:10 +03:00
|
|
|
)
|
2022-10-17 00:11:48 +03:00
|
|
|
|
|
|
|
order = Order.objects.get(id=order_id)
|
|
|
|
|
|
|
|
if not (request.user == order.maker or request.user == order.taker):
|
|
|
|
return Response(
|
2022-10-20 12:56:10 +03:00
|
|
|
{"bad_request": "You are not participant in this order"},
|
2022-10-17 00:11:48 +03:00
|
|
|
status.HTTP_400_BAD_REQUEST,
|
|
|
|
)
|
|
|
|
|
2022-10-20 23:53:51 +03:00
|
|
|
if order.status not in [Order.Status.CHA, Order.Status.FSE]:
|
2022-10-17 00:11:48 +03:00
|
|
|
return Response(
|
2022-10-20 12:56:10 +03:00
|
|
|
{"bad_request": "Order is not in chat status"},
|
2022-10-17 00:11:48 +03:00
|
|
|
status.HTTP_400_BAD_REQUEST,
|
|
|
|
)
|
|
|
|
|
|
|
|
queryset = Message.objects.filter(order=order, index__gt=offset)
|
2022-11-15 19:38:31 +03:00
|
|
|
chatroom, created = ChatRoom.objects.get_or_create(
|
|
|
|
id=order_id,
|
|
|
|
order=order,
|
|
|
|
room_group_name=f"chat_order_{order_id}",
|
|
|
|
defaults={
|
|
|
|
"maker": order.maker,
|
|
|
|
"maker_connected": order.maker == request.user,
|
|
|
|
"taker": order.taker,
|
|
|
|
"taker_connected": order.taker == request.user,
|
|
|
|
},
|
|
|
|
)
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2023-12-25 16:44:27 +03:00
|
|
|
# is_peer_connected() mockup. Update connection status based on last time a GET request was sent
|
2022-10-17 00:11:48 +03:00
|
|
|
if chatroom.maker == request.user:
|
2023-05-07 14:54:09 +03:00
|
|
|
chatroom.taker_connected = order.taker.last_login > (
|
2022-10-20 12:56:10 +03:00
|
|
|
timezone.now() - timedelta(minutes=1)
|
|
|
|
)
|
2022-10-17 00:11:48 +03:00
|
|
|
chatroom.maker_connected = True
|
|
|
|
peer_connected = chatroom.taker_connected
|
2023-05-01 13:30:53 +03:00
|
|
|
peer_public_key = order.taker.robot.public_key
|
2022-10-17 00:11:48 +03:00
|
|
|
elif chatroom.taker == request.user:
|
2023-05-07 14:54:09 +03:00
|
|
|
chatroom.maker_connected = order.maker.last_login > (
|
2022-10-20 12:56:10 +03:00
|
|
|
timezone.now() - timedelta(minutes=1)
|
|
|
|
)
|
2022-10-17 00:11:48 +03:00
|
|
|
chatroom.taker_connected = True
|
|
|
|
peer_connected = chatroom.maker_connected
|
2023-05-01 13:30:53 +03:00
|
|
|
peer_public_key = order.maker.robot.public_key
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2023-05-08 21:10:37 +03:00
|
|
|
chatroom.save(update_fields=["maker_connected", "taker_connected"])
|
|
|
|
|
2022-10-17 00:11:48 +03:00
|
|
|
messages = []
|
|
|
|
for message in queryset:
|
2023-11-28 20:21:27 +03:00
|
|
|
d = InMessageSerializer(message).data
|
2022-10-17 00:11:48 +03:00
|
|
|
# Re-serialize so the response is identical to the consumer message
|
|
|
|
data = {
|
2022-10-20 12:56:10 +03:00
|
|
|
"index": d["index"],
|
|
|
|
"time": d["created_at"],
|
|
|
|
"message": d["PGP_message"],
|
|
|
|
"nick": User.objects.get(id=d["sender"]).username,
|
|
|
|
}
|
2022-10-17 00:11:48 +03:00
|
|
|
messages.append(data)
|
|
|
|
|
2022-11-11 12:28:09 +03:00
|
|
|
response = {
|
|
|
|
"peer_connected": peer_connected,
|
|
|
|
"messages": messages,
|
|
|
|
"peer_pubkey": peer_public_key,
|
|
|
|
}
|
2022-10-17 00:11:48 +03:00
|
|
|
|
|
|
|
return Response(response, status.HTTP_200_OK)
|
|
|
|
|
2023-11-28 20:21:27 +03:00
|
|
|
@extend_schema(request=PostMessageSerializer, responses=ChatSerializer)
|
2022-10-17 00:11:48 +03:00
|
|
|
def post(self, request, format=None):
|
|
|
|
"""
|
2023-11-28 20:21:27 +03:00
|
|
|
Adds one new message to the chatroom. If `offset` is given, will return every new message as well.
|
2022-10-17 00:11:48 +03:00
|
|
|
"""
|
|
|
|
|
2023-11-28 20:21:27 +03:00
|
|
|
serializer = PostMessageSerializer(data=request.data)
|
2022-10-20 12:56:10 +03:00
|
|
|
# Return bad request if serializer is not valid
|
2022-10-17 00:11:48 +03:00
|
|
|
if not serializer.is_valid():
|
|
|
|
context = {"bad_request": "Invalid serializer"}
|
|
|
|
return Response(context, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
2022-11-11 12:28:09 +03:00
|
|
|
order_id = serializer.data.get("order_id")
|
2022-10-17 00:11:48 +03:00
|
|
|
|
|
|
|
if order_id is None:
|
|
|
|
return Response(
|
2022-10-20 12:56:10 +03:00
|
|
|
{"bad_request": "Order ID does not exist"},
|
2022-10-17 00:11:48 +03:00
|
|
|
status.HTTP_400_BAD_REQUEST,
|
2022-10-20 12:56:10 +03:00
|
|
|
)
|
|
|
|
|
2022-10-17 00:11:48 +03:00
|
|
|
order = Order.objects.get(id=order_id)
|
|
|
|
|
|
|
|
if not (request.user == order.maker or request.user == order.taker):
|
|
|
|
return Response(
|
2022-10-20 12:56:10 +03:00
|
|
|
{"bad_request": "You are not participant in this order"},
|
2022-10-17 00:11:48 +03:00
|
|
|
status.HTTP_400_BAD_REQUEST,
|
|
|
|
)
|
|
|
|
|
2022-10-20 23:53:51 +03:00
|
|
|
if order.status not in [Order.Status.CHA, Order.Status.FSE]:
|
2022-10-17 00:11:48 +03:00
|
|
|
return Response(
|
2022-10-20 12:56:10 +03:00
|
|
|
{"bad_request": "Order is not in chat status"},
|
2022-10-17 00:11:48 +03:00
|
|
|
status.HTTP_400_BAD_REQUEST,
|
|
|
|
)
|
|
|
|
|
|
|
|
if order.maker == request.user:
|
|
|
|
sender = order.maker
|
|
|
|
receiver = order.taker
|
|
|
|
elif order.taker == request.user:
|
|
|
|
sender = order.taker
|
|
|
|
receiver = order.maker
|
|
|
|
|
|
|
|
chatroom, _ = ChatRoom.objects.get_or_create(
|
2022-10-20 12:56:10 +03:00
|
|
|
id=order_id,
|
|
|
|
order=order,
|
|
|
|
room_group_name=f"chat_order_{order_id}",
|
|
|
|
defaults={
|
|
|
|
"maker": order.maker,
|
|
|
|
"maker_connected": order.maker == request.user,
|
|
|
|
"taker": order.taker,
|
|
|
|
"taker_connected": order.taker == request.user,
|
|
|
|
},
|
|
|
|
)
|
2022-10-17 00:11:48 +03:00
|
|
|
|
|
|
|
last_index = Message.objects.filter(order=order, chatroom=chatroom).count()
|
|
|
|
new_message = Message.objects.create(
|
2022-10-20 12:56:10 +03:00
|
|
|
index=last_index + 1,
|
2022-10-17 00:11:48 +03:00
|
|
|
PGP_message=serializer.data.get("PGP_message"),
|
|
|
|
order=order,
|
|
|
|
chatroom=chatroom,
|
|
|
|
sender=sender,
|
|
|
|
receiver=receiver,
|
2022-10-20 12:56:10 +03:00
|
|
|
)
|
2022-10-17 00:11:48 +03:00
|
|
|
|
2023-03-27 12:37:36 +03:00
|
|
|
# send Telegram notification for new message (if conditions apply)
|
|
|
|
send_notification.delay(
|
|
|
|
chat_message_id=new_message.id, message="new_chat_message"
|
|
|
|
)
|
|
|
|
|
2022-10-17 00:11:48 +03:00
|
|
|
# Send websocket message
|
|
|
|
if chatroom.maker == request.user:
|
|
|
|
peer_connected = chatroom.taker_connected
|
2023-11-28 20:21:27 +03:00
|
|
|
peer_public_key = order.taker.robot.public_key
|
2022-10-17 00:11:48 +03:00
|
|
|
elif chatroom.taker == request.user:
|
|
|
|
peer_connected = chatroom.maker_connected
|
2023-11-28 20:21:27 +03:00
|
|
|
peer_public_key = order.maker.robot.public_key
|
2022-10-17 00:11:48 +03:00
|
|
|
|
|
|
|
channel_layer = get_channel_layer()
|
|
|
|
async_to_sync(channel_layer.group_send)(
|
|
|
|
f"chat_order_{order_id}",
|
|
|
|
{
|
2022-10-20 12:56:10 +03:00
|
|
|
"type": "PGP_message",
|
|
|
|
"index": new_message.index,
|
|
|
|
"message": new_message.PGP_message,
|
|
|
|
"time": str(new_message.created_at),
|
|
|
|
"nick": new_message.sender.username,
|
|
|
|
"peer_connected": peer_connected,
|
|
|
|
},
|
2022-10-17 00:11:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# if offset is given, reply with messages
|
|
|
|
offset = serializer.data.get("offset", None)
|
2022-11-15 19:38:31 +03:00
|
|
|
if offset or offset == 0:
|
2022-10-17 00:11:48 +03:00
|
|
|
queryset = Message.objects.filter(order=order, index__gt=offset)
|
|
|
|
messages = []
|
|
|
|
for message in queryset:
|
2023-11-28 20:21:27 +03:00
|
|
|
d = InMessageSerializer(message).data
|
2022-10-17 00:11:48 +03:00
|
|
|
# Re-serialize so the response is identical to the consumer message
|
|
|
|
data = {
|
2022-10-20 12:56:10 +03:00
|
|
|
"index": d["index"],
|
|
|
|
"time": d["created_at"],
|
|
|
|
"message": d["PGP_message"],
|
|
|
|
"nick": User.objects.get(id=d["sender"]).username,
|
|
|
|
}
|
2022-10-17 00:11:48 +03:00
|
|
|
messages.append(data)
|
|
|
|
|
2023-11-28 20:21:27 +03:00
|
|
|
response = {
|
|
|
|
"peer_connected": peer_connected,
|
|
|
|
"messages": messages,
|
|
|
|
"peer_pubkey": peer_public_key,
|
|
|
|
}
|
2022-10-17 00:11:48 +03:00
|
|
|
else:
|
|
|
|
response = {}
|
|
|
|
|
|
|
|
return Response(response, status.HTTP_200_OK)
|