From 94766845fd14119bdff06b5e02d2b35e7bf9291e Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Thu, 16 Mar 2023 17:40:07 +0100 Subject: [PATCH] fix chat newmessages broadcast and some thigns not sure what --- back/volume/src/chat/chat.controller.ts | 16 ++++++++++++++++ back/volume/src/chat/chat.gateway.ts | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index b8fc61f..f91b1b8 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -232,6 +232,22 @@ export class ChatController { }) } + @Get(':id/leave') + async leaveChannel ( + @Profile42() profile: Profile, + @Param('id', ParseIntPipe) id: number + ): Promise { + if (await this.channelService.isOwner(id, +profile.id)) { + this.channelService.removeChannel(id) // -> verify that the deletion the break others users behaviors. + } + const channel = await this.channelService.getFullChannel(id) + channel.users = channel.users.filter((usr: User) => { + return usr.ftId !== profile.id + }) + await this.channelService.save(channel) + } + + @Post() async createChannel (@Body() channel: CreateChannelDto): Promise { return await this.channelService.createChannel(channel) diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index 3c7821d..642ea03 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -81,7 +81,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { } @SubscribeMessage('addMessage') - async onAddMessage (socket: Socket, message: CreateMessageDto): Promise { + async onAddMessage (message: CreateMessageDto): Promise { console.log(JSON.stringify(message)) const channel = await this.chatService.getChannel(message.ChannelId) if ( @@ -93,7 +93,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { message ) console.log(createdMessage) - socket.in(channel.id.toString()).emit('newMessage', createdMessage) + this.server.to(channel.id.toString()).emit('newMessage', createdMessage) } @SubscribeMessage('kickUser')