|
2 | 2 |
|
3 | 3 | from rest_framework import status |
4 | 4 | from rest_framework.generics import ( |
| 5 | + GenericAPIView, |
5 | 6 | ListAPIView, |
6 | 7 | ListCreateAPIView, |
7 | 8 | RetrieveAPIView, |
8 | 9 | ) |
9 | | - |
| 10 | +from drf_yasg.utils import swagger_auto_schema |
| 11 | +from drf_yasg import openapi |
10 | 12 | from rest_framework.permissions import IsAuthenticated |
11 | 13 | from rest_framework.response import Response |
12 | 14 |
|
@@ -190,3 +192,37 @@ def get_queryset(self): |
190 | 192 | return get_all_files(messages) |
191 | 193 | except ProjectChat.DoesNotExist: |
192 | 194 | return UserFile.objects.none() |
| 195 | + |
| 196 | + |
| 197 | +class HasChatUnreadsView(GenericAPIView): |
| 198 | + """Returns True if user has unread messages""" |
| 199 | + |
| 200 | + permission_classes = [IsAuthenticated] |
| 201 | + |
| 202 | + @swagger_auto_schema( |
| 203 | + responses={ |
| 204 | + 200: openapi.Response( |
| 205 | + description="Gives you True or False whether user has unread messages or not", |
| 206 | + schema=openapi.Schema( |
| 207 | + type=openapi.TYPE_OBJECT, |
| 208 | + properties={ |
| 209 | + "has_unreads": openapi.Schema( |
| 210 | + type=openapi.TYPE_BOOLEAN, |
| 211 | + description="True if user has unread messages", |
| 212 | + ) |
| 213 | + }, |
| 214 | + ), |
| 215 | + ) |
| 216 | + } |
| 217 | + ) |
| 218 | + def get(self, request, *args, **kwargs): |
| 219 | + user = request.user |
| 220 | + # get all user chats |
| 221 | + direct_messages = user.direct_chats.all().messages.all() |
| 222 | + project_messages = user.get_project_chats().messages.all() |
| 223 | + |
| 224 | + has_direct_messages_unread = direct_messages.filter(is_read=False).exists() |
| 225 | + has_project_messages_unread = project_messages.filter(is_read=False).exists() |
| 226 | + return Response( |
| 227 | + {"has_unreads": has_direct_messages_unread or has_project_messages_unread} |
| 228 | + ) |
0 commit comments