Skip to content
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
5 changes: 4 additions & 1 deletion baseapp-chats/baseapp_chats/graphql/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions baseapp-chats/baseapp_chats/graphql/object_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
50 changes: 50 additions & 0 deletions baseapp-chats/baseapp_chats/tests/test_graphql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading