From 7961fc4ba24ee20a5bb5c50f9be2555434d4b651 Mon Sep 17 00:00:00 2001 From: Pheuw1 Date: Fri, 17 Mar 2023 16:45:30 +0100 Subject: [PATCH] mute ban kick in back fron --- back/volume/src/chat/chat.controller.ts | 39 +++++------------------- back/volume/src/chat/chat.gateway.ts | 19 +++++------- back/volume/src/chat/chat.service.ts | 15 ++++----- back/volume/src/chat/dto/kickUser.dto.ts | 12 ++++++++ front/volume/src/components/Chat.svelte | 6 ++-- 5 files changed, 38 insertions(+), 53 deletions(-) create mode 100644 back/volume/src/chat/dto/kickUser.dto.ts diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index 2f7a53c..c70efd6 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -87,30 +87,6 @@ export class ChatController { await this.channelService.save(channel) } - @Delete(':id/kick') - async removeUser ( - @Param('id', ParseIntPipe) id: number, - @Body() target: IdDto, - @Profile42() profile: Profile - ): Promise { - const channel = await this.channelService.getFullChannel(id) - if (!(await this.channelService.isAdmin(channel.id, +profile.id))) { - throw new BadRequestException( - 'You are not allowed to kick users from this channel' - ) - } - if (!(await this.channelService.isUser(channel.id, target.id))) { - throw new BadRequestException('User is not in this channel') - } - if (await this.channelService.isOwner(channel.id, target.id)) { - throw new BadRequestException('You cannot kick the owner of the channel') - } - channel.users = channel.users.filter((usr: User) => { - return usr.ftId !== target.id - }) - await this.channelService.save(channel) - } - @Get(':id/users') async getUsersOfChannel ( @Param('id', ParseIntPipe) id: number @@ -166,27 +142,28 @@ export class ChatController { @Post(':id/ban') async addBan ( @Param('id', ParseIntPipe) id: number, - @Body() target: IdDto, - @Body() duration: number, + @Body() target: MuteDto, @Profile42() profile: Profile ): Promise { const channel = await this.channelService.getFullChannel(id) - const user: User | null = await this.usersService.findUser(target.id) + const user: User | null = await this.usersService.findUser(target.data[0]) + console.log(target) if (user == null) { - throw new NotFoundException(`User #${target.id} not found`) + throw new NotFoundException(`User #${target.data[0]} not found`) } if (!(await this.channelService.isAdmin(channel.id, +profile.id))) { throw new BadRequestException( 'You are not allowed to ban users from this channel' ) } - if (await this.channelService.isOwner(channel.id, target.id)) { + if (await this.channelService.isOwner(channel.id, target.data[0])) { throw new BadRequestException('You cannot ban the owner of the channel') } - if (await this.channelService.isBanned(channel.id, target.id)) { + if (await this.channelService.isBanned(channel.id, target.data[0])) { throw new BadRequestException('User is already banned from this channel') } - channel.banned.push([user.id, duration]) + channel.banned.push([target.data[0], Date.now() + target.data[1] * 1000]) + console.log(channel.banned) await this.channelService.save(channel) } diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index 8ca26ab..b4b5454 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -15,6 +15,7 @@ import * as bcrypt from 'bcrypt' import { MessageService } from './message.service' import { CreateMessageDto } from './dto/create-message.dto' import { ConnectionDto } from './dto/connection.dto' +import { kickUserDto } from './dto/kickUser.dto' @WebSocketGateway({ cors: { @@ -52,7 +53,6 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { if (channel.banned.findIndex((ban) => ban[0] === connect.UserId) !== -1) { throw new WsException('You are banned from entering this channel') } - const user = await this.userService.getFullUser(connect.UserId) if (channel.password && channel.password !== '') { if ( @@ -68,7 +68,6 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { ) this.server.to(socket.id).emit('messages', messages) await socket.join(channel.id.toString()) - console.log("joinchan succ") } @SubscribeMessage('getMessages') @@ -84,7 +83,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { @SubscribeMessage('leaveChannel') async onLeaveChannel (socket: Socket): Promise { - const id = socket.id as any + socket.disconnect() } @SubscribeMessage('addMessage') @@ -102,16 +101,12 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { } @SubscribeMessage('kickUser') - async onKickUser ( - socket: Socket, - chan: number, - from: number, - to: number - ): Promise { - const channel = await this.chatService.getChannel(chan) + async onKickUser (socket: Socket, msg: kickUserDto): Promise { + console.log('kick called') + const channel = await this.chatService.getFullChannel(msg.chan) if ( - channel.owner.id !== from || - channel.admins.find((e) => e.id === from) == null + channel.owner.id !== msg.from && + channel.admins.find((e) => e.id == msg.from) == null ) { throw new WsException('You do not have the required privileges') } diff --git a/back/volume/src/chat/chat.service.ts b/back/volume/src/chat/chat.service.ts index e40f8bc..603eeeb 100644 --- a/back/volume/src/chat/chat.service.ts +++ b/back/volume/src/chat/chat.service.ts @@ -40,8 +40,8 @@ export class ChatService { const channels = await this.getChannelsForUser(user.id) const dmAlreadyExists = channels.find((channel: Channel) => { return ( - (channel.name === (user.ftId + '&' + otherUser.ftId) || - channel.name === (otherUser.ftId + '&' + user.ftId)) && + (channel.name === user.ftId + '&' + otherUser.ftId || + channel.name === otherUser.ftId + '&' + user.ftId) && channel.isPrivate && (channel.password === undefined || channel.password === '') ) @@ -110,7 +110,7 @@ export class ChatService { const channels = await this.ChannelRepository.find({}) channels.forEach((channel) => { channel.muted = channel.muted.filter((data) => { - return data[0] - Date.now() > 0 + return data[1] - Date.now() > 0 }) void this.ChannelRepository.save(channel) }) @@ -118,13 +118,14 @@ export class ChatService { @Cron('*/6 * * * * *') async updateBanlists (): Promise { + console.log('checking bans') const channels = await this.ChannelRepository.find({}) - channels.forEach((channel) => { + for (const channel of channels) { + console.log((channel.banned.length) > 0) channel.banned = channel.banned.filter((data) => { return data[1] - Date.now() > 0 }) - void this.ChannelRepository.save(channel) - }) + } } async addUserToChannel (channel: Channel, user: User): Promise { @@ -141,7 +142,7 @@ export class ChatService { } // Warning: those channels users contains socketKey. - // they have to be hidden before returned from a route + // they have to be hidden before returned from a route // but not save them without the key. async getFullChannel (id: number): Promise { const channel = await this.ChannelRepository.findOne({ diff --git a/back/volume/src/chat/dto/kickUser.dto.ts b/back/volume/src/chat/dto/kickUser.dto.ts new file mode 100644 index 0000000..f626ede --- /dev/null +++ b/back/volume/src/chat/dto/kickUser.dto.ts @@ -0,0 +1,12 @@ +import { IsNumber } from 'class-validator' + +export class kickUserDto { + @IsNumber() + chan: number + + @IsNumber() + from: number + + @IsNumber() + to: number +} diff --git a/front/volume/src/components/Chat.svelte b/front/volume/src/components/Chat.svelte index e839ff4..fef317a 100644 --- a/front/volume/src/components/Chat.svelte +++ b/front/volume/src/components/Chat.svelte @@ -162,7 +162,7 @@ headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ id: target.ftId, duration: duration}), + body: JSON.stringify({ data: [target.ftId, duration]}), }); socket.emit("kickUser", channel.id, $store.ftId, target.ftId); } @@ -203,8 +203,8 @@ }); if (response.ok) { const target = await response.json(); - socket.emit("kickUser", channel.id, $store.ftId, target.ftId); - } + socket.emit("kickUser", {chan : channel.id, from : $store.ftId, to: target.ftId}); + } else {alert("merde")} }; //--------------------------------------------------------------------------------/