From d3434367a52d5c4962241940b5d2b87e11a0191e Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Wed, 29 Nov 2023 11:40:26 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[BE]=20Feat=20:=20=EB=B9=88=20=EB=B0=A9?= =?UTF-8?q?=EC=9D=B8=20=EA=B2=BD=EC=9A=B0=20=EB=B0=A9=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/chat/chats.gateway.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/BE/src/chat/chats.gateway.ts b/BE/src/chat/chats.gateway.ts index 485a74c..5f62589 100644 --- a/BE/src/chat/chats.gateway.ts +++ b/BE/src/chat/chats.gateway.ts @@ -36,21 +36,26 @@ export class ChatsGateway implements OnGatewayConnection { @SubscribeMessage('join-room') joinRoom( - @MessageBody() roomName: string, + @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])); } @SubscribeMessage('leave-room') leaveRoom( - @MessageBody() roomName: string, + @MessageBody() message: object, @ConnectedSocket() client: Websocket, ) { - this.rooms.get(roomName).delete(client); + const roomName = message['room']; + const room = this.rooms.get(roomName); + room.delete(client); + if (room.size === 0) { + this.rooms.delete(roomName); + } console.log(this.rooms); } } From 12f184505646fe3cc8db5de3cb33d7063e5a87d7 Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Wed, 29 Nov 2023 11:53:13 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[BE]=20Feat=20:=20=EB=B0=9C=EC=8B=A0?= =?UTF-8?q?=EC=9E=90=20=EC=A0=95=EB=B3=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/chat/chats.gateway.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/BE/src/chat/chats.gateway.ts b/BE/src/chat/chats.gateway.ts index 5f62589..e666e7c 100644 --- a/BE/src/chat/chats.gateway.ts +++ b/BE/src/chat/chats.gateway.ts @@ -15,6 +15,7 @@ export class ChatsGateway implements OnGatewayConnection { @WebSocketServer() server: Server; private rooms = new Map>(); handleConnection(client: Websocket) { + // 인증 로직 console.log(`on connnect : ${client}`); } @@ -29,8 +30,10 @@ export class ChatsGateway implements OnGatewayConnection { ) { console.log(message); const room = this.rooms.get(message['room']); + const sender = message['sender']; room.forEach((people) => { - if (people !== client) people.send(JSON.stringify(message['message'])); + if (people !== client) + people.send(JSON.stringify({ sender, message: message['message'] })); }); } From db998de9c2b3dd3072c06ae336ec9a146ca761a2 Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Wed, 29 Nov 2023 14:33:44 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[BE]=20Feat=20:=20=EC=B1=84=ED=8C=85?= =?UTF-8?q?=EB=B0=A9=20=EB=A7=8C=EB=93=9C=EB=8A=94=20API=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/chat/chat.controller.ts | 11 ++++++++ BE/src/chat/chat.module.ts | 4 +++ BE/src/chat/chat.service.ts | 22 +++++++++++++++- BE/src/chat/createRoom.dto.ts | 8 ++++++ BE/src/entities/chatRoom.entity.ts | 41 ++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 BE/src/chat/createRoom.dto.ts create mode 100644 BE/src/entities/chatRoom.entity.ts diff --git a/BE/src/chat/chat.controller.ts b/BE/src/chat/chat.controller.ts index bfb685c..a1df320 100644 --- a/BE/src/chat/chat.controller.ts +++ b/BE/src/chat/chat.controller.ts @@ -6,10 +6,21 @@ import { Patch, Param, Delete, + UseGuards, } from '@nestjs/common'; import { ChatService } from './chat.service'; +import { AuthGuard } from '../utils/auth.guard'; +import { UserHash } from '../utils/auth.decorator'; +import { CreateRoomDto } from './createRoom.dto'; @Controller('chat') export class ChatController { constructor(private readonly chatService: ChatService) {} + + // 게시글에서 채팅하기 버튼 누르면 채팅방 만드는 API (테스트는 안해봄, 좀더 수정 필요) + @Post('room') + @UseGuards(AuthGuard) + async roomCreate(@Body() body: CreateRoomDto, @UserHash() userId: string) { + await this.chatService.createRoom(body.post_id, userId, body.writer); + } } diff --git a/BE/src/chat/chat.module.ts b/BE/src/chat/chat.module.ts index ef9c3c2..3f8d889 100644 --- a/BE/src/chat/chat.module.ts +++ b/BE/src/chat/chat.module.ts @@ -2,8 +2,12 @@ import { Module } from '@nestjs/common'; import { ChatService } from './chat.service'; import { ChatController } from './chat.controller'; import { ChatsGateway } from './chats.gateway'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { ChatRoomEntity } from '../entities/chatRoom.entity'; +import { PostEntity } from '../entities/post.entity'; @Module({ + imports: [TypeOrmModule.forFeature([ChatRoomEntity, PostEntity])], controllers: [ChatController], providers: [ChatService, ChatsGateway], }) diff --git a/BE/src/chat/chat.service.ts b/BE/src/chat/chat.service.ts index 408edcc..596125c 100644 --- a/BE/src/chat/chat.service.ts +++ b/BE/src/chat/chat.service.ts @@ -1,4 +1,24 @@ import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { PostEntity } from '../entities/post.entity'; +import { Repository } from 'typeorm'; +import { ChatRoomEntity } from '../entities/chatRoom.entity'; @Injectable() -export class ChatService {} +export class ChatService { + constructor( + @InjectRepository(ChatRoomEntity) + private chatRoomRepository: Repository, + @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; + } +} diff --git a/BE/src/chat/createRoom.dto.ts b/BE/src/chat/createRoom.dto.ts new file mode 100644 index 0000000..260bda9 --- /dev/null +++ b/BE/src/chat/createRoom.dto.ts @@ -0,0 +1,8 @@ +import { IsNumber, IsString } from 'class-validator'; + +export class CreateRoomDto { + @IsNumber() + post_id: number; + @IsString() + writer: string; +} diff --git a/BE/src/entities/chatRoom.entity.ts b/BE/src/entities/chatRoom.entity.ts new file mode 100644 index 0000000..6fc2ac0 --- /dev/null +++ b/BE/src/entities/chatRoom.entity.ts @@ -0,0 +1,41 @@ +import { + Column, + CreateDateColumn, + DeleteDateColumn, + Entity, + PrimaryGeneratedColumn, + UpdateDateColumn, +} from 'typeorm'; +import { PostEntity } from './post.entity'; +import { BlockUserEntity } from './blockUser.entity'; +import { BlockPostEntity } from './blockPost.entity'; + +@Entity('chat_room') +export class ChatRoomEntity { + @PrimaryGeneratedColumn() + id: number; + + @Column({ type: 'int' }) + post_id: number; + + @Column({ length: 45, nullable: false, charset: 'utf8', unique: true }) + writer: string; + + @Column({ length: 45, nullable: false, charset: 'utf8', unique: true }) + sender: string; + + @CreateDateColumn({ + type: 'timestamp', + nullable: false, + }) + create_date: Date; + + @UpdateDateColumn({ + type: 'timestamp', + nullable: true, + }) + update_date: Date; + + @DeleteDateColumn() + delete_date: Date; +}