@@ -6,28 +6,29 @@ import {
66 OnGatewayConnection ,
77 OnGatewayDisconnect ,
88 MessageBody ,
9- ConnectedSocket ,
9+ ConnectedSocket
1010} from '@nestjs/websockets' ;
1111import { UseGuards } from '@nestjs/common' ;
1212import { Server , Socket } from 'socket.io' ;
1313import {
14- CHATTING_SOCKET_DEFAULT_EVENT ,
15- CHATTING_SOCKET_RECEIVE_EVENT , CHATTING_SOCKET_SEND_EVENT
14+ CHATTING_SOCKET_DEFAULT_EVENT , CHATTING_SOCKET_RECEIVE_EVENT , CHATTING_SOCKET_SEND_EVENT
1615} from '../event/constants' ;
1716import {
17+ BanUserIncomingMessageDto ,
1818 NormalIncomingMessageDto , NoticeIncomingMessageDto , QuestionDoneIncomingMessageDto , QuestionIncomingMessageDto
1919} from '../event/dto/IncomingMessage.dto' ;
2020import { JoiningRoomDto } from '../event/dto/JoiningRoom.dto' ;
2121import { RoomService } from '../room/room.service' ;
2222import { createAdapter } from '@socket.io/redis-adapter' ;
23- import { HostGuard , MessageGuard } from './chat.guard' ;
23+ import { BlacklistGuard , HostGuard , MessageGuard } from './chat.guard' ;
2424import { LeavingRoomDto } from '../event/dto/LeavingRoom.dto' ;
2525import {
2626 NormalOutgoingMessageDto ,
2727 NoticeOutgoingMessageDto ,
2828 QuestionOutgoingMessageDto
2929} from '../event/dto/OutgoingMessage.dto' ;
3030import { QuestionDto } from '../event/dto/Question.dto' ;
31+ import { ChatException , CHATTING_SOCKET_ERROR } from './chat.error' ;
3132
3233@WebSocketGateway ( { cors : true } )
3334export class ChatGateway implements OnGatewayInit , OnGatewayConnection , OnGatewayDisconnect {
@@ -69,6 +70,7 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
6970 }
7071
7172 // 특정 방에 참여하기 위한 메서드
73+ @UseGuards ( BlacklistGuard )
7274 @SubscribeMessage ( CHATTING_SOCKET_DEFAULT_EVENT . JOIN_ROOM )
7375 async handleJoinRoom ( client : Socket , payload : JoiningRoomDto ) {
7476 const { roomId, userId } = payload ;
@@ -93,7 +95,7 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
9395 }
9496
9597 // 방에 NORMAL 메시지를 보내기 위한 메서드
96- @UseGuards ( MessageGuard )
98+ @UseGuards ( MessageGuard , BlacklistGuard )
9799 @SubscribeMessage ( CHATTING_SOCKET_SEND_EVENT . NORMAL )
98100 async handleNormalMessage ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : NormalIncomingMessageDto ) {
99101 const { roomId, userId, msg } = payload ;
@@ -103,7 +105,8 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
103105 ...user ,
104106 msg,
105107 msgTime : new Date ( ) . toISOString ( ) ,
106- msgType : 'normal'
108+ msgType : 'normal' ,
109+ socketId : client . id
107110 } ;
108111 console . log ( 'Normal Message Come In: ' , normalOutgoingMessage ) ;
109112 const hostId = await this . roomService . getHostOfRoom ( roomId ) ;
@@ -121,7 +124,7 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
121124 }
122125
123126 // 방에 QUESTION 메시지를 보내기 위한 메서드
124- @UseGuards ( MessageGuard )
127+ @UseGuards ( MessageGuard , BlacklistGuard )
125128 @SubscribeMessage ( CHATTING_SOCKET_SEND_EVENT . QUESTION )
126129 async handleQuestionMessage ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : QuestionIncomingMessageDto ) {
127130 const { roomId, msg } = payload ;
@@ -132,7 +135,8 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
132135 msg,
133136 msgTime : new Date ( ) . toISOString ( ) ,
134137 msgType : 'question' ,
135- questionDone : false
138+ questionDone : false ,
139+ socketId : client . id
136140 } ;
137141
138142 const question : QuestionOutgoingMessageDto = await this . roomService . addQuestion ( roomId , questionWithoutId ) ;
@@ -150,8 +154,7 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
150154 }
151155
152156 // 방에 NOTICE 메시지를 보내기 위한 메서드
153- @UseGuards ( MessageGuard )
154- @UseGuards ( HostGuard )
157+ @UseGuards ( MessageGuard , HostGuard )
155158 @SubscribeMessage ( CHATTING_SOCKET_SEND_EVENT . NOTICE )
156159 async handleNoticeMessage ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : NoticeIncomingMessageDto ) {
157160 const { roomId, msg } = payload ;
@@ -165,4 +168,20 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
165168 } ;
166169 this . server . to ( roomId ) . emit ( CHATTING_SOCKET_RECEIVE_EVENT . NOTICE , noticeOutgoingMessage ) ;
167170 }
171+
172+ @UseGuards ( HostGuard )
173+ @SubscribeMessage ( CHATTING_SOCKET_DEFAULT_EVENT . BAN_USER )
174+ async handleBanUserMessage ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : BanUserIncomingMessageDto ) {
175+ const { roomId, socketId } = payload ;
176+ const banUser = this . server . sockets . sockets . get ( socketId ) ;
177+ const address = banUser ?. handshake . address . replaceAll ( '::ffff:' , '' ) ;
178+
179+ if ( ! address ) throw new ChatException ( CHATTING_SOCKET_ERROR . INVALID_USER ) ;
180+
181+ const forwarded = banUser ?. handshake . headers . forwarded ?? address ;
182+ console . log ( 'ban:' , roomId , address , forwarded ) ;
183+
184+ await this . roomService . addUserToBlacklist ( roomId , address , forwarded ) ;
185+ console . log ( await this . roomService . getUserBlacklist ( roomId , address ) ) ;
186+ }
168187}
0 commit comments