robosats/chat/views.py
Reckless_Satoshi 3446fc33d3
Add turtle chat component
Squashed commit of the following:

commit f60870fcfe574dc4ab1343e25241b6ef7cc2721b
Author: Reckless_Satoshi <reckless.satoshi@protonmail.com>
Date:   Thu Nov 10 10:30:42 2022 -0800

    Fix internal error when entering chat

commit f1eeb49f2a86575eb2e85cdff20460276e71b806
Author: Reckless_Satoshi <reckless.satoshi@protonmail.com>
Date:   Tue Nov 8 10:08:22 2022 -0800

    Fix final serializer

commit d0c08ba6ad4378a9539c0be83b6f4f8b958b532e
Author: Reckless_Satoshi <reckless.satoshi@protonmail.com>
Date:   Tue Nov 8 09:44:57 2022 -0800

    Chat API changes

commit a66bf64edc06d936612db6bf75476b54e6a84334
Author: Reckless_Satoshi <reckless.satoshi@protonmail.com>
Date:   Tue Nov 8 09:28:29 2022 -0800

    Fix param on post

commit 60b18d13c2
Author: Reckless_Satoshi <reckless.satoshi@protonmail.com>
Date:   Tue Nov 8 08:56:25 2022 -0800

    Fix serializer

commit 11212d30ee
Author: KoalaSat <yv1vtrul@duck.com>
Date:   Sun Nov 6 21:07:18 2022 +0100

    CR 2

commit c82790cb81
Author: KoalaSat <yv1vtrul@duck.com>
Date:   Sun Nov 6 20:09:18 2022 +0100

    Fix

commit 605a3b69a1
Author: KoalaSat <yv1vtrul@duck.com>
Date:   Sun Nov 6 14:44:42 2022 +0100

    CR

commit 09776e9c8f
Author: KoalaSat <yv1vtrul@duck.com>
Date:   Wed Nov 2 18:12:29 2022 +0100

    translations

commit 432e4d2399
Author: KoalaSat <yv1vtrul@duck.com>
Date:   Wed Nov 2 17:39:02 2022 +0100

    Switch and better UX

commit df6e476613
Author: KoalaSat <yv1vtrul@duck.com>
Date:   Tue Nov 1 18:20:01 2022 +0100

    Unused code

commit 5b8d6b4d32
Author: Reckless_Satoshi <reckless.satoshi@protonmail.com>
Date:   Mon Oct 31 09:20:20 2022 -0700

    Add Chat Turtle Mode
2022-11-11 01:28:09 -08:00

193 lines
6.7 KiB
Python

from datetime import timedelta
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
from django.utils import timezone
from rest_framework import status, viewsets
from rest_framework.response import Response
from api.models import Order, User
from chat.models import ChatRoom, Message
from chat.serializers import ChatSerializer, PostMessageSerializer
class ChatView(viewsets.ViewSet):
serializer_class = PostMessageSerializer
lookup_url_kwarg = ["order_id", "offset"]
queryset = Message.objects.filter(
order__status__in=[Order.Status.CHA, Order.Status.FSE]
)
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(
{"bad_request": "Order ID does not exist"},
status.HTTP_400_BAD_REQUEST,
)
order = Order.objects.get(id=order_id)
if not (request.user == order.maker or request.user == order.taker):
return Response(
{"bad_request": "You are not participant in this order"},
status.HTTP_400_BAD_REQUEST,
)
if order.status not in [Order.Status.CHA, Order.Status.FSE]:
return Response(
{"bad_request": "Order is not in chat status"},
status.HTTP_400_BAD_REQUEST,
)
queryset = Message.objects.filter(order=order, index__gt=offset)
chatroom = ChatRoom.objects.get(order=order)
# Poor idea: is_peer_connected() mockup. Update connection status based on last time a GET request was sent
if chatroom.maker == request.user:
chatroom.taker_connected = order.taker_last_seen > (
timezone.now() - timedelta(minutes=1)
)
chatroom.maker_connected = True
chatroom.save()
peer_connected = chatroom.taker_connected
peer_public_key = order.taker.profile.public_key
elif chatroom.taker == request.user:
chatroom.maker_connected = order.maker_last_seen > (
timezone.now() - timedelta(minutes=1)
)
chatroom.taker_connected = True
chatroom.save()
peer_connected = chatroom.maker_connected
peer_public_key = order.maker.profile.public_key
messages = []
for message in queryset:
d = ChatSerializer(message).data
# Re-serialize so the response is identical to the consumer message
data = {
"index": d["index"],
"time": d["created_at"],
"message": d["PGP_message"],
"nick": User.objects.get(id=d["sender"]).username,
}
messages.append(data)
response = {
"peer_connected": peer_connected,
"messages": messages,
"peer_pubkey": peer_public_key,
}
return Response(response, status.HTTP_200_OK)
def post(self, request, format=None):
"""
Adds one new message to the chatroom.
"""
serializer = self.serializer_class(data=request.data)
# Return bad request if serializer is not valid
if not serializer.is_valid():
context = {"bad_request": "Invalid serializer"}
return Response(context, status=status.HTTP_400_BAD_REQUEST)
order_id = serializer.data.get("order_id")
if order_id is None:
return Response(
{"bad_request": "Order ID does not exist"},
status.HTTP_400_BAD_REQUEST,
)
order = Order.objects.get(id=order_id)
if not (request.user == order.maker or request.user == order.taker):
return Response(
{"bad_request": "You are not participant in this order"},
status.HTTP_400_BAD_REQUEST,
)
if order.status not in [Order.Status.CHA, Order.Status.FSE]:
return Response(
{"bad_request": "Order is not in chat status"},
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(
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,
},
)
last_index = Message.objects.filter(order=order, chatroom=chatroom).count()
new_message = Message.objects.create(
index=last_index + 1,
PGP_message=serializer.data.get("PGP_message"),
order=order,
chatroom=chatroom,
sender=sender,
receiver=receiver,
)
# Send websocket message
if chatroom.maker == request.user:
peer_connected = chatroom.taker_connected
elif chatroom.taker == request.user:
peer_connected = chatroom.maker_connected
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(
f"chat_order_{order_id}",
{
"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,
},
)
# if offset is given, reply with messages
offset = serializer.data.get("offset", None)
if offset:
queryset = Message.objects.filter(order=order, index__gt=offset)
messages = []
for message in queryset:
d = ChatSerializer(message).data
# Re-serialize so the response is identical to the consumer message
data = {
"index": d["index"],
"time": d["created_at"],
"message": d["PGP_message"],
"nick": User.objects.get(id=d["sender"]).username,
}
messages.append(data)
response = {"peer_connected": peer_connected, "messages": messages}
else:
response = {}
return Response(response, status.HTTP_200_OK)