From e87a66b5a48f10a64529cd07f5ed0048d036716a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=92?= =?UTF-8?q?=E1=85=AE=E1=86=AB?= Date: Wed, 29 Nov 2023 16:58:28 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20Feat=20:=20=EC=B1=84=ED=8C=85=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20=EB=88=84=EB=A5=B4=EB=A9=B4=20=EC=B1=84=ED=8C=85?= =?UTF-8?q?=EB=B0=A9=20=EC=83=9D=EC=84=B1=20=ED=98=B9=EC=9D=80=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/chat/chat.controller.ts | 3 ++- BE/src/chat/chat.service.ts | 26 ++++++++++++++++++-------- BE/src/chat/chats.gateway.ts | 4 +++- BE/src/chat/createRoom.dto.ts | 1 + BE/src/config/mysql.config.ts | 2 ++ BE/src/entities/chatRoom.entity.ts | 8 +------- BE/src/utils/auth.decorator.ts | 2 +- 7 files changed, 28 insertions(+), 18 deletions(-) diff --git a/BE/src/chat/chat.controller.ts b/BE/src/chat/chat.controller.ts index a1df320..b3dafb2 100644 --- a/BE/src/chat/chat.controller.ts +++ b/BE/src/chat/chat.controller.ts @@ -21,6 +21,7 @@ export class ChatController { @Post('room') @UseGuards(AuthGuard) async roomCreate(@Body() body: CreateRoomDto, @UserHash() userId: string) { - await this.chatService.createRoom(body.post_id, userId, body.writer); + console.log(userId); + await this.chatService.createOrFindRoom(body.post_id, userId, body.writer); } } diff --git a/BE/src/chat/chat.service.ts b/BE/src/chat/chat.service.ts index 596125c..5b022a3 100644 --- a/BE/src/chat/chat.service.ts +++ b/BE/src/chat/chat.service.ts @@ -12,13 +12,23 @@ export class ChatService { @InjectRepository(PostEntity) private postRepository: Repository, ) {} - async createRoom(postId: number, userId: string, writerId: string) { - const chatRoom = new ChatRoomEntity(); - chatRoom.post_id = postId; - chatRoom.writer = writerId; - chatRoom.sender = userId; - const newChatRoom = await this.chatRoomRepository.save(chatRoom); - // 좀 더 가공해서 채팅 룸에 대한 정보 줄 수 있게 수정하면 될듯 - return newChatRoom; + + async createOrFindRoom(postId: number, userId: string, writerId: string) { + const roomNumber = await this.chatRoomRepository.findOne({ + where: { writer: writerId, user: userId, post_id: postId }, + }); // 해당 게시글과 사람들에 대한 채팅방이 있는지 확인한다 + + if (roomNumber) { + return roomNumber; // 있으면 채팅방 번호 리턴 + } else { + const chatRoom = new ChatRoomEntity(); + chatRoom.post_id = postId; + chatRoom.writer = writerId; + chatRoom.user = userId; + const newChatRoom = await this.chatRoomRepository.save(chatRoom); // 없으면 새로 만들어서 저장후 리턴 + return newChatRoom; + } + + //이후에 채팅과 Join 해서 채팅 목록도 가져와야함 } } diff --git a/BE/src/chat/chats.gateway.ts b/BE/src/chat/chats.gateway.ts index e666e7c..4c61da9 100644 --- a/BE/src/chat/chats.gateway.ts +++ b/BE/src/chat/chats.gateway.ts @@ -7,6 +7,7 @@ import { WebSocketServer, } from '@nestjs/websockets'; import { Server, Websocket } from 'ws'; +import { ChatService } from './chat.service'; @WebSocketGateway({ path: 'chats', @@ -14,6 +15,8 @@ import { Server, Websocket } from 'ws'; export class ChatsGateway implements OnGatewayConnection { @WebSocketServer() server: Server; private rooms = new Map>(); + + constructor(private readonly chatService: ChatService) {} handleConnection(client: Websocket) { // 인증 로직 console.log(`on connnect : ${client}`); @@ -42,7 +45,6 @@ export class ChatsGateway implements OnGatewayConnection { @MessageBody() message: object, @ConnectedSocket() client: Websocket, ) { - // MessageBody 에 있는 (a , b) 쌍을 통해 DB 에서 해당 방을 찾아서 roomName 을 설정해야함 const roomName = message['room']; if (this.rooms.has(roomName)) this.rooms.get(roomName).add(client); else this.rooms.set(roomName, new Set([client])); diff --git a/BE/src/chat/createRoom.dto.ts b/BE/src/chat/createRoom.dto.ts index 260bda9..4784794 100644 --- a/BE/src/chat/createRoom.dto.ts +++ b/BE/src/chat/createRoom.dto.ts @@ -3,6 +3,7 @@ import { IsNumber, IsString } from 'class-validator'; export class CreateRoomDto { @IsNumber() post_id: number; + @IsString() writer: string; } diff --git a/BE/src/config/mysql.config.ts b/BE/src/config/mysql.config.ts index 458950a..3a9ad3c 100644 --- a/BE/src/config/mysql.config.ts +++ b/BE/src/config/mysql.config.ts @@ -6,6 +6,7 @@ import { PostEntity } from '../entities/post.entity'; import { BlockUserEntity } from '../entities/blockUser.entity'; import { PostImageEntity } from '../entities/postImage.entity'; import { BlockPostEntity } from '../entities/blockPost.entity'; +import { ChatRoomEntity } from 'src/entities/chatRoom.entity'; @Injectable() export class MysqlConfigProvider implements TypeOrmOptionsFactory { @@ -25,6 +26,7 @@ export class MysqlConfigProvider implements TypeOrmOptionsFactory { PostImageEntity, BlockUserEntity, BlockPostEntity, + ChatRoomEntity, ], synchronize: false, }; diff --git a/BE/src/entities/chatRoom.entity.ts b/BE/src/entities/chatRoom.entity.ts index 6fc2ac0..33c7843 100644 --- a/BE/src/entities/chatRoom.entity.ts +++ b/BE/src/entities/chatRoom.entity.ts @@ -22,7 +22,7 @@ export class ChatRoomEntity { writer: string; @Column({ length: 45, nullable: false, charset: 'utf8', unique: true }) - sender: string; + user: string; @CreateDateColumn({ type: 'timestamp', @@ -30,12 +30,6 @@ export class ChatRoomEntity { }) create_date: Date; - @UpdateDateColumn({ - type: 'timestamp', - nullable: true, - }) - update_date: Date; - @DeleteDateColumn() delete_date: Date; } diff --git a/BE/src/utils/auth.decorator.ts b/BE/src/utils/auth.decorator.ts index 8beceae..fef7f84 100644 --- a/BE/src/utils/auth.decorator.ts +++ b/BE/src/utils/auth.decorator.ts @@ -10,6 +10,6 @@ export const UserHash = createParamDecorator( Buffer.from(jwtPayload, 'base64').toString(), ); - return jwtPayloadJson.userId; + return jwtPayloadJson.user_hash; }, );