From 428c096fa67ac5283da52071f29bbc2b05f0b65a Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 30 Mar 2023 18:39:06 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BE=9D=E5=AD=98=E9=96=A2=E4=BF=82?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/usbharu/hideout/Application.kt | 8 ++++---- .../hideout/service/ActivityPubUserService.kt | 15 ++------------- .../hideout/service/IWebFingerService.kt | 3 +++ .../hideout/service/WebFingerService.kt | 18 ++++++++++++++---- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/Application.kt b/src/main/kotlin/dev/usbharu/hideout/Application.kt index ea766705..6a16a761 100644 --- a/src/main/kotlin/dev/usbharu/hideout/Application.kt +++ b/src/main/kotlin/dev/usbharu/hideout/Application.kt @@ -41,14 +41,14 @@ fun Application.module() { .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) ) } + single { HttpClient(CIO) } single { UserRepository(get()) } single { UserAuthRepository(get()) } single { UserAuthService(get(), get()) } single { UserService(get()) } - single { WebFingerService(get(),get(),get(),get()) } - single { ActivityPubUserService(get(), get(),get(),get()) } single { ActivityPubService() } - single { HttpClient(CIO) } + single { ActivityPubUserService(get(), get(),get(),get()) } + single { WebFingerService(get(),get()) } } configureKoin(module) val configData by inject() @@ -70,5 +70,5 @@ fun Application.module() { register(userAuthService) wellKnown(userService) val activityPubService by inject() - userActivityPubRouting(activityPubService) + userActivityPubRouting(activityPubService, activityPubUserService) } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt index e1469bae..b870c6dc 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt @@ -12,7 +12,7 @@ class ActivityPubUserService( private val httpClient: HttpClient, private val userService: UserService, private val userAuthService: IUserAuthService, - private val webFingerService: WebFingerService + private val webFingerService: IWebFingerService ) { suspend fun generateUserModel(name: String): Person { val userEntity = userService.findByName(name) @@ -43,20 +43,9 @@ class ActivityPubUserService( ) } - suspend fun fetchUserModel(url: String): Person? { - return try { - httpClient.get(url).body() - } catch (e: ResponseException) { - if (e.response.status == HttpStatusCode.NotFound) { - return null - } - throw e - } - } - suspend fun receiveFollow(follow: Follow) { val actor = follow.actor ?: throw IllegalArgumentException("actor is null") - val person = fetchUserModel(actor) ?: throw IllegalArgumentException("actor is not found") + val person = webFingerService.fetchUserModel(actor) ?: throw IllegalArgumentException("actor is not found") val inboxUrl = person.inbox ?: throw IllegalArgumentException("inbox is not found") httpClient.post(inboxUrl) { setBody(Accept( diff --git a/src/main/kotlin/dev/usbharu/hideout/service/IWebFingerService.kt b/src/main/kotlin/dev/usbharu/hideout/service/IWebFingerService.kt index d13f33c1..2d01e147 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/IWebFingerService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/IWebFingerService.kt @@ -1,5 +1,6 @@ package dev.usbharu.hideout.service +import dev.usbharu.hideout.ap.Person import dev.usbharu.hideout.domain.model.UserEntity import dev.usbharu.hideout.webfinger.WebFinger @@ -12,4 +13,6 @@ interface IWebFingerService { val webFinger = fetch(acct)?: throw IllegalArgumentException() return sync(webFinger) } + + suspend fun fetchUserModel(actor: String): Person? } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/WebFingerService.kt b/src/main/kotlin/dev/usbharu/hideout/service/WebFingerService.kt index 7e3fe711..90e99a07 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/WebFingerService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/WebFingerService.kt @@ -1,5 +1,6 @@ package dev.usbharu.hideout.service +import dev.usbharu.hideout.ap.Person import dev.usbharu.hideout.domain.model.User import dev.usbharu.hideout.domain.model.UserEntity import dev.usbharu.hideout.util.HttpUtil @@ -12,9 +13,7 @@ import io.ktor.http.* class WebFingerService( private val httpClient: HttpClient, - private val userService: UserService, - private val userAuthService: IUserAuthService, - private val activityPubUserService: ActivityPubUserService + private val userService: UserService ) : IWebFingerService { override suspend fun fetch(acct: String): WebFinger? { @@ -32,6 +31,17 @@ class WebFingerService( } } + override suspend fun fetchUserModel(url: String): Person? { + return try { + httpClient.get(url).body() + } catch (e: ResponseException) { + if (e.response.status == HttpStatusCode.NotFound) { + return null + } + throw e + } + } + override suspend fun sync(webFinger: WebFinger): UserEntity { val link = webFinger.links.find { @@ -46,7 +56,7 @@ class WebFingerService( val domain = fullName.substringAfterLast("@") val userName = fullName.substringBeforeLast("@") - val userModel = activityPubUserService.fetchUserModel(link) ?: throw Exception() + val userModel = fetchUserModel(link) ?: throw Exception() val user = User( userModel.preferredUsername ?: throw IllegalStateException(),