From 91622bb33f117a6a84c2ad4039a042db72552be5 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 26 Jan 2024 12:06:53 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Account=20API=E3=81=AE=E3=83=AD?= =?UTF-8?q?=E3=82=B0=E3=82=A4=E3=83=B3=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC?= =?UTF-8?q?=E5=8F=96=E5=BE=97=E9=83=A8=E5=88=86=E3=82=92=E5=85=B1=E9=80=9A?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../account/MastodonAccountApiController.kt | 74 ++++++------------- 1 file changed, 22 insertions(+), 52 deletions(-) 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()