2023-11-27 09:10:06 -05:00
|
|
|
import json
|
|
|
|
from channels.generic.websocket import WebsocketConsumer
|
|
|
|
from asgiref.sync import async_to_sync
|
2023-12-15 14:32:43 -05:00
|
|
|
from .models import MemberModel, MessageModel
|
|
|
|
import time
|
2023-11-27 09:10:06 -05:00
|
|
|
|
|
|
|
class ChatConsumer(WebsocketConsumer):
|
|
|
|
def connect(self):
|
2023-12-15 14:32:43 -05:00
|
|
|
channel_id : str = self.scope['path'].split('/')[3]
|
|
|
|
|
|
|
|
self.room_group_name = 'chat' + channel_id
|
|
|
|
|
|
|
|
user = self.scope["user"]
|
|
|
|
if (user.is_anonymous or not user.is_authenticated):
|
|
|
|
return
|
|
|
|
|
|
|
|
if MemberModel.objects.filter(member_id=user.pk, channel_id=int(channel_id)).count() != 1:
|
|
|
|
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):
|
|
|
|
if text_data == None:
|
|
|
|
return
|
|
|
|
|
2023-11-27 09:10:06 -05:00
|
|
|
text_data_json = json.loads(text_data)
|
|
|
|
message = text_data_json['message']
|
|
|
|
|
2023-12-15 14:32:43 -05:00
|
|
|
channel_id : int = int(self.scope['path'].split('/')[3])
|
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-15 14:32:43 -05:00
|
|
|
if MemberModel.objects.filter(member_id=user.pk, channel_id=channel_id).count() != 1:
|
|
|
|
return
|
|
|
|
|
|
|
|
if (self.channel_layer == None):
|
|
|
|
return
|
|
|
|
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
message_time : int = int(time.time() * 1000)
|
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
|
|
|
|
|
|
|
new_message = MessageModel()
|
|
|
|
new_message.channel_id = channel_id
|
|
|
|
new_message.author_id = user.pk
|
|
|
|
new_message.content = message
|
|
|
|
new_message.time = message_time
|
|
|
|
new_message.save()
|
|
|
|
|
2023-11-27 09:10:06 -05:00
|
|
|
|
|
|
|
def chat_message(self, event):
|
|
|
|
|
2023-12-15 14:32:43 -05:00
|
|
|
channel_id : int = int(self.scope['path'].split('/')[3])
|
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
|
|
|
|
|
|
|
|
if MemberModel.objects.filter(member_id=user.pk, channel_id=channel_id).count() != 1:
|
|
|
|
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
|
|
|
}))
|