From 7b1fa18a84a20ed1df168827f0831addeace1914 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Fri, 17 Mar 2023 13:05:30 +0100 Subject: [PATCH] fix chat socket not recreated --- back/volume/src/chat/chat.controller.ts | 68 ++++++++++++--------- back/volume/src/chat/chat.gateway.ts | 16 ++--- back/volume/src/chat/message.service.ts | 1 + front/volume/src/components/Channels.svelte | 26 ++++---- front/volume/src/components/Chat.svelte | 29 +++++---- 5 files changed, 74 insertions(+), 66 deletions(-) diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index eda3ec6..3c9b907 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -30,6 +30,34 @@ export class ChatController { private readonly usersService: UsersService ) {} + @Get('dms/:otherName') + async getDMsForUser ( + @Profile42() profile: Profile, + @Param('otherName') otherName: string + ): Promise { + const user = await this.usersService.findUser(profile.fdId) + const other = await this.usersService.findUserByName(otherName) + const channels = await this.channelService.getChannelsForUser(+profile.id) + + if (user === null) { + throw new BadRequestException('User not found') + } + const dms = channels.filter((channel: Channel) => { + return ( + (channel.name === (user.ftId + '&' + other.ftId) || + channel.name === (other.ftId + '&' + user.ftId)) && + channel.isPrivate && + (channel.password === undefined || channel.password === '') + ) + }) + dms.forEach((c) => { + c.users.forEach((u) => u.socketKey = '') + c.admins.forEach((u) => u.socketKey = '') + c.owner.socketKey = '' + }) + return dms + } + @Post(':id/invite') async addUser ( @Param('id', ParseIntPipe) id: number, @@ -211,34 +239,6 @@ export class ChatController { await this.channelService.updatePassword(id, data.password) } - @Get() - async getChannelsForUser (@Profile42() profile: Profile): Promise { - return await this.channelService.getChannelsForUser(+profile.id) - } - - @Get('dms/:otherName') - async getDMsForUser ( - @Profile42() profile: Profile, - @Param('otherName') otherName: string - ): Promise { - const user = await this.usersService.findUser(profile.fdId) - const other = await this.usersService.findUserByName(otherName) - const channels = await this.channelService.getChannelsForUser(+profile.id) - - if (user === null) { - throw new BadRequestException('User not found') - } - const dms = channels.filter((channel: Channel) => { - return ( - (channel.name === (user.ftId + '&' + other.ftId) || - channel.name === (other.ftId + '&' + user.ftId)) && - channel.isPrivate && - (channel.password === undefined || channel.password === '') - ) - }) - return dms - } - @Get(':id/leave') async leaveChannel ( @Profile42() profile: Profile, @@ -254,8 +254,18 @@ export class ChatController { await this.channelService.save(channel) } + @Get() + async getChannelsForUser (@Profile42() profile: Profile): Promise { + let chan = await this.channelService.getChannelsForUser(+profile.id) + return chan; + } + @Post() async createChannel (@Body() channel: CreateChannelDto): Promise { - return await this.channelService.createChannel(channel) + let chan = await this.channelService.createChannel(channel) + chan.users.forEach((u) => u.socketKey = '') + chan.admins.forEach((u) => u.socketKey = '') + chan.owner.socketKey = '' + return chan; } } diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index b43cc23..7178b11 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -45,14 +45,10 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { connect.pwd ) const channel = await this.chatService.getFullChannel(connect.ChannelId) - console.log('1') if (channel.banned.findIndex((ban) => ban.ftId === connect.UserId) !== -1) { throw new WsException('You are banned from entering this channel') } - console.log('2') const user = await this.userService.getFullUser(connect.UserId) - console.log('3') - console.log('Channel psw: ', channel.password) if (channel.password && channel.password !== '') { if ( !connect.pwd || @@ -61,18 +57,22 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { throw new WsException('Wrong password') } } else await this.chatService.addUserToChannel(channel, user) - console.log('5') - console.log('8') const messages = await this.messageService.findMessagesInChannelForUser( channel, user ) - console.log('9') this.server.to(socket.id).emit('messages', messages) - console.log('10') await socket.join(channel.id.toString()) } + @SubscribeMessage('getMessages') + async onGetMessages (socket: Socket, connect: ConnectionDto): Promise { + const user = await this.userService.getFullUser(connect.UserId) + const channel = await this.chatService.getFullChannel(connect.ChannelId) + const messages = await this.messageService.findMessagesInChannelForUser(channel, user) + this.server.to(socket.id).emit('messages', messages) + } + @SubscribeMessage('leaveChannel') async onLeaveChannel (socket: Socket): Promise { const id = socket.id as any diff --git a/back/volume/src/chat/message.service.ts b/back/volume/src/chat/message.service.ts index 4629edd..ad3d81f 100644 --- a/back/volume/src/chat/message.service.ts +++ b/back/volume/src/chat/message.service.ts @@ -37,6 +37,7 @@ export class MessageService { .innerJoin('message.channel', 'channel') .where('channel.id = :chanId', { chanId: channel.id }) .leftJoinAndSelect('message.author', 'author') + .orderBy('message.created_at', 'ASC') .getMany() return messages.filter((m) => !blockeds.includes(m.author.ftId)) } diff --git a/front/volume/src/components/Channels.svelte b/front/volume/src/components/Channels.svelte index 2af43f4..ac05730 100644 --- a/front/volume/src/components/Channels.svelte +++ b/front/volume/src/components/Channels.svelte @@ -4,11 +4,12 @@ name: string; isPrivate: boolean; password: string; - owner: number; + owner: User; } import { onMount } from "svelte"; import { API_URL, store } from "../Auth"; import { socket } from "../socket"; + import type User from "./Profile.svelte";