diff --git a/baseapp-chats/baseapp_chats/graphql/mutations.py b/baseapp-chats/baseapp_chats/graphql/mutations.py index 7476cbb9..3e86d006 100644 --- a/baseapp-chats/baseapp_chats/graphql/mutations.py +++ b/baseapp-chats/baseapp_chats/graphql/mutations.py @@ -137,13 +137,16 @@ def mutate_and_get_payload(cls, root, info, profile_id, participants, is_group, image=image, ) - ChatRoomParticipant.objects.bulk_create( + created_participants = ChatRoomParticipant.objects.bulk_create( [ ChatRoomParticipant(profile=participant, room=room, accepted_at=timezone.now()) for participant in participants ] ) + room.participants_count = len(created_participants) + room.save() + return ChatRoomCreate( profile=profile, room=ChatRoomObjectType._meta.connection.Edge( diff --git a/baseapp-chats/baseapp_chats/graphql/object_types.py b/baseapp-chats/baseapp_chats/graphql/object_types.py index badcee31..08d2adcd 100644 --- a/baseapp-chats/baseapp_chats/graphql/object_types.py +++ b/baseapp-chats/baseapp_chats/graphql/object_types.py @@ -119,6 +119,19 @@ def get_node(cls, info, id): return None def resolve_all_messages(self, info, **kwargs): + if self.is_group: + profile = ( + info.context.user.current_profile + if hasattr(info.context.user, "current_profile") + else ( + info.context.user.profile.pk if hasattr(info.context.user, "profile") else None + ) + ) + participant = self.participants.filter(profile=profile).first() + if participant: + return self.messages.filter(created__gte=participant.accepted_at).order_by( + "-created" + ) return self.messages.all().order_by("-created") def resolve_is_archived(self, info, profile_id=None, **kwargs): diff --git a/baseapp-chats/baseapp_chats/tests/test_graphql_queries.py b/baseapp-chats/baseapp_chats/tests/test_graphql_queries.py index 1194f961..cf10278c 100644 --- a/baseapp-chats/baseapp_chats/tests/test_graphql_queries.py +++ b/baseapp-chats/baseapp_chats/tests/test_graphql_queries.py @@ -2,9 +2,11 @@ import pytest from baseapp_blocks.tests.factories import BlockFactory +from baseapp_core.graphql.testing.fixtures import graphql_query from baseapp_core.tests.factories import UserFactory from baseapp_profiles.tests.factories import ProfileFactory from django.utils import timezone +from freezegun import freeze_time from .factories import ChatRoomFactory, ChatRoomParticipantFactory, MessageFactory @@ -334,3 +336,51 @@ def test_user_cant_open_room_if_blocked(graphql_user_client, django_user_client) response = graphql_user_client(ROOM_GRAPHQL, variables={"roomId": room.relay_id}) content = response.json() assert content["data"]["chatRoom"] is None + + +def test_new_participant_cant_list_previous_messages_when_joining_group_room( + graphql_user_client, django_user_client, django_client +): + with freeze_time("2024-12-01 10:00:00") as frozen_time: + existing_profile_1 = ProfileFactory() + existing_profile_2 = ProfileFactory() + room = ChatRoomFactory(created_by=existing_profile_1.owner, participants_count=2) + + ChatRoomParticipantFactory( + profile=existing_profile_1, room=room, accepted_at=timezone.now() + ) + ChatRoomParticipantFactory( + profile=existing_profile_2, room=room, accepted_at=timezone.now() + ) + + MessageFactory(room=room, profile=existing_profile_1) + MessageFactory(room=room, profile=existing_profile_2) + + frozen_time.move_to(timezone.now() + timedelta(hours=1, minutes=5)) + + ChatRoomParticipantFactory( + profile=django_user_client.user.profile, room=room, accepted_at=timezone.now() + ) + room.participants_count = 3 + room.is_group = True + room.save() + room.refresh_from_db() + + MessageFactory(room=room, profile=existing_profile_1) + MessageFactory(room=room, profile=existing_profile_2) + + response = graphql_user_client(ROOM_GRAPHQL, variables={"roomId": room.relay_id}) + + content = response.json() + + assert len(content["data"]["chatRoom"]["allMessages"]["edges"]) == 2 + + django_client.force_login(existing_profile_1.owner) + + response = graphql_query( + ROOM_GRAPHQL, variables={"roomId": room.relay_id}, client=django_client + ) + + content = response.json() + + assert len(content["data"]["chatRoom"]["allMessages"]["edges"]) == 4 diff --git a/baseapp-chats/baseapp_chats/tests/test_graphql_subscriptions.py b/baseapp-chats/baseapp_chats/tests/test_graphql_subscriptions.py index 0ecd99c5..8f7c066d 100644 --- a/baseapp-chats/baseapp_chats/tests/test_graphql_subscriptions.py +++ b/baseapp-chats/baseapp_chats/tests/test_graphql_subscriptions.py @@ -181,7 +181,7 @@ async def test_user_recieves_message_count_update(django_user_client, graphql_ws resp = await client.receive(assert_id=sub_id, assert_type="next") - assert resp["data"]["chatRoomOnMessagesCountUpdate"]["profile"]["unreadMessagesCount"] == 1 - # Disconnect and wait the application to finish gracefully. await client.finalize() + + assert resp["data"]["chatRoomOnMessagesCountUpdate"]["profile"]["unreadMessagesCount"] == 1