Skip to content

Commit

Permalink
feat: 만료된 보틀 보여주지 않기 (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
injoon2019 authored Aug 26, 2024
1 parent 142be84 commit fea0e05
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
package com.nexters.bottles.api.auth.controller

import com.nexters.bottles.api.auth.facade.AuthFacade
import com.nexters.bottles.api.auth.facade.dto.AppleRevokeResponse
import com.nexters.bottles.api.auth.facade.dto.AppleSignInUpRequest
import com.nexters.bottles.api.auth.facade.dto.AppleSignInUpResponse
import com.nexters.bottles.api.auth.facade.dto.AuthSmsRequest
import com.nexters.bottles.api.auth.facade.dto.KakaoSignInUpRequest
import com.nexters.bottles.api.auth.facade.dto.KakaoSignInUpResponse
import com.nexters.bottles.api.auth.facade.dto.LogoutRequest
import com.nexters.bottles.api.auth.facade.dto.ReissueTokenRequest
import com.nexters.bottles.api.auth.facade.dto.ReissueTokenResponse
import com.nexters.bottles.api.auth.facade.dto.SendSmsResponse
import com.nexters.bottles.api.auth.facade.dto.SignUpResponse
import com.nexters.bottles.api.auth.facade.dto.SmsSendRequest
import com.nexters.bottles.api.auth.facade.dto.SmsSignInRequest
import com.nexters.bottles.api.auth.facade.dto.SmsSignInResponse
import com.nexters.bottles.api.auth.facade.dto.*
import com.nexters.bottles.api.global.interceptor.AuthRequired
import com.nexters.bottles.api.global.interceptor.RefreshAuthRequired
import com.nexters.bottles.api.global.resolver.AccessToken
Expand Down Expand Up @@ -100,4 +87,11 @@ class AuthController(
fun getAppleClientSecret(): AppleRevokeResponse {
return authFacade.getAppleClientSecret()
}

@ApiOperation("fcm 토큰 갱신")
@PostMapping("/fcm")
@AuthRequired
fun insertFcmToken(@AuthUserId userId: Long, @RequestBody fcmUpdateRequest: FcmUpdateRequest) {
authFacade.updateFcmToken(userId, fcmUpdateRequest.fcmToken)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ class AuthFacade(
return AppleRevokeResponse(clientSecret = clientSecret)
}

fun updateFcmToken(userId: Long, fcmToken: String) {
fcmTokenService.registerFcmToken(userId, fcmToken)
}

private fun isSuperUser(phoneNumber: String) = phoneNumber == superUserNumber
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.nexters.bottles.api.auth.facade.dto

data class FcmUpdateRequest(
val fcmToken: String,
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class BottleFacade(
?: emptyList()
val doneBottles =
(groupByStatus[PingPongStatus.STOPPED].orEmpty() + groupByStatus[PingPongStatus.MATCHED].orEmpty())
.filter { it.isNotExpired(LocalDateTime.now()) }
.map { toPingPongBottleDto(it, user) }
.filter { it.userId !in blockedUserIds }
return PingPongListResponse(activeBottles = activeBottles, doneBottles = doneBottles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ class Bottle(
return bottleStatus == BottleStatus.SENT && pingPongStatus == PingPongStatus.NONE
}

fun isExpired(now: LocalDateTime): Boolean {
return expiredAt.plusDays(1L) <= now
}

fun isNotExpired(now: LocalDateTime): Boolean {
return !isExpired(now)
}

companion object {
private const val DELETE_AFTER_DAYS = 3
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@ import com.google.firebase.messaging.Message
import com.google.firebase.messaging.MulticastMessage
import com.google.firebase.messaging.Notification
import com.nexters.bottles.app.notification.component.dto.FcmNotification
import com.nexters.bottles.app.notification.repository.FcmTokenRepository
import mu.KotlinLogging
import org.springframework.stereotype.Component


@Component
class FcmClient {
class FcmClient(
private val fcmTokenRepository: FcmTokenRepository,
) {

private val log = KotlinLogging.logger { }

fun sendNotificationTo(userToken: String, fcmNotification: FcmNotification): String? {
val notification = makeNotification(fcmNotification)
val message = Message.builder()
.setNotification(notification)
.setToken(userToken)
.build()
return FirebaseMessaging.getInstance().send(message)

try {
return FirebaseMessaging.getInstance().send(message)
} catch (e: Exception) {
fcmTokenRepository.deleteByToken(userToken)
log.info { "fcmToken 404 예외 - 더이상 유효하지 않아 삭제 $userToken" }
return null
}
}

fun sendNotificationAll(userTokens: List<String>, fcmNotification: FcmNotification) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ interface FcmTokenRepository : JpaRepository<FcmToken, Long> {
fun findByUserIdAndToken(userId: Long, token: String): FcmToken?

fun findAllByUserIdIn(@Param("userIds") userIds: List<Long>): List<FcmToken>

fun deleteByToken(userToken: String)
}

0 comments on commit fea0e05

Please sign in to comment.