diff --git a/back/volume/src/users/users.controller.ts b/back/volume/src/users/users.controller.ts index 8248e46..c0016fc 100644 --- a/back/volume/src/users/users.controller.ts +++ b/back/volume/src/users/users.controller.ts @@ -62,13 +62,13 @@ export class UsersController { return await this.usersService.getInvits(profile.id) } - @Get('leader') + @Get('leaderboard') @UseGuards(AuthenticatedGuard) - async getLeader (): Promise { - return await this.usersService.getLeader() + async getLeaderboard (): Promise { + return await this.usersService.getLeaderboard() } - @Get('leader/:id') + @Get('rank/:id') @UseGuards(AuthenticatedGuard) async getRank (@Param('id', ParseIntPipe) id: number): Promise { return await this.usersService.getRank(id) @@ -110,7 +110,7 @@ export class UsersController { description: 'A new avatar for the user', type: AvatarUploadDto }) - async addAvatar ( + async changeAvatar ( @FtUser() profile: Profile, @UploadedFile() file: Express.Multer.File ): Promise { @@ -131,16 +131,20 @@ export class UsersController { return await this.usersService.findUserByName(username) } - @Get('invit/:id') + @Get('invit/:username') @UseGuards(AuthenticatedGuard) async invitUser ( @FtUser() profile: Profile, - @Param('id', ParseIntPipe) id: number + @Param('username') username: string ): Promise { - if (profile.id === id) { + const target = (await this.usersService.findUserByName(username))!; + + if (!target) + throw new BadRequestException("Target unknown.") + if (profile.id === target.ftId) throw new BadRequestException("You can't invit yourself.") - } - return await this.usersService.invit(profile.id, id) + + return await this.usersService.invit(profile.id, target.id) } @Get('avatar/:id') @@ -185,15 +189,11 @@ export class UsersController { @Post() @UseGuards(AuthenticatedGuard) - async create ( + async updateUser ( @Body() payload: UserDto, @FtUser() profile: Profile ): Promise { - const user = await this.usersService.findUser(profile.id) - if (user != null) { - return await this.usersService.update(user, payload) - } else { - return await this.usersService.create(payload) - } + const user = (await this.usersService.findUser(profile.id))! + return await this.usersService.update(user, payload) } } diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index a40c26d..5ffece3 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -1,179 +1,169 @@ -import { Catch, Injectable, NotFoundException } from '@nestjs/common' -import { InjectRepository } from '@nestjs/typeorm' -import { EntityNotFoundError, QueryFailedError, Repository } from 'typeorm' -import { User } from './entity/user.entity' -import { type UserDto } from './dto/user.dto' -import { type Channel } from 'src/chat/entity/channel.entity' -import type Result from 'src/pong/entity/result.entity' -import { Cron } from '@nestjs/schedule' +import { + BadRequestException, + Catch, + Injectable, + NotFoundException, +} from "@nestjs/common"; +import { InjectRepository } from "@nestjs/typeorm"; +import { EntityNotFoundError, QueryFailedError, Repository } from "typeorm"; +import { User } from "./entity/user.entity"; +import { type UserDto } from "./dto/user.dto"; +import { type Channel } from "src/chat/entity/channel.entity"; +import type Result from "src/pong/entity/result.entity"; +import { Cron } from "@nestjs/schedule"; @Injectable() @Catch(QueryFailedError, EntityNotFoundError) export class UsersService { - constructor ( + constructor( @InjectRepository(User) private readonly usersRepository: Repository ) {} - save (user: User) { - this.usersRepository.save(user) + save(user: User) { + this.usersRepository.save(user); } - async findUsers (): Promise { - return await this.usersRepository.find({}) + async findUsers(): Promise { + return await this.usersRepository.find({}); } - async findUserByName (username: string): Promise { + async findUserByName(username: string): Promise { const user = await this.usersRepository.findOne({ where: { username }, - relations: { results: true } - }) - return user + relations: { results: true }, + }); + return user; } - @Cron('0 * * * * *') - async updateStatus () { - const users = await this.usersRepository.find({}) + @Cron("0 * * * * *") + async updateStatus() { + const users = await this.usersRepository.find({}); users.forEach((usr) => { if (Date.now() - usr.lastAccess > 60000) { - usr.status = 'offline' - this.usersRepository.save(usr) + usr.status = "offline"; + this.usersRepository.save(usr); } - }) + }); } - async findUser (ftId: number): Promise { - const user = await this.usersRepository.findOneBy({ ftId }) - if (user == null) return null - user.lastAccess = Date.now() - user.status = 'online' - this.usersRepository.save(user) - return user + async findUser(ftId: number): Promise { + const user = await this.usersRepository.findOneBy({ ftId }); + if (user == null) return null; + user.lastAccess = Date.now(); + user.status = "online"; + this.usersRepository.save(user); + return user; } - async findOnlineUsers (): Promise { - return await this.usersRepository.find({ where: { status: 'online' } }) + async findOnlineUsers(): Promise { + return await this.usersRepository.find({ where: { status: "online" } }); } - async create (userData: UserDto) { + async create(userData: UserDto) { try { - const newUser = this.usersRepository.create(userData) - return await this.usersRepository.save(newUser) + const newUser = this.usersRepository.create(userData); + return await this.usersRepository.save(newUser); } catch (err) { - throw new Error(`Error creating user ${err}`) + throw new Error(`Error creating user ${err}`); } } - async findOnlineInChannel (channel: Channel): Promise { + async findOnlineInChannel(channel: Channel): Promise { return await this.usersRepository - .createQueryBuilder('user') - .where('user.channel = :chan', { chan: channel }) - .andWhere('user.status := status)', { status: 'online' }) - .getMany() + .createQueryBuilder("user") + .where("user.channel = :chan", { chan: channel }) + .andWhere("user.status := status)", { status: "online" }) + .getMany(); } - async update (user: User, changes: UserDto): Promise { - this.usersRepository.merge(user, changes) - return await this.usersRepository.save(user) + async update(user: User, changes: UserDto): Promise { + this.usersRepository.merge(user, changes); + return await this.usersRepository.save(user); } - async addAvatar (ftId: number, filename: string) { + async addAvatar(ftId: number, filename: string) { return await this.usersRepository.update( { ftId }, { - avatar: filename + avatar: filename, } - ) + ); } - async getFriends (ftId: number): Promise { + async getFriends(ftId: number): Promise { const user = await this.usersRepository.findOne({ where: { ftId }, relations: { - friends: true - } - }) - if (user != null) return user.friends - return [] + friends: true, + }, + }); + if (user != null) return user.friends; + return []; } - async getInvits (ftId: number): Promise { + async getInvits(ftId: number): Promise { const user = await this.usersRepository.findOne({ where: { ftId }, relations: { - followers: true - } - }) - if (user != null) return user.followers - return [] + followers: true, + }, + }); + if (user != null) return user.followers; + return []; } - async getResults (ftId: number): Promise { + async getResults(ftId: number): Promise { const user = await this.usersRepository.findOne({ where: { ftId }, relations: { results: { - players: true - } - } - }) - if (user != null) return user.results - return [] + players: true, + }, + }, + }); + if (user != null) return user.results; + return []; } - async getLeader (): Promise { + async getLeaderboard(): Promise { return await this.usersRepository.find({ order: { - winrate: 'DESC' - } - }) + winrate: "DESC", + }, + }); } - async getRank (ftId: number): Promise { - const leader = await this.getLeader() - return leader.findIndex((user) => user.ftId === ftId) + async getRank(ftId: number): Promise { + const leader = await this.getLeaderboard(); + return leader.findIndex((user) => user.ftId === ftId); } - async invit (ftId: number, targetFtId: number): Promise { - const user = await this.usersRepository.findOne({ + async invit(ftId: number, targetFtId: number): Promise { + const user: User = (await this.usersRepository.findOne({ where: { ftId }, relations: { followers: true, - friends: true - } - }) - if (user == null) { - return new NotFoundException(`Error: user id ${ftId} isn't in our db.`) - } - if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) { - return null - } - const target = await this.usersRepository.findOne({ + friends: true, + }, + }))!; + if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) + return new BadRequestException("You are already friends."); + const target = (await this.usersRepository.findOne({ where: { ftId: targetFtId }, relations: { followers: true, - friends: true - } - }) - if (target == null) { - return new NotFoundException( - `Error: user id ${targetFtId} isn't in our db.` - ) - } + friends: true, + }, + }))!; const id = user.followers.findIndex( (follower) => follower.ftId === targetFtId - ) + ); if (id !== -1) { - console.log( - `Friend relation complete between ${user.username} and ${target.username}` - ) - user.friends.push(target) - if (user.ftId !== target.ftId) target.friends.push(user) - user.followers.slice(id, 1) - await this.usersRepository.save(user) - } else { - console.log(`You asked ${target.username} to be your friend.`) - target.followers.push(user) - } - await this.usersRepository.save(target) + user.friends.push(target); + if (user.ftId !== target.ftId) target.friends.push(user); + user.followers.slice(id, 1); + await this.usersRepository.save(user); + } else target.followers.push(user); + await this.usersRepository.save(target); } } diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index 26f3ce8..4e4dc2c 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -1,19 +1,22 @@ diff --git a/front/volume/src/components/Leaderboard.svelte b/front/volume/src/components/Leaderboard.svelte index d231201..5ce2ecc 100644 --- a/front/volume/src/components/Leaderboard.svelte +++ b/front/volume/src/components/Leaderboard.svelte @@ -1,15 +1,6 @@ - diff --git a/front/volume/src/components/Profile.svelte b/front/volume/src/components/Profile.svelte index 4b0993e..68aea1e 100644 --- a/front/volume/src/components/Profile.svelte +++ b/front/volume/src/components/Profile.svelte @@ -1,15 +1,20 @@ + +