From 8c6a8768900a1d92c0b8ba2c364394c7246f84a3 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Wed, 1 Mar 2023 16:20:13 +0100 Subject: [PATCH] friends --- back/volume/src/auth/42.strategy.ts | 10 ++-- back/volume/src/chat/chat.gateway.ts | 4 +- .../src/chat/model/update-channel.dto.ts | 8 +-- back/volume/src/users/user.dto.ts | 2 +- back/volume/src/users/user.entity.ts | 7 +-- back/volume/src/users/users.controller.ts | 12 ++--- back/volume/src/users/users.service.ts | 52 +++++++++++-------- 7 files changed, 52 insertions(+), 43 deletions(-) diff --git a/back/volume/src/auth/42.strategy.ts b/back/volume/src/auth/42.strategy.ts index d4db811..02855b9 100644 --- a/back/volume/src/auth/42.strategy.ts +++ b/back/volume/src/auth/42.strategy.ts @@ -30,15 +30,15 @@ export class FtStrategy extends PassportStrategy(Strategy, '42') { ): Promise { request.session.accessToken = accessToken console.log('accessToken', accessToken, 'refreshToken', refreshToken) - const id_42 = profile.id as number + const ftId = profile.id as number console.log(profile) - if ((await this.usersService.getOneUser42(id_42)) === null) { + if ((await this.usersService.findUser(ftId)) === null) { const newUser = new User() - newUser.id_42 = profile.id as number + newUser.ftId = profile.id as number newUser.username = profile.displayName as string - newUser.avatar = id_42 + '.jpg' + newUser.avatar = ftId + '.jpg' this.usersService.create(newUser) - const file = createWriteStream('avatars/' + id_42 + '.jpg') + const file = createWriteStream('avatars/' + ftId + '.jpg') get(profile._json.image.versions.small, function (response) { response.pipe(file) }) diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index 39f2ee5..ba37ccf 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -36,7 +36,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { async handleConnection (socket: Socket) { try { - const user: User = await this.userService.findOne(socket.data.user.id) + const user: User = await this.userService.findUser(socket.data.user.ftId) if (!user) { socket.emit('Error', new UnauthorizedException()) // socket.disconnect(); @@ -64,7 +64,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { ): Promise { const channel = new Channel() channel.name = channeldto.name - const owner = await this.userService.findOne(channeldto.owner) + const owner = await this.userService.findUser(channeldto.owner) channel.owners.push(owner) channel.password = channeldto.password /// .../// diff --git a/back/volume/src/chat/model/update-channel.dto.ts b/back/volume/src/chat/model/update-channel.dto.ts index b9f324a..e31db41 100644 --- a/back/volume/src/chat/model/update-channel.dto.ts +++ b/back/volume/src/chat/model/update-channel.dto.ts @@ -11,11 +11,13 @@ export class UpdateChannelDto extends PartialType(CreateChannelDto) { messages: [Message] - owners: [number] // user id + owners: [number] // ftId + + admins: [number] - banned: [number] // user id + banned: [number] // ftId - muted: [number] // user id + muted: [number] // ftId @IsString() password: string diff --git a/back/volume/src/users/user.dto.ts b/back/volume/src/users/user.dto.ts index 76370b6..09a4925 100644 --- a/back/volume/src/users/user.dto.ts +++ b/back/volume/src/users/user.dto.ts @@ -11,7 +11,7 @@ import { Express } from 'express' export class UserDto { @IsPositive() @IsNotEmpty() - readonly id_42: number + readonly ftId: number @IsString() @IsNotEmpty() diff --git a/back/volume/src/users/user.entity.ts b/back/volume/src/users/user.entity.ts index bdda256..af8a8b6 100644 --- a/back/volume/src/users/user.entity.ts +++ b/back/volume/src/users/user.entity.ts @@ -16,7 +16,7 @@ export class User { id: number; @Column({ unique: true }) - id_42: number + ftId: number @Column({ unique: true }) username: string @@ -37,13 +37,10 @@ export class User { @JoinTable() blocked: User[] - @ManyToMany(() => User, (user) => user.following) + @ManyToMany(() => User) @JoinTable() followers: User[] - @ManyToMany(() => User, (user) => user.followers) - following: User[] - @ManyToMany(() => User, (user) => user.friends) friends: User[] diff --git a/back/volume/src/users/users.controller.ts b/back/volume/src/users/users.controller.ts index 44c2b65..f1886ac 100644 --- a/back/volume/src/users/users.controller.ts +++ b/back/volume/src/users/users.controller.ts @@ -36,7 +36,7 @@ export class UsersController { @Get() async getAllUsers(): Promise { - return await this.usersService.getAllUsers() + return await this.usersService.findUsers() } @Post() @@ -44,7 +44,7 @@ export class UsersController { async create( @Body() payload: UserDto, @FtUser() profile: Profile) { - const user = await this.usersService.getOneUser42(profile.id); + const user = await this.usersService.findUser(profile.id); if (user) { return await this.usersService.update(user.id, payload) } else { @@ -52,13 +52,13 @@ export class UsersController { } } - @Post("follow/:target") + @Post("invit/:id") @UseGuards(AuthenticatedGuard) followUser( @FtUser() profile: Profile, - @Param('target, ParseIntPipe') target: number, + @Param('id, ParseIntPipe') id: number, ) { - this.usersService.follow(profile.id, target); + this.usersService.invit(profile.id, id); } @Post('avatar') @@ -94,7 +94,7 @@ export class UsersController { @FtUser() profile: Profile, @Res({ passthrough: true }) response: Response ) { - const user = await this.usersService.getOneUser42(profile.id) + const user = await this.usersService.findUser(profile.id) const filename = user.avatar const stream = createReadStream(join(process.cwd(), 'avatars/' + filename)) response.set({ diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index 2d71898..c8a3a7b 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -7,23 +7,23 @@ import { type Channel } from 'src/chat/model/channel.entity' @Injectable() export class UsersService { - constructor ( + constructor( @InjectRepository(User) private readonly usersRepository: Repository - ) {} + ) { } - async getAllUsers (): Promise { + async findUsers(): Promise { return await this.usersRepository.find({}) } - async getOneUser (username: string): Promise { + async findUserByName(username: string): Promise { return await this.usersRepository.findOneBy({ username }) } - async getOneUser42 (id_42: number): Promise { - return await this.usersRepository.findOneBy({ id_42 }) + async findUser(ftId: number): Promise { + return await this.usersRepository.findOneBy({ ftId }) } - async create (userData: UserDto) { + async create(userData: UserDto) { try { const newUser = this.usersRepository.create(userData) return await this.usersRepository.save(newUser) @@ -32,13 +32,7 @@ export class UsersService { } } - async findOne (id: number) { - const user = await this.usersRepository.findOneBy({ id }) - if (user) return user - throw new NotFoundException(`User #${id} not found`) - } - - async findOnlineInChannel (channel: Channel): Promise { + async findOnlineInChannel(channel: Channel): Promise { return await this.usersRepository .createQueryBuilder('user') .where('user.channel = :chan', { chan: channel }) @@ -46,20 +40,36 @@ export class UsersService { .getMany() } - async update (id: number, changes: UserDto) { - const updatedUser = await this.findOne(id) + async update(ftId: number, changes: UserDto) { + const updatedUser = await this.findUser(ftId) this.usersRepository.merge(updatedUser, changes) return await this.usersRepository.save(updatedUser) } - async addAvatar (userId: number, filename: string) { - await this.usersRepository.update(userId, { + async addAvatar(ftId: number, filename: string) { + await this.usersRepository.update(ftId, { avatar: filename }) } - async follow(userFtId: number, targetFtId: number) { - const user = await this.getOneUser42(userFtId); - const target = await this.getOneUser42(targetFtId); + async invit(ftId: number, targetFtId: number) { + const user = await this.usersRepository.findOne({ + where: { ftId }, + relations: { friends: true, followers: true }, + }); + const target = await this.usersRepository.findOne({ + where: { ftId: targetFtId }, + relations: { friends: true, followers: true }, + }); + const id = user.followers.findIndex((follower) => follower.ftId == ftId) + if (id != -1) { + user.friends.push(target); + target.friends.push(user); + user.followers.slice(id, 1); + this.usersRepository.save(user); + } else { + target.followers.push(user); + } + this.usersRepository.save(target); } }