2022-01-13 14:08:21 +00:00
|
|
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
2022-03-10 21:35:16 +00:00
|
|
|
from channels.db import database_sync_to_async
|
2022-01-13 19:22:54 +00:00
|
|
|
from api.models import Order
|
2022-05-28 13:01:50 +00:00
|
|
|
from chat.models import ChatRoom, Message
|
2022-05-24 00:31:34 +00:00
|
|
|
from django.utils import timezone
|
2022-01-13 19:22:54 +00:00
|
|
|
|
2022-01-13 14:08:21 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
class ChatRoomConsumer(AsyncWebsocketConsumer):
|
2022-01-13 19:22:54 +00:00
|
|
|
|
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.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):
|
|
|
|
'''Creates or updates the ChatRoom object'''
|
|
|
|
|
|
|
|
order = Order.objects.get(id=self.order_id)
|
|
|
|
|
|
|
|
if order.maker == self.user:
|
|
|
|
ChatRoom.objects.update_or_create(
|
|
|
|
id=self.order_id,
|
|
|
|
order=order,
|
|
|
|
room_group_name=self.room_group_name,
|
|
|
|
defaults={
|
|
|
|
"maker": self.user,
|
|
|
|
"maker_connected": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
elif order.taker == self.user:
|
|
|
|
ChatRoom.objects.update_or_create(
|
|
|
|
id=self.order_id,
|
|
|
|
order=order,
|
|
|
|
room_group_name=self.room_group_name,
|
|
|
|
defaults={
|
|
|
|
"taker": self.user,
|
|
|
|
"taker_connected": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2022-05-28 13:01:50 +00:00
|
|
|
@database_sync_to_async
|
|
|
|
def save_new_message(self):
|
|
|
|
'''Creates a Message object'''
|
|
|
|
|
|
|
|
order = Order.objects.get(id=self.order_id)
|
|
|
|
chatroom = ChatRoom.objects.get(order=order)
|
|
|
|
|
|
|
|
index = 0
|
|
|
|
last_message = Message.objects.filter(order=order).latest()
|
|
|
|
if last_message:
|
|
|
|
index = last_message.index + 1
|
|
|
|
|
|
|
|
sender = self.scope["user"]
|
|
|
|
|
|
|
|
Message.objects.create(
|
|
|
|
order=order,
|
|
|
|
chatroom=chatroom,
|
|
|
|
index=index,
|
|
|
|
sender = sender,
|
|
|
|
PGP_message=self.PGP_message,
|
|
|
|
)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2022-03-10 21:35:16 +00:00
|
|
|
@database_sync_to_async
|
|
|
|
def save_disconnect_user(self):
|
|
|
|
'''Creates or updates the ChatRoom object'''
|
|
|
|
|
|
|
|
order = Order.objects.get(id=self.order_id)
|
|
|
|
if order.maker == self.user:
|
|
|
|
ChatRoom.objects.update_or_create(
|
|
|
|
id=self.order_id,
|
|
|
|
defaults={
|
|
|
|
"maker_connected": False
|
|
|
|
}
|
|
|
|
)
|
|
|
|
elif order.taker == self.user:
|
|
|
|
ChatRoom.objects.update_or_create(
|
|
|
|
id=self.order_id,
|
|
|
|
defaults={
|
|
|
|
"taker_connected": False
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
|
|
@database_sync_to_async
|
|
|
|
def is_peer_connected(self):
|
2022-05-28 13:01:50 +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-01-13 14:08:21 +00:00
|
|
|
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}"
|
2022-01-13 19:22:54 +00:00
|
|
|
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()
|
|
|
|
await self.channel_layer.group_add(self.room_group_name,
|
|
|
|
self.channel_name)
|
2022-01-13 14:08:21 +00:00
|
|
|
|
2022-03-10 21:35:16 +00:00
|
|
|
await self.accept()
|
2022-01-13 14:08:21 +00:00
|
|
|
|
|
|
|
async def disconnect(self, close_code):
|
2022-03-10 21:35:16 +00:00
|
|
|
await self.save_disconnect_user()
|
2022-02-17 19:50: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",
|
|
|
|
"message": 'peer-disconnected',
|
|
|
|
"nick": self.scope["user"].username,
|
|
|
|
"peer_connected": False,
|
|
|
|
},
|
|
|
|
)
|
2022-01-13 14:08:21 +00:00
|
|
|
|
|
|
|
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 13:01:50 +00:00
|
|
|
|
|
|
|
# Encrypted messages are stored. They are served later when a user reconnects.
|
|
|
|
if message[0,27] == '-----BEGIN PGP MESSAGE-----':
|
|
|
|
self.PGP_message = message
|
|
|
|
await self.save_new_message()
|
2022-03-10 21:35:16 +00:00
|
|
|
|
|
|
|
peer_connected = await self.is_peer_connected()
|
2022-01-13 14:08:21 +00:00
|
|
|
await self.channel_layer.group_send(
|
|
|
|
self.room_group_name,
|
|
|
|
{
|
2022-02-17 19:50:10 +00:00
|
|
|
"type": "chatroom_message",
|
|
|
|
"message": message,
|
2022-03-10 21:35:16 +00:00
|
|
|
"nick": self.scope["user"].username,
|
|
|
|
"peer_connected": peer_connected,
|
2022-02-17 19:50:10 +00:00
|
|
|
},
|
2022-01-13 14:08:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-01-13 19:22:54 +00:00
|
|
|
|
2022-01-13 14:08:21 +00:00
|
|
|
await self.send(text_data=json.dumps({
|
2022-03-02 13:49:12 +00:00
|
|
|
"message": message,
|
2022-02-17 19:50:10 +00:00
|
|
|
"user_nick": nick,
|
2022-03-10 21:35:16 +00:00
|
|
|
"peer_connected": peer_connected,
|
2022-05-24 00:31:34 +00:00
|
|
|
"time":str(timezone.now()),
|
2022-01-13 14:08:21 +00:00
|
|
|
}))
|
|
|
|
|
2022-02-17 19:50:10 +00:00
|
|
|
pass
|