diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index e13aca0..accdd58 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -29,6 +29,24 @@ export class ChatController { private readonly usersService: UsersService ) {} + @Post(':id/invite') + async addUser (@Param('id') id: number, @Body() userId: number) { + const channel = await this.channelService.getChannel(id) + const user: User | null = await this.usersService.findUser(userId) + if (user == null) throw new NotFoundException(`User #${userId} not found`) + channel.users.push(user) + this.channelService.update(channel) + } + + @Delete(':id/kick') + async removeUser (@Param('id') id: number, @Body() userId: number) { + const channel = await this.channelService.getChannel(id) + channel.users = channel.admins.filter((usr: User) => { + return usr.ftId !== userId + }) + this.channelService.update(channel) + } + @Post(':id/admin') async addAdmin (@Param('id') id: number, @Body() userId: number) { const channel = await this.channelService.getChannel(id) @@ -76,7 +94,10 @@ export class ChatController { @Delete(':id') @UseGuards(AuthenticatedGuard) - async deleteChannel (@Profile42() profile: Profile, @Param('id') id: number) { + async deleteChannel ( + @Profile42() profile: Profile, + @Param('id') id: number + ) { if (await this.channelService.isOwner(id, +profile.id)) { await this.channelService.removeChannel(id) return @@ -84,10 +105,25 @@ export class ChatController { throw new BadRequestException('You are not the owner of this channel') } + @Post(':id/password') + @UseGuards(AuthenticatedGuard) + async updatePassword ( + @Profile42() profile: Profile, + @Param('id') id: number, + @Body() password: string + ) { + if (await this.channelService.isOwner(id, +profile.id)) { + await this.channelService.updatePassword(id, password) + return + } + throw new BadRequestException('You are not the owner of this channel') + } + + @Get() @UseGuards(AuthenticatedGuard) async getChannelsForUser (@Profile42() profile: Profile): Promise { - return await this.channelService.getChannelsForUser(profile.id) + return await this.channelService.getChannelsForUser(+profile.id) } @Post() diff --git a/back/volume/src/chat/chat.service.ts b/back/volume/src/chat/chat.service.ts index 0b50500..29eea35 100644 --- a/back/volume/src/chat/chat.service.ts +++ b/back/volume/src/chat/chat.service.ts @@ -19,9 +19,8 @@ export class ChannelService { async createChannel (channel: CreateChannelDto): Promise { const user: User | null = await this.usersService.findUser(channel.owner) - if (user == null) { + if (user == null) throw new NotFoundException(`User #${channel.owner} not found`) - } const newChannel = new Channel() newChannel.owner = user newChannel.users = [user] @@ -32,6 +31,13 @@ export class ChannelService { return await this.ChannelRepository.save(newChannel) } + async updatePassword (id: number, password: string) { + let channel: Channel | null = await this.ChannelRepository.findOneBy({id}) + if (channel === null) { throw new NotFoundException(`Channel #${id} not found`) } + channel.password = password + await this.ChannelRepository.save(channel) + } + async getChannelsForUser (ftId: number): Promise { let rooms: Channel[] = [] rooms = [ @@ -76,8 +82,7 @@ export class ChannelService { relations: { owner: true } }) if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) } - console.log(channel.owner.ftId, userId) - return channel.owner.ftId == userId + return channel.owner.ftId === userId } async isAdmin (id: number, userId: number): Promise { @@ -86,7 +91,7 @@ export class ChannelService { relations: { admins: true } }) if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) } - return channel.admins.findIndex((user) => user.ftId == userId) != -1 + return channel.admins.findIndex((user) => user.ftId === userId) != -1 } async isUser (id: number, userId: number): Promise { @@ -95,7 +100,7 @@ export class ChannelService { relations: { users: true } }) if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) } - return channel.users.findIndex((user) => user.ftId == userId) != -1 + return channel.users.findIndex((user) => user.ftId === userId) != -1 } async isBanned (id: number, userId: number): Promise { @@ -104,7 +109,7 @@ export class ChannelService { relations: { banned: true } }) if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) } - return channel.banned.findIndex((user) => user.ftId == userId) != -1 + return channel.banned.findIndex((user) => user.ftId === userId) != -1 } async isMuted (id: number, userId: number): Promise { @@ -113,6 +118,6 @@ export class ChannelService { relations: { muted: true } }) if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) } - return channel.muted.findIndex((user) => user.ftId == userId) != -1 + return channel.muted.findIndex((user) => user.ftId === userId) != -1 } } diff --git a/back/volume/src/users/users.controller.ts b/back/volume/src/users/users.controller.ts index 6673313..a4b04a5 100644 --- a/back/volume/src/users/users.controller.ts +++ b/back/volume/src/users/users.controller.ts @@ -31,9 +31,11 @@ import { type Request, Response } from 'express' import { createReadStream } from 'fs' import { join } from 'path' -@Controller('users') +@Controller("users") export class UsersController { - constructor (private readonly usersService: UsersService) {} + constructor ( + private readonly usersService: UsersService, + ) {} @Get('all') async getAllUsers (): Promise { @@ -68,7 +70,6 @@ export class UsersController { async getRank (@Param('id', ParseIntPipe) id: number): Promise { return await this.usersService.getRank(id) } - @Post('avatar') @UseGuards(AuthenticatedGuard) @Redirect('http://localhost') @@ -118,7 +119,7 @@ export class UsersController { @Get('invit/:username') @UseGuards(AuthenticatedGuard) async invitUser ( - @Profile42() profile: Profile, + @Profile42() profile: Profile, @Param('username') username: string ): Promise { const target: User | null = await this.usersService.findUserByName( @@ -127,7 +128,7 @@ export class UsersController { if (target === null) { throw new BadRequestException(`User ${username} not found.`) } - if (profile.id === target.ftId) { + if (+profile.id === +target.ftId) { throw new BadRequestException("You can't invite yourself.") } const ret: string = await this.usersService.invit(profile.id, target.ftId) diff --git a/front/volume/src/components/Profile.svelte b/front/volume/src/components/Profile.svelte index 4af3aeb..8a9a71b 100644 --- a/front/volume/src/components/Profile.svelte +++ b/front/volume/src/components/Profile.svelte @@ -68,7 +68,7 @@

===| {user.username}'s Profile |===

- {#if edit} + {#if !edit} avatar {:else}