Skip to content

Commit c48d243

Browse files
authored
Merge pull request #215 from PROCOLLAB-github/feature/unread-messages-route
Unread messages view
2 parents b449684 + 4b953b8 commit c48d243

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

chats/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from chats.views import (
44
DirectChatList,
55
DirectChatMessageList,
6+
HasChatUnreadsView,
67
ProjectChatList,
78
ProjectChatMessageList,
89
ProjectChatDetail,
@@ -38,4 +39,9 @@
3839
ProjectChatFileList.as_view(),
3940
name="project-chat-files",
4041
),
42+
path(
43+
"has-unreads/",
44+
HasChatUnreadsView.as_view(),
45+
name="has-chat-unreads",
46+
),
4147
]

chats/views.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
from rest_framework import status
44
from rest_framework.generics import (
5+
GenericAPIView,
56
ListAPIView,
67
ListCreateAPIView,
78
RetrieveAPIView,
89
)
9-
10+
from drf_yasg.utils import swagger_auto_schema
11+
from drf_yasg import openapi
1012
from rest_framework.permissions import IsAuthenticated
1113
from rest_framework.response import Response
1214

@@ -190,3 +192,37 @@ def get_queryset(self):
190192
return get_all_files(messages)
191193
except ProjectChat.DoesNotExist:
192194
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+
)

users/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ def calculate_ordering_score(self) -> int:
134134
return score
135135

136136
def get_project_chats(self) -> list:
137-
collaborations = self.collaborations.all()
137+
collaborations = self.collaborations.prefetch_related(
138+
"project__project_chats"
139+
).all()
138140
projects = []
139141
for collaboration in collaborations:
140142
projects.extend(list(collaboration.project.project_chats.all()))

0 commit comments

Comments
 (0)