Skip to content

Commit

Permalink
Merge pull request #221 from boostcampwm2023/BE-chatRoomDB-#220
Browse files Browse the repository at this point in the history
[BE/#220] Feat : 채팅하기 누르면 채팅방 생성 혹은 조회 구현
  • Loading branch information
koomin1227 authored Nov 29, 2023
2 parents 2240844 + e87a66b commit dc6bfe7
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
3 changes: 2 additions & 1 deletion BE/src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
26 changes: 18 additions & 8 deletions BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ export class ChatService {
@InjectRepository(PostEntity)
private postRepository: Repository<PostEntity>,
) {}
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 해서 채팅 목록도 가져와야함
}
}
4 changes: 3 additions & 1 deletion BE/src/chat/chats.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import {
WebSocketServer,
} from '@nestjs/websockets';
import { Server, Websocket } from 'ws';
import { ChatService } from './chat.service';

@WebSocketGateway({
path: 'chats',
})
export class ChatsGateway implements OnGatewayConnection {
@WebSocketServer() server: Server;
private rooms = new Map<string, Set<Websocket>>();

constructor(private readonly chatService: ChatService) {}
handleConnection(client: Websocket) {
// 인증 로직
console.log(`on connnect : ${client}`);
Expand Down Expand Up @@ -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]));
Expand Down
1 change: 1 addition & 0 deletions BE/src/chat/createRoom.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IsNumber, IsString } from 'class-validator';
export class CreateRoomDto {
@IsNumber()
post_id: number;

@IsString()
writer: string;
}
2 changes: 2 additions & 0 deletions BE/src/config/mysql.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,6 +26,7 @@ export class MysqlConfigProvider implements TypeOrmOptionsFactory {
PostImageEntity,
BlockUserEntity,
BlockPostEntity,
ChatRoomEntity,
],
synchronize: false,
};
Expand Down
8 changes: 1 addition & 7 deletions BE/src/entities/chatRoom.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,14 @@ export class ChatRoomEntity {
writer: string;

@Column({ length: 45, nullable: false, charset: 'utf8', unique: true })
sender: string;
user: string;

@CreateDateColumn({
type: 'timestamp',
nullable: false,
})
create_date: Date;

@UpdateDateColumn({
type: 'timestamp',
nullable: true,
})
update_date: Date;

@DeleteDateColumn()
delete_date: Date;
}
2 changes: 1 addition & 1 deletion BE/src/utils/auth.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export const UserHash = createParamDecorator(
Buffer.from(jwtPayload, 'base64').toString(),
);

return jwtPayloadJson.userId;
return jwtPayloadJson.user_hash;
},
);

0 comments on commit dc6bfe7

Please sign in to comment.