Skip to content

Commit

Permalink
Merge pull request #290 from apeun-gidaechi/fix/#289
Browse files Browse the repository at this point in the history
Fix :: 개인 채팅방 반환시 이름 사진 반환 안되는것 수정 #289
  • Loading branch information
yeseong0412 authored Oct 13, 2024
2 parents e2e310f + 24bd96e commit 4638aea
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.seugi.api.domain.chat.domain.enums.type.RoomType.PERSONAL
import com.seugi.api.domain.chat.domain.room.ChatRoomEntity
import com.seugi.api.domain.chat.domain.room.ChatRoomRepository
import com.seugi.api.domain.chat.domain.room.mapper.RoomMapper
import com.seugi.api.domain.chat.domain.room.model.Room
import com.seugi.api.domain.chat.exception.ChatErrorCode
import com.seugi.api.domain.chat.presentation.chat.room.dto.request.CreateRoomRequest
import com.seugi.api.domain.chat.presentation.chat.room.dto.request.SearchRoomRequest
Expand Down Expand Up @@ -73,20 +74,20 @@ class ChatRoomServiceImpl(
chatRoomRepository.save(chatRoom).joinedUserInfo.find { it.userId == userId }?.timestamp
}

private fun toResponse(chatRoomEntity: ChatRoomEntity, userId: Long): RoomResponse {
val lastMessageEntity = messageService.getMessage(chatRoomEntity.id.toString())
private fun toResponse(room: Room, userId: Long): RoomResponse {
val lastMessageEntity = messageService.getMessage(room.id.toString())
return chatRoomMapper.toResponse(
room = chatRoomMapper.toDomain(chatRoomEntity),
members = getUserInfo(chatRoomEntity),
room = room,
members = getUserInfo(room),
lastMessage = lastMessageEntity?.message ?: "",
lastMessageTimeStamp = (lastMessageEntity?.timestamp ?: "").toString(),
notReadCnt = messageService.getNotReadMessageCount(chatRoomEntity, userId)
notReadCnt = messageService.getNotReadMessageCount(room, userId)
)
}

//채팅방 반환시 유저 모델 전달용 함수
private fun getUserInfo(chatRoomEntity: ChatRoomEntity): Set<RetrieveMemberResponse> {
return chatRoomEntity.joinedUserInfo.map { RetrieveMemberResponse(loadMemberPort.loadMemberWithId(it.userId)) }
private fun getUserInfo(room: Room): Set<RetrieveMemberResponse> {
return room.joinUserInfo.map { RetrieveMemberResponse(loadMemberPort.loadMemberWithId(it.userId)) }
.toSet()
}

Expand Down Expand Up @@ -166,23 +167,28 @@ class ChatRoomServiceImpl(
if (data.joinedUserInfo.none { it.userId == userId }) {
throw CustomException(ChatErrorCode.NO_ACCESS_ROOM)
}

val room = chatRoomMapper.toDomain(data)

when (type) {
PERSONAL -> {
data.apply {
val member = loadMemberPort.loadMemberWithId(joinedUserInfo.first { it.userId != userId }.userId)
chatName = member.name.value
val member = loadMemberPort.loadMemberWithId(room.joinUserInfo.first { it.userId != userId }.userId)
val chatRoom = room.copy(
chatName = member.name.value,
chatRoomImg = member.picture.value
}
)


return BaseResponse(
message = "채팅방 단건 조회성공!",
data = toResponse(data, userId)
data = toResponse(chatRoom, userId)
)
}

GROUP -> {
return BaseResponse(
message = "채팅방 단건 조회성공!",
data = toResponse(data, userId)
data = toResponse(room, userId)
)
}
}
Expand All @@ -191,39 +197,38 @@ class ChatRoomServiceImpl(

@Transactional(readOnly = true)
override fun getRooms(workspaceId: String, userId: Long, type: RoomType): BaseResponse<List<RoomResponse>> {
val chatRoomEntity = chatRoomRepository.findByWorkspaceIdAndChatStatusAndRoomTypeAndJoinedUserInfoUserId(
workspaceId = workspaceId,
chatStatus = ChatStatusEnum.ALIVE,
roomType = type,
userId = userId
).orEmpty()

val chatRoomEntity =
chatRoomRepository.findByWorkspaceIdAndChatStatusAndRoomTypeAndJoinedUserInfoUserId(
workspaceId = workspaceId,
chatStatus = ChatStatusEnum.ALIVE,
roomType = type,
userId = userId
).orEmpty()
val chatRooms = chatRoomEntity.map { chatRoomMapper.toDomain(it) }

when (type) {
val rooms: List<RoomResponse> = when (type) {
PERSONAL -> {
val rooms: List<ChatRoomEntity> = chatRoomEntity.map { it ->

val room = findChatRoomById(it.id.toString())
room.apply {
val member =
loadMemberPort.loadMemberWithId(joinedUserInfo.first { it.userId != userId }.userId)
chatName = member.name.value
chatRooms.map { room ->
val member = loadMemberPort.loadMemberWithId(
room.joinUserInfo.first { it.userId != userId }.userId
)
val updatedRoom = room.copy(
chatName = member.name.value,
chatRoomImg = member.picture.value
}
)
toResponse(updatedRoom, userId)
}

return BaseResponse(
message = "채팅방 불러오기 성공",
data = rooms.map { toResponse(it, userId) }
)
}

GROUP -> return BaseResponse(
message = "채팅방 불러오기 성공",
data = chatRoomEntity.map { toResponse(it, userId) }
)
else -> {
chatRooms.map { toResponse(it, userId) }
}
}

return BaseResponse(
message = "채팅방 불러오기 성공",
data = rooms
)
}

@Transactional
Expand Down Expand Up @@ -279,16 +284,17 @@ class ChatRoomServiceImpl(

when (type) {
PERSONAL -> {

val entity = chatRoomEntities.mapNotNull { it ->
val member =
loadMemberPort.loadMemberWithId(it.joinedUserInfo.first { it.userId != userId }.userId)
val member = loadMemberPort.loadMemberWithId(
it.joinedUserInfo.first { it.userId != userId }.userId
)
if (member.name.value.contains(searchRoomRequest.word)) {
val chatRoom = chatRoomRepository.findById(it.id!!)
.orElseThrow { CustomException(ChatErrorCode.CHAT_ROOM_NOT_FOUND) }
chatRoom.chatName = member.name.value
chatRoom.chatRoomImg = member.picture.value
return@mapNotNull chatRoom
val chatRoom = chatRoomMapper.toDomain(it)
val updatedChatRoom = chatRoom.copy(
chatName = member.name.value,
chatRoomImg = member.picture.value
)
return@mapNotNull updatedChatRoom
} else {
null
}
Expand All @@ -305,13 +311,12 @@ class ChatRoomServiceImpl(
GROUP -> return BaseResponse(
message = "채팅방 검색 성공",
data = chatRoomEntities.map {
toResponse(it, userId)
toResponse(chatRoomMapper.toDomain(it), userId)
}
)

}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.seugi.api.domain.chat.application.service.message
import com.seugi.api.domain.chat.domain.chat.MessageEntity
import com.seugi.api.domain.chat.domain.chat.embeddable.AddEmoji
import com.seugi.api.domain.chat.domain.chat.embeddable.DeleteMessage
import com.seugi.api.domain.chat.domain.room.ChatRoomEntity
import com.seugi.api.domain.chat.domain.room.model.Room
import com.seugi.api.domain.chat.presentation.chat.member.dto.response.GetMessageResponse
import com.seugi.api.domain.chat.presentation.websocket.dto.ChatMessageDto
import com.seugi.api.domain.chat.presentation.websocket.dto.MessageEventDto
Expand All @@ -16,7 +16,7 @@ interface MessageService {
fun sendEventMessage(message: MessageEventDto, roomId: String)
fun getMessage(roomId: String): MessageEntity?
fun getMessages(chatRoomId: String, userId: Long, timestamp: LocalDateTime): BaseResponse<GetMessageResponse>
fun getNotReadMessageCount(chatRoom: ChatRoomEntity, userId: Long): Int
fun getNotReadMessageCount(room: Room, userId: Long): Int

fun addEmojiToMessage(userId: Long, emoji: AddEmoji): BaseResponse<Unit>
fun deleteEmojiToMessage(userId: Long, emoji: AddEmoji): BaseResponse<Unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.seugi.api.domain.chat.domain.chat.mapper.MessageMapper
import com.seugi.api.domain.chat.domain.chat.model.Message
import com.seugi.api.domain.chat.domain.chat.model.Type
import com.seugi.api.domain.chat.domain.enums.status.ChatStatusEnum
import com.seugi.api.domain.chat.domain.room.ChatRoomEntity
import com.seugi.api.domain.chat.domain.room.model.Room
import com.seugi.api.domain.chat.exception.ChatErrorCode
import com.seugi.api.domain.chat.presentation.chat.member.dto.response.GetMessageResponse
import com.seugi.api.domain.chat.presentation.message.dto.MessageResponse
Expand Down Expand Up @@ -99,10 +99,10 @@ class MessageServiceImpl(
}

@Transactional(readOnly = true)
override fun getNotReadMessageCount(chatRoom: ChatRoomEntity, userId: Long): Int {
val timestamp = chatRoom.joinedUserInfo.find { it.userId == userId }?.timestamp
override fun getNotReadMessageCount(room: Room, userId: Long): Int {
val timestamp = room.joinUserInfo.find { it.userId == userId }?.timestamp
return messageRepository.findByChatRoomIdEqualsAndTimestampBefore(
chatRoomId = chatRoom.id.toString(),
chatRoomId = room.id.toString(),
timestamp = timestamp ?: LocalDateTime.now()
).count()
}
Expand Down

0 comments on commit 4638aea

Please sign in to comment.