1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-24 02:28:49 +02:00

Manage private chats

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello
2024-04-28 15:35:32 +02:00
parent ddd2280ae4
commit 2438bb9bcc
3 changed files with 67 additions and 6 deletions

View File

@ -37,6 +37,7 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
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)
await self.channel_layer.group_add(f"user-{user.id}", self.channel_name)
async def disconnect(self, close_code) -> None:
"""
@ -50,6 +51,7 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
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)
await self.channel_layer.group_discard(f"user-{self.scope['user'].id}", self.channel_name)
async def receive_json(self, content, **kwargs):
"""
@ -63,6 +65,8 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
await self.receive_message(content)
case 'fetch_messages':
await self.fetch_messages(**content)
case 'start_private_chat':
await self.start_private_chat(content['user_id'])
case unknown:
print("Unknown message type:", unknown)
@ -76,12 +80,12 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
'channels': [
{
'id': channel.id,
'name': channel.name,
'name': channel.get_visible_name(user),
'category': channel.category,
'read_access': True,
'write_access': await write_channels.acontains(channel),
}
async for channel in read_channels.all()
async for channel in read_channels.prefetch_related('invited').all()
]
}
await self.send_json(message)
@ -105,6 +109,7 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
'id': message.id,
'channel_id': channel.id,
'timestamp': message.created_at.isoformat(),
'author_id': message.author_id,
'author': await message.aget_author_name(),
'content': message.content,
})
@ -125,6 +130,7 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
{
'id': message.id,
'timestamp': message.created_at.isoformat(),
'author_id': message.author_id,
'author': await message.aget_author_name(),
'content': message.content,
}
@ -132,7 +138,50 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
]))
})
async def start_private_chat(self, user_id: int) -> None:
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 channel.asave()
await self.channel_layer.group_add(f"chat-{channel.id}", self.channel_name)
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,
}
})
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,
}
})
async def chat_send_message(self, message) -> None:
await self.send_json({'type': 'send_message', 'id': message['id'], 'channel_id': message['channel_id'],
'timestamp': message['timestamp'], 'author': message['author'],
'content': message['content']})
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']})