From ae2a1afccaf5aaaa8ca934d8d4a4e8900891f421 Mon Sep 17 00:00:00 2001 From: Ronan-Fernandes Date: Tue, 10 Dec 2024 12:21:41 -0300 Subject: [PATCH 1/4] Create chat group --- .../migrations/0004_chatroom_is_group.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 baseapp-chats/baseapp_chats/migrations/0004_chatroom_is_group.py diff --git a/baseapp-chats/baseapp_chats/migrations/0004_chatroom_is_group.py b/baseapp-chats/baseapp_chats/migrations/0004_chatroom_is_group.py new file mode 100644 index 00000000..6fe25a9f --- /dev/null +++ b/baseapp-chats/baseapp_chats/migrations/0004_chatroom_is_group.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.9 on 2024-12-09 23:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("baseapp_chats", "0003_alter_unreadmessagecount_room"), + ] + + operations = [ + migrations.AddField( + model_name="chatroom", + name="is_group", + field=models.BooleanField(default=False), + ), + ] From 8c386cc10db970b64ddfcd30bb3d338c0d649a13 Mon Sep 17 00:00:00 2001 From: Ronan-Fernandes Date: Sat, 14 Dec 2024 12:39:43 -0300 Subject: [PATCH 2/4] PR updates --- .../migrations/0004_chatroom_is_group.py | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 baseapp-chats/baseapp_chats/migrations/0004_chatroom_is_group.py diff --git a/baseapp-chats/baseapp_chats/migrations/0004_chatroom_is_group.py b/baseapp-chats/baseapp_chats/migrations/0004_chatroom_is_group.py deleted file mode 100644 index 6fe25a9f..00000000 --- a/baseapp-chats/baseapp_chats/migrations/0004_chatroom_is_group.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 5.0.9 on 2024-12-09 23:37 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ("baseapp_chats", "0003_alter_unreadmessagecount_room"), - ] - - operations = [ - migrations.AddField( - model_name="chatroom", - name="is_group", - field=models.BooleanField(default=False), - ), - ] From 91eda3addc4aafb0e55b3089cc239e6090512cdb Mon Sep 17 00:00:00 2001 From: gh-pedrotpo Date: Mon, 16 Dec 2024 12:48:11 -0500 Subject: [PATCH 3/4] feat: filter past messages for new chat group participant --- .../baseapp_chats/graphql/mutations.py | 3 ++ .../baseapp_chats/graphql/object_types.py | 13 +++++ .../tests/test_graphql_queries.py | 50 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/baseapp-chats/baseapp_chats/graphql/mutations.py b/baseapp-chats/baseapp_chats/graphql/mutations.py index 7476cbb9..05115d64 100644 --- a/baseapp-chats/baseapp_chats/graphql/mutations.py +++ b/baseapp-chats/baseapp_chats/graphql/mutations.py @@ -144,6 +144,9 @@ def mutate_and_get_payload(cls, root, info, profile_id, participants, is_group, ] ) + room.participants_count = len(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 From 80101a498422e32b52e4b6c25c726412155b952c Mon Sep 17 00:00:00 2001 From: Pedro Tiburcio Date: Tue, 7 Jan 2025 09:20:26 -0500 Subject: [PATCH 4/4] fix: tests and PR suggestions --- baseapp-chats/baseapp_chats/graphql/mutations.py | 4 ++-- .../baseapp_chats/tests/test_graphql_subscriptions.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/baseapp-chats/baseapp_chats/graphql/mutations.py b/baseapp-chats/baseapp_chats/graphql/mutations.py index 05115d64..3e86d006 100644 --- a/baseapp-chats/baseapp_chats/graphql/mutations.py +++ b/baseapp-chats/baseapp_chats/graphql/mutations.py @@ -137,14 +137,14 @@ 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(participants) + room.participants_count = len(created_participants) room.save() return ChatRoomCreate( 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