-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from boostcampwm-2024/refactor/chatgateway-SoC
♻️ refactor: chatgateway의 관심사 분리, 채팅 비즈니스 로직을 chatservice로 구현
- Loading branch information
Showing
3 changed files
with
132 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ChatGateway } from './chat.gateway'; | ||
import { ScheduleModule } from '@nestjs/schedule'; | ||
import { ChatService } from './chat.service'; | ||
|
||
@Module({ | ||
imports: [ScheduleModule.forRoot()], | ||
providers: [ChatGateway], | ||
providers: [ChatGateway, ChatService], | ||
}) | ||
export class ChatModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Socket } from 'socket.io'; | ||
import { RedisService } from '../common/redis/redis.service'; | ||
import { getRandomNickname } from '@woowa-babble/random-nickname'; | ||
import { Cron, CronExpression } from '@nestjs/schedule'; | ||
|
||
const MAX_CLIENTS = 500; | ||
const CLIENT_KEY_PREFIX = 'socket_client:'; | ||
const CHAT_HISTORY_KEY = 'chat:history'; | ||
const CHAT_HISTORY_LIMIT = 20; | ||
const CHAT_MIDNIGHT_CLIENT_NAME = 'system'; | ||
|
||
type BroadcastPayload = { | ||
username: string; | ||
message: string; | ||
timestamp: Date; | ||
}; | ||
|
||
@Injectable() | ||
export class ChatService { | ||
private dayInit: boolean = false; | ||
|
||
constructor(private readonly redisService: RedisService) {} | ||
|
||
isMaxClientExceeded(userCount: number) { | ||
return userCount > MAX_CLIENTS; | ||
} | ||
|
||
private getClientIp(client: Socket) { | ||
const forwardedFor = client.handshake.headers['x-forwarded-for'] as string; | ||
const ip = forwardedFor | ||
? forwardedFor.split(',')[0].trim() | ||
: client.handshake.address; | ||
|
||
return ip; | ||
} | ||
|
||
async getClientNameByIp(client: Socket) { | ||
const ip = this.getClientIp(client); | ||
const redisKey = CLIENT_KEY_PREFIX + ip; | ||
const clientName: string = await this.getClientName(redisKey); | ||
if (clientName) { | ||
return clientName; | ||
} | ||
const createdClientName = await this.setClientName(redisKey); | ||
return createdClientName; | ||
} | ||
|
||
private async getClientName(redisKey: string) { | ||
return await this.redisService.redisClient.get(redisKey); | ||
} | ||
|
||
private async setClientName(redisKey: string) { | ||
const clientName = this.generateRandomUsername(); | ||
await this.redisService.redisClient.set( | ||
redisKey, | ||
clientName, | ||
'EX', | ||
3600 * 24, | ||
); | ||
return clientName; | ||
} | ||
|
||
private generateRandomUsername(): string { | ||
const type = 'animals'; | ||
|
||
return getRandomNickname(type); | ||
} | ||
|
||
async getChatHistory() { | ||
return (await this.getRecentChatMessages()) | ||
.map((msg) => JSON.parse(msg)) | ||
.reverse(); | ||
} | ||
|
||
private async getRecentChatMessages() { | ||
return await this.redisService.redisClient.lrange( | ||
CHAT_HISTORY_KEY, | ||
0, | ||
CHAT_HISTORY_LIMIT - 1, | ||
); | ||
} | ||
|
||
async saveMessageToRedis(payload: BroadcastPayload) { | ||
await this.redisService.redisClient.lpush( | ||
CHAT_HISTORY_KEY, | ||
JSON.stringify(payload), | ||
); | ||
|
||
await this.redisService.redisClient.ltrim( | ||
CHAT_HISTORY_KEY, | ||
0, | ||
CHAT_HISTORY_LIMIT - 1, | ||
); | ||
} | ||
|
||
async saveDateMessage() { | ||
const broadcastPayload: BroadcastPayload = { | ||
username: CHAT_MIDNIGHT_CLIENT_NAME, | ||
message: '', | ||
timestamp: new Date(), | ||
}; | ||
|
||
await this.saveMessageToRedis(broadcastPayload); | ||
|
||
return broadcastPayload; | ||
} | ||
|
||
async handleDateMessage() { | ||
if (this.dayInit) { | ||
this.dayInit = false; | ||
return await this.saveDateMessage(); | ||
} | ||
} | ||
|
||
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT) | ||
private midnightInitializer() { | ||
this.dayInit = true; | ||
} | ||
} |