Skip to content
Open
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
33 changes: 25 additions & 8 deletions friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -62,20 +64,26 @@ 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)
Expand All @@ -87,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
)
Expand Down
10 changes: 8 additions & 2 deletions static/js/redux/ui/modals/PeerModalComponents/FindNewFriends.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 (
<Box width="100%" display="flex" flexDirection="column" alignItems="center">
Expand Down