profiles: friend requests

This commit is contained in:
AdrienLSH 2024-04-18 11:40:42 +02:00
parent 5a2da91d6e
commit 9f61cda73f
4 changed files with 73 additions and 8 deletions

View File

@ -19,12 +19,22 @@ class MyProfile extends Profile
* @type {[Profile]} * @type {[Profile]}
*/ */
this.friends = []; this.friends = [];
/**
* @type {[Profile]}
*/
this.incomingFriendRequests = [];
/**
* @type {[Profile]}
*/
this.outgoingFriendRequests = [];
} }
async init() { async init() {
await super.init(); await super.init();
await this.getBlockedUsers(); await this.getBlockedUsers();
await this.getFriends(); await this.getFriends();
await this.getIncomingFriendRequests()
await this.getOutgoingFriendRequests()
} }
async getBlockedUsers() { async getBlockedUsers() {
@ -38,6 +48,20 @@ class MyProfile extends Profile
const data = await response.json(); const data = await response.json();
data.forEach(profileData => this.friends.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar))); data.forEach(profileData => this.friends.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar)));
} }
async getIncomingFriendRequests() {
const response = await this.client._get('/api/profiles/incoming_friend_requests');
const data = await response.json();
data.forEach(profileData => this.incomingFriendRequests.push(
new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar)
));
}
async getOutgoingFriendRequests() {
const response = await this.client._get('/api/profiles/outgoing_friend_requests');
const data = await response.json();
data.forEach(profileData => this.outgoingFriendRequests.push(
new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar)
));
}
/** /**
* *
* @param {File} selectedFile * @param {File} selectedFile

View File

@ -44,7 +44,7 @@ class ProfileModel(Model):
).delete() ).delete()
def is_friend_requested_by(self, profile): def is_friend_requested_by(self, profile):
return self.get_received_friend_request_from(profile) is None return FriendRequestModel.objects.filter(author=profile, target=self).exists()
def get_received_friend_request_from(self, profile): def get_received_friend_request_from(self, profile):
return FriendRequestModel.objects.filter(author=profile, target=self).first() return FriendRequestModel.objects.filter(author=profile, target=self).first()
@ -52,10 +52,13 @@ class ProfileModel(Model):
def is_friend_requesting(self, profile): def is_friend_requesting(self, profile):
return FriendRequestModel.objects.filter(author=self, target=profile).exists() return FriendRequestModel.objects.filter(author=self, target=profile).exists()
def get_sent_friend_requests(self) -> list[ProfileModel]: def get_outgoing_friend_request_to(self, profile):
return FriendRequestModel.objects.filter(author=self, target=profile).first()
def get_outgoing_friend_requests(self) -> list[ProfileModel]:
return FriendRequestModel.objects.filter(author=self) return FriendRequestModel.objects.filter(author=self)
def get_received_friend_requests(self) -> list[ProfileModel]: def get_incoming_friend_requests(self) -> list[ProfileModel]:
return FriendRequestModel.objects.filter(target=self) return FriendRequestModel.objects.filter(target=self)

View File

@ -3,7 +3,10 @@ from django.urls import path
from .viewsets.ProfileViewSet import ProfileViewSet from .viewsets.ProfileViewSet import ProfileViewSet
from .viewsets.MyProfileViewSet import MyProfileViewSet from .viewsets.MyProfileViewSet import MyProfileViewSet
from .views.blocks import GetBlocksView, EditBlocksView from .views.blocks import GetBlocksView, EditBlocksView
from .views.friends import GetFriendsView, EditFriendView from .views.friends import (GetFriendsView,
EditFriendView,
GetIncomingFriendRequestView,
GetOutgoingFriendRequestView)
urlpatterns = [ urlpatterns = [
path("settings", MyProfileViewSet.as_view({'patch': 'partial_update', 'delete': 'delete_avatar'}), name="my_profile_page"), path("settings", MyProfileViewSet.as_view({'patch': 'partial_update', 'delete': 'delete_avatar'}), name="my_profile_page"),
@ -13,6 +16,8 @@ urlpatterns = [
path("block/<int:pk>", EditBlocksView.as_view(), name="block_page"), path("block/<int:pk>", EditBlocksView.as_view(), name="block_page"),
path("friends", GetFriendsView.as_view(), name="friends_list_page"), path("friends", GetFriendsView.as_view(), name="friends_list_page"),
path("friends/<int:pk>", EditFriendView.as_view(), name="friends_edit_page"), path("friends/<int:pk>", EditFriendView.as_view(), name="friends_edit_page"),
path("incoming_friend_requests", GetIncomingFriendRequestView.as_view(), name="incoming_friend_requests"),
path("outgoing_friend_requests", GetOutgoingFriendRequestView.as_view(), name="outgoing_friend_requests"),
path("user/<str:username>", ProfileViewSet.as_view({'get': 'retrieve'}), name="profile_page"), path("user/<str:username>", ProfileViewSet.as_view({'get': 'retrieve'}), name="profile_page"),
path("id/<int:pk>", ProfileViewSet.as_view({'get': 'retrieve_id'}), name="profile_page"), path("id/<int:pk>", ProfileViewSet.as_view({'get': 'retrieve_id'}), name="profile_page"),
] ]

View File

@ -6,7 +6,7 @@ from rest_framework.authentication import SessionAuthentication
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from ..models import ProfileModel, FriendModel from ..models import ProfileModel, FriendRequestModel
from ..serializers.ProfileSerializer import ProfileSerializer from ..serializers.ProfileSerializer import ProfileSerializer
@ -26,7 +26,7 @@ class EditFriendView(APIView):
return self.request.user.profilemodel return self.request.user.profilemodel
def post(self, request, pk=None): def post(self, request, pk=None):
user_profile = self.get_object() user_profile: ProfileModel = self.get_object()
friend_profile = get_object_or_404(ProfileModel, pk=pk) friend_profile = get_object_or_404(ProfileModel, pk=pk)
if user_profile.pk == pk: if user_profile.pk == pk:
@ -35,8 +35,16 @@ class EditFriendView(APIView):
if user_profile.is_friend(friend_profile): if user_profile.is_friend(friend_profile):
return Response(_('You are already friend with this user.'), status.HTTP_400_BAD_REQUEST) return Response(_('You are already friend with this user.'), status.HTTP_400_BAD_REQUEST)
FriendModel(friend1=user_profile, friend2=friend_profile).save() if user_profile.is_friend_requesting(friend_profile):
return Response(_('Friendship succssfully created.'), status.HTTP_201_CREATED) return Response(_('You already sent a request to this user.'), status.HTTP_400_BAD_REQUEST)
incoming_request = user_profile.get_received_friend_request_from(friend_profile)
if incoming_request:
incoming_request.accept()
return Response(_('Friendship succssfully created.'), status.HTTP_201_CREATED)
FriendRequestModel(author=user_profile, target=friend_profile).save()
return Response(_('Friend request sent.'), status.HTTP_200_OK)
def delete(self, request, pk=None): def delete(self, request, pk=None):
user_profile = self.get_object() user_profile = self.get_object()
@ -45,5 +53,30 @@ class EditFriendView(APIView):
if not user_profile.is_friend(friend_profile): if not user_profile.is_friend(friend_profile):
return Response(_('You are not friend with this user.'), status.HTTP_400_BAD_REQUEST) return Response(_('You are not friend with this user.'), status.HTTP_400_BAD_REQUEST)
outgoing_request = user_profile.get_outgoing_friend_request_to(friend_profile)
if outgoing_request:
outgoing_request.delete()
return Response(_('Friend request cancelled.'))
user_profile.delete_friend(friend_profile) user_profile.delete_friend(friend_profile)
return Response(_('Friendship succssfully deleted.')) return Response(_('Friendship succssfully deleted.'))
class GetIncomingFriendRequestView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def get(self, request):
requests = request.user.profilemodel.get_incoming_friend_requests()
profiles = [request.author for request in requests]
return Response(ProfileSerializer(profiles, many=True).data)
class GetOutgoingFriendRequestView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def get(self, request):
requests = request.user.profilemodel.get_outgoing_friend_requests()
profiles = [request.target for request in requests]
return Response(ProfileSerializer(profiles, many=True).data)