robosats/chat/consumers.py

270 lines
8.5 KiB
Python
Raw Normal View History

from channels.generic.websocket import AsyncWebsocketConsumer
2022-03-10 21:35:16 +00:00
from channels.db import database_sync_to_async
from api.models import Order
2022-05-28 13:01:50 +00:00
from chat.models import ChatRoom, Message
from asgiref.sync import async_to_sync
import json
2022-10-20 09:56:10 +00:00
class ChatRoomConsumer(AsyncWebsocketConsumer):
2022-03-10 21:35:16 +00:00
@database_sync_to_async
def allow_in_chatroom(self):
order = Order.objects.get(id=self.order_id)
if not order.status in [Order.Status.CHA, Order.Status.FSE]:
print("Order is not in chat status")
return False
2022-03-10 21:35:16 +00:00
if not (order.maker == self.user or order.taker == self.user):
print("Not allowed in this chat")
return False
return True
@database_sync_to_async
def save_connect_user(self):
2022-10-20 09:56:10 +00:00
"""Creates or updates the ChatRoom object"""
2022-03-10 21:35:16 +00:00
order = Order.objects.get(id=self.order_id)
if order.maker == self.user:
ChatRoom.objects.update_or_create(
2022-10-20 09:56:10 +00:00
id=self.order_id,
order=order,
2022-03-10 21:35:16 +00:00
room_group_name=self.room_group_name,
defaults={
"maker": self.user,
"maker_connected": True,
2022-10-20 09:56:10 +00:00
},
)
2022-03-10 21:35:16 +00:00
elif order.taker == self.user:
ChatRoom.objects.update_or_create(
2022-10-20 09:56:10 +00:00
id=self.order_id,
order=order,
2022-03-10 21:35:16 +00:00
room_group_name=self.room_group_name,
defaults={
"taker": self.user,
"taker_connected": True,
2022-10-20 09:56:10 +00:00
},
)
2022-03-10 21:35:16 +00:00
return None
2022-05-28 13:01:50 +00:00
@database_sync_to_async
2022-05-28 14:59:32 +00:00
def save_new_PGP_message(self, PGP_message):
2022-10-20 09:56:10 +00:00
"""Creates a Message object"""
2022-05-28 13:01:50 +00:00
order = Order.objects.get(id=self.order_id)
chatroom = ChatRoom.objects.get(order=order)
2022-05-28 14:59:32 +00:00
try:
last_message = Message.objects.filter(order=order).latest()
2022-05-28 13:01:50 +00:00
index = last_message.index + 1
2022-05-28 14:59:32 +00:00
except:
index = 1
2022-05-28 13:01:50 +00:00
sender = self.scope["user"]
2022-05-28 14:59:32 +00:00
if order.taker == sender:
receiver = order.maker
elif order.maker == sender:
receiver = order.taker
2022-05-28 13:01:50 +00:00
2022-05-28 14:59:32 +00:00
msg_obj = Message.objects.create(
2022-10-20 09:56:10 +00:00
order=order,
chatroom=chatroom,
index=index,
sender=sender,
receiver=receiver,
PGP_message=PGP_message,
)
2022-05-28 14:59:32 +00:00
return msg_obj
2022-05-28 13:01:50 +00:00
2022-03-10 21:35:16 +00:00
@database_sync_to_async
def save_disconnect_user(self):
2022-10-20 09:56:10 +00:00
"""Creates or updates the ChatRoom object"""
2022-03-10 21:35:16 +00:00
order = Order.objects.get(id=self.order_id)
if order.maker == self.user:
ChatRoom.objects.update_or_create(
2022-10-20 09:56:10 +00:00
id=self.order_id, defaults={"maker_connected": False}
)
2022-03-10 21:35:16 +00:00
elif order.taker == self.user:
ChatRoom.objects.update_or_create(
2022-10-20 09:56:10 +00:00
id=self.order_id, defaults={"taker_connected": False}
)
2022-03-10 21:35:16 +00:00
return None
@database_sync_to_async
def is_peer_connected(self):
2022-10-20 09:56:10 +00:00
"""Returns whether the consumer's peer is connected"""
2022-03-10 21:35:16 +00:00
chatroom = ChatRoom.objects.get(id=self.order_id)
if chatroom.maker == self.user:
return chatroom.taker_connected
if chatroom.taker == self.user:
return chatroom.maker_connected
2022-05-28 14:59:32 +00:00
@database_sync_to_async
def get_peer_PGP_public_key(self):
2022-10-20 09:56:10 +00:00
"""Returns peer PGP public key"""
2022-05-28 14:59:32 +00:00
order = Order.objects.get(id=self.order_id)
if order.maker == self.user:
return order.taker.profile.public_key
if order.taker == self.user:
return order.maker.profile.public_key
@database_sync_to_async
def get_all_PGP_messages(self):
2022-10-20 09:56:10 +00:00
"""Returns all PGP messages"""
2022-05-28 14:59:32 +00:00
order = Order.objects.get(id=self.order_id)
messages = Message.objects.filter(order=order)
msgs = []
for message in messages:
2022-10-20 09:56:10 +00:00
msgs.append(
{
"index": message.index,
"time": str(message.created_at),
"message": message.PGP_message,
"nick": str(message.sender),
}
)
2022-05-28 14:59:32 +00:00
return msgs
async def connect(self):
2022-02-17 19:50:10 +00:00
self.order_id = self.scope["url_route"]["kwargs"]["order_id"]
self.room_group_name = f"chat_order_{self.order_id}"
self.user = self.scope["user"]
self.user_nick = str(self.user)
2022-03-10 21:35:16 +00:00
allowed = await self.allow_in_chatroom()
2022-02-17 19:50:10 +00:00
2022-03-10 21:35:16 +00:00
if allowed:
await self.save_connect_user()
2022-10-20 09:56:10 +00:00
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
2022-03-10 21:35:16 +00:00
await self.accept()
2022-05-28 14:59:32 +00:00
# Send peer PGP public keys
peer_public_key = await self.get_peer_PGP_public_key()
peer_connected = await self.is_peer_connected()
2022-05-28 14:59:32 +00:00
await self.channel_layer.group_send(
self.room_group_name,
{
"type": "chatroom_message",
"message": peer_public_key,
"nick": self.scope["user"].username,
"peer_connected": peer_connected,
2022-05-28 14:59:32 +00:00
},
)
async def disconnect(self, close_code):
2022-03-10 21:35:16 +00:00
await self.save_disconnect_user()
2022-10-20 09:56:10 +00:00
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
2022-03-10 21:35:16 +00:00
await self.channel_layer.group_send(
self.room_group_name,
{
"type": "chatroom_message",
2022-10-20 09:56:10 +00:00
"message": "peer-disconnected",
2022-03-10 21:35:16 +00:00
"nick": self.scope["user"].username,
"peer_connected": False,
},
)
async def receive(self, text_data):
text_data_json = json.loads(text_data)
2022-02-17 19:50:10 +00:00
message = text_data_json["message"]
2022-05-28 14:59:32 +00:00
peer_connected = await self.is_peer_connected()
2022-10-20 09:56:10 +00:00
2022-05-28 13:01:50 +00:00
# Encrypted messages are stored. They are served later when a user reconnects.
2022-10-20 09:56:10 +00:00
if message[0:27] == "-----BEGIN PGP MESSAGE-----":
2022-05-28 14:59:32 +00:00
# save to database
msg_obj = await self.save_new_PGP_message(message)
index = msg_obj.index
message = msg_obj.PGP_message
time = str(msg_obj.created_at)
await self.channel_layer.group_send(
self.room_group_name,
{
"type": "PGP_message",
"index": index,
"message": message,
"time": time,
"nick": self.scope["user"].username,
"peer_connected": peer_connected,
},
)
2022-10-20 09:56:10 +00:00
# Encrypted messages are served when the user requests them
2022-10-20 09:56:10 +00:00
elif message[0:23] == "-----SERVE HISTORY-----":
# If there is any stored message, serve them.
msgs = await self.get_all_PGP_messages()
peer_connected = await self.is_peer_connected()
for msg in msgs:
await self.channel_layer.group_send(
self.room_group_name,
{
"type": "PGP_message",
2022-10-20 09:56:10 +00:00
"index": msg["index"],
"time": msg["time"],
"message": msg["message"],
"nick": msg["nick"],
"peer_connected": peer_connected,
},
)
# Unencrypted messages are not stored, just echoed.
2022-05-28 14:59:32 +00:00
else:
await self.channel_layer.group_send(
self.room_group_name,
{
"type": "chatroom_message",
"message": message,
"nick": self.scope["user"].username,
"peer_connected": peer_connected,
},
)
async def chatroom_message(self, event):
2022-02-17 19:50:10 +00:00
message = event["message"]
nick = event["nick"]
2022-03-10 21:35:16 +00:00
peer_connected = event["peer_connected"]
2022-10-20 09:56:10 +00:00
await self.send(
text_data=json.dumps(
{
"message": message,
"user_nick": nick,
"peer_connected": peer_connected,
}
)
)
2022-05-28 14:59:32 +00:00
async def PGP_message(self, event):
message = event["message"]
nick = event["nick"]
index = event["index"]
peer_connected = event["peer_connected"]
time = event["time"]
2022-10-20 09:56:10 +00:00
await self.send(
text_data=json.dumps(
{
"index": index,
"message": message,
"user_nick": nick,
"peer_connected": peer_connected,
"time": time,
}
)
)