profiles: friend requests
This commit is contained in:
parent
5a2da91d6e
commit
9f61cda73f
@ -19,12 +19,22 @@ class MyProfile extends Profile
|
||||
* @type {[Profile]}
|
||||
*/
|
||||
this.friends = [];
|
||||
/**
|
||||
* @type {[Profile]}
|
||||
*/
|
||||
this.incomingFriendRequests = [];
|
||||
/**
|
||||
* @type {[Profile]}
|
||||
*/
|
||||
this.outgoingFriendRequests = [];
|
||||
}
|
||||
|
||||
async init() {
|
||||
await super.init();
|
||||
await this.getBlockedUsers();
|
||||
await this.getFriends();
|
||||
await this.getIncomingFriendRequests()
|
||||
await this.getOutgoingFriendRequests()
|
||||
}
|
||||
|
||||
async getBlockedUsers() {
|
||||
@ -38,6 +48,20 @@ class MyProfile extends Profile
|
||||
const data = await response.json();
|
||||
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
|
||||
|
@ -44,7 +44,7 @@ class ProfileModel(Model):
|
||||
).delete()
|
||||
|
||||
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):
|
||||
return FriendRequestModel.objects.filter(author=profile, target=self).first()
|
||||
@ -52,10 +52,13 @@ class ProfileModel(Model):
|
||||
def is_friend_requesting(self, profile):
|
||||
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)
|
||||
|
||||
def get_received_friend_requests(self) -> list[ProfileModel]:
|
||||
def get_incoming_friend_requests(self) -> list[ProfileModel]:
|
||||
return FriendRequestModel.objects.filter(target=self)
|
||||
|
||||
|
||||
|
@ -3,7 +3,10 @@ from django.urls import path
|
||||
from .viewsets.ProfileViewSet import ProfileViewSet
|
||||
from .viewsets.MyProfileViewSet import MyProfileViewSet
|
||||
from .views.blocks import GetBlocksView, EditBlocksView
|
||||
from .views.friends import GetFriendsView, EditFriendView
|
||||
from .views.friends import (GetFriendsView,
|
||||
EditFriendView,
|
||||
GetIncomingFriendRequestView,
|
||||
GetOutgoingFriendRequestView)
|
||||
|
||||
urlpatterns = [
|
||||
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("friends", GetFriendsView.as_view(), name="friends_list_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("id/<int:pk>", ProfileViewSet.as_view({'get': 'retrieve_id'}), name="profile_page"),
|
||||
]
|
||||
|
@ -6,7 +6,7 @@ from rest_framework.authentication import SessionAuthentication
|
||||
from django.utils.translation import gettext as _
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from ..models import ProfileModel, FriendModel
|
||||
from ..models import ProfileModel, FriendRequestModel
|
||||
from ..serializers.ProfileSerializer import ProfileSerializer
|
||||
|
||||
|
||||
@ -26,7 +26,7 @@ class EditFriendView(APIView):
|
||||
return self.request.user.profilemodel
|
||||
|
||||
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)
|
||||
|
||||
if user_profile.pk == pk:
|
||||
@ -35,8 +35,16 @@ class EditFriendView(APIView):
|
||||
if user_profile.is_friend(friend_profile):
|
||||
return Response(_('You are already friend with this user.'), status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
FriendModel(friend1=user_profile, friend2=friend_profile).save()
|
||||
return Response(_('Friendship succssfully created.'), status.HTTP_201_CREATED)
|
||||
if user_profile.is_friend_requesting(friend_profile):
|
||||
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):
|
||||
user_profile = self.get_object()
|
||||
@ -45,5 +53,30 @@ class EditFriendView(APIView):
|
||||
if not user_profile.is_friend(friend_profile):
|
||||
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)
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user