Skip to content

#150, 158, 167 - 에버 관련한 기능 구현 수정 및 쪽지 읽음 처리를 수정합니다. #168

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package com.wespot.message.port.`in`

import com.wespot.message.v2.MessageV2
import com.wespot.message.dto.request.CreatedMessageV2Request
import com.wespot.user.User

interface CreatedMessageV2UseCase {

fun createMessage(createdMessageV2Request: CreatedMessageV2Request): MessageV2

fun welcomeMessage(signUpUser: User)

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package com.wespot.message.service.listener

import com.wespot.message.port.`in`.CreateMessageUseCase
import com.wespot.user.User
import com.wespot.message.port.`in`.CreatedMessageV2UseCase
import com.wespot.user.event.WelcomeMessageEvent
import org.springframework.context.event.EventListener
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.event.TransactionPhase
import org.springframework.transaction.event.TransactionalEventListener

@Component
class MessageEventListener(
private val createMessageUseCase: CreateMessageUseCase
private val createMessageUseCase: CreateMessageUseCase,
private val createdMessageV2UseCase: CreatedMessageV2UseCase
) {

@EventListener
fun welcomeMessage(event: WelcomeMessageEvent) {
createMessageUseCase.welcomeMessage(event.signUpUser)
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
fun welcomeMessageV2(event: WelcomeMessageEvent){
createdMessageV2UseCase.welcomeMessage(event.signUpUser)
}


}
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package com.wespot.message.service.v2

import com.wespot.EventUtils
import com.wespot.auth.service.SecurityUtils
import com.wespot.exception.CustomException
import com.wespot.exception.ExceptionView
import com.wespot.message.v2.MessageV2
import com.wespot.message.MessageContent
import com.wespot.message.dto.request.CreatedMessageV2Request
import com.wespot.message.event.ReceivedMessageEvent
import com.wespot.message.port.`in`.CreatedMessageV2UseCase
import com.wespot.message.port.out.MessageV2Port
import com.wespot.message.v2.MessageV2
import com.wespot.user.User
import com.wespot.user.dto.request.CreatedAnonymousProfileRequest
import com.wespot.user.message.AnonymousProfile
import com.wespot.user.port.`in`.AnonymousProfileUseCase
import com.wespot.user.port.out.BlockedUserPort
import com.wespot.user.port.out.UserPort
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class CreatedMessageV2Service(
private val userPort: UserPort,
private val profileUseCase: AnonymousProfileUseCase,
private val messageV2Port: MessageV2Port,
private val blockedUserPort: BlockedUserPort
) : CreatedMessageV2UseCase {

override fun createMessage(createdMessageV2Request: CreatedMessageV2Request): MessageV2 {
Expand All @@ -35,21 +34,14 @@ class CreatedMessageV2Service(
)
val anonymousProfile = createAnonymousProfile(createdMessageV2Request)

val message = MessageV2.createInitial(
return MessageV2.createInitial(
content = createdMessageV2Request.content,
sender = sender,
receiver = receiver,
anonymousProfile = anonymousProfile,
savedMessageFunction = { message -> messageV2Port.save(message) },
alreadyUsedMessageOnToday = messageV2Port.countTodaySendMessages(sender.id),
// isBlockedFromReceiver = blockedUserPort.existsByBlockerIdAndBlockedId(
// blockerId = receiver.id,
// blockedId = sender.id
// )
)

EventUtils.publish(ReceivedMessageEvent(receiver = receiver, messageId = message.id))

return messageV2Port.save(message)
}

private fun createAnonymousProfile(createdMessageV2Request: CreatedMessageV2Request): AnonymousProfile? {
Expand All @@ -66,5 +58,24 @@ class CreatedMessageV2Service(
return null
}

@Transactional
override fun welcomeMessage(signUpUser: User) {
val ever = userPort.findByName(name = User.EVER_NAME) ?: throw CustomException(
message = "해당 계정이 존재하지 않습니다.",
view = ExceptionView.TOAST,
status = HttpStatus.NOT_FOUND,
)

val welcomeMessage = MessageV2.createInitial(
content = MessageContent.createWelcomeMessage(receiverName = signUpUser.name).content,
sender = ever,
receiver = signUpUser,
anonymousProfile = null,
savedMessageFunction = { message -> messageV2Port.save(message) },
alreadyUsedMessageOnToday = 0,
)
messageV2Port.save(messageV2 = welcomeMessage)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ class GetMessageV2Service(
val messageDetails = messageV2Port.findAllByMessageRoomId(messageRoomId = messageId)

val room = MessageRoom.of(viewer = loginUser, roomMessage = roomMessage, messages = messageDetails)
return MessageV2DetailsResponse.from(room = room)
val response = MessageV2DetailsResponse.from(room = room)
room.readUnreadMessages()
.forEach { messageV2Port.save(it) }
Comment on lines +75 to +77
Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider marking unread messages as read before constructing the response to ensure that the returned data reflects the updated read status.

Suggested change
val response = MessageV2DetailsResponse.from(room = room)
room.readUnreadMessages()
.forEach { messageV2Port.save(it) }
room.readUnreadMessages()
.forEach { messageV2Port.save(it) }
val response = MessageV2DetailsResponse.from(room = room)

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no my intention isn't like that

return response
}

}
3 changes: 3 additions & 0 deletions core/src/main/kotlin/com/wespot/user/port/out/UserPort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ interface UserPort {
withdrawalRequestAt: LocalDateTime,
withdrawalStatus: WithdrawalStatus
): List<User>

fun findByName(name: String): User?

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,11 @@ data class MessageDetails(
return messages.filter { it.isNotDeleted(viewer = viewer) }
}

fun readUnreadMessage(viewer: User): List<MessageV2> {
return messages.filter { it.isUnread(viewer = viewer) }
.onEach { it.message.read(viewer = viewer) }
.map { it.message }

}

}
4 changes: 4 additions & 0 deletions domain/src/main/kotlin/com/wespot/message/v2/MessageRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,8 @@ data class MessageRoom(
return messages.asList(viewer = viewer)
}

fun readUnreadMessages():List<MessageV2>{
return messages.readUnreadMessage(viewer = viewer)
}

}
19 changes: 8 additions & 11 deletions domain/src/main/kotlin/com/wespot/message/v2/MessageV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import com.wespot.exception.ExceptionView
import com.wespot.message.MessageContent
import com.wespot.message.event.MessageAnswerEvent
import com.wespot.message.event.ReadMessageByReceiverEvent
import com.wespot.message.event.ReceivedMessageEvent
import com.wespot.user.User
import com.wespot.user.event.UsedAnswerFeatureEvent
import com.wespot.user.message.AnonymousProfile
import org.hibernate.event.internal.EventUtil
import org.springframework.http.HttpStatus
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -56,23 +56,15 @@ data class MessageV2(
receiver: User,
anonymousProfile: AnonymousProfile?,
alreadyUsedMessageOnToday: Int,
// isBlockedFromReceiver: Boolean // 정책 논의중
savedMessageFunction: (MessageV2) -> MessageV2
): MessageV2 {
if (COUNT_OF_MAX_ABLE_TO_SEND_MESSAGE_PER_DAY <= alreadyUsedMessageOnToday) {
throw CustomException(
HttpStatus.BAD_REQUEST, ExceptionView.TOAST, "하루에 쪽지는 3개만 보낼 수 있습니다."
)
}

// if (isBlockedFromReceiver) {
// throw CustomException(
// HttpStatus.FORBIDDEN,
// ExceptionView.TOAST,
// "차단당한 상대에게는 쪽지를 보낼 수 없습니다."
// )
// }

return MessageV2(
val message = MessageV2(
id = 0,
content = MessageContent.from(content),
sender = sender,
Expand Down Expand Up @@ -102,6 +94,11 @@ data class MessageV2(

anonymousProfile = anonymousProfile
)

val savedMessage = savedMessageFunction.invoke(message)
EventUtils.publish(ReceivedMessageEvent(receiver = receiver, messageId = savedMessage.id))

return savedMessage
}

}
Expand Down
2 changes: 1 addition & 1 deletion domain/src/main/kotlin/com/wespot/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ data class User(
companion object {

private const val WITHDRAW_USER_NAME = "탈퇴한 유저입니다."
private const val EVER_NAME = "에버"
const val EVER_NAME = "에버"

fun create(
email: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,13 @@ class UserPersistenceAdapter(
}
}

override fun findByName(name: String): User? {
return userJpaRepository.findByName(name = name)?.let {
UserMapper.mapToDomainEntity(
userJpaEntity = it,
schoolJpaEntity = getBySchool(it)
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ interface UserJpaRepository : JpaRepository<UserJpaEntity, Long> {
@Param("loginUserId") loginUserId: Long,
pageable: Pageable
): List<UserJpaEntity>

@Query(
"""
SELECT COUNT(u)
Expand Down Expand Up @@ -127,4 +128,7 @@ interface UserJpaRepository : JpaRepository<UserJpaEntity, Long> {
withdrawalRequestAt: LocalDateTime,
withdrawalStatus: WithdrawalStatus
): List<UserJpaEntity>

fun findByName(name: String): UserJpaEntity?

}
Loading