From e781d1467fbbe289e1e4f8c8c31aae9fd13f92d0 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 10 Aug 2023 19:07:48 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=8D=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E9=96=A2=E6=95=B0=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ActivityPubReactionServiceImpl.kt | 16 +++++++++------- .../hideout/service/auth/JwtServiceImpl.kt | 6 +++--- .../usbharu/hideout/service/user/IUserService.kt | 2 -- .../usbharu/hideout/service/user/UserService.kt | 12 ------------ .../hideout/service/auth/JwtServiceImplTest.kt | 4 ++-- 5 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubReactionServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubReactionServiceImpl.kt index 217ccfbd..9fe4ff2e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubReactionServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubReactionServiceImpl.kt @@ -9,9 +9,10 @@ import dev.usbharu.hideout.domain.model.job.DeliverReactionJob import dev.usbharu.hideout.domain.model.job.DeliverRemoveReactionJob import dev.usbharu.hideout.exception.PostNotFoundException import dev.usbharu.hideout.plugins.postAp +import dev.usbharu.hideout.query.FollowerQueryService +import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.repository.IPostRepository import dev.usbharu.hideout.service.job.JobQueueParentService -import dev.usbharu.hideout.service.user.IUserService import io.ktor.client.* import kjob.core.job.JobProps import org.koin.core.annotation.Single @@ -19,14 +20,15 @@ import java.time.Instant @Single class ActivityPubReactionServiceImpl( - private val userService: IUserService, private val jobQueueParentService: JobQueueParentService, private val iPostRepository: IPostRepository, - private val httpClient: HttpClient + private val httpClient: HttpClient, + private val userQueryService: UserQueryService, + private val followerQueryService: FollowerQueryService ) : ActivityPubReactionService { override suspend fun reaction(like: Reaction) { - val followers = userService.findFollowersById(like.userId) - val user = userService.findById(like.userId) + val followers = followerQueryService.findFollowersById(like.userId) + val user = userQueryService.findById(like.userId) val post = iPostRepository.findOneById(like.postId) ?: throw PostNotFoundException("${like.postId} was not found.") followers.forEach { follower -> @@ -41,8 +43,8 @@ class ActivityPubReactionServiceImpl( } override suspend fun removeReaction(like: Reaction) { - val followers = userService.findFollowersById(like.userId) - val user = userService.findById(like.userId) + val followers = followerQueryService.findFollowersById(like.userId) + val user = userQueryService.findById(like.userId) val post = iPostRepository.findOneById(like.postId) ?: throw PostNotFoundException("${like.postId} was not found.") followers.forEach { follower -> diff --git a/src/main/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImpl.kt index 97fd4505..d611ebdd 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImpl.kt @@ -8,9 +8,9 @@ import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.domain.model.hideout.form.RefreshToken import dev.usbharu.hideout.exception.InvalidRefreshTokenException +import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.repository.IJwtRefreshTokenRepository import dev.usbharu.hideout.service.core.IMetaService -import dev.usbharu.hideout.service.user.IUserService import dev.usbharu.hideout.util.RsaUtil import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -25,7 +25,7 @@ import java.util.* class JwtServiceImpl( private val metaService: IMetaService, private val refreshTokenRepository: IJwtRefreshTokenRepository, - private val userService: IUserService + private val userQueryService: UserQueryService ) : IJwtService { private val privateKey by lazy { @@ -72,7 +72,7 @@ class JwtServiceImpl( val token = refreshTokenRepository.findByToken(refreshToken.refreshToken) ?: throw InvalidRefreshTokenException("Invalid Refresh Token") - val user = userService.findById(token.userId) + val user = userQueryService.findById(token.userId) val now = Instant.now() if (token.createdAt.isAfter(now)) { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt index 782bb8d5..1df65bf9 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt @@ -7,8 +7,6 @@ import dev.usbharu.hideout.domain.model.hideout.entity.User @Suppress("TooManyFunctions") interface IUserService { - suspend fun findById(id: Long): User - suspend fun findByNameLocalUser(name: String): User suspend fun findByUrl(url: String): User diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt index 024a3659..43bda21e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt @@ -9,7 +9,6 @@ import dev.usbharu.hideout.exception.UserNotFoundException import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.service.activitypub.ActivityPubSendFollowService import org.koin.core.annotation.Single -import java.lang.Integer.min import java.time.Instant @Single @@ -20,17 +19,6 @@ class UserService( ) : IUserService { - private val maxLimit = 100 - override suspend fun findAll(limit: Int?, offset: Long?): List { - return userRepository.findAllByLimitAndByOffset( - min(limit ?: maxLimit, maxLimit), - offset ?: 0 - ) - } - - override suspend fun findById(id: Long): User = - userRepository.findById(id) ?: throw UserNotFoundException("$id was not found.") - override suspend fun findByNameLocalUser(name: String): User { return userRepository.findByNameAndDomain(name, Config.configData.domain) ?: throw UserNotFoundException("$name was not found.") diff --git a/src/test/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImplTest.kt index f5cc0ec6..9c8fbee8 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/auth/JwtServiceImplTest.kt @@ -12,9 +12,9 @@ import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.domain.model.hideout.form.RefreshToken import dev.usbharu.hideout.exception.InvalidRefreshTokenException +import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.repository.IJwtRefreshTokenRepository import dev.usbharu.hideout.service.core.IMetaService -import dev.usbharu.hideout.service.user.IUserService import dev.usbharu.hideout.util.Base64Util import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest @@ -102,7 +102,7 @@ class JwtServiceImplTest { ) onBlocking { generateId() } doReturn 2L } - val userService = mock { + val userService = mock { onBlocking { findById(1L) } doReturn User( id = 1L, name = "test",