diff --git a/chat/consumers.py b/chat/consumers.py index 4812af7..5fa30de 100644 --- a/chat/consumers.py +++ b/chat/consumers.py @@ -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,