diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index 4027e2a..71c315e 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -1,106 +1,100 @@ -import { UnauthorizedException } from '@nestjs/common' +import { UnauthorizedException, UseGuards } from "@nestjs/common"; import { type OnGatewayConnection, type OnGatewayDisconnect, MessageBody, SubscribeMessage, WebSocketGateway, - WebSocketServer -} from '@nestjs/websockets' -import { Socket, Server } from 'socket.io' + WebSocketServer, +} from "@nestjs/websockets"; +import { Socket, Server } from "socket.io"; -import { ChatService } from './chat.service' -import { type User } from 'src/users/entity/user.entity' -import { UsersService } from 'src/users/users.service' -import { Channel } from './entity/channel.entity' -import { Message } from './entity/message.entity' +import { ChatService } from "./chat.service"; +import { type User } from "src/users/entity/user.entity"; +import { UsersService } from "src/users/users.service"; +import { Channel } from "./entity/channel.entity"; +import { Message } from "./entity/message.entity"; -import { CreateChannelDto } from './dto/createChannel.dto' +import { CreateChannelDto } from "./dto/createChannel.dto"; @WebSocketGateway({ - cors: { - origin: [ - 'http://localhost:5000', - 'http://localhost:80', - 'http://localhost:8080' - ] - } + cors: { origin: /^(http|ws):\/\/localhost(:\d+)?$/ }, }) export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { @WebSocketServer() - server: Server + server: Server; - constructor ( + constructor( private readonly userService: UsersService, private readonly chatService: ChatService ) {} - async handleConnection (socket: Socket) { + async handleConnection(socket: Socket) { try { const user: User | null = await this.userService.findUser( socket.data.user.ftId - ) + ); if (user == null) { - socket.emit('Error', new UnauthorizedException()) + socket.emit("Error", new UnauthorizedException()); // socket.disconnect(); - return + return; } else { - socket.data.user = user - const channels = await this.chatService.getChannelsForUser(user.id) + socket.data.user = user; + const channels = await this.chatService.getChannelsForUser(user.id); // Only emit rooms to the specific connected client - return this.server.to(socket.id).emit('channel', channels) + return this.server.to(socket.id).emit("channel", channels); } } catch { - socket.emit('Error', new UnauthorizedException()) + socket.emit("Error", new UnauthorizedException()); // socket.disconnect(); } } - handleDisconnect (socket: Socket) { - // socket.disconnect(); + handleDisconnect(socket: Socket) { + // socket.disconnect() } - @SubscribeMessage('createChannel') - async onCreateChannel ( + @SubscribeMessage("createChannel") + async onCreateChannel( socket: Socket, @MessageBody() channeldto: CreateChannelDto ): Promise { - const channel = new Channel() - channel.name = channeldto.name - const owner = await this.userService.findUser(channeldto.owner) - if (owner == null) return null - channel.owners.push(owner) - channel.password = channeldto.password + const channel = new Channel(); + channel.name = channeldto.name; + const owner = await this.userService.findUser(channeldto.owner); + if (owner == null) return null; + channel.owners.push(owner); + channel.password = channeldto.password; /// .../// - return await this.chatService.createChannel(channel, socket.data.user) + return await this.chatService.createChannel(channel, socket.data.user); } - @SubscribeMessage('joinChannel') - async onJoinChannel (socket: Socket, channel: Channel) { + @SubscribeMessage("joinChannel") + async onJoinChannel(socket: Socket, channel: Channel) { // add user to channel const messages = await this.chatService.findMessagesInChannelForUser( channel, socket.data.user - ) - this.server.to(socket.id).emit('messages', messages) + ); + this.server.to(socket.id).emit("messages", messages); } - @SubscribeMessage('leaveChannel') - async onLeaveChannel (socket: Socket) { - await this.chatService.deleteBySocketId(socket.id) + @SubscribeMessage("leaveChannel") + async onLeaveChannel(socket: Socket) { + await this.chatService.deleteBySocketId(socket.id); } - @SubscribeMessage('addMessage') - async onAddMessage (socket: Socket, message: Message) { + @SubscribeMessage("addMessage") + async onAddMessage(socket: Socket, message: Message) { const createdMessage: Message = await this.chatService.createMessage({ ...message, - author: socket.data.user - }) + author: socket.data.user, + }); const channel = await this.chatService.getChannel( createdMessage.channel.id - ) + ); if (channel != null) { - const users = await this.userService.findOnlineInChannel(channel) + const users = await this.userService.findOnlineInChannel(channel); } /// TODO: Send message to users } diff --git a/back/volume/src/main.ts b/back/volume/src/main.ts index 458e476..bbc9c80 100644 --- a/back/volume/src/main.ts +++ b/back/volume/src/main.ts @@ -16,7 +16,7 @@ async function bootstrap (): Promise { ? +process.env.BACK_PORT : 3001 const cors = { - origin: ['http://localhost:80', 'http://localhost', '*'], + origin: /^(http|ws):\/\/localhost(:\d+)?$/, methods: 'GET, HEAD, PUT, PATCH, POST, DELETE, OPTIONS', preflightContinue: false, optionsSuccessStatus: 204,