From 80e7335c8d032e38a3ce64bd9b12b2ee96cd897a Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Sun, 7 Apr 2024 17:43:30 +0200 Subject: [PATCH] profiles(block): get profiles of blocked users --- frontend/static/js/api/MyProfile.js | 16 ++++++++++ frontend/static/js/api/Profile.js | 41 +------------------------ profiles/serializers/BlockSerializer.py | 15 +++++++-- profiles/views/blocks.py | 15 ++++++--- 4 files changed, 40 insertions(+), 47 deletions(-) diff --git a/frontend/static/js/api/MyProfile.js b/frontend/static/js/api/MyProfile.js index a461de0..a65e2fe 100644 --- a/frontend/static/js/api/MyProfile.js +++ b/frontend/static/js/api/MyProfile.js @@ -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))); } /** diff --git a/frontend/static/js/api/Profile.js b/frontend/static/js/api/Profile.js index b088f49..bd38d9f 100644 --- a/frontend/static/js/api/Profile.js +++ b/frontend/static/js/api/Profile.js @@ -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 */ @@ -122,4 +83,4 @@ export class Profile extends AExchangeable { super.export([...["username", "avatar", "id"], ...additionalFieldList]) } -} \ No newline at end of file +} diff --git a/profiles/serializers/BlockSerializer.py b/profiles/serializers/BlockSerializer.py index c1e8fb5..c51feed 100644 --- a/profiles/serializers/BlockSerializer.py +++ b/profiles/serializers/BlockSerializer.py @@ -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' diff --git a/profiles/views/blocks.py b/profiles/views/blocks.py index 2c28701..6e69f4b 100644 --- a/profiles/views/blocks.py +++ b/profiles/views/blocks.py @@ -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):