Compare commits

...

2 Commits

5 changed files with 166 additions and 119 deletions

View File

@ -223,10 +223,9 @@ class LiveRoomController(private val service: LiveRoomService) {
@PostMapping("/quit")
fun quitRoom(
@RequestParam("id") roomId: Long,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
) = run {
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
ApiResponse.ok(service.quitRoom(roomId, member))
ApiResponse.ok(service.quitRoom(member))
}
}

View File

@ -53,6 +53,8 @@ import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Date
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.write
@Service
@Transactional(readOnly = true)
@ -88,6 +90,8 @@ class LiveRoomService(
@Value("\${cloud.aws.cloud-front.host}")
private val cloudFrontHost: String
) {
private val tokenLocks: MutableMap<Long, ReentrantReadWriteLock> = mutableMapOf()
fun getRoomList(
dateString: String?,
status: LiveRoomStatus,
@ -415,6 +419,8 @@ class LiveRoomService(
throw SodaException("비밀번호가 일치하지 않습니다.\n다시 확인 후 입력해주세요.")
}
val lock = getOrCreateLock(memberId = member.id!!)
lock.write {
var roomInfo = roomInfoRepository.findByIdOrNull(request.roomId)
if (roomInfo == null) {
roomInfo = roomInfoRepository.save(LiveRoomInfo(roomId = request.roomId))
@ -459,6 +465,7 @@ class LiveRoomService(
roomInfoRepository.save(roomInfo)
roomVisitService.roomVisit(room, member)
}
}
fun getRecentRoomInfo(member: Member): GetRecentRoomInfoResponse {
return repository.getRecentRoomInfo(memberId = member.id!!, cloudFrontHost = cloudFrontHost)
@ -621,6 +628,8 @@ class LiveRoomService(
}
fun deleteDonationMessage(request: DeleteLiveRoomDonationMessage, member: Member) {
val lock = getOrCreateLock(memberId = member.id!!)
lock.write {
val room = repository.findByIdOrNull(request.roomId)
?: throw SodaException("해당하는 라이브가 없습니다.")
@ -634,6 +643,7 @@ class LiveRoomService(
roomInfo.removeDonationMessage(request.messageUUID)
roomInfoRepository.save(roomInfo)
}
}
fun getUserProfile(roomId: Long, userId: Long, member: Member): GetLiveRoomUserProfileResponse {
val room = repository.getLiveRoom(roomId)
@ -701,28 +711,33 @@ class LiveRoomService(
}
fun setSpeaker(request: SetManagerOrSpeakerOrAudienceRequest) {
val lock = getOrCreateLock(memberId = request.memberId)
lock.write {
val roomInfo = roomInfoRepository.findByIdOrNull(request.roomId)
?: throw SodaException("해당하는 라이브의 정보가 없습니다.")
val account = memberRepository.findByIdOrNull(request.accountId)
val member = memberRepository.findByIdOrNull(request.memberId)
?: throw SodaException("로그인 정보를 확인해 주세요.")
if (roomInfo.speakerCount > 9) {
throw SodaException("스피커 정원이 초과하였습니다.")
}
roomInfo.removeListener(account)
roomInfo.removeManager(account)
roomInfo.addSpeaker(account, cloudFrontHost)
roomInfo.removeListener(member)
roomInfo.removeManager(member)
roomInfo.addSpeaker(member, cloudFrontHost)
roomInfoRepository.save(roomInfo)
}
}
fun setListener(request: SetManagerOrSpeakerOrAudienceRequest) {
val lock = getOrCreateLock(memberId = request.memberId)
lock.write {
val roomInfo = roomInfoRepository.findByIdOrNull(request.roomId)
?: throw SodaException("해당하는 라이브의 정보가 없습니다.")
val member = memberRepository.findByIdOrNull(request.accountId)
val member = memberRepository.findByIdOrNull(request.memberId)
?: throw SodaException("로그인 정보를 확인해 주세요.")
roomInfo.removeSpeaker(member)
@ -731,14 +746,17 @@ class LiveRoomService(
roomInfoRepository.save(roomInfo)
}
}
fun setManager(request: SetManagerOrSpeakerOrAudienceRequest, member: Member) {
val lock = getOrCreateLock(memberId = member.id!!)
lock.write {
val room = repository.getLiveRoom(request.roomId) ?: throw SodaException("잘못된 요청입니다.")
if (room.member!!.id!! != member.id!!) {
throw SodaException("권한이 없습니다.")
}
val user = memberRepository.findByIdOrNull(request.accountId) ?: throw SodaException("해당하는 유저가 없습니다.")
val user = memberRepository.findByIdOrNull(request.memberId) ?: throw SodaException("해당하는 유저가 없습니다.")
val roomInfo = roomInfoRepository.findByIdOrNull(request.roomId)
?: throw SodaException("해당하는 라이브의 정보가 없습니다.")
@ -760,6 +778,7 @@ class LiveRoomService(
roomInfoRepository.save(roomInfo)
}
}
@Transactional
fun donation(request: LiveRoomDonationRequest, member: Member) {
@ -781,6 +800,8 @@ class LiveRoomService(
)
if (request.message.isNotBlank()) {
val lock = getOrCreateLock(memberId = member.id!!)
lock.write {
val roomInfo = roomInfoRepository.findByIdOrNull(room.id!!)
?: throw SodaException("해당하는 라이브의 정보가 없습니다.")
@ -793,6 +814,7 @@ class LiveRoomService(
roomInfoRepository.save(roomInfo)
}
}
}
@Transactional
fun refundDonation(roomId: Long, member: Member) {
@ -843,11 +865,17 @@ class LiveRoomService(
)
}
fun quitRoom(roomId: Long, member: Member) {
val room = repository.getLiveRoom(roomId)
fun quitRoom(member: Member) {
val roomVisit = roomVisitService.getLatestRoomVisit(member.id!!)
val roomId = roomVisit?.room?.id
if (roomId != null) {
val lock = getOrCreateLock(memberId = member.id!!)
lock.write {
val roomInfo = roomInfoRepository.findByIdOrNull(roomId)
if (roomInfo != null) {
if (room?.member != null && room.member!! == member) {
val room = repository.getLiveRoom(roomId) ?: return
if (room.member != null && room.member!! == member) {
room.isActive = false
kickOutService.deleteKickOutData(roomId = room.id!!)
roomInfoRepository.deleteById(roomInfo.roomId)
@ -860,3 +888,9 @@ class LiveRoomService(
}
}
}
}
private fun getOrCreateLock(memberId: Long): ReentrantReadWriteLock {
return tokenLocks.computeIfAbsent(memberId) { ReentrantReadWriteLock() }
}
}

View File

@ -2,5 +2,5 @@ package kr.co.vividnext.sodalive.live.room
data class SetManagerOrSpeakerOrAudienceRequest(
val roomId: Long,
val accountId: Long
val memberId: Long
)

View File

@ -13,6 +13,7 @@ interface LiveRoomVisitRepository : JpaRepository<LiveRoomVisit, Long>, LiveRoom
interface LiveRoomVisitQueryRepository {
fun findByRoomIdAndMemberId(roomId: Long, memberId: Long): LiveRoomVisit?
fun findFirstByMemberIdOrderByUpdatedAtDesc(memberId: Long): LiveRoomVisit?
fun getLatestRoomVisit(memberId: Long): LiveRoomVisit?
}
@Repository
@ -38,4 +39,13 @@ class LiveRoomVisitQueryRepositoryImpl(private val queryFactory: JPAQueryFactory
.orderBy(liveRoomVisit.updatedAt.desc())
.fetchFirst()
}
override fun getLatestRoomVisit(memberId: Long): LiveRoomVisit? {
return queryFactory
.selectFrom(liveRoomVisit)
.innerJoin(liveRoomVisit.member, member)
.where(member.id.eq(memberId))
.orderBy(liveRoomVisit.updatedAt.desc())
.fetchFirst()
}
}

View File

@ -22,4 +22,8 @@ class LiveRoomVisitService(private val repository: LiveRoomVisitRepository) {
repository.save(roomVisit)
}
fun getLatestRoomVisit(memberId: Long): LiveRoomVisit? {
return repository.getLatestRoomVisit(memberId)
}
}