ft_transcendence/chat/consumers.py

100 lines
2.4 KiB
Python
Raw Normal View History

2023-11-27 09:10:06 -05:00
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
2023-12-27 10:14:39 -05:00
from .models import ChatMemberModel, ChatMessageModel
2023-12-20 17:48:52 -05:00
from profiles.models import BlockModel
2023-12-27 10:14:39 -05:00
2023-12-15 14:32:43 -05:00
import time
2023-12-27 10:14:39 -05:00
import json
2023-11-27 09:10:06 -05:00
class ChatConsumer(WebsocketConsumer):
2023-12-15 14:32:43 -05:00
2023-12-27 10:14:39 -05:00
def connect(self):
2023-12-15 14:32:43 -05:00
user = self.scope["user"]
if (user.is_anonymous or not user.is_authenticated):
return
2023-12-27 10:14:39 -05:00
channel_id : int = int(self.scope['url_route']['kwargs']['chat_id'])
self.room_group_name = f'chat{channel_id}'
if ChatMemberModel.objects.filter(member_id=user.pk, channel_id=int(channel_id)).count() != 1:
2023-12-15 14:32:43 -05:00
return
if (self.channel_layer == None):
return
2023-11-27 09:10:06 -05:00
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
)
self.accept()
2023-12-15 14:32:43 -05:00
def receive(self, text_data=None, bytes_data=None):
2023-12-27 10:14:39 -05:00
2023-12-15 14:32:43 -05:00
if text_data == None:
return
2023-12-12 04:05:13 -05:00
user = self.scope["user"]
2023-12-15 14:32:43 -05:00
if (user.is_anonymous or not user.is_authenticated):
return
2023-12-12 04:05:13 -05:00
2023-12-27 10:14:39 -05:00
text_data_json: dict = json.loads(text_data)
message = text_data_json.get('message')
if (message is None):
return
receivers_id = text_data_json.get('receivers_id')
if (receivers_id is None):
return
channel_id : int = int(self.scope['url_route']['kwargs']['chat_id'])
if ChatMemberModel.objects.filter(member_id = user.pk, channel_id = channel_id).count() != 1:
2023-12-15 14:32:43 -05:00
return
if (self.channel_layer == None):
return
2023-12-27 10:14:39 -05:00
message_time: int = int(time.time() * 1000)
2023-12-20 17:48:52 -05:00
if (len(receivers_id) == 1 and
BlockModel.objects.filter(blocker=user.pk, blocked=receivers_id[0]) or
BlockModel.objects.filter(blocker=receivers_id[0], blocked=user.pk)
):
return
2023-11-27 09:10:06 -05:00
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type':'chat_message',
2023-12-15 14:32:43 -05:00
'author_id':user.pk,
'content':message,
'time':message_time,
2023-11-27 09:10:06 -05:00
}
)
2023-12-15 14:32:43 -05:00
2023-12-27 10:14:39 -05:00
new_message = ChatMessageModel(channel_id = channel_id, author_id = user.pk, content = message, time = message_time).save()
2023-11-27 09:10:06 -05:00
def chat_message(self, event):
2023-12-12 04:05:13 -05:00
user = self.scope["user"]
2023-12-15 14:32:43 -05:00
if (user.is_anonymous or not user.is_authenticated):
return
2023-12-27 10:14:39 -05:00
channel_id : int = int(self.scope['url_route']['kwargs']['chat_id'])
if ChatMemberModel.objects.filter(member_id = user.pk, channel_id = channel_id).count() != 1:
2023-12-15 14:32:43 -05:00
return
2023-12-12 04:05:13 -05:00
2023-11-27 09:10:06 -05:00
self.send(text_data=json.dumps({
'type':'chat',
2023-12-15 14:32:43 -05:00
'author_id':event['author_id'],
'content':event['content'],
'time': event['time'],
2023-11-27 09:10:06 -05:00
}))