profiles(block): get profiles of blocked users

This commit is contained in:
AdrienLSH 2024-04-07 17:43:30 +02:00
parent 8912e39fa4
commit 80e7335c8d
4 changed files with 40 additions and 47 deletions

View File

@ -10,6 +10,22 @@ class MyProfile extends Profile
constructor (client)
{
super(client, "../me");
/**
* @type {[Profile]}
*/
this.blockedUsers = [];
}
async init() {
await super.init();
await this.getBlockedUsers();
}
async getBlockedUsers() {
const response = await this.client._get('/api/profiles/block');
const data = await response.json();
data.forEach(profileData => this.blockedUsers.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar)));
}
/**

View File

@ -57,9 +57,6 @@ export class Profile extends AExchangeable
this.username = response_data.username;
this.avatar = response_data.avatar;
await this.getBlock();
await this.getFriend();
}
/**
@ -79,42 +76,6 @@ export class Profile extends AExchangeable
return games;
}
async getBlock() {
let block_response = await this.client._get("/api/profiles/block");
if (block_response.status != 200)
return;
let block_data = await block_response.json();
let block_list = JSON.parse(block_data.blockeds);
let client_id = block_data.user_id;
block_list.forEach(block => {
let blocker = block.fields.blocker;
let blocked = block.fields.blocked;
if (blocker == client_id && blocked == this.id)
return (this.isBlocked = true);
});
}
async getFriend() {
let friend_response = await this.client._get("/api/profiles/friend");
this.isFriend = false;
if (friend_response.status != 200)
return this.isFriend;
let friend_data = await friend_response.json();
let friends_list = friend_data.friends;
let client_id = friend_data.user_id;
friends_list.forEach(friend => {
if (friend == this.id) {
this.isFriend = true;
return this.isFriend;
}
});
return this.isFriend;
}
/**
* @param {[String]} additionalFieldList
*/

View File

@ -1,10 +1,21 @@
from rest_framework.serializers import ModelSerializer
from rest_framework.serializers import (ModelSerializer,
ReadOnlyField,
SerializerMethodField)
from ..models import ProfileModel
from ..models import BlockModel
class BlockSerializer(ModelSerializer):
blocked_username = ReadOnlyField(source='blocked.username')
blocked_avatar = SerializerMethodField()
class Meta:
model = BlockModel
fields = ['blocked']
fields = ['blocked', 'blocked_username', 'blocked_avatar']
def get_blocked_avatar(self, instance):
blocked_profile = ProfileModel.objects.filter(user_id=instance.blocked.pk)
if (blocked_profile.exists()):
return blocked_profile[0].avatar.name
return '/static/avatars/default.avif'

View File

@ -6,8 +6,8 @@ from rest_framework.authentication import SessionAuthentication
from django.contrib.auth.models import User
from django.utils.translation import gettext as _
from ..models import BlockModel
from ..serializers.BlockSerializer import BlockSerializer
from ..models import BlockModel, ProfileModel
from ..serializers.ProfileSerializer import ProfileSerializer
class GetBlocksView(APIView):
@ -15,9 +15,14 @@ class GetBlocksView(APIView):
authentication_classes = (SessionAuthentication,)
def get(self, request):
blocked_profiles = BlockModel.objects.filter(blocker=request.user.pk)
response_status: int = status.HTTP_200_OK if blocked_profiles else status.HTTP_204_NO_CONTENT
return Response(BlockSerializer(blocked_profiles, many=True).data, response_status)
profiles = []
blocks = BlockModel.objects.filter(blocker=request.user.pk)
for block in blocks:
profile = ProfileModel.objects.filter(user_id=block.blocked.pk)
if (profile.exists()):
profiles.append(profile[0])
return Response(ProfileSerializer(profiles, many=True).data)
class EditBlocksView(APIView):