feat: suspendなcontrollerに変更

This commit is contained in:
usbharu 2023-10-15 10:40:26 +09:00
parent e3b7e0ed23
commit e314c01ad5
13 changed files with 42 additions and 45 deletions

View File

@ -59,6 +59,7 @@ tasks.create<GenerateTask>("openApiGenerateMastodonCompatibleApi", GenerateTask:
modelPackage.set("dev.usbharu.hideout.domain.mastodon.model.generated") modelPackage.set("dev.usbharu.hideout.domain.mastodon.model.generated")
configOptions.put("interfaceOnly", "true") configOptions.put("interfaceOnly", "true")
configOptions.put("useSpringBoot3", "true") configOptions.put("useSpringBoot3", "true")
configOptions.put("reactive", "true")
additionalProperties.put("useTags", "true") additionalProperties.put("useTags", "true")
importMappings.put("org.springframework.core.io.Resource", "org.springframework.web.multipart.MultipartFile") importMappings.put("org.springframework.core.io.Resource", "org.springframework.web.multipart.MultipartFile")
@ -122,7 +123,8 @@ dependencies {
implementation("org.jetbrains.exposed:exposed-spring-boot-starter:0.44.0") implementation("org.jetbrains.exposed:exposed-spring-boot-starter:0.44.0")
implementation("io.trbl:blurhash:1.0.0") implementation("io.trbl:blurhash:1.0.0")
implementation("software.amazon.awssdk:s3:2.20.157") implementation("software.amazon.awssdk:s3:2.20.157")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.7.3")
implementation("io.ktor:ktor-client-logging-jvm:$ktor_version") implementation("io.ktor:ktor-client-logging-jvm:$ktor_version")

View File

@ -18,5 +18,5 @@ interface InboxController {
], ],
method = [RequestMethod.GET, RequestMethod.POST] method = [RequestMethod.GET, RequestMethod.POST]
) )
fun inbox(@RequestBody string: String): ResponseEntity<Unit> = ResponseEntity(HttpStatus.ACCEPTED) suspend fun inbox(@RequestBody string: String): ResponseEntity<Unit> = ResponseEntity(HttpStatus.ACCEPTED)
} }

View File

@ -1,7 +1,6 @@
package dev.usbharu.hideout.controller package dev.usbharu.hideout.controller
import dev.usbharu.hideout.service.ap.APService import dev.usbharu.hideout.service.ap.APService
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
@ -10,11 +9,11 @@ import org.springframework.web.bind.annotation.RestController
@RestController @RestController
class InboxControllerImpl(private val apService: APService) : InboxController { class InboxControllerImpl(private val apService: APService) : InboxController {
override fun inbox(@RequestBody string: String): ResponseEntity<Unit> = runBlocking { override suspend fun inbox(@RequestBody string: String): ResponseEntity<Unit> {
val parseActivity = apService.parseActivity(string) val parseActivity = apService.parseActivity(string)
LOGGER.info("INBOX Processing Activity Type: {}", parseActivity) LOGGER.info("INBOX Processing Activity Type: {}", parseActivity)
apService.processActivity(string, parseActivity) apService.processActivity(string, parseActivity)
ResponseEntity(HttpStatus.ACCEPTED) return ResponseEntity(HttpStatus.ACCEPTED)
} }
companion object { companion object {

View File

@ -10,5 +10,5 @@ import org.springframework.web.bind.annotation.RestController
@RestController @RestController
interface OutboxController { interface OutboxController {
@RequestMapping("/outbox", "/users/{username}/outbox", method = [RequestMethod.POST, RequestMethod.GET]) @RequestMapping("/outbox", "/users/{username}/outbox", method = [RequestMethod.POST, RequestMethod.GET])
fun outbox(@RequestBody string: String): ResponseEntity<Unit> = ResponseEntity(HttpStatus.ACCEPTED) suspend fun outbox(@RequestBody string: String): ResponseEntity<Unit> = ResponseEntity(HttpStatus.ACCEPTED)
} }

View File

@ -7,5 +7,6 @@ import org.springframework.web.bind.annotation.RestController
@RestController @RestController
class OutboxControllerImpl : OutboxController { class OutboxControllerImpl : OutboxController {
override fun outbox(@RequestBody string: String): ResponseEntity<Unit> = ResponseEntity(HttpStatus.NOT_IMPLEMENTED) override suspend fun outbox(@RequestBody string: String): ResponseEntity<Unit> =
ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
} }

View File

@ -9,5 +9,5 @@ import org.springframework.web.bind.annotation.RestController
@RestController @RestController
interface UserAPController { interface UserAPController {
@GetMapping("/users/{username}") @GetMapping("/users/{username}")
fun userAp(@PathVariable("username") username: String): ResponseEntity<Person> suspend fun userAp(@PathVariable("username") username: String): ResponseEntity<Person>
} }

View File

@ -2,16 +2,15 @@ package dev.usbharu.hideout.controller
import dev.usbharu.hideout.domain.model.ap.Person import dev.usbharu.hideout.domain.model.ap.Person
import dev.usbharu.hideout.service.ap.APUserService import dev.usbharu.hideout.service.ap.APUserService
import kotlinx.coroutines.runBlocking
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
@RestController @RestController
class UserAPControllerImpl(private val apUserService: APUserService) : UserAPController { class UserAPControllerImpl(private val apUserService: APUserService) : UserAPController {
override fun userAp(username: String): ResponseEntity<Person> = runBlocking { override suspend fun userAp(username: String): ResponseEntity<Person> {
val person = apUserService.getPersonByName(username) val person = apUserService.getPersonByName(username)
person.context += listOf("https://www.w3.org/ns/activitystreams") person.context += listOf("https://www.w3.org/ns/activitystreams")
ResponseEntity(person, HttpStatus.OK) return ResponseEntity(person, HttpStatus.OK)
} }
} }

View File

@ -5,7 +5,6 @@ import dev.usbharu.hideout.domain.mastodon.model.generated.CredentialAccount
import dev.usbharu.hideout.domain.model.hideout.dto.UserCreateDto import dev.usbharu.hideout.domain.model.hideout.dto.UserCreateDto
import dev.usbharu.hideout.service.api.mastodon.AccountApiService import dev.usbharu.hideout.service.api.mastodon.AccountApiService
import dev.usbharu.hideout.service.core.Transaction import dev.usbharu.hideout.service.core.Transaction
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
@ -19,28 +18,28 @@ class MastodonAccountApiController(
private val accountApiService: AccountApiService, private val accountApiService: AccountApiService,
private val transaction: Transaction private val transaction: Transaction
) : AccountApi { ) : AccountApi {
override fun apiV1AccountsVerifyCredentialsGet(): ResponseEntity<CredentialAccount> = runBlocking { override suspend fun apiV1AccountsVerifyCredentialsGet(): ResponseEntity<CredentialAccount> {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
ResponseEntity( return ResponseEntity(
accountApiService.verifyCredentials(principal.getClaim<String>("uid").toLong()), accountApiService.verifyCredentials(principal.getClaim<String>("uid").toLong()),
HttpStatus.OK HttpStatus.OK
) )
} }
override fun apiV1AccountsPost( override suspend fun apiV1AccountsPost(
username: String, username: String,
password: String, password: String,
email: String?, email: String?,
agreement: Boolean?, agreement: Boolean?,
locale: Boolean?, locale: Boolean?,
reason: String? reason: String?
): ResponseEntity<Unit> = runBlocking { ): ResponseEntity<Unit> {
transaction.transaction { transaction.transaction {
accountApiService.registerAccount(UserCreateDto(username, username, "", password)) accountApiService.registerAccount(UserCreateDto(username, username, "", password))
} }
val httpHeaders = HttpHeaders() val httpHeaders = HttpHeaders()
httpHeaders.location = URI("/users/$username") httpHeaders.location = URI("/users/$username")
ResponseEntity(Unit, httpHeaders, HttpStatus.FOUND) return ResponseEntity(Unit, httpHeaders, HttpStatus.FOUND)
} }
} }

View File

@ -4,7 +4,6 @@ import dev.usbharu.hideout.controller.mastodon.generated.AppApi
import dev.usbharu.hideout.domain.mastodon.model.generated.Application import dev.usbharu.hideout.domain.mastodon.model.generated.Application
import dev.usbharu.hideout.domain.mastodon.model.generated.AppsRequest import dev.usbharu.hideout.domain.mastodon.model.generated.AppsRequest
import dev.usbharu.hideout.service.api.mastodon.AppApiService import dev.usbharu.hideout.service.api.mastodon.AppApiService
import kotlinx.coroutines.runBlocking
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
@ -14,8 +13,8 @@ import org.springframework.web.bind.annotation.RequestParam
@Controller @Controller
class MastodonAppsApiController(private val appApiService: AppApiService) : AppApi { class MastodonAppsApiController(private val appApiService: AppApiService) : AppApi {
override fun apiV1AppsPost(appsRequest: AppsRequest): ResponseEntity<Application> = runBlocking { override suspend fun apiV1AppsPost(appsRequest: AppsRequest): ResponseEntity<Application> {
ResponseEntity( return ResponseEntity(
appApiService.createApp(appsRequest), appApiService.createApp(appsRequest),
HttpStatus.OK HttpStatus.OK
) )
@ -27,10 +26,10 @@ class MastodonAppsApiController(private val appApiService: AppApiService) : AppA
produces = ["application/json"], produces = ["application/json"],
consumes = ["application/x-www-form-urlencoded"] consumes = ["application/x-www-form-urlencoded"]
) )
fun apiV1AppsPost(@RequestParam map: Map<String, String>): ResponseEntity<Application> = runBlocking { suspend fun apiV1AppsPost(@RequestParam map: Map<String, String>): ResponseEntity<Application> {
val appsRequest = val appsRequest =
AppsRequest(map.getValue("client_name"), map.getValue("redirect_uris"), map["scopes"], map["website"]) AppsRequest(map.getValue("client_name"), map.getValue("redirect_uris"), map["scopes"], map["website"])
ResponseEntity( return ResponseEntity(
appApiService.createApp(appsRequest), appApiService.createApp(appsRequest),
HttpStatus.OK HttpStatus.OK
) )

View File

@ -3,14 +3,12 @@ package dev.usbharu.hideout.controller.mastodon
import dev.usbharu.hideout.controller.mastodon.generated.InstanceApi import dev.usbharu.hideout.controller.mastodon.generated.InstanceApi
import dev.usbharu.hideout.domain.mastodon.model.generated.V1Instance import dev.usbharu.hideout.domain.mastodon.model.generated.V1Instance
import dev.usbharu.hideout.service.api.mastodon.InstanceApiService import dev.usbharu.hideout.service.api.mastodon.InstanceApiService
import kotlinx.coroutines.runBlocking
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
@Controller @Controller
class MastodonInstanceApiController(private val instanceApiService: InstanceApiService) : InstanceApi { class MastodonInstanceApiController(private val instanceApiService: InstanceApiService) : InstanceApi {
override fun apiV1InstanceGet(): ResponseEntity<V1Instance> = runBlocking { override suspend fun apiV1InstanceGet(): ResponseEntity<V1Instance> =
ResponseEntity(instanceApiService.v1Instance(), HttpStatus.OK) ResponseEntity(instanceApiService.v1Instance(), HttpStatus.OK)
}
} }

View File

@ -4,20 +4,19 @@ import dev.usbharu.hideout.controller.mastodon.generated.MediaApi
import dev.usbharu.hideout.domain.mastodon.model.generated.MediaAttachment import dev.usbharu.hideout.domain.mastodon.model.generated.MediaAttachment
import dev.usbharu.hideout.domain.model.hideout.form.Media import dev.usbharu.hideout.domain.model.hideout.form.Media
import dev.usbharu.hideout.service.api.mastodon.MediaApiService import dev.usbharu.hideout.service.api.mastodon.MediaApiService
import kotlinx.coroutines.runBlocking
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
import org.springframework.web.multipart.MultipartFile import org.springframework.web.multipart.MultipartFile
@Controller @Controller
class MastodonMediaApiController(private val mediaApiService: MediaApiService) : MediaApi { class MastodonMediaApiController(private val mediaApiService: MediaApiService) : MediaApi {
override fun apiV1MediaPost( override suspend fun apiV1MediaPost(
file: MultipartFile, file: MultipartFile,
thumbnail: MultipartFile?, thumbnail: MultipartFile?,
description: String?, description: String?,
focus: String? focus: String?
): ResponseEntity<MediaAttachment> = runBlocking { ): ResponseEntity<MediaAttachment> {
ResponseEntity.ok( return ResponseEntity.ok(
mediaApiService.postMedia( mediaApiService.postMedia(
Media( Media(
file, file,

View File

@ -4,7 +4,6 @@ import dev.usbharu.hideout.controller.mastodon.generated.StatusApi
import dev.usbharu.hideout.domain.mastodon.model.generated.Status import dev.usbharu.hideout.domain.mastodon.model.generated.Status
import dev.usbharu.hideout.domain.model.mastodon.StatusesRequest import dev.usbharu.hideout.domain.model.mastodon.StatusesRequest
import dev.usbharu.hideout.service.api.mastodon.StatusesApiService import dev.usbharu.hideout.service.api.mastodon.StatusesApiService
import kotlinx.coroutines.runBlocking
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.core.context.SecurityContextHolder
@ -13,19 +12,19 @@ import org.springframework.stereotype.Controller
@Controller @Controller
class MastodonStatusesApiContoller(private val statusesApiService: StatusesApiService) : StatusApi { class MastodonStatusesApiContoller(private val statusesApiService: StatusesApiService) : StatusApi {
override fun apiV1StatusesPost( override suspend fun apiV1StatusesPost(
devUsbharuHideoutDomainModelMastodonStatusesRequest: StatusesRequest devUsbharuHideoutDomainModelMastodonStatusesRequest: StatusesRequest
): ResponseEntity<Status> { ): ResponseEntity<Status> {
return runBlocking {
val jwt = SecurityContextHolder.getContext().authentication.principal as Jwt
ResponseEntity( val jwt = SecurityContextHolder.getContext().authentication.principal as Jwt
statusesApiService.postStatus(
devUsbharuHideoutDomainModelMastodonStatusesRequest, return ResponseEntity(
jwt.getClaim<String>("uid").toLong() statusesApiService.postStatus(
), devUsbharuHideoutDomainModelMastodonStatusesRequest,
HttpStatus.OK jwt.getClaim<String>("uid").toLong()
) ),
} HttpStatus.OK
)
} }
} }

View File

@ -3,6 +3,8 @@ package dev.usbharu.hideout.controller.mastodon
import dev.usbharu.hideout.controller.mastodon.generated.TimelineApi import dev.usbharu.hideout.controller.mastodon.generated.TimelineApi
import dev.usbharu.hideout.domain.mastodon.model.generated.Status import dev.usbharu.hideout.domain.mastodon.model.generated.Status
import dev.usbharu.hideout.service.api.mastodon.TimelineApiService import dev.usbharu.hideout.service.api.mastodon.TimelineApiService
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
@ -17,7 +19,7 @@ class MastodonTimelineApiController(private val timelineApiService: TimelineApiS
sinceId: String?, sinceId: String?,
minId: String?, minId: String?,
limit: Int? limit: Int?
): ResponseEntity<List<Status>> = runBlocking { ): ResponseEntity<Flow<Status>> = runBlocking {
val jwt = SecurityContextHolder.getContext().authentication.principal as Jwt val jwt = SecurityContextHolder.getContext().authentication.principal as Jwt
val homeTimeline = timelineApiService.homeTimeline( val homeTimeline = timelineApiService.homeTimeline(
userId = jwt.getClaim<String>("uid").toLong(), userId = jwt.getClaim<String>("uid").toLong(),
@ -26,7 +28,7 @@ class MastodonTimelineApiController(private val timelineApiService: TimelineApiS
sinceId = sinceId?.toLongOrNull(), sinceId = sinceId?.toLongOrNull(),
limit = limit ?: 20 limit = limit ?: 20
) )
ResponseEntity(homeTimeline, HttpStatus.OK) ResponseEntity(homeTimeline.asFlow(), HttpStatus.OK)
} }
override fun apiV1TimelinesPublicGet( override fun apiV1TimelinesPublicGet(
@ -37,7 +39,7 @@ class MastodonTimelineApiController(private val timelineApiService: TimelineApiS
sinceId: String?, sinceId: String?,
minId: String?, minId: String?,
limit: Int? limit: Int?
): ResponseEntity<List<Status>> = runBlocking { ): ResponseEntity<Flow<Status>> = runBlocking {
val publicTimeline = timelineApiService.publicTimeline( val publicTimeline = timelineApiService.publicTimeline(
localOnly = local ?: false, localOnly = local ?: false,
remoteOnly = remote ?: false, remoteOnly = remote ?: false,
@ -47,6 +49,6 @@ class MastodonTimelineApiController(private val timelineApiService: TimelineApiS
sinceId = sinceId?.toLongOrNull(), sinceId = sinceId?.toLongOrNull(),
limit = limit ?: 20 limit = limit ?: 20
) )
ResponseEntity(publicTimeline, HttpStatus.OK) ResponseEntity(publicTimeline.asFlow(), HttpStatus.OK)
} }
} }