feat: Account APIのログインユーザー取得部分を共通化

This commit is contained in:
usbharu 2024-01-26 12:06:53 +09:00
parent e2365c632b
commit 91622bb33f
1 changed files with 22 additions and 52 deletions

View File

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