diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt index d2066b68..cb7cbf70 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt @@ -2,6 +2,7 @@ package dev.usbharu.hideout.mastodon.interfaces.api.account import dev.usbharu.hideout.application.external.Transaction import dev.usbharu.hideout.controller.mastodon.generated.AccountApi +import dev.usbharu.hideout.core.infrastructure.springframework.security.LoginUserContextHolder import dev.usbharu.hideout.core.service.user.UserCreateDto import dev.usbharu.hideout.domain.mastodon.model.generated.* import dev.usbharu.hideout.mastodon.service.account.AccountApiService @@ -11,37 +12,32 @@ import kotlinx.coroutines.runBlocking import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity -import org.springframework.security.core.context.SecurityContextHolder -import org.springframework.security.oauth2.jwt.Jwt import org.springframework.stereotype.Controller import java.net.URI @Controller class MastodonAccountApiController( private val accountApiService: AccountApiService, - private val transaction: Transaction + private val transaction: Transaction, + private val loginUserContextHolder: LoginUserContextHolder ) : AccountApi { override suspend fun apiV1AccountsIdFollowPost( id: String, followRequestBody: FollowRequestBody? ): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt + val userid = loginUserContextHolder.getLoginUserId() - return ResponseEntity.ok(accountApiService.follow(principal.getClaim("uid").toLong(), id.toLong())) + return ResponseEntity.ok(accountApiService.follow(userid, id.toLong())) } override suspend fun apiV1AccountsIdGet(id: String): ResponseEntity = ResponseEntity.ok(accountApiService.account(id.toLong())) - override suspend fun apiV1AccountsVerifyCredentialsGet(): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - return ResponseEntity( - accountApiService.verifyCredentials(principal.getClaim("uid").toLong()), - HttpStatus.OK - ) - } + override suspend fun apiV1AccountsVerifyCredentialsGet(): ResponseEntity = ResponseEntity( + accountApiService.verifyCredentials(loginUserContextHolder.getLoginUserId()), + HttpStatus.OK + ) override suspend fun apiV1AccountsPost( username: String, @@ -71,9 +67,7 @@ class MastodonAccountApiController( pinned: Boolean, tagged: String? ): ResponseEntity> = runBlocking { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val statusFlow = accountApiService.accountsStatuses( userid = id.toLong(), maxId = maxId?.toLongOrNull(), @@ -94,9 +88,7 @@ class MastodonAccountApiController( id: List?, withSuspended: Boolean ): ResponseEntity> = runBlocking { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() ResponseEntity.ok( accountApiService.relationships(userid, id.orEmpty().mapNotNull { it.toLongOrNull() }, withSuspended) @@ -105,9 +97,7 @@ class MastodonAccountApiController( } override suspend fun apiV1AccountsIdBlockPost(id: String): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val block = accountApiService.block(userid, id.toLong()) @@ -115,9 +105,7 @@ class MastodonAccountApiController( } override suspend fun apiV1AccountsIdUnblockPost(id: String): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val unblock = accountApiService.unblock(userid, id.toLong()) @@ -125,9 +113,7 @@ class MastodonAccountApiController( } override suspend fun apiV1AccountsIdUnfollowPost(id: String): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val unfollow = accountApiService.unfollow(userid, id.toLong()) @@ -135,9 +121,7 @@ class MastodonAccountApiController( } override suspend fun apiV1AccountsIdRemoveFromFollowersPost(id: String): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val removeFromFollowers = accountApiService.removeFromFollowers(userid, id.toLong()) @@ -146,9 +130,7 @@ class MastodonAccountApiController( override suspend fun apiV1AccountsUpdateCredentialsPatch(updateCredentials: UpdateCredentials?): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val removeFromFollowers = accountApiService.updateProfile(userid, updateCredentials) @@ -156,9 +138,7 @@ class MastodonAccountApiController( } override suspend fun apiV1FollowRequestsAccountIdAuthorizePost(accountId: String): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val acceptFollowRequest = accountApiService.acceptFollowRequest(userid, accountId.toLong()) @@ -166,9 +146,7 @@ class MastodonAccountApiController( } override suspend fun apiV1FollowRequestsAccountIdRejectPost(accountId: String): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val rejectFollowRequest = accountApiService.rejectFollowRequest(userid, accountId.toLong()) @@ -177,9 +155,7 @@ class MastodonAccountApiController( override fun apiV1FollowRequestsGet(maxId: String?, sinceId: String?, limit: Int?): ResponseEntity> = runBlocking { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val accountFlow = accountApiService.followRequests(userid, maxId?.toLong(), sinceId?.toLong(), limit ?: 20, false) @@ -188,9 +164,7 @@ class MastodonAccountApiController( } override suspend fun apiV1AccountsIdMutePost(id: String): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val mute = accountApiService.mute(userid, id.toLong()) @@ -198,9 +172,7 @@ class MastodonAccountApiController( } override suspend fun apiV1AccountsIdUnmutePost(id: String): ResponseEntity { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val unmute = accountApiService.unmute(userid, id.toLong()) @@ -209,9 +181,7 @@ class MastodonAccountApiController( override fun apiV1MutesGet(maxId: String?, sinceId: String?, limit: Int?): ResponseEntity> = runBlocking { - val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - - val userid = principal.getClaim("uid").toLong() + val userid = loginUserContextHolder.getLoginUserId() val unmute = accountApiService.mutesAccount(userid, maxId?.toLong(), sinceId?.toLong(), limit ?: 20).asFlow()