def get_queryset(self): if 'chat_uuid' not in self.request.query_params: return NotFound('Required parameter missing') chat_uuid = self.request.query_params.get('chat_uuid') user = self.request.user try: chat = MessageParticipants.objects.get( chat_uuid=chat_uuid ) except MessageParticipants.DoesNotExist: raise NotFound('Chat not found.') try: Participant.objects.get(user=user, chat=chat, is_active=True) except Participant.DoesNotExist: raise NotFound('You are not a participant of this chat.') messages = Message.objects.filter( message_participant=chat ) if messages: number_of_participants = Participant.objects.filter( chat=chat, is_active=True ).count() set_read(messages.exclude(sender=user), user, number_of_participants==2) return messages
def get(self, request, id, format=None): try: message_queryset = Message.objects.filter(id=id) message = message_queryset[0] except Message.DoesNotExist: raise NotFound('This message does not exist') try: chat = MessageParticipants.objects.get( chat_uuid=message.message_participant.chat_uuid ) Participant.objects.get(user=request.user, chat=chat, is_active=True) except Participant.DoesNotExist: raise NotFound('You are not a participant of this chat.') if not message.sender == request.user: set_read(message_queryset, self.request.user, True) serializer = MessageSerializer(message, many=False, context={'request': request}) return Response(serializer.data, status=status.HTTP_200_OK)