mirror of
https://gitlab.com/animath/si/plateforme.git
synced 2025-06-23 20:36:39 +02:00
Initialize chat interface
Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
46
chat/consumers.py
Normal file
46
chat/consumers.py
Normal file
@ -0,0 +1,46 @@
|
||||
# Copyright (C) 2024 by Animath
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
||||
from registration.models import Registration
|
||||
|
||||
|
||||
class ChatConsumer(AsyncJsonWebsocketConsumer):
|
||||
"""
|
||||
This consumer manages the websocket of the chat interface.
|
||||
"""
|
||||
async def connect(self) -> None:
|
||||
"""
|
||||
This function is called when a new websocket is trying to connect to the server.
|
||||
We accept only if this is a user of a team of the associated tournament, or a volunteer
|
||||
of the tournament.
|
||||
"""
|
||||
|
||||
# Fetch the registration of the current user
|
||||
user = self.scope['user']
|
||||
if user.is_anonymous:
|
||||
# User is not authenticated
|
||||
await self.close()
|
||||
return
|
||||
|
||||
reg = await Registration.objects.aget(user_id=user.id)
|
||||
self.registration = reg
|
||||
|
||||
# Accept the connection
|
||||
await self.accept()
|
||||
|
||||
async def disconnect(self, close_code) -> None:
|
||||
"""
|
||||
Called when the websocket got disconnected, for any reason.
|
||||
:param close_code: The error code.
|
||||
"""
|
||||
if self.scope['user'].is_anonymous:
|
||||
# User is not authenticated
|
||||
return
|
||||
|
||||
async def receive_json(self, content, **kwargs):
|
||||
"""
|
||||
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
|
Reference in New Issue
Block a user