From 7e906de1287781a9cb9ade4252bd96060ad0da62 Mon Sep 17 00:00:00 2001 From: Me Date: Fri, 23 Dec 2022 23:36:04 +0100 Subject: [PATCH] ok so i think i need to change a whole bunch of stuff all over in the User module and Friendship module, namely that we should be searching the DB by ID's not Usernames... currently my Blocking of a user doesn't wquite work cuz findAll() sends ID to findIfUserIsBlockedOrHasBlocked() when it should be sending usernames, but everything else sends Usernames so i'm saving now in case changing everything fucks everything up... --- .../src/friendship/friendship.controller.ts | 2 +- .../src/friendship/friendship.service.ts | 154 +++++++++++++++--- .../api_back/src/users/users.service.ts | 14 +- .../src/pages/profile/ProfileFriends.svelte | 14 +- 4 files changed, 153 insertions(+), 31 deletions(-) diff --git a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts index 50de11ed..94c4d0bb 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -26,7 +26,7 @@ export class FriendshipController { console.log('GET myfriend') console.log(friendUsername); const user = req.user; - return this.friendshipService.findOneFriendByUsername(friendUsername, user.username); + return this.friendshipService.findOneRelationshipByUsername(friendUsername, user.username); } // GET http://transcendance:8080/api/v2/network/blocked diff --git a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts index fdac2c9a..ffb3f84e 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -15,7 +15,46 @@ export class FriendshipService { private readonly userRepository: Repository, ) { } - async findOneFriendByUsername(friendUsername : string, username : string) { + // old, only Accept and Request but not Decline or Block + // async findOneFriendByUsername(friendUsername : string, username : string) { + // const friendship = await this.friendshipRepository + // .createQueryBuilder('friendship') + // .where( + // new Brackets((qb) => { + // qb.where( + // new Brackets((subAQb) => { + // subAQb.where('friendship.senderUsername = :username', {username : username}) + // .andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : friendUsername}) + // }) + // ) + // .orWhere( + // new Brackets((subBQb) => { + // subBQb.where('friendship.senderUsername = :friendUsername', {friendUsername : friendUsername}) + // .andWhere('friendship.receiverUsername = :username', {username : username}) + // }) + // ) + // }), + // ) + // .andWhere( + // new Brackets((qb2) => { + // qb2.where('friendship.status = :statusA', {statusA : FriendshipStatus.ACCEPTED}) + // .orWhere('friendship.status = :statusR', {statusR : FriendshipStatus.REQUESTED}) + // }), + // ) + // .getOne() + + // // console.log('END Find one friend by username: ') + // // console.log({...friendship}) + + // if (!friendship) { + // throw new HttpException(`There is no such friendship`, HttpStatus.NOT_FOUND); + // } + // return friendship; + // } + + // should i be using UserID rather than Username???? i mean prolly... + + async findOneRelationshipByUsername(friendUsername : string, username : string) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') .where( @@ -34,12 +73,7 @@ export class FriendshipService { ) }), ) - .andWhere( - new Brackets((qb2) => { - qb2.where('friendship.status = :statusA', {statusA : FriendshipStatus.ACCEPTED}) - .orWhere('friendship.status = :statusR', {statusR : FriendshipStatus.REQUESTED}) - }), - ) + .andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED}) .getOne() // console.log('END Find one friend by username: ') @@ -94,6 +128,10 @@ export class FriendshipService { for (const friendship of friendships) { partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status}); } + console.log('friendship.service findAllBlockedFriends, friendships:') + console.log({...friendships}) + console.log('friendship.service findAllBlockedFriends, partial friendship:') + console.log({...partialFriendship}) return partialFriendship; } @@ -133,7 +171,12 @@ export class FriendshipService { throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND); if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED) throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND); + const friendship = await this.friendshipRepository.findOneBy({ sender: creator, receiver: receiver }); + + console.log('friendship.service create, friendship: ') + console.log({...friendship}) + if (friendship) { if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED) throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK); @@ -157,7 +200,10 @@ export class FriendshipService { receiverUsername: (await savedFriendship).receiverUsername, status : (await savedFriendship).status } + console.log('friendship.service create friendship, partial friendship') console.log({...partialFriendship}) + console.log('friendship.service create friendship, NEW friendship') + console.log({...newFriendship}) return partialFriendship; } @@ -199,6 +245,8 @@ export class FriendshipService { receiverUsername: (await savedFriendship).receiverUsername, status : (await savedFriendship).status } + console.log('decline friend request') + console.log({...partialFriendship}) return partialFriendship } @@ -206,18 +254,36 @@ export class FriendshipService { let relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); if (!relation[0]) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); - if (relation[0].sender.id === user.id) { - throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND); + + // do i need to check if they've already been blocked? + + if (relation[0].receiver.id === user.id) { + // throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND); + console.log('friendship.service blockFriendship trying to delete and recreate a friendship with block') + console.log({...relation}) + // const newFriendshipDto = new CreateFriendshipDto(relation[0].receiver, FriendshipStatus.BLOCKED) + // const newFriendshipDto = new CreateFriendshipDto({"receiverUsername": relation[0].receiver, "status": "R"}) + // we create a new one where you are the sender + const newFriendshipDto = {"receiverUsername": relation[0].sender.username, "status": FriendshipStatus.BLOCKED}; + // can't do it this way cuz READONLY + // const newFriendshipDto = new CreateFriendshipDto(); + // newFriendshipDto.receiverUsername = relation[0].sender.username; + // newFriendshipDto.status = FriendshipStatus.BLOCKED; + // we delete that relation + await this.removeFriendship(relationshipId, user); + // we set it to blocked like we do below... + return await this.create(newFriendshipDto, user); + } else { + relation[0].status = FriendshipStatus.BLOCKED; + const savedFriendship = this.friendshipRepository.save(relation[0]); + const partialFriendship : Partial = { + id : (await savedFriendship).id, + date : (await savedFriendship).date, + receiverUsername: (await savedFriendship).receiverUsername, + status : (await savedFriendship).status + } + return partialFriendship } - relation[0].status = FriendshipStatus.BLOCKED; - const savedFriendship = this.friendshipRepository.save(relation[0]); - const partialFriendship : Partial = { - id : (await savedFriendship).id, - date : (await savedFriendship).date, - receiverUsername: (await savedFriendship).receiverUsername, - status : (await savedFriendship).status - } - return partialFriendship } async removeFriendship(relationshipId: string, user : User) { @@ -237,18 +303,62 @@ export class FriendshipService { return this.friendshipRepository.remove(friendship[0]); } + + //i think maybe i should be using ID rather than username... + + // async findIfUserIsBlockedOrHasBlocked(userConnectedUsername: string, userToFindUsername: string) { async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToFindId: string) { console.log("finding if user is blocked") + console.log('user connected: ' + userConnectedId) + console.log('user to find: ' + userToFindId) + // const friendship = await this.friendshipRepository + // .createQueryBuilder('friendship') + // .where('friendship.senderUsername = :requestee', { requestee: userConnectedId }) + // .orWhere('friendship.senderUsername = :requesteeBis', { requesteeBis: userToFindId }) + // .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) + // .getOne(); + + + // const friendship = await this.friendshipRepository + // .createQueryBuilder('friendship') + // .where( + // new Brackets((qb) => { + // qb.where('friendship.senderUsername = :requestee', { requestee: userConnectedId }) + // .orWhere('friendship.receiverUsername = :requesteeBis', { requesteeBis: userToFindId }) + // }), + // ) + // .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) + // .getOne(); + const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.senderUsername = :requestee', { requestee: userConnectedId }) - .orWhere('friendship.senderUsername = :requesteeBis', { requesteeBis: userToFindId }) - .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) - .getOne(); + .where( + new Brackets((qb) => { + qb.where( + new Brackets((subAQb) => { + subAQb.where('friendship.senderUsername = :username', {username : userConnectedId}) + .andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : userToFindId}) + }) + ) + .orWhere( + new Brackets((subBQb) => { + subBQb.where('friendship.senderUsername = :friendUsername', {friendUsername : userToFindId}) + .andWhere('friendship.receiverUsername = :username', {username : userConnectedId}) + }) + ) + }), + ) + // .andWhere('friendship.status = :status', {status : FriendshipStatus.BLOCKED}) + .getOne() + + + console.log('printing friendship queried') + console.log({...friendship}) if (friendship) { console.log('we are blocked in friendship.service') return true; } + console.log('we are not blocked in friendship service') return false; } } diff --git a/srcs/requirements/nestjs/api_back/src/users/users.service.ts b/srcs/requirements/nestjs/api_back/src/users/users.service.ts index 01d2d1df..3e72a454 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.service.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.service.ts @@ -155,15 +155,21 @@ export class UsersService { // The fuck Pagination version async findAll(paginationquery : PaginationQueryDto, userConnectedId: string) { const { limit, offset } = paginationquery; - const users = await this.userRepository.find({where: {id: Not(+userConnectedId)}, order: {username: "ASC"}, skip: offset, take: limit,}); + const otherUsers = await this.userRepository.find({where: {id: Not(+userConnectedId)}, order: {username: "ASC"}, skip: offset, take: limit,}); let partialUsers : Partial[] = []; - for (const user of users) { - if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()) === false) { - partialUsers.push({username: user.username, image_url: user.image_url, status: user.status, stats: user.stats}); + for (const otherUser of otherUsers) { + let tmp = await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, otherUser.id.toString()); + console.log('user.services findIF Blocked... : ') + console.log(tmp) + // if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()) === false) { + if (tmp === false) { + partialUsers.push({username: otherUser.username, image_url: otherUser.image_url, status: otherUser.status, stats: otherUser.stats}); } } + console.log('user.services findAll, partialUsers:') + console.log({...partialUsers}) return partialUsers; } diff --git a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte index 80659127..1c2e944f 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -2,7 +2,6 @@ import { onMount } from "svelte"; import { binding_callbacks, empty } from "svelte/internal"; // WTF is this? - import App from "../../App.svelte"; import Button from "../../pieces/Button.svelte"; import DisplayAUser from "../../pieces/DisplayAUser.svelte"; import Tabs from "../../pieces/Tabs.svelte"; @@ -90,9 +89,13 @@ }; /**** END OF MAIN FETCH ****/ + // returns everything but BLOCKED const fetchFriendshipFull = async(aUsername) => { + console.log('fetch friendship from a username') + console.log(aUsername) friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`) .then( x => x.json()); + console.log({...friendshipStatusFull}) }; @@ -106,6 +109,7 @@ }) }) .then( x => x.json()) + fetchFriendshipFull(aUsername); }; const viewAUser = async(aUsername) => { @@ -165,8 +169,8 @@ }; const blockANonFriendUser = async(aUsername) => { - console.log('Block someone username') - console.log({...aUsername}) + console.log('Block a non friend user, their username') + console.log(aUsername) const blockResp = await fetch("http://transcendance:8080/api/v2/network/myfriends", { method : "POST", @@ -187,6 +191,8 @@ }; const blockAFriend = async(relationshipId) => { + console.log('blocking a friend, the relationshipID') + console.log(relationshipId) const resp = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/block`, {method: "PATCH"}) .then( x => x.json() ); console.log('blocked a user response') @@ -279,7 +285,7 @@
You have not Blocked any Users
{/if} {#each blockedUsers as aUser} - +
{/each}