Skip to content

Commit a026e9e

Browse files
authored
Merge pull request #92 from sobhanvn7/fix-last-message-bug
Fix last message bug
2 parents f832d65 + 8f58643 commit a026e9e

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ In this project **Dragonfly**, I am developing a fully functional open-source ch
2121
Thanks to valuable contributors 💖
2222

2323
- [@jitendra-ky](https://github.com/jitendra-ky)
24+
- [@sobhanvn7](https://github.com/sobhanvn7)

static/js/home.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ function rerender_contacts_view() {
168168
const profile = $('<div>').addClass('chat-item-profile')
169169
const details = $('<div>').addClass('chat-item-details')
170170
details.append($('<h4>').text(contact.email))
171-
details.append($('<p>').text('Hey, how are you?'))
171+
// Append the actual last message from the contact if available
172+
if (contact.last_message) {
173+
details.append($('<p>').text(contact.last_message))
174+
}
172175
contactContent.append(profile, details)
173176
contactElement.append(contactContent)
174177
chatList.append(contactElement)

zserver/serializers/user_profile.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
from django.db.models import Q # Import Q object for building complex queries using OR conditions
12
from rest_framework import serializers
23

3-
from zserver.models import Session, SignUpOTP, UnverifiedUserProfile, UserProfile, VerifyUserOTP
4+
from zserver.models import (
5+
Message,
6+
Session,
7+
SignUpOTP,
8+
UnverifiedUserProfile,
9+
UserProfile,
10+
VerifyUserOTP,
11+
)
412

513

614
# Serializer class for the UserProfile model
715
class UserProfileSerializer(serializers.ModelSerializer):
16+
# Add dynamic field for last message between the user and the contact
17+
last_message = serializers.SerializerMethodField()
818
class Meta:
919
"""Meta class to specify the model and fields to be serialized."""
1020

@@ -14,11 +24,27 @@ class Meta:
1424
"fullname",
1525
"email",
1626
"password",
27+
"last_message",
1728
] # Fields to be included in the serialization
1829
extra_kwargs = {
1930
"password": {"write_only": True}, # Make the password field write-only
2031
}
2132

33+
def get_last_message(self, contact: UserProfile) -> str:
34+
"""Retrieve the last message exchanged with the given contact."""
35+
# Get the authenticated user from serializer context
36+
user = self.context.get("user")
37+
if not user:
38+
return None
39+
40+
# Fetch the latest message between user and this contact
41+
last_msg = Message.objects.filter(
42+
Q(sender=user, receiver=contact) | Q(sender=contact, receiver=user),
43+
).order_by("-timestamp").first()
44+
45+
# Return message text if available, else None
46+
return last_msg.content if last_msg else None
47+
2248
def update(self, instance: UserProfile, validated_data: dict) -> UserProfile:
2349
"""Update an existing user profile."""
2450
instance.fullname = validated_data.get("fullname", instance.fullname)

zserver/views/message.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def get(self, request: Request) -> Response:
6767
contacts_sender = { msg.receiver for msg in Message.objects.filter(sender=user) }
6868
contacts_receiver = { msg.sender for msg in Message.objects.filter(receiver=user) }
6969
contacts = contacts_sender.union(contacts_receiver)
70-
serializer = UserProfileSerializer(contacts, many=True)
70+
# With this one — pass the authenticated user via context for custom field logic
71+
serializer = UserProfileSerializer(contacts, many=True, context={"user": user})
7172
return Response(serializer.data)
7273

7374

0 commit comments

Comments
 (0)