Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark messages as viewed #8

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions chit_chat/viewsets.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
15 changes: 15 additions & 0 deletions tests/integration/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()