mirror of
https://gitlab.com/animath/si/plateforme.git
synced 2025-06-30 08:31:09 +02:00
@ -3,9 +3,10 @@
|
||||
|
||||
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
||||
from django.contrib.auth.models import User
|
||||
from participation.models import Team, Pool, Tournament
|
||||
from registration.models import Registration
|
||||
|
||||
from .models import Channel
|
||||
from .models import Channel, Message
|
||||
|
||||
|
||||
class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
@ -34,6 +35,10 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
# Accept the connection
|
||||
await self.accept()
|
||||
|
||||
channels = await Channel.get_accessible_channels(user, 'read')
|
||||
async for channel in channels.all():
|
||||
await self.channel_layer.group_add(f"chat-{channel.id}", self.channel_name)
|
||||
|
||||
async def disconnect(self, close_code) -> None:
|
||||
"""
|
||||
Called when the websocket got disconnected, for any reason.
|
||||
@ -43,6 +48,10 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
# User is not authenticated
|
||||
return
|
||||
|
||||
channels = await Channel.get_accessible_channels(self.scope['user'], 'read')
|
||||
async for channel in channels.all():
|
||||
await self.channel_layer.group_discard(f"chat-{channel.id}", self.channel_name)
|
||||
|
||||
async def receive_json(self, content, **kwargs):
|
||||
"""
|
||||
Called when the client sends us some data, parsed as JSON.
|
||||
@ -51,6 +60,8 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
match content['type']:
|
||||
case 'fetch_channels':
|
||||
await self.fetch_channels()
|
||||
case 'send_message':
|
||||
await self.receive_message(content)
|
||||
case unknown:
|
||||
print("Unknown message type:", unknown)
|
||||
|
||||
@ -59,7 +70,6 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
|
||||
read_channels = await Channel.get_accessible_channels(user, 'read')
|
||||
write_channels = await Channel.get_accessible_channels(user, 'write')
|
||||
print([channel async for channel in write_channels.all()])
|
||||
message = {
|
||||
'type': 'fetch_channels',
|
||||
'channels': [
|
||||
@ -73,3 +83,29 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
]
|
||||
}
|
||||
await self.send_json(message)
|
||||
|
||||
async def receive_message(self, message: dict) -> None:
|
||||
user = self.scope['user']
|
||||
channel = await Channel.objects.prefetch_related('tournament__pools__juries', 'pool', 'team', 'invited') \
|
||||
.aget(id=message['channel_id'])
|
||||
write_channels = await Channel.get_accessible_channels(user, 'write')
|
||||
if not await write_channels.acontains(channel):
|
||||
return
|
||||
|
||||
message = await Message.objects.acreate(
|
||||
author=user,
|
||||
channel=channel,
|
||||
content=message['content'],
|
||||
)
|
||||
|
||||
await self.channel_layer.group_send(f'chat-{channel.id}', {
|
||||
'type': 'chat.send_message',
|
||||
'id': message.id,
|
||||
'timestamp': message.created_at.isoformat(),
|
||||
'author': await message.aget_author_name(),
|
||||
'content': message.content,
|
||||
})
|
||||
|
||||
async def chat_send_message(self, message) -> None:
|
||||
await self.send_json({'type': 'send_message', 'id': message['id'], 'timestamp': message['timestamp'],
|
||||
'author': message['author'], 'content': message['content']})
|
||||
|
Reference in New Issue
Block a user