Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-04-29 00:03:25 +02:00
parent a41c17576f
commit ed5944e044
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 7 additions and 7 deletions

View File

@ -3,7 +3,7 @@
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from django.contrib.auth.models import User
from django.db.models import Exists, OuterRef, Count, Q
from django.db.models import Count, Exists, OuterRef, Q
from registration.models import Registration
from .models import Channel, Message
@ -92,8 +92,8 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
'write_access': await self.write_channels.acontains(channel),
'unread_messages': channel.unread_messages,
}
async for channel in self.read_channels.prefetch_related('invited') \
.annotate(unread_messages=Count('messages', filter=~Q(messages__users_read=user))).all()
async for channel in self.read_channels.prefetch_related('invited')
.annotate(unread_messages=Count('messages', filter=~Q(messages__users_read=user))).all()
]
}
await self.send_json(message)
@ -159,10 +159,10 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
limit = min(limit, 200) # Fetch only maximum 200 messages at the time
messages = Message.objects \
.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()
.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()
await self.send_json({
'type': 'fetch_messages',
'channel_id': channel_id,