fix: 依存関係を修正

This commit is contained in:
usbharu 2023-03-30 18:39:06 +09:00
parent 64ef0b70b4
commit 428c096fa6
4 changed files with 23 additions and 21 deletions

View File

@ -41,14 +41,14 @@ fun Application.module() {
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY) .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
) )
} }
single<HttpClient> { HttpClient(CIO) }
single<IUserRepository> { UserRepository(get()) } single<IUserRepository> { UserRepository(get()) }
single<IUserAuthRepository> { UserAuthRepository(get()) } single<IUserAuthRepository> { UserAuthRepository(get()) }
single<IUserAuthService> { UserAuthService(get(), get()) } single<IUserAuthService> { UserAuthService(get(), get()) }
single<UserService> { UserService(get()) } single<UserService> { UserService(get()) }
single<WebFingerService> { WebFingerService(get(),get(),get(),get()) }
single<ActivityPubUserService> { ActivityPubUserService(get(), get(),get(),get()) }
single<ActivityPubService> { ActivityPubService() } single<ActivityPubService> { ActivityPubService() }
single<HttpClient> { HttpClient(CIO) } single<ActivityPubUserService> { ActivityPubUserService(get(), get(),get(),get()) }
single<IWebFingerService> { WebFingerService(get(),get()) }
} }
configureKoin(module) configureKoin(module)
val configData by inject<ConfigData>() val configData by inject<ConfigData>()
@ -70,5 +70,5 @@ fun Application.module() {
register(userAuthService) register(userAuthService)
wellKnown(userService) wellKnown(userService)
val activityPubService by inject<ActivityPubService>() val activityPubService by inject<ActivityPubService>()
userActivityPubRouting(activityPubService) userActivityPubRouting(activityPubService, activityPubUserService)
} }

View File

@ -12,7 +12,7 @@ class ActivityPubUserService(
private val httpClient: HttpClient, private val httpClient: HttpClient,
private val userService: UserService, private val userService: UserService,
private val userAuthService: IUserAuthService, private val userAuthService: IUserAuthService,
private val webFingerService: WebFingerService private val webFingerService: IWebFingerService
) { ) {
suspend fun generateUserModel(name: String): Person { suspend fun generateUserModel(name: String): Person {
val userEntity = userService.findByName(name) val userEntity = userService.findByName(name)
@ -43,20 +43,9 @@ class ActivityPubUserService(
) )
} }
suspend fun fetchUserModel(url: String): Person? {
return try {
httpClient.get(url).body<Person>()
} catch (e: ResponseException) {
if (e.response.status == HttpStatusCode.NotFound) {
return null
}
throw e
}
}
suspend fun receiveFollow(follow: Follow) { suspend fun receiveFollow(follow: Follow) {
val actor = follow.actor ?: throw IllegalArgumentException("actor is null") 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") val inboxUrl = person.inbox ?: throw IllegalArgumentException("inbox is not found")
httpClient.post(inboxUrl) { httpClient.post(inboxUrl) {
setBody(Accept( setBody(Accept(

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.service package dev.usbharu.hideout.service
import dev.usbharu.hideout.ap.Person
import dev.usbharu.hideout.domain.model.UserEntity import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.webfinger.WebFinger import dev.usbharu.hideout.webfinger.WebFinger
@ -12,4 +13,6 @@ interface IWebFingerService {
val webFinger = fetch(acct)?: throw IllegalArgumentException() val webFinger = fetch(acct)?: throw IllegalArgumentException()
return sync(webFinger) return sync(webFinger)
} }
suspend fun fetchUserModel(actor: String): Person?
} }

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.service package dev.usbharu.hideout.service
import dev.usbharu.hideout.ap.Person
import dev.usbharu.hideout.domain.model.User import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserEntity import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.util.HttpUtil import dev.usbharu.hideout.util.HttpUtil
@ -12,9 +13,7 @@ import io.ktor.http.*
class WebFingerService( class WebFingerService(
private val httpClient: HttpClient, private val httpClient: HttpClient,
private val userService: UserService, private val userService: UserService
private val userAuthService: IUserAuthService,
private val activityPubUserService: ActivityPubUserService
) : IWebFingerService { ) : IWebFingerService {
override suspend fun fetch(acct: String): WebFinger? { 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<Person>()
} catch (e: ResponseException) {
if (e.response.status == HttpStatusCode.NotFound) {
return null
}
throw e
}
}
override suspend fun sync(webFinger: WebFinger): UserEntity { override suspend fun sync(webFinger: WebFinger): UserEntity {
val link = webFinger.links.find { val link = webFinger.links.find {
@ -46,7 +56,7 @@ class WebFingerService(
val domain = fullName.substringAfterLast("@") val domain = fullName.substringAfterLast("@")
val userName = fullName.substringBeforeLast("@") val userName = fullName.substringBeforeLast("@")
val userModel = activityPubUserService.fetchUserModel(link) ?: throw Exception() val userModel = fetchUserModel(link) ?: throw Exception()
val user = User( val user = User(
userModel.preferredUsername ?: throw IllegalStateException(), userModel.preferredUsername ?: throw IllegalStateException(),