From 80f41ba21f26bc473648efe66f950ab34ea50052 Mon Sep 17 00:00:00 2001 From: "Jiaqi (Jacky) Wang" Date: Sat, 27 Apr 2024 16:22:02 -0400 Subject: [PATCH 1/2] fix search to show in correct order and exclude urself --- friends/views.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/friends/views.py b/friends/views.py index 12720c87e..13826779f 100644 --- a/friends/views.py +++ b/friends/views.py @@ -2,6 +2,8 @@ from django.http import JsonResponse from django.db.models import Q from django.contrib.auth.models import User +from django.db.models import Value as V +from django.db.models.functions import Concat from student.models import Student from friends.models import FriendRequest @@ -62,20 +64,24 @@ def search_friends(request, query): Searches for friends based on the given query and returns a list of matches. Excludes existing friends. """ logged_in_student = get_object_or_404(Student, user=request.user) - search_results = ( - User.objects.filter( - Q(first_name__icontains=query) - | Q(last_name__icontains=query) - | Q(username__icontains=query) - ) - .select_related("student") - .only("email", "first_name", "last_name", "username", "student__img_url") - ) + + full_name = User.objects.annotate(full_name=Concat('first_name', V(' '), 'last_name')) + + full_name_starts_with = full_name.filter(full_name__startswith=query) + + full_name_contains = full_name.filter(full_name__icontains=query) + + # exclusive and on full_name_starts_with and full_name_contains + search_results = full_name_starts_with.select_related("student")\ + .union(full_name_contains.select_related("student")) + + search_results_excluding_friends = [] for user in search_results: if ( hasattr(user, "student") and user.student not in logged_in_student.friends.all() + and user.student != logged_in_student ): search_results_excluding_friends.append(get_student_data(user.student)) return JsonResponse(search_results_excluding_friends, safe=False) From 736ac0242190c2dfe7fed0928892d5f444675b5c Mon Sep 17 00:00:00 2001 From: "Jiaqi (Jacky) Wang" Date: Thu, 16 May 2024 16:35:45 -0700 Subject: [PATCH 2/2] fix: friends search and prevent sending request to urself --- friends/views.py | 23 ++++++++++++++----- .../PeerModalComponents/FindNewFriends.tsx | 10 ++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/friends/views.py b/friends/views.py index 13826779f..0941c48d5 100644 --- a/friends/views.py +++ b/friends/views.py @@ -3,7 +3,7 @@ from django.db.models import Q from django.contrib.auth.models import User from django.db.models import Value as V -from django.db.models.functions import Concat +from django.db.models.functions import Concat from student.models import Student from friends.models import FriendRequest @@ -65,16 +65,18 @@ def search_friends(request, query): """ logged_in_student = get_object_or_404(Student, user=request.user) - full_name = User.objects.annotate(full_name=Concat('first_name', V(' '), 'last_name')) - + full_name = User.objects.annotate( + full_name=Concat("first_name", V(" "), "last_name") + ) + full_name_starts_with = full_name.filter(full_name__startswith=query) full_name_contains = full_name.filter(full_name__icontains=query) # exclusive and on full_name_starts_with and full_name_contains - search_results = full_name_starts_with.select_related("student")\ - .union(full_name_contains.select_related("student")) - + search_results = full_name_starts_with.select_related("student").union( + full_name_contains.select_related("student") + ) search_results_excluding_friends = [] for user in search_results: @@ -93,6 +95,15 @@ def send_friend_request(request, userId): """ from_student = get_object_or_404(Student, user=request.user) to_student = get_object_or_404(Student, user__id=userId) + + # check if there's already a request from to_student to from_student + if FriendRequest.objects.filter( + from_friend=to_student, to_friend=from_student + ).exists(): + return JsonResponse( + {"message": "The friend already has a request to you"}, status=400 + ) + friend_request, created = FriendRequest.objects.get_or_create( from_friend=from_student, to_friend=to_student ) diff --git a/static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx b/static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx index cfe2e5404..4668939a7 100644 --- a/static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx +++ b/static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx @@ -65,7 +65,7 @@ const FindNewFriends = () => { const endpoint = requestSent[userId] ? getRejectFriendRequestEndpoint(userId) : getSendFriendRequestEndpoint(userId); - await fetch(endpoint, { + const res = await fetch(endpoint, { headers: { "X-CSRFToken": Cookie.get("csrftoken"), Accept: "application/json", @@ -74,7 +74,13 @@ const FindNewFriends = () => { method: "POST", credentials: "include", }); - setRequestSent((prevStatus) => ({ ...prevStatus, [userId]: !prevStatus[userId] })); + const json = await res.json(); + if (res.status === 200) { + setRequestSent((prevStatus) => ({ + ...prevStatus, + [userId]: !prevStatus[userId], + })); + } }; return (