From 042e6879b146977f903fbe7872a7fcad872d8163 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Tue, 7 Mar 2023 05:27:19 +0100 Subject: [PATCH] fix profile username change and avatar sizing --- .gitignore | 1 + back/volume/src/app.module.ts | 2 ++ back/volume/src/auth/42.strategy.ts | 3 --- back/volume/src/auth/auth.controller.ts | 2 +- back/volume/src/users/dto/user.dto.ts | 3 +++ back/volume/src/users/entity/user.entity.ts | 8 ++++---- back/volume/src/users/users.controller.ts | 9 +++++++-- back/volume/src/users/users.module.ts | 6 ++++-- back/volume/src/users/users.service.ts | 10 +++++----- front/volume/src/App.svelte | 6 +++--- front/volume/src/components/Profile.svelte | 15 +++++++-------- 11 files changed, 37 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 4c49bd7..26deeea 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +postgres diff --git a/back/volume/src/app.module.ts b/back/volume/src/app.module.ts index 490ccf2..e5aae51 100644 --- a/back/volume/src/app.module.ts +++ b/back/volume/src/app.module.ts @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common' import { ConfigModule, ConfigService } from '@nestjs/config' import { TypeOrmModule } from '@nestjs/typeorm' import * as Joi from 'joi' +import { ScheduleModule } from '@nestjs/schedule' import { AuthModule } from './auth/auth.module' import { ChatModule } from './chat/chat.module' @@ -10,6 +11,7 @@ import { UsersModule } from './users/users.module' @Module({ imports: [ + ScheduleModule.forRoot(), ConfigModule.forRoot({ validationSchema: Joi.object({ POSTGRES_HOST: Joi.string().required(), diff --git a/back/volume/src/auth/42.strategy.ts b/back/volume/src/auth/42.strategy.ts index 4ab5974..4bab1b0 100644 --- a/back/volume/src/auth/42.strategy.ts +++ b/back/volume/src/auth/42.strategy.ts @@ -7,7 +7,6 @@ import { createWriteStream } from 'fs' import { UsersService } from 'src/users/users.service' import { User } from 'src/users/entity/user.entity' -import { randomUUID } from 'crypto' @Injectable() export class FtStrategy extends PassportStrategy(Strategy, '42') { @@ -37,10 +36,8 @@ export class FtStrategy extends PassportStrategy(Strategy, '42') { if ((await this.usersService.findUser(ftId)) === null) { const newUser = new User() newUser.ftId = profile.id as number - newUser.socketKey = randomUUID() newUser.username = profile.username as string newUser.avatar = `${ftId}.jpg` - newUser.lastAccess = Date.now() void this.usersService.create(newUser) const file = createWriteStream(`avatars/${ftId}.jpg`) get(profile._json.image.versions.small, function (response) { diff --git a/back/volume/src/auth/auth.controller.ts b/back/volume/src/auth/auth.controller.ts index 1de89b7..9c5c14c 100644 --- a/back/volume/src/auth/auth.controller.ts +++ b/back/volume/src/auth/auth.controller.ts @@ -9,7 +9,7 @@ import { FtUser } from './42.decorator' export class AuthController { @Get('in') @UseGuards(FtOauthGuard) - ftAuth () {} + ftAuth (): void {} @Get('inReturn') @UseGuards(FtOauthGuard) diff --git a/back/volume/src/users/dto/user.dto.ts b/back/volume/src/users/dto/user.dto.ts index 399dc2a..411ab87 100644 --- a/back/volume/src/users/dto/user.dto.ts +++ b/back/volume/src/users/dto/user.dto.ts @@ -14,6 +14,9 @@ export class UserDto { @IsOptional() readonly status: string + + @IsOptional() + readonly avatar: string } export class AvatarUploadDto { diff --git a/back/volume/src/users/entity/user.entity.ts b/back/volume/src/users/entity/user.entity.ts index 319d851..c55b00f 100644 --- a/back/volume/src/users/entity/user.entity.ts +++ b/back/volume/src/users/entity/user.entity.ts @@ -7,6 +7,7 @@ import { JoinTable, UpdateDateColumn } from 'typeorm' +import { randomUUID } from 'crypto' import Message from 'src/chat/entity/message.entity' import Channel from 'src/chat/entity/channel.entity' @@ -17,13 +18,13 @@ export class User { @PrimaryGeneratedColumn() id: number - @Column({type: "bigint"}) + @Column({ type: 'bigint', default: Date.now() }) lastAccess: number @Column({ unique: true }) ftId: number - @Column({ unique: true }) + @Column({ unique: true, default: randomUUID() }) socketKey: string @Column({ unique: true }) @@ -32,7 +33,7 @@ export class User { @Column({ default: 'online' }) status: string - @Column({ name: 'avatar' }) + @Column({ name: 'avatar'}) avatar: string @Column({ default: 0 }) @@ -71,7 +72,6 @@ export class User { @ManyToMany(() => User) @JoinTable() friends: User[] - } export default User diff --git a/back/volume/src/users/users.controller.ts b/back/volume/src/users/users.controller.ts index ad7d1fa..72f826f 100644 --- a/back/volume/src/users/users.controller.ts +++ b/back/volume/src/users/users.controller.ts @@ -82,7 +82,9 @@ export class UsersController { @Get('history/:id') @UseGuards(AuthenticatedGuard) - async getHistoryById (@Param('id', ParseIntPipe) id: number): Promise { + async getHistoryById ( + @Param('id', ParseIntPipe) id: number + ): Promise { return await this.pongService.getHistoryById(id) } @@ -180,7 +182,10 @@ export class UsersController { @Post() @UseGuards(AuthenticatedGuard) - async create (@Body() payload: UserDto, @FtUser() profile: Profile): Promise { + async create ( + @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) diff --git a/back/volume/src/users/users.module.ts b/back/volume/src/users/users.module.ts index 16aed34..471f760 100644 --- a/back/volume/src/users/users.module.ts +++ b/back/volume/src/users/users.module.ts @@ -4,10 +4,12 @@ import { User } from './entity/user.entity' import { UsersController } from './users.controller' import { UsersService } from './users.service' import { PongModule } from 'src/pong/pong.module' -import { ScheduleModule } from '@nestjs/schedule' @Module({ - imports: [forwardRef(() => PongModule), TypeOrmModule.forFeature([User]), ScheduleModule.forRoot()], + imports: [ + forwardRef(() => PongModule), + TypeOrmModule.forFeature([User]) + ], controllers: [UsersController], providers: [UsersService], exports: [UsersService] diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index 2c743fc..ee4e604 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -34,18 +34,18 @@ export class UsersService { async updateStatus() { let users = await this.usersRepository.find({}) users.forEach((usr) => { - if (Date.now() - usr.lastAccess > 60) { - usr.status= "offline" + if (Date.now() - usr.lastAccess > 60000) { + usr.status = 'offline' this.usersRepository.save(usr) } }) } async findUser (ftId: number): Promise { - let user = await this.usersRepository.findOneBy({ ftId }) - if (!user) return null + const user = await this.usersRepository.findOneBy({ ftId }) + if (user == null) return null user.lastAccess = Date.now() - user.status = "online" + user.status = 'online' this.usersRepository.save(user) return user } diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index 11e9bd3..ab4c758 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -24,7 +24,7 @@ }); setInterval(() => { getUser(); - },15000) + }, 15000); let isProfileOpen = false; function clickProfile() { @@ -90,7 +90,7 @@ friendsInterval = setInterval(async () => { friends = await getFriends(); invits = await getInvits(); - }, 5000) + }, 5000); } // SPECTATE @@ -198,7 +198,7 @@
{ isFriendOpen = false; - clearInterval(friendsInterval) + clearInterval(friendsInterval); }} on:keydown={() => { isFriendOpen = false; diff --git a/front/volume/src/components/Profile.svelte b/front/volume/src/components/Profile.svelte index 482220c..453f82b 100644 --- a/front/volume/src/components/Profile.svelte +++ b/front/volume/src/components/Profile.svelte @@ -1,7 +1,7 @@