mirror of
https://gitlab.com/animath/si/plateforme.git
synced 2025-08-03 04:43:59 +02:00
Manage channels permissions
Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
||||
from django.contrib.auth.models import User
|
||||
from registration.models import Registration
|
||||
|
||||
from .models import Channel
|
||||
|
||||
|
||||
class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
"""
|
||||
@@ -15,6 +18,8 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
We accept only if this is a user of a team of the associated tournament, or a volunteer
|
||||
of the tournament.
|
||||
"""
|
||||
if '_fake_user_id' in self.scope['session']:
|
||||
self.scope['user'] = await User.objects.aget(pk=self.scope['session']['_fake_user_id'])
|
||||
|
||||
# Fetch the registration of the current user
|
||||
user = self.scope['user']
|
||||
@@ -43,4 +48,28 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
Called when the client sends us some data, parsed as JSON.
|
||||
:param content: The sent data, decoded from JSON text. Must content a `type` field.
|
||||
"""
|
||||
# TODO Process chat protocol
|
||||
match content['type']:
|
||||
case 'fetch_channels':
|
||||
await self.fetch_channels()
|
||||
case unknown:
|
||||
print("Unknown message type:", unknown)
|
||||
|
||||
async def fetch_channels(self) -> None:
|
||||
user = self.scope['user']
|
||||
|
||||
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': [
|
||||
{
|
||||
'id': channel.id,
|
||||
'name': channel.name,
|
||||
'read_access': True,
|
||||
'write_access': await write_channels.acontains(channel),
|
||||
}
|
||||
async for channel in read_channels.all()
|
||||
]
|
||||
}
|
||||
await self.send_json(message)
|
||||
|
Reference in New Issue
Block a user