2024-04-27 06:57:01 +00:00
|
|
|
# Copyright (C) 2024 by Animath
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
2024-04-27 07:53:55 +00:00
|
|
|
from django.contrib.auth.models import User
|
2024-04-28 22:03:25 +00:00
|
|
|
from django.db.models import Count, Exists, OuterRef, Q
|
2024-04-27 06:57:01 +00:00
|
|
|
from registration.models import Registration
|
|
|
|
|
2024-04-27 10:59:50 +00:00
|
|
|
from .models import Channel, Message
|
2024-04-27 07:53:55 +00:00
|
|
|
|
2024-04-27 06:57:01 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2024-04-27 07:53:55 +00:00
|
|
|
if '_fake_user_id' in self.scope['session']:
|
|
|
|
self.scope['user'] = await User.objects.aget(pk=self.scope['session']['_fake_user_id'])
|
2024-04-27 06:57:01 +00:00
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
2024-04-28 21:25:15 +00:00
|
|
|
self.read_channels = await Channel.get_accessible_channels(user, 'read')
|
|
|
|
self.write_channels = await Channel.get_accessible_channels(user, 'write')
|
|
|
|
|
|
|
|
async for channel in self.read_channels.all():
|
2024-04-27 10:59:50 +00:00
|
|
|
await self.channel_layer.group_add(f"chat-{channel.id}", self.channel_name)
|
2024-04-28 13:35:32 +00:00
|
|
|
await self.channel_layer.group_add(f"user-{user.id}", self.channel_name)
|
2024-04-27 10:59:50 +00:00
|
|
|
|
2024-04-27 06:57:01 +00:00
|
|
|
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
|
|
|
|
|
2024-04-28 21:25:15 +00:00
|
|
|
async for channel in self.read_channels.all():
|
2024-04-27 10:59:50 +00:00
|
|
|
await self.channel_layer.group_discard(f"chat-{channel.id}", self.channel_name)
|
2024-04-28 13:35:32 +00:00
|
|
|
await self.channel_layer.group_discard(f"user-{self.scope['user'].id}", self.channel_name)
|
2024-04-27 10:59:50 +00:00
|
|
|
|
2024-04-27 06:57:01 +00:00
|
|
|
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.
|
|
|
|
"""
|
2024-04-27 07:53:55 +00:00
|
|
|
match content['type']:
|
|
|
|
case 'fetch_channels':
|
|
|
|
await self.fetch_channels()
|
2024-04-27 10:59:50 +00:00
|
|
|
case 'send_message':
|
2024-04-28 14:56:30 +00:00
|
|
|
await self.receive_message(**content)
|
|
|
|
case 'edit_message':
|
|
|
|
await self.edit_message(**content)
|
|
|
|
case 'delete_message':
|
|
|
|
await self.delete_message(**content)
|
2024-04-27 11:27:27 +00:00
|
|
|
case 'fetch_messages':
|
2024-04-27 12:12:08 +00:00
|
|
|
await self.fetch_messages(**content)
|
2024-04-28 21:25:15 +00:00
|
|
|
case 'mark_read':
|
|
|
|
await self.mark_read(**content)
|
2024-04-28 13:35:32 +00:00
|
|
|
case 'start_private_chat':
|
2024-04-28 14:56:30 +00:00
|
|
|
await self.start_private_chat(**content)
|
2024-04-27 07:53:55 +00:00
|
|
|
case unknown:
|
|
|
|
print("Unknown message type:", unknown)
|
|
|
|
|
|
|
|
async def fetch_channels(self) -> None:
|
|
|
|
user = self.scope['user']
|
|
|
|
|
|
|
|
message = {
|
|
|
|
'type': 'fetch_channels',
|
|
|
|
'channels': [
|
|
|
|
{
|
|
|
|
'id': channel.id,
|
2024-04-28 13:35:32 +00:00
|
|
|
'name': channel.get_visible_name(user),
|
2024-04-28 11:50:04 +00:00
|
|
|
'category': channel.category,
|
2024-04-27 07:53:55 +00:00
|
|
|
'read_access': True,
|
2024-04-28 21:25:15 +00:00
|
|
|
'write_access': await self.write_channels.acontains(channel),
|
|
|
|
'unread_messages': channel.unread_messages,
|
2024-04-27 07:53:55 +00:00
|
|
|
}
|
2024-04-28 22:03:25 +00:00
|
|
|
async for channel in self.read_channels.prefetch_related('invited')
|
|
|
|
.annotate(unread_messages=Count('messages', filter=~Q(messages__users_read=user))).all()
|
2024-04-27 07:53:55 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
await self.send_json(message)
|
2024-04-27 10:59:50 +00:00
|
|
|
|
2024-04-28 14:56:30 +00:00
|
|
|
async def receive_message(self, channel_id: int, content: str, **kwargs) -> None:
|
2024-04-27 10:59:50 +00:00
|
|
|
user = self.scope['user']
|
|
|
|
channel = await Channel.objects.prefetch_related('tournament__pools__juries', 'pool', 'team', 'invited') \
|
2024-04-28 14:56:30 +00:00
|
|
|
.aget(id=channel_id)
|
2024-04-28 21:25:15 +00:00
|
|
|
if not await self.write_channels.acontains(channel):
|
2024-04-27 10:59:50 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
message = await Message.objects.acreate(
|
|
|
|
author=user,
|
|
|
|
channel=channel,
|
2024-04-28 14:56:30 +00:00
|
|
|
content=content,
|
2024-04-27 10:59:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
await self.channel_layer.group_send(f'chat-{channel.id}', {
|
|
|
|
'type': 'chat.send_message',
|
|
|
|
'id': message.id,
|
2024-04-27 11:27:27 +00:00
|
|
|
'channel_id': channel.id,
|
2024-04-27 10:59:50 +00:00
|
|
|
'timestamp': message.created_at.isoformat(),
|
2024-04-28 13:35:32 +00:00
|
|
|
'author_id': message.author_id,
|
2024-04-27 10:59:50 +00:00
|
|
|
'author': await message.aget_author_name(),
|
|
|
|
'content': message.content,
|
|
|
|
})
|
|
|
|
|
2024-04-28 14:56:30 +00:00
|
|
|
async def edit_message(self, message_id: int, content: str, **kwargs) -> None:
|
|
|
|
message = await Message.objects.aget(id=message_id)
|
|
|
|
user = self.scope['user']
|
|
|
|
if user.id != message.author_id and not user.is_superuser:
|
|
|
|
return
|
|
|
|
|
|
|
|
message.content = content
|
|
|
|
await message.asave()
|
|
|
|
|
|
|
|
await self.channel_layer.group_send(f'chat-{message.channel_id}', {
|
|
|
|
'type': 'chat.edit_message',
|
|
|
|
'id': message_id,
|
|
|
|
'channel_id': message.channel_id,
|
|
|
|
'content': content,
|
|
|
|
})
|
|
|
|
|
|
|
|
async def delete_message(self, message_id: int, **kwargs) -> None:
|
|
|
|
message = await Message.objects.aget(id=message_id)
|
|
|
|
user = self.scope['user']
|
|
|
|
if user.id != message.author_id and not user.is_superuser:
|
|
|
|
return
|
|
|
|
|
|
|
|
await message.adelete()
|
|
|
|
|
|
|
|
await self.channel_layer.group_send(f'chat-{message.channel_id}', {
|
|
|
|
'type': 'chat.delete_message',
|
|
|
|
'id': message_id,
|
|
|
|
'channel_id': message.channel_id,
|
|
|
|
})
|
|
|
|
|
2024-04-27 12:12:08 +00:00
|
|
|
async def fetch_messages(self, channel_id: int, offset: int = 0, limit: int = 50, **_kwargs) -> None:
|
2024-04-27 11:27:27 +00:00
|
|
|
channel = await Channel.objects.aget(id=channel_id)
|
2024-04-28 21:25:15 +00:00
|
|
|
if not await self.read_channels.acontains(channel):
|
2024-04-27 11:27:27 +00:00
|
|
|
return
|
|
|
|
|
2024-04-27 12:12:08 +00:00
|
|
|
limit = min(limit, 200) # Fetch only maximum 200 messages at the time
|
|
|
|
|
2024-04-28 21:25:15 +00:00
|
|
|
messages = Message.objects \
|
2024-04-28 22:03:25 +00:00
|
|
|
.filter(channel=channel) \
|
|
|
|
.annotate(read=Exists(User.objects.filter(pk=self.scope['user'].pk)
|
|
|
|
.filter(pk=OuterRef('users_read')))) \
|
|
|
|
.order_by('-created_at')[offset:offset + limit].all()
|
2024-04-27 11:27:27 +00:00
|
|
|
await self.send_json({
|
|
|
|
'type': 'fetch_messages',
|
|
|
|
'channel_id': channel_id,
|
2024-04-27 12:12:08 +00:00
|
|
|
'messages': list(reversed([
|
2024-04-27 11:27:27 +00:00
|
|
|
{
|
|
|
|
'id': message.id,
|
|
|
|
'timestamp': message.created_at.isoformat(),
|
2024-04-28 13:35:32 +00:00
|
|
|
'author_id': message.author_id,
|
2024-04-27 11:27:27 +00:00
|
|
|
'author': await message.aget_author_name(),
|
|
|
|
'content': message.content,
|
2024-04-28 21:25:15 +00:00
|
|
|
'read': message.read,
|
2024-04-27 11:27:27 +00:00
|
|
|
}
|
|
|
|
async for message in messages
|
2024-04-27 12:12:08 +00:00
|
|
|
]))
|
2024-04-27 11:27:27 +00:00
|
|
|
})
|
|
|
|
|
2024-04-28 21:25:15 +00:00
|
|
|
async def mark_read(self, message_ids: list[int], **_kwargs) -> None:
|
|
|
|
messages = Message.objects.filter(id__in=message_ids)
|
|
|
|
async for message in messages.all():
|
|
|
|
await message.users_read.aadd(self.scope['user'])
|
|
|
|
|
|
|
|
unread_messages_by_channel = Message.objects.exclude(users_read=self.scope['user']).values('channel_id') \
|
|
|
|
.annotate(unread_messages=Count('channel_id'))
|
|
|
|
|
|
|
|
await self.send_json({
|
|
|
|
'type': 'mark_read',
|
|
|
|
'messages': [{'id': message.id, 'channel_id': message.channel_id} async for message in messages.all()],
|
|
|
|
'unread_messages': {group['channel_id']: group['unread_messages']
|
|
|
|
async for group in unread_messages_by_channel.all()},
|
|
|
|
})
|
|
|
|
|
2024-04-28 14:56:30 +00:00
|
|
|
async def start_private_chat(self, user_id: int, **kwargs) -> None:
|
2024-04-28 13:35:32 +00:00
|
|
|
user = self.scope['user']
|
|
|
|
other_user = await User.objects.aget(id=user_id)
|
|
|
|
channel_qs = Channel.objects.filter(private=True).filter(invited=user).filter(invited=other_user)
|
|
|
|
if not await channel_qs.aexists():
|
|
|
|
channel = await Channel.objects.acreate(
|
|
|
|
name=f"{user.first_name} {user.last_name}, {other_user.first_name} {other_user.last_name}",
|
|
|
|
category=Channel.ChannelCategory.PRIVATE,
|
|
|
|
private=True,
|
|
|
|
)
|
|
|
|
await channel.invited.aset([user, other_user])
|
|
|
|
|
|
|
|
await self.channel_layer.group_add(f"chat-{channel.id}", self.channel_name)
|
2024-04-28 13:37:45 +00:00
|
|
|
|
|
|
|
if user != other_user:
|
|
|
|
await self.channel_layer.group_send(f"user-{other_user.id}", {
|
|
|
|
'type': 'chat.start_private_chat',
|
|
|
|
'channel': {
|
|
|
|
'id': channel.id,
|
|
|
|
'name': f"{user.first_name} {user.last_name}",
|
|
|
|
'category': channel.category,
|
|
|
|
'read_access': True,
|
|
|
|
'write_access': True,
|
|
|
|
}
|
|
|
|
})
|
2024-04-28 13:35:32 +00:00
|
|
|
else:
|
|
|
|
channel = await channel_qs.afirst()
|
|
|
|
|
|
|
|
await self.channel_layer.group_send(f"user-{user.id}", {
|
|
|
|
'type': 'chat.start_private_chat',
|
|
|
|
'channel': {
|
|
|
|
'id': channel.id,
|
|
|
|
'name': f"{other_user.first_name} {other_user.last_name}",
|
|
|
|
'category': channel.category,
|
|
|
|
'read_access': True,
|
|
|
|
'write_access': True,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-04-27 10:59:50 +00:00
|
|
|
async def chat_send_message(self, message) -> None:
|
2024-04-27 11:27:27 +00:00
|
|
|
await self.send_json({'type': 'send_message', 'id': message['id'], 'channel_id': message['channel_id'],
|
|
|
|
'timestamp': message['timestamp'], 'author': message['author'],
|
|
|
|
'content': message['content']})
|
2024-04-28 13:35:32 +00:00
|
|
|
|
2024-04-28 14:56:30 +00:00
|
|
|
async def chat_edit_message(self, message) -> None:
|
|
|
|
print(message)
|
|
|
|
await self.send_json({'type': 'edit_message', 'id': message['id'], 'channel_id': message['channel_id'],
|
|
|
|
'content': message['content']})
|
|
|
|
|
|
|
|
async def chat_delete_message(self, message) -> None:
|
|
|
|
await self.send_json({'type': 'delete_message', 'id': message['id'], 'channel_id': message['channel_id']})
|
|
|
|
|
2024-04-28 13:35:32 +00:00
|
|
|
async def chat_start_private_chat(self, message) -> None:
|
|
|
|
await self.channel_layer.group_add(f"chat-{message['channel']['id']}", self.channel_name)
|
|
|
|
await self.send_json({'type': 'start_private_chat', 'channel': message['channel']})
|