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 3fb61e19..57b67493 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 @@ -1,11 +1,10 @@ -import { IsEnum, IsString } from 'class-validator'; +import { IsEnum, IsNotEmpty, IsString } from 'class-validator'; import { FriendshipStatus } from '../entities/friendship.entity'; export class CreateFriendshipDto { @IsString() - readonly requesterUsername: string; - @IsString() - readonly addresseeUsername: string; + @IsNotEmpty() + readonly receiverUsername: string; @IsEnum(FriendshipStatus) readonly status: FriendshipStatus; } diff --git a/srcs/requirements/nestjs/api_back/src/friendship/entities/friendship.entity.ts b/srcs/requirements/nestjs/api_back/src/friendship/entities/friendship.entity.ts index 12526de6..28459b11 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/entities/friendship.entity.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/entities/friendship.entity.ts @@ -17,13 +17,17 @@ export class Friendship { @CreateDateColumn() date : Date; - @Column() @ManyToOne(type => User, user => user.username) - requesterUsername: string; + sender: User; + + @ManyToOne(type => User, user => user.username) + receiver: User; @Column() - @ManyToOne(type => User, user => user.username) - addresseeUsername: string; + senderUsername : string; + + @Column() + receiverUsername : string; @Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED}) 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 552a35a9..48e5150f 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -12,59 +12,20 @@ export class FriendshipController { @Get('myfriends') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) - findEmpty(@Query('username') username: string, @Req() req) { + findEmpty(@Req() req) { const user = req.user; - if (username === undefined) { - console.log("WHAT IS UP MY GUYS IT IS YOUR BOI THAT IDIOT YOU HATE 11111"); - return this.friendshipService.findAllFriends(user.id); - } else { - - } + return this.friendshipService.findAllFriends(user.id); } // GET http://transcendance:8080/api/v2/network/myfriends/relationshipId - @Get('myfriends/:relationshipId') + @Get('myfriend/:relationshipId') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) findOneFriend(@Param('relationshipId') relationshipId: string, @Req() req) { const user = req.user; - return this.friendshipService.findOneFriend(relationshipId, user.id); + return this.friendshipService.findOneFriend(relationshipId, user.username); } - // POST http://transcendance:8080/api/v2/network/myfriends - @Post('myfriends') - @HttpCode(HttpStatus.CREATED) - @UseGuards(AuthenticateGuard) - @UseGuards(TwoFactorGuard) - create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) { - const user = req.user; - - console.log(`User id: ${user.id}\nUser name: ${createFriendshipDto.requesterUsername}\nStatus: ${createFriendshipDto.status}`); - - if (user.username === createFriendshipDto.requesterUsername) - return this.friendshipService.create(createFriendshipDto, user); - return new HttpException('You can\'t request a frienship for another user', HttpStatus.FORBIDDEN); - } - - // PATCH http://transcendance:8080/api/v2/network/myfriends/relationshipId?status=A - @Patch('myfriends/:relationshipId') - @UseGuards(AuthenticateGuard) - @UseGuards(TwoFactorGuard) - update(@Param('relationshipId') relationshipId: string, @Query('status') status : string, @Req() req) - { - const user : User = req.user; - return this.friendshipService.updateFriendship(relationshipId, user, status); - } - - // DELETE http://transcendance:8080/api/v2/network/myfriends/relationshipId - @Delete('myfriends/:relationshipId') - @UseGuards(AuthenticateGuard) - @UseGuards(TwoFactorGuard) - remove(@Param('relationshipId') relationshipId: string) { - return this.friendshipService.removeFriendship(relationshipId); - } - - // GET http://transcendance:8080/api/v2/network/blocked @Get('blocked') @UseGuards(AuthenticateGuard) @@ -74,15 +35,69 @@ export class FriendshipController { return this.friendshipService.findAllBlockedFriends(user.username); } + @Get('blocked/:relationshipId') + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + findOneBlocked(@Param('relationshipId') relationshipId: string, @Req() req) { + const user = req.user; + return this.friendshipService.findOneBlocked(relationshipId, user.username); + } + + // POST http://transcendance:8080/api/v2/network/myfriends + @Post('myfriends') + @HttpCode(HttpStatus.CREATED) + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) { + const user = req.user; + 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); + } + + // PATCH http://transcendance:8080/api/v2/network/myfriends/relationshipId/accept + @Patch('myfriends/:relationshipId/accept') + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + updateAccept(@Param('relationshipId') relationshipId: string, @Req() req) + { + const user : User = req.user; + return this.friendshipService.acceptFriendship(relationshipId, user); + } + + @Patch('myfriends/:relationshipId/decline') + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + updateDecline(@Param('relationshipId') relationshipId: string, @Req() req) + { + const user : User = req.user; + return this.friendshipService.declineFriendship(relationshipId, user); + } + + @Patch('myfriends/:relationshipId/block') + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + updateBlock(@Param('relationshipId') relationshipId: string, @Req() req) + { + const user : User = req.user; + return this.friendshipService.blockFriendship(relationshipId, user); + } + + // DELETE http://transcendance:8080/api/v2/network/myfriends/relationshipId + @Delete(':relationshipId') + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + remove(@Param('relationshipId') relationshipId: string, @Req() req) { + const user : User = req.user; + return this.friendshipService.removeFriendship(relationshipId, user); + } + // GET http://transcendance:8080/api/v2/network/pending @Get('pending') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) findAllPendantFriendshipRequested(@Req() req) { const user = req.user; - - // console.log("WHAT IS UP MY GUYS IT IS YOUR BOI THAT IDIOT YOU HATE Pending 33333"); - return this.friendshipService.findAllPendantRequestsForFriendship(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 d4288d9a..45f14568 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -17,23 +17,16 @@ export class FriendshipService { async findOneFriend(friendshipId: string, username: string) { - const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterUsername: username, status: FriendshipStatus.ACCEPTED } }); + const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderUsername: username, status: FriendshipStatus.ACCEPTED } }); if (!friendship) throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND); return friendship; } - // async findOneFriendByUsername(friendUsername: string, username: string) { - // const friendship = await this.friendshipRepository.find({ where: { friendUsername: +friendshipId, requesterUsername: username, status: FriendshipStatus.ACCEPTED } }); - // if (!friendship) - // throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND); - // return friendship; - // } - - async findOneBlocked(friendshipId: string) { - const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, status: FriendshipStatus.BLOCKED } }); + async findOneBlocked(friendshipId: string, username: string) { + const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderUsername: username, status: FriendshipStatus.BLOCKED } }); if (!friendship) - throw new HttpException(`The requested user not found.`, HttpStatus.NOT_FOUND); + throw new HttpException(`The requested blocked not found.`, HttpStatus.NOT_FOUND); return friendship; } @@ -41,8 +34,8 @@ export class FriendshipService { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') .where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) - .andWhere('friendship.addresseeUsername = :addressee', { addressee: username }) - .orWhere('friendship.requesterUsername = :requester', { requester: username }) + .andWhere('friendship.receiverUsername = :addressee', { addressee: username }) + .orWhere('friendship.senderUsername = :requester', { requester: username }) .andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) .getMany(); for (const friend of friendship) @@ -51,49 +44,53 @@ export class FriendshipService { } async findAllBlockedFriends(username: string) { - return await this.friendshipRepository + const friendships : Friendship[] = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.requesterUsername = :requestee', { requestee: username }) + .where('friendship.senderUsername = :requestee', { requestee: username }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .getMany(); + let partialFriendship : Partial[] = []; + for (const friendship of friendships) { + partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status}); + } + return partialFriendship; } async findAllPendantRequestsForFriendship(username: string) { - return await this.friendshipRepository + const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.requesterUsername = :requestee', { requestee: username }) + .where('friendship.senderUsername = :requestee', { requestee: username }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .getMany(); + let partialFriendship : Partial[] = []; + for (const friend of friendship) { + partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status}); + } + return partialFriendship; } async findAllReceivedRequestsForFriendship(username: string) { - return await this.friendshipRepository + const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.addresseeUsername = :addressee', { addressee: username }) + .where('friendship.receiverUsername = :addressee', { addressee: username }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .getMany(); + let partialFriendship : Partial[] = []; + for (const friend of friendship) { + partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status}); + } + return partialFriendship; } - //GROS CHANTIER - async create(createFriendshipDto: CreateFriendshipDto, creator : User) { - const addressee = await this.userRepository.findOneBy({username: createFriendshipDto.addresseeUsername}); - console.log('made it to create friendship from friendship controller') - - if (!addressee) + async create(createFriendshipDto: CreateFriendshipDto, creator : User) : Promise > { + console.log("DTO : \n") + console.log({...createFriendshipDto}) + const receiver = await this.userRepository.findOneBy({username: createFriendshipDto.receiverUsername}); + if (!receiver) throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND); - console.log('Addressee ID: ' + addressee.id + ' Username: ' + addressee.username) - console.log('Creator ID: ' + creator.id + ' Username: ' + creator.username) - if (creator.id === addressee.id) - throw new HttpException(`You can't add yourself.`, HttpStatus.FORBIDDEN); - console.log('test 1') if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED) throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND); - console.log('test 2') - - const friendship = await this.friendshipRepository.findOneBy({ requesterUsername: createFriendshipDto.requesterUsername, addresseeUsername: createFriendshipDto.addresseeUsername }); - - console.log('test 3') - + const friendship = await this.friendshipRepository.findOneBy({ sender: creator, receiver: receiver }); if (friendship) { if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED) throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK); @@ -104,57 +101,95 @@ export class FriendshipService { else if (friendship.status && friendship.status === FriendshipStatus.DECLINED) throw new HttpException(`The request has been declined.`, HttpStatus.OK); } - console.log('test 7') - - const newFriendship = this.friendshipRepository.create(createFriendshipDto); - console.log('test 8, New Friendship: ') - console.log({...newFriendship}) - - const tmp = this.friendshipRepository.save(newFriendship); - console.log('tmp: ') - console.log({...tmp}) - return tmp; - // return this.friendshipRepository.save(newFriendship); + const newFriendship = new Friendship(); + newFriendship.sender = creator; + newFriendship.senderUsername = creator.username; + newFriendship.receiver = receiver; + newFriendship.receiverUsername = receiver.username; + newFriendship.status = createFriendshipDto.status; + const savedFriendship = this.friendshipRepository.save(newFriendship); + const partialFriendship : Partial = { + id : (await savedFriendship).id, + date : (await savedFriendship).date, + receiverUsername: (await savedFriendship).receiverUsername, + status : (await savedFriendship).status + } + console.log({...partialFriendship}) + return partialFriendship; } - async updateFriendship(relationshipId: string, user: User, status: string) { + async acceptFriendship(relationshipId: string, user: User) { const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); - if (+relation.requesterUsername === user.id) { + if (relation.sender.id === user.id) { throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); } - if (status === FriendshipStatus.ACCEPTED) - relation.status = FriendshipStatus.ACCEPTED; - else if (status === FriendshipStatus.DECLINED) - relation.status = FriendshipStatus.DECLINED; - else if (status === FriendshipStatus.BLOCKED) - relation.status = FriendshipStatus.BLOCKED; - else - throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND); - if (relation.status !== status) - throw new HttpException(`We could not update the status.`, HttpStatus.OK); - return this.friendshipRepository.save(relation); + relation.status = FriendshipStatus.ACCEPTED; + const savedFriendship = this.friendshipRepository.save(relation); + 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) { + async declineFriendship(relationshipId: string, user: User) { + const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); + if (!relation) + throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); + if (relation.sender.id === user.id) { + throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); + } + relation.status = FriendshipStatus.DECLINED; + const savedFriendship = this.friendshipRepository.save(relation); + const partialFriendship : Partial = { + id : (await savedFriendship).id, + date : (await savedFriendship).date, + receiverUsername: (await savedFriendship).receiverUsername, + status : (await savedFriendship).status + } + return partialFriendship + } + + async blockFriendship(relationshipId: string, user: User) { + const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); + if (!relation) + throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); + if (relation.sender.id === user.id) { + throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); + } + relation.status = FriendshipStatus.BLOCKED; + const savedFriendship = this.friendshipRepository.save(relation); + 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) { const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId }); if (!friendship) throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND); + if (friendship.sender.id !== user.id || friendship.receiver.id !== user.id) { + throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN); + } return this.friendshipRepository.remove(friendship); } - async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) { + async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToFindId: string) { console.log("finding if user is blocked") const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.requesterUsername = :requestee', { requestee: userConnectedId }) - .orWhere('friendship.requesterUsername = :requesteeBis', { requesteeBis: userToCheckId }) + .where('friendship.senderUsername = :requestee', { requestee: userConnectedId }) + .orWhere('friendship.senderUsername = :requesteeBis', { requesteeBis: userToFindId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .getOne(); - console.log("printing friendship") - console.log(friendship) - // console.log({...friendship}) if (friendship) { console.log('we are blocked in friendship.service') return true; diff --git a/srcs/requirements/nestjs/api_back/src/users/entities/user.entity.ts b/srcs/requirements/nestjs/api_back/src/users/entities/user.entity.ts index a3c3f904..c00427b3 100644 --- a/srcs/requirements/nestjs/api_back/src/users/entities/user.entity.ts +++ b/srcs/requirements/nestjs/api_back/src/users/entities/user.entity.ts @@ -43,13 +43,11 @@ export class User { @Column({ nullable: true }) secretTwoFactorAuth: string; - @JoinTable() - @OneToMany(type => Friendship , (friendship) => friendship.requesterUsername) - requesterId: Friendship[]; + @OneToMany(type => Friendship , (friendship) => friendship.sender) + sentFriendRequest: Friendship[]; - @JoinTable() - @OneToMany(type => Friendship , (friendship) => friendship.addresseeUsername) - addresseeId: Friendship[]; + @OneToMany(type => Friendship , (friendship) => friendship.receiver) + receivedFriendRequest: Friendship[]; @JoinColumn() @OneToOne(() => UserStats, { cascade: true }) 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 41c9713e..929d93d9 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.service.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.service.ts @@ -60,31 +60,26 @@ export class UsersService { return true; } - async findOneByUsername(userConnectedId : string, username: string) { - const user : User = await this.userRepository.createQueryBuilder('user') + async findOneByUsername(userConnectedId : string, usernameToFind: string) { + const userToFind : User = await this.userRepository.createQueryBuilder('user') .leftJoinAndSelect('user.stats', 'stats') - .where('user.username = :username', { username: username }) + .where('user.username = :username', { username: usernameToFind }) .getOne(); - console.log('USERNAME OF FOUND USER : ' + user.username); + console.log('USERNAME OF FOUND USER : ' + userToFind.username); // console.log({...user}) // you can't do that, you need to do user === undefined // if (user === undefined) - if (!user) + if (!userToFind) throw new HttpException(`The user could not be found 1.`,HttpStatus.NOT_FOUND); - - let tmp = await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()) - console.log('printing return of checking if blocked') - console.log(tmp) - if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString())) { + if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, userToFind.id.toString())) { console.log('we are blocked in user.service') throw new HttpException(`The user could not be found 2.`,HttpStatus.NOT_FOUND); } - const partialUser : Partial = { - username: user.username, - image_url: user.image_url, - status: user.status, - stats: user.stats, + username: userToFind.username, + image_url: userToFind.image_url, + status: userToFind.status, + stats: userToFind.stats, }; return partialUser; } 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 f6175a0c..590a37a3 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -15,7 +15,7 @@ Ideas could be a list of friends and if they're active but i can't see that yet - I click on a thing and it lets me see all the users and i get a button that lets me add them - would that be like a serachable list of all users? - - am i skipping optimization? i mean for now yes, but like + - am i skipping optimization? i mean for now yes, but like @@ -32,12 +32,12 @@ could be a list of friends and if they're active but i can't see that yet let myFriends; let requestsMade, requestsRecieved; let userBeingViewed; - + onMount( async() => { - // yea no idea what - // i mean do i fetch user? i will for now + // yea no idea what + // i mean do i fetch user? i will for now user = await fetch('http://transcendance:8080/api/v2/user') .then( (x) => x.json() ); @@ -82,13 +82,13 @@ could be a list of friends and if they're active but i can't see that yet const displayAllFriends = async() => { myFriends = await fetch('http://transcendance:8080/api/v2/network/myfriends') - .then( x => x.json() ); + .then( x => x.json() ); // console.log('got all friends ' + allFriends) }; const displayRequestsMade = async() => { requestsMade = await fetch('http://transcendance:8080/api/v2/network/pending') - .then( x => x.json() ); + .then( x => x.json() ); // console.log('got requests made ' + requestsMade) }; @@ -110,8 +110,7 @@ could be a list of friends and if they're active but i can't see that yet method : "POST", headers: { 'Content-Type': 'application/json'}, body: JSON.stringify({ - "requesterUsername": user.username, - "addresseeUsername": set.friendUsername, + "receiverUsername": set.friendUsername, "status": "R" }) }) @@ -131,7 +130,7 @@ could be a list of friends and if they're active but i can't see that yet }; const blockAUser = async(friendshipId) => { - + }; @@ -146,7 +145,7 @@ could be a list of friends and if they're active but i can't see that yet {#if allUsers === undefined}
Failed to load all users
{:else} - @@ -196,7 +195,7 @@ could be a list of friends and if they're active but i can't see that yet - + {:catch}

No user to view yet

{/await} --> @@ -227,7 +226,7 @@ could be a list of friends and if they're active but i can't see that yet {/each} {/if} --> - + @@ -291,4 +290,4 @@ could be a list of friends and if they're active but i can't see that yet font-weight: bold; color: red; } - \ No newline at end of file +