-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Chat] 채팅방에 초대할 때 사용할 코드 생성 API 구현 #149
Changes from 9 commits
7d7cc9b
0f52c3d
9a93d03
edd40ed
66ffbc6
b0b114c
5b70b4b
9826a24
0a934ba
7dfae34
a5a16e1
d67ff43
18b7f84
47038d4
a9c1453
406d11d
a797346
7ecfec9
46e80af
18b7cb6
ac747fb
18c44ef
1854afd
6020df3
2383b40
be2f961
640d3ac
7804e42
0449e6f
df2ecfd
c583e10
dde725d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,57 @@ | ||
package kpring.chat.chatroom.repository | ||
|
||
import kpring.chat.global.config.PropertyConfig | ||
import kpring.chat.global.exception.ErrorCode | ||
import kpring.chat.global.exception.GlobalException | ||
import kpring.core.chat.chat.dto.response.InvitationResponse | ||
import kpring.chat.global.config.ChatRoomProperty | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.data.redis.core.ValueOperations | ||
import org.springframework.stereotype.Component | ||
import java.time.LocalDateTime | ||
import java.nio.charset.StandardCharsets | ||
import java.security.MessageDigest | ||
import java.util.* | ||
|
||
@Component | ||
class UserChatRoomInvitationRepository( | ||
class InvitationRepository( | ||
private val redisTemplate: RedisTemplate<String, String>, | ||
private val propertyConfig: PropertyConfig, | ||
private val invitationChatRoomRepository: InvitationChatRoomRepository, | ||
private val chatRoomProperty: ChatRoomProperty, | ||
) { | ||
fun getInvitation( | ||
fun getInvitationCode( | ||
userId: String, | ||
chatRoomId: String, | ||
): InvitationResponse { | ||
): String { | ||
val key = generateKey(userId, chatRoomId) | ||
var value = redisTemplate.opsForValue().get(key) | ||
if (value == null) { | ||
value = setInvitation(key, chatRoomId) | ||
} | ||
|
||
val expiration = redisTemplate.getExpire(key) | ||
|
||
return InvitationResponse(value, LocalDateTime.now().plusSeconds(expiration).toString()) | ||
return generateCode(key, value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getInviationCode이지만 코드를 생성하고 저장하는 역할을 같이 구현되어있는 것 같아요. 겳합도를 낮추는 방향으로 개선하면 좋을 것 같아요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 좋습니다 |
||
} | ||
|
||
fun setInvitation( | ||
key: String, | ||
chatRoomId: String, | ||
): String { | ||
val invitationCode = generateCode() | ||
val value = generateValue() | ||
val ops: ValueOperations<String, String> = redisTemplate.opsForValue() | ||
ops.set(key, invitationCode, propertyConfig.getExpiration()) | ||
invitationChatRoomRepository.setInvitationCode(invitationCode, chatRoomId) | ||
return invitationCode | ||
ops.set(key, value, chatRoomProperty.getExpiration()) | ||
return value | ||
} | ||
|
||
fun getExpiration( | ||
userId: String, | ||
chatRoomId: String, | ||
): Long { | ||
val key = generateKey(userId, chatRoomId) | ||
val expiration = redisTemplate.getExpire(key) | ||
return expiration | ||
} | ||
|
||
fun generateCode( | ||
key: String, | ||
value: String, | ||
): String { | ||
val combine = "$key,$value" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. combine 동사이기도 하고 무슨 의미인지 직관적으로 파악이 힘든 것 같아요!, key, value의 의미가 무엇인지 파악이 힘들어서 주석으로 무슨 의미인지를 좀 더 설명을 해주면 좋을 것 같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네~! 변수명이 가독성이 떨어지는거 같긴 하네요 |
||
val digest = MessageDigest.getInstance("SHA-256") | ||
val hash = digest.digest(combine.toByteArray(StandardCharsets.UTF_8)) | ||
return Base64.getEncoder().encodeToString(hash) | ||
} | ||
|
||
private fun generateKey( | ||
|
@@ -49,7 +61,7 @@ class UserChatRoomInvitationRepository( | |
return "$userId:$chatRoomId" | ||
} | ||
|
||
private fun generateCode(): String { | ||
private fun generateValue(): String { | ||
return UUID.randomUUID().toString() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
초대 저장소가 하는 역할이 무엇인가요? 서비스 layer에 더 어울리는 역할을 수행하는 것 같은 느낌?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
재사용할 수 있는 컴포넌트인가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findbyId 나 save등만 있는게 아니라서 재사용이 불가능할 것 같아요
chatService랑은 좀 동떨어진 일을 하는 것 같아서 저 repository로 초대/참여 관련 로직을 분리했었는데 생각해보면 다른 Service로 분리하는게 더 나아보이네요