From 000987fbde5af9d1b55e59bc432e680dc8b1c6a8 Mon Sep 17 00:00:00 2001 From: Me Date: Tue, 3 Jan 2023 19:42:14 +0100 Subject: [PATCH] ok after talking with cherif we decided not to send things like the user.id so i need to recreate more or less what i had before, but i'm gonna keep using the user.id everywhere in the back and keep my better simpler friendship.entity, working on all that now, got a thing that compiles in the front and back, a few more things to fix and then test everything --- .../friendship/dto/create-friendship.dto.ts | 10 ++-- .../src/friendship/friendship.controller.ts | 13 ++--- .../src/friendship/friendship.service.ts | 22 +++++---- .../api_back/src/users/users.controller.ts | 16 +++---- .../api_back/src/users/users.service.ts | 23 +++++---- .../src/pages/profile/ProfileFriends.svelte | 47 +++++++++---------- .../api_front/src/pieces/DisplayAUser.svelte | 10 ++-- 7 files changed, 75 insertions(+), 66 deletions(-) diff --git a/srcs/requirements/nestjs/api_back/src/friendship/dto/create-friendship.dto.ts b/srcs/requirements/nestjs/api_back/src/friendship/dto/create-friendship.dto.ts index e789f09b..2f4128fa 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/dto/create-friendship.dto.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/dto/create-friendship.dto.ts @@ -2,12 +2,12 @@ import { IsEnum, IsNotEmpty, IsString, IsPositive } from 'class-validator'; import { FriendshipStatus } from '../entities/friendship.entity'; export class CreateFriendshipDto { - @IsPositive() + // @IsPositive() // @Max(1000) ? - readonly receiverId: number; - // @IsNotEmpty() - // @IsString() - // readonly receiverUsername: string; + // readonly receiverId: number; + @IsNotEmpty() + @IsString() + readonly receiverUsername: string; @IsEnum(FriendshipStatus) readonly status: FriendshipStatus; } 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 c8925519..e03d9f24 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -25,12 +25,10 @@ export class FriendshipController { @Get('myfriends') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) - findOne(@Query('username') otherUsername: string, @Query('id') id: number, @Req() req) { + findOne(@Query('username') otherUsername: string, @Req() req) { console.log('GET myfriend') const user = req.user; - if (id !== undefined) { - return this.friendshipService.findOneRelationshipById(id, user.id) - } else if (otherUsername !== undefined) { + if (otherUsername !== undefined) { return this.friendshipService.findOneRelationshipByUsername(otherUsername, user.username); } // might change this @@ -59,7 +57,7 @@ export class FriendshipController { @UseGuards(TwoFactorGuard) create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) { const user = req.user; - if (user.id !== createFriendshipDto.receiverId) + if (user.username !== createFriendshipDto.receiverUsername) return this.friendshipService.create(createFriendshipDto, user); return new HttpException('You can\'t request a frienship to yourself', HttpStatus.BAD_REQUEST); } @@ -124,8 +122,11 @@ export class FriendshipController { @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) findBlocked(@Query('relationshipId') relationshipId: number, @Req() req) { + console.log('friendship.controller fetching blocked users') + console.log(relationshipId) const user = req.user; - if (relationshipId === undefined) + // if (relationshipId === undefined) + if (Number.isNaN(relationshipId)) return this.friendshipService.findAllBlockedFriends(user.id); else return this.friendshipService.findOneBlocked(relationshipId, user.id); 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 2de9fa9f..dfb9f5fa 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -103,7 +103,7 @@ export class FriendshipService { // lets see what happens here, doing directly receiver.id not LeftJoinAndSelect ... async findAllFriends(userId: number) { - const friendship = await this.friendshipRepository + const friendships = await this.friendshipRepository .createQueryBuilder('friendship') .leftJoinAndSelect('friendship.sender', 'sender') .leftJoinAndSelect('friendship.receiver', 'receiver') @@ -115,7 +115,13 @@ export class FriendshipService { // for (const friend of friendship) // console.log("FRIENDSHIP : " + friend.status); // return friendship; - return new SendableFriendship(friendship); + // return new SendableFriendship(friendship); + let sendFrienships: SendableFriendship[] = [] + for (const friendship of friendships) { + sendFrienships.push(new SendableFriendship(friendship)); + } + // return new SendableFriendship(friendship); + return sendFrienships; } async findOneBlocked(friendshipId: number, userId: number) { @@ -225,7 +231,7 @@ export class FriendshipService { async create(createFriendshipDto: CreateFriendshipDto, creator : User) { console.log("DTO : \n") console.log({...createFriendshipDto}) - const receiver = await this.userRepository.findOneBy({id: createFriendshipDto.receiverId}); + const receiver = await this.userRepository.findOneBy({username: createFriendshipDto.receiverUsername}); if (!receiver) throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND); if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED) @@ -278,7 +284,7 @@ export class FriendshipService { // console.log({...relation}) if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); - if (relation.senderId === user.id) { + if (relation.sender.id === user.id) { throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); } relation.status = FriendshipStatus.ACCEPTED; @@ -301,7 +307,7 @@ export class FriendshipService { const relation = await this.friendshipRepository.findOneBy({id: relationshipId }); if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); - if (relation.senderId === user.id) { + if (relation.sender.id === user.id) { throw new HttpException(`You can't decline your own request.`, HttpStatus.NOT_FOUND); } relation.status = FriendshipStatus.DECLINED; @@ -325,14 +331,14 @@ export class FriendshipService { // do i need to check if they've already been blocked? - if (relation.receiverId === user.id) { + if (relation.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.senderUsername, "receiverId": relation.senderId, "status": FriendshipStatus.BLOCKED}; + const newFriendshipDto = {"receiverUsername": relation.sender.username, "receiverId": relation.sender.id, "status": FriendshipStatus.BLOCKED}; // can't do it this way cuz READONLY // const newFriendshipDto = new CreateFriendshipDto(); // newFriendshipDto.receiverUsername = relation[0].sender.username; @@ -366,7 +372,7 @@ export class FriendshipService { console.log({...user}) if (!friendship) throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND); - if (friendship.senderId !== user.id && friendship.receiverId !== user.id) { + if (friendship.sender.id !== user.id && friendship.receiver.id !== user.id) { throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN); } console.log('.service deleted a friendship') diff --git a/srcs/requirements/nestjs/api_back/src/users/users.controller.ts b/srcs/requirements/nestjs/api_back/src/users/users.controller.ts index 6ed1b928..5739b49b 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.controller.ts @@ -62,15 +62,15 @@ export class UsersController { @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) @Get() - findOne(@Query('id') toFindId: number, @Req() req) { - console.log('users service findOne toFindId:') - console.log(toFindId) - console.log('users service findOne my Id:') - console.log(req.user.id) - if (toFindId === undefined) - return this.usersService.findOne(req.user.id); + findOne(@Query('username') usernameToFind: string, @Req() req) { + console.log('users service findOne usernameToFind:') + console.log(usernameToFind) + console.log('users service findOne my Username:') + console.log(req.user.username) + if (usernameToFind === undefined) + return this.usersService.findOne(req.user.username); else - return this.usersService.findOne(toFindId); + return this.usersService.findOne(usernameToFind); // i would rather just use numbers but i'm guessing Cherif uses this all over } 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 bb3e99be..0fe72cc6 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.service.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.service.ts @@ -32,11 +32,11 @@ export class UsersService { return user; } - async findOne(id: number) { - console.log(`FIND ONE USER SERVICE Find user ${id}`); + async findOne(username: string) { + console.log(`FIND ONE USER SERVICE Find user ${username}`); const user = await this.userRepository.createQueryBuilder('user') .leftJoinAndSelect('user.stats', 'stats') - .where('user.id = :id', { id: id }) + .where('user.username = :username', { username: username }) .getOne(); if (!user) throw new NotFoundException(`The requested user not found.`); @@ -53,6 +53,9 @@ export class UsersService { return partialUser; } + + /***** THIS IS THE THING I REALLY NEED TO FIX!!!!!!! *****/ + // Ok this gets called in the Authenitcation Service, but like i was still able to make a username === someone else's async isUsernameExists(usernameToSearch: string): Promise { const user = await this.userRepository.findOneBy({username : usernameToSearch}); @@ -123,28 +126,28 @@ export class UsersService { return this.userRepository.save(user); } - async update(id: string, updateUserDto: UpdateUsersDto) { - console.log(`Update user ${id} with ${updateUserDto.isEnabledTwoFactorAuth}`); + async update(id: number, updateUserDto: UpdateUsersDto) { + // console.log(`Update user ${id} with ${updateUserDto.isEnabledTwoFactorAuth}`); const user = await this.userRepository.preload( - {id: +id, + {id: id, ...updateUserDto}); if (!user) throw new HttpException(`The user could not be updated.`,HttpStatus.NOT_FOUND); return this.userRepository.save(user); } - async remove(id: string) { - const user = await this.userRepository.findOneBy({id: +id}); + async remove(id: number) { + const user = await this.userRepository.findOneBy({id: id}); if (!user) throw new HttpException(`The user could not be deleted.`,HttpStatus.NOT_FOUND); return this.userRepository.remove(user); } - async enableTwoFactorAuth(id: string) { + async enableTwoFactorAuth(id: number) { return this.userRepository.update(id, {isEnabledTwoFactorAuth: true}); } - async authenticateUserWith2FA(id: string) { + async authenticateUserWith2FA(id: number) { return this.userRepository.update(id, { isTwoFactorAuthenticated: true}) } 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 c3f107c1..fa06c7f6 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -14,7 +14,7 @@ let myFriends; let requestsMade, requestsRecieved; let blockedUsers; - let userIdBeingViewed; + let usernameBeingViewed; let friendshipStatusFull; // id, date, senderUsername, reveiverUsername, status /**** Layout variables ****/ @@ -60,7 +60,7 @@ }; const fetchMyFriends = async() => { - myFriends = await fetch('http://transcendance:8080/api/v2/network/relations') + myFriends = await fetch('http://transcendance:8080/api/v2/network/myfriends') .then( x => x.json() ); console.log('got my friends ') console.log({...myFriends}) @@ -90,10 +90,10 @@ /**** END OF MAIN FETCH ****/ // returns everything but BLOCKED - const fetchFriendshipFull = async(aUserId) => { + const fetchFriendshipFull = async(aUsername) => { console.log('fetch friendship from a username') - console.log(aUserId) - friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/relations/${aUserId}`) + console.log(aUsername) + friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`) .then( x => x.json()); console.log({...friendshipStatusFull}) }; @@ -105,21 +105,20 @@ headers: { 'Content-Type': 'application/json'}, body: JSON.stringify({ "receiverUsername": aUser.username, - "receiverId": aUser.id, "status": "R" }) }) .then( x => x.json()) - fetchFriendshipFull(aUser.id); + fetchFriendshipFull(aUser.username); }; - const viewAUser = async(aUserId) => { + const viewAUser = async(aUsername) => { console.log('Profile Friend updating userBeingViewed') - userIdBeingViewed = aUserId; + usernameBeingViewed = aUsername; // friendshipStatusFull = undefined; // id, date, senderUsername, reveiverUsername, status - await fetchFriendshipFull(aUserId); + await fetchFriendshipFull(aUsername); console.log('Friendship Status Full') console.log({...friendshipStatusFull}) @@ -135,7 +134,7 @@ // maybe not the most robust things, not super reusable cuz it depends on outside vars but works for now... console.log('accepted friend request, now response') console.log({...resp}) - await fetchFriendshipFull(userIdBeingViewed); + await fetchFriendshipFull(usernameBeingViewed); // will this make it reload? C'est un peu bourain... do i even need it? activeTabItem = activeTabItem; @@ -150,7 +149,7 @@ // maybe not the most robust things, not super reusable cuz it depends on outside vars but works for now... console.log('declined friend request, now response') console.log({...resp}) - await fetchFriendshipFull(userIdBeingViewed); + await fetchFriendshipFull(usernameBeingViewed); }; const unfriend = async(relationshipId) => { @@ -161,7 +160,7 @@ // friendshipStatusFull = undefined; // OR - await fetchFriendshipFull(userIdBeingViewed); + await fetchFriendshipFull(usernameBeingViewed); if (Object.keys(friendshipStatusFull).length === 0) friendshipStatusFull = undefined; @@ -178,14 +177,14 @@ headers: { 'Content-Type': 'application/json'}, body: JSON.stringify({ "receiverUsername": aUser.username, - "receiverId": aUser.id, + "receiverId": aUser.username, "status": "B" }) }) .then( x => x.json()) await fetchBlockedUsers(); await fetchAllUsers(); - userIdBeingViewed = undefined; + usernameBeingViewed = undefined; friendshipStatusFull = undefined; // will this make it reload? @@ -201,7 +200,7 @@ console.log({...resp}) await fetchBlockedUsers(); await fetchAllUsers(); - userIdBeingViewed = undefined; + usernameBeingViewed = undefined; friendshipStatusFull = undefined; // will this make it reload? @@ -252,9 +251,9 @@ {/if} - - {#each allUsers as aUser (aUser.id)} - + + {#each allUsers as aUser} + @@ -268,7 +267,7 @@ {/if} {#each myFriends as aUser} - +
{/each} @@ -300,9 +299,9 @@
- {#if userIdBeingViewed !== undefined} + {#if usernameBeingViewed !== undefined} - +
{#if friendshipStatusFull && friendshipStatusFull.id} @@ -333,8 +332,8 @@ {/if} {/if} {:else} - - + + {/if}
{:else} diff --git a/srcs/requirements/svelte/api_front/src/pieces/DisplayAUser.svelte b/srcs/requirements/svelte/api_front/src/pieces/DisplayAUser.svelte index 4c1904fc..7fa528af 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/DisplayAUser.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/DisplayAUser.svelte @@ -4,14 +4,14 @@ import GenerateUserDisplay from './GenerateUserDisplay.svelte'; // export let aUsername; - export let aUserId; + export let aUsername; let user; onMount( async() => { // console.log('Display aUser username: '+ aUsername) // http://transcendance:8080/api/v2/user?username=NomDuUserATrouver // user = await fetch(`http://transcendance:8080/api/v2/user?username=${aUsername}`) - user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUserId}`) + user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUsername}`) .then( (x) => x.json() ); // console.log('Display a user: ') @@ -32,12 +32,12 @@ // console.log('Display Update aUser username: '+ aUsername) // http://transcendance:8080/api/v2/user?username=NomDuUserATrouver // user = await fetch(`http://transcendance:8080/api/v2/user?username=${aUsername}`) - user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUserId}`) + user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUsername}`) .then( (x) => x.json() ); }; // $: aUsername, updateUser(); - $: aUserId, updateUser(); + $: aUsername, updateUser(); @@ -49,7 +49,7 @@ {:else}

Sorry

-
Failed to load user {aUserId}
+
Failed to load user {aUsername}
{/if}