diff --git a/chit_chat/viewsets.py b/chit_chat/viewsets.py index 60e0563..0f64db9 100644 --- a/chit_chat/viewsets.py +++ b/chit_chat/viewsets.py @@ -1,5 +1,7 @@ from rest_framework import viewsets, mixins from django.db.models import Max +from rest_framework.decorators import action +from rest_framework.response import Response from chit_chat.models import Room from chit_chat.serializers import RoomSerializer @@ -22,3 +24,9 @@ def get_queryset(self): 'members', ) return qs + + @action(methods=['post'], detail=True) + def viewed_all_messages(self, request, pk, *args, **kwargs): + room = self.get_object() + request.user.chat_room_messages_viewed.set(room.messages.all()) + return Response() diff --git a/tests/integration/test_chat.py b/tests/integration/test_chat.py index e8d2973..086188b 100644 --- a/tests/integration/test_chat.py +++ b/tests/integration/test_chat.py @@ -83,3 +83,18 @@ def test_rooms_are_ordered_by_latest_message(self): data = resp.json()['results'] assert data[0]['id'] == room_1.pk assert data[1]['id'] == room_2.pk + + def test_mark_all_messages_in_room_as_viewed(self): + room_1 = RoomFactory(members=[self.user]) + message_1 = MessageFactory(room=room_1) + room_2 = RoomFactory(members=[self.user]) + message_2 = MessageFactory(room=room_2) + + assert not message_1.users_who_viewed.filter(pk=self.user.pk).exists() + assert not message_2.users_who_viewed.filter(pk=self.user.pk).exists() + + resp = self.client.post(reverse('room-viewed-all-messages', args=(room_1.pk,))) + assert resp.status_code == 200 + + assert message_1.users_who_viewed.filter(pk=self.user.pk).exists() + assert not message_2.users_who_viewed.filter(pk=self.user.pk).exists()