From e314c01ad5b0a26e870464a7aefdb1b5810c0fa4 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 15 Oct 2023 10:40:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20suspend=E3=81=AAcontroller=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 4 +++- .../hideout/controller/InboxController.kt | 2 +- .../hideout/controller/InboxControllerImpl.kt | 5 ++-- .../hideout/controller/OutboxController.kt | 2 +- .../controller/OutboxControllerImpl.kt | 3 ++- .../hideout/controller/UserAPController.kt | 2 +- .../controller/UserAPControllerImpl.kt | 5 ++-- .../mastodon/MastodonAccountApiController.kt | 11 ++++----- .../mastodon/MastodonAppsApiController.kt | 9 ++++---- .../mastodon/MastodonInstanceApiController.kt | 4 +--- .../mastodon/MastodonMediaApiController.kt | 7 +++--- .../mastodon/MastodonStatusesApiContoller.kt | 23 +++++++++---------- .../mastodon/MastodonTimelineApiController.kt | 10 ++++---- 13 files changed, 42 insertions(+), 45 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 6c4c1d48..289e4815 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -59,6 +59,7 @@ tasks.create("openApiGenerateMastodonCompatibleApi", GenerateTask: modelPackage.set("dev.usbharu.hideout.domain.mastodon.model.generated") configOptions.put("interfaceOnly", "true") configOptions.put("useSpringBoot3", "true") + configOptions.put("reactive", "true") additionalProperties.put("useTags", "true") 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("io.trbl:blurhash:1.0.0") 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") diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/InboxController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/InboxController.kt index 35e18206..826e8597 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/InboxController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/InboxController.kt @@ -18,5 +18,5 @@ interface InboxController { ], method = [RequestMethod.GET, RequestMethod.POST] ) - fun inbox(@RequestBody string: String): ResponseEntity = ResponseEntity(HttpStatus.ACCEPTED) + suspend fun inbox(@RequestBody string: String): ResponseEntity = ResponseEntity(HttpStatus.ACCEPTED) } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/InboxControllerImpl.kt b/src/main/kotlin/dev/usbharu/hideout/controller/InboxControllerImpl.kt index a79ce5b0..7afc1871 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/InboxControllerImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/InboxControllerImpl.kt @@ -1,7 +1,6 @@ package dev.usbharu.hideout.controller import dev.usbharu.hideout.service.ap.APService -import kotlinx.coroutines.runBlocking import org.slf4j.LoggerFactory import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity @@ -10,11 +9,11 @@ import org.springframework.web.bind.annotation.RestController @RestController class InboxControllerImpl(private val apService: APService) : InboxController { - override fun inbox(@RequestBody string: String): ResponseEntity = runBlocking { + override suspend fun inbox(@RequestBody string: String): ResponseEntity { val parseActivity = apService.parseActivity(string) LOGGER.info("INBOX Processing Activity Type: {}", parseActivity) apService.processActivity(string, parseActivity) - ResponseEntity(HttpStatus.ACCEPTED) + return ResponseEntity(HttpStatus.ACCEPTED) } companion object { diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/OutboxController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/OutboxController.kt index ad463860..b62e5c11 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/OutboxController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/OutboxController.kt @@ -10,5 +10,5 @@ import org.springframework.web.bind.annotation.RestController @RestController interface OutboxController { @RequestMapping("/outbox", "/users/{username}/outbox", method = [RequestMethod.POST, RequestMethod.GET]) - fun outbox(@RequestBody string: String): ResponseEntity = ResponseEntity(HttpStatus.ACCEPTED) + suspend fun outbox(@RequestBody string: String): ResponseEntity = ResponseEntity(HttpStatus.ACCEPTED) } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/OutboxControllerImpl.kt b/src/main/kotlin/dev/usbharu/hideout/controller/OutboxControllerImpl.kt index b6b114f6..ba2f6542 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/OutboxControllerImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/OutboxControllerImpl.kt @@ -7,5 +7,6 @@ import org.springframework.web.bind.annotation.RestController @RestController class OutboxControllerImpl : OutboxController { - override fun outbox(@RequestBody string: String): ResponseEntity = ResponseEntity(HttpStatus.NOT_IMPLEMENTED) + override suspend fun outbox(@RequestBody string: String): ResponseEntity = + ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/UserAPController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/UserAPController.kt index 2c1da4ec..5390fff2 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/UserAPController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/UserAPController.kt @@ -9,5 +9,5 @@ import org.springframework.web.bind.annotation.RestController @RestController interface UserAPController { @GetMapping("/users/{username}") - fun userAp(@PathVariable("username") username: String): ResponseEntity + suspend fun userAp(@PathVariable("username") username: String): ResponseEntity } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/UserAPControllerImpl.kt b/src/main/kotlin/dev/usbharu/hideout/controller/UserAPControllerImpl.kt index b18a30cf..fe485244 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/UserAPControllerImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/UserAPControllerImpl.kt @@ -2,16 +2,15 @@ package dev.usbharu.hideout.controller import dev.usbharu.hideout.domain.model.ap.Person import dev.usbharu.hideout.service.ap.APUserService -import kotlinx.coroutines.runBlocking import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.RestController @RestController class UserAPControllerImpl(private val apUserService: APUserService) : UserAPController { - override fun userAp(username: String): ResponseEntity = runBlocking { + override suspend fun userAp(username: String): ResponseEntity { val person = apUserService.getPersonByName(username) person.context += listOf("https://www.w3.org/ns/activitystreams") - ResponseEntity(person, HttpStatus.OK) + return ResponseEntity(person, HttpStatus.OK) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonAccountApiController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonAccountApiController.kt index b9943422..86ecc737 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonAccountApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonAccountApiController.kt @@ -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.service.api.mastodon.AccountApiService import dev.usbharu.hideout.service.core.Transaction -import kotlinx.coroutines.runBlocking import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity @@ -19,28 +18,28 @@ class MastodonAccountApiController( private val accountApiService: AccountApiService, private val transaction: Transaction ) : AccountApi { - override fun apiV1AccountsVerifyCredentialsGet(): ResponseEntity = runBlocking { + override suspend fun apiV1AccountsVerifyCredentialsGet(): ResponseEntity { val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt - ResponseEntity( + return ResponseEntity( accountApiService.verifyCredentials(principal.getClaim("uid").toLong()), HttpStatus.OK ) } - override fun apiV1AccountsPost( + override suspend fun apiV1AccountsPost( username: String, password: String, email: String?, agreement: Boolean?, locale: Boolean?, reason: String? - ): ResponseEntity = runBlocking { + ): ResponseEntity { transaction.transaction { accountApiService.registerAccount(UserCreateDto(username, username, "", password)) } val httpHeaders = HttpHeaders() httpHeaders.location = URI("/users/$username") - ResponseEntity(Unit, httpHeaders, HttpStatus.FOUND) + return ResponseEntity(Unit, httpHeaders, HttpStatus.FOUND) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonAppsApiController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonAppsApiController.kt index 2b37eb9a..19eae835 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonAppsApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonAppsApiController.kt @@ -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.AppsRequest import dev.usbharu.hideout.service.api.mastodon.AppApiService -import kotlinx.coroutines.runBlocking import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.stereotype.Controller @@ -14,8 +13,8 @@ import org.springframework.web.bind.annotation.RequestParam @Controller class MastodonAppsApiController(private val appApiService: AppApiService) : AppApi { - override fun apiV1AppsPost(appsRequest: AppsRequest): ResponseEntity = runBlocking { - ResponseEntity( + override suspend fun apiV1AppsPost(appsRequest: AppsRequest): ResponseEntity { + return ResponseEntity( appApiService.createApp(appsRequest), HttpStatus.OK ) @@ -27,10 +26,10 @@ class MastodonAppsApiController(private val appApiService: AppApiService) : AppA produces = ["application/json"], consumes = ["application/x-www-form-urlencoded"] ) - fun apiV1AppsPost(@RequestParam map: Map): ResponseEntity = runBlocking { + suspend fun apiV1AppsPost(@RequestParam map: Map): ResponseEntity { val appsRequest = AppsRequest(map.getValue("client_name"), map.getValue("redirect_uris"), map["scopes"], map["website"]) - ResponseEntity( + return ResponseEntity( appApiService.createApp(appsRequest), HttpStatus.OK ) diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonInstanceApiController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonInstanceApiController.kt index 5741fd2e..5b2afba2 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonInstanceApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonInstanceApiController.kt @@ -3,14 +3,12 @@ package dev.usbharu.hideout.controller.mastodon import dev.usbharu.hideout.controller.mastodon.generated.InstanceApi import dev.usbharu.hideout.domain.mastodon.model.generated.V1Instance import dev.usbharu.hideout.service.api.mastodon.InstanceApiService -import kotlinx.coroutines.runBlocking import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.stereotype.Controller @Controller class MastodonInstanceApiController(private val instanceApiService: InstanceApiService) : InstanceApi { - override fun apiV1InstanceGet(): ResponseEntity = runBlocking { + override suspend fun apiV1InstanceGet(): ResponseEntity = ResponseEntity(instanceApiService.v1Instance(), HttpStatus.OK) - } } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonMediaApiController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonMediaApiController.kt index e357e2cc..a7c9658e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonMediaApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonMediaApiController.kt @@ -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.model.hideout.form.Media import dev.usbharu.hideout.service.api.mastodon.MediaApiService -import kotlinx.coroutines.runBlocking import org.springframework.http.ResponseEntity import org.springframework.stereotype.Controller import org.springframework.web.multipart.MultipartFile @Controller class MastodonMediaApiController(private val mediaApiService: MediaApiService) : MediaApi { - override fun apiV1MediaPost( + override suspend fun apiV1MediaPost( file: MultipartFile, thumbnail: MultipartFile?, description: String?, focus: String? - ): ResponseEntity = runBlocking { - ResponseEntity.ok( + ): ResponseEntity { + return ResponseEntity.ok( mediaApiService.postMedia( Media( file, diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonStatusesApiContoller.kt b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonStatusesApiContoller.kt index 11a3c753..796894ae 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonStatusesApiContoller.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonStatusesApiContoller.kt @@ -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.model.mastodon.StatusesRequest import dev.usbharu.hideout.service.api.mastodon.StatusesApiService -import kotlinx.coroutines.runBlocking import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.security.core.context.SecurityContextHolder @@ -13,19 +12,19 @@ import org.springframework.stereotype.Controller @Controller class MastodonStatusesApiContoller(private val statusesApiService: StatusesApiService) : StatusApi { - override fun apiV1StatusesPost( + override suspend fun apiV1StatusesPost( devUsbharuHideoutDomainModelMastodonStatusesRequest: StatusesRequest ): ResponseEntity { - return runBlocking { - val jwt = SecurityContextHolder.getContext().authentication.principal as Jwt - ResponseEntity( - statusesApiService.postStatus( - devUsbharuHideoutDomainModelMastodonStatusesRequest, - jwt.getClaim("uid").toLong() - ), - HttpStatus.OK - ) - } + val jwt = SecurityContextHolder.getContext().authentication.principal as Jwt + + return ResponseEntity( + statusesApiService.postStatus( + devUsbharuHideoutDomainModelMastodonStatusesRequest, + jwt.getClaim("uid").toLong() + ), + HttpStatus.OK + ) + } } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonTimelineApiController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonTimelineApiController.kt index 5d7b1bdb..26a5c392 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonTimelineApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/mastodon/MastodonTimelineApiController.kt @@ -3,6 +3,8 @@ package dev.usbharu.hideout.controller.mastodon import dev.usbharu.hideout.controller.mastodon.generated.TimelineApi import dev.usbharu.hideout.domain.mastodon.model.generated.Status import dev.usbharu.hideout.service.api.mastodon.TimelineApiService +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.runBlocking import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity @@ -17,7 +19,7 @@ class MastodonTimelineApiController(private val timelineApiService: TimelineApiS sinceId: String?, minId: String?, limit: Int? - ): ResponseEntity> = runBlocking { + ): ResponseEntity> = runBlocking { val jwt = SecurityContextHolder.getContext().authentication.principal as Jwt val homeTimeline = timelineApiService.homeTimeline( userId = jwt.getClaim("uid").toLong(), @@ -26,7 +28,7 @@ class MastodonTimelineApiController(private val timelineApiService: TimelineApiS sinceId = sinceId?.toLongOrNull(), limit = limit ?: 20 ) - ResponseEntity(homeTimeline, HttpStatus.OK) + ResponseEntity(homeTimeline.asFlow(), HttpStatus.OK) } override fun apiV1TimelinesPublicGet( @@ -37,7 +39,7 @@ class MastodonTimelineApiController(private val timelineApiService: TimelineApiS sinceId: String?, minId: String?, limit: Int? - ): ResponseEntity> = runBlocking { + ): ResponseEntity> = runBlocking { val publicTimeline = timelineApiService.publicTimeline( localOnly = local ?: false, remoteOnly = remote ?: false, @@ -47,6 +49,6 @@ class MastodonTimelineApiController(private val timelineApiService: TimelineApiS sinceId = sinceId?.toLongOrNull(), limit = limit ?: 20 ) - ResponseEntity(publicTimeline, HttpStatus.OK) + ResponseEntity(publicTimeline.asFlow(), HttpStatus.OK) } }