diff --git a/src/main/kotlin/dev/usbharu/hideout/Application.kt b/src/main/kotlin/dev/usbharu/hideout/Application.kt index 1ccccca8..0d19054d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/Application.kt +++ b/src/main/kotlin/dev/usbharu/hideout/Application.kt @@ -12,6 +12,8 @@ import dev.usbharu.hideout.domain.model.job.DeliverReactionJob import dev.usbharu.hideout.domain.model.job.DeliverRemoveReactionJob import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob import dev.usbharu.hideout.plugins.* +import dev.usbharu.hideout.query.FollowerQueryService +import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.routing.register import dev.usbharu.hideout.service.activitypub.ActivityPubService @@ -120,7 +122,8 @@ fun Application.parent() { userAuthService = inject().value, userRepository = inject().value, jwtService = inject().value, - metaService = inject().value + userQueryService = inject().value, + followerQueryService = inject().value ) } diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt index 711d9c9a..59d39369 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt @@ -1,5 +1,7 @@ package dev.usbharu.hideout.plugins +import dev.usbharu.hideout.query.FollowerQueryService +import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.routing.activitypub.inbox import dev.usbharu.hideout.routing.activitypub.outbox @@ -14,7 +16,6 @@ import dev.usbharu.hideout.service.api.IPostApiService import dev.usbharu.hideout.service.api.IUserApiService import dev.usbharu.hideout.service.auth.HttpSignatureVerifyService import dev.usbharu.hideout.service.auth.IJwtService -import dev.usbharu.hideout.service.core.IMetaService import dev.usbharu.hideout.service.reaction.IReactionService import dev.usbharu.hideout.service.user.IUserAuthService import dev.usbharu.hideout.service.user.IUserService @@ -34,14 +35,15 @@ fun Application.configureRouting( userAuthService: IUserAuthService, userRepository: IUserRepository, jwtService: IJwtService, - metaService: IMetaService + userQueryService: UserQueryService, + followerQueryService: FollowerQueryService ) { install(AutoHeadResponse) routing { inbox(httpSignatureVerifyService, activityPubService) outbox() - usersAP(activityPubUserService, userService) - webfinger(userService) + usersAP(activityPubUserService, userQueryService, followerQueryService) + webfinger(userQueryService) route("/api/internal/v1") { posts(postService, reactionService) users(userService, userApiService) diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt index deca6e5e..df0d6d37 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt @@ -1,9 +1,11 @@ package dev.usbharu.hideout.routing.activitypub +import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.exception.ParameterNotExistException import dev.usbharu.hideout.plugins.respondAp +import dev.usbharu.hideout.query.FollowerQueryService +import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.activitypub.ActivityPubUserService -import dev.usbharu.hideout.service.user.IUserService import dev.usbharu.hideout.util.HttpUtil.Activity import dev.usbharu.hideout.util.HttpUtil.JsonLd import io.ktor.http.* @@ -12,7 +14,11 @@ import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -fun Routing.usersAP(activityPubUserService: ActivityPubUserService, userService: IUserService) { +fun Routing.usersAP( + activityPubUserService: ActivityPubUserService, + userQueryService: UserQueryService, + followerQueryService: FollowerQueryService +) { route("/users/{name}") { createChild(ContentTypeRouteSelector(ContentType.Application.Activity, ContentType.Application.JsonLd)).handle { call.application.log.debug("Signature: ${call.request.header("Signature")}") @@ -26,10 +32,11 @@ fun Routing.usersAP(activityPubUserService: ActivityPubUserService, userService: ) } get { - val userEntity = userService.findByNameLocalUser( - call.parameters["name"] ?: throw ParameterNotExistException("Parameter(name='name') does not exist.") + val userEntity = userQueryService.findByNameAndDomain( + call.parameters["name"] ?: throw ParameterNotExistException("Parameter(name='name') does not exist."), + Config.configData.domain ) - call.respondText(userEntity.toString() + "\n" + userService.findFollowersById(userEntity.id)) + call.respondText(userEntity.toString() + "\n" + followerQueryService.findFollowersById(userEntity.id)) } } } diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/wellknown/WebfingerRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/wellknown/WebfingerRouting.kt index 9106d6bd..38e342b3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/wellknown/WebfingerRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/wellknown/WebfingerRouting.kt @@ -4,14 +4,14 @@ import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.domain.model.wellknown.WebFinger import dev.usbharu.hideout.exception.IllegalParameterException import dev.usbharu.hideout.exception.ParameterNotExistException -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.util.HttpUtil.Activity import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.response.* import io.ktor.server.routing.* -fun Routing.webfinger(userService: IUserService) { +fun Routing.webfinger(userQueryService: UserQueryService) { route("/.well-known/webfinger") { get { val acct = call.request.queryParameters["resource"]?.decodeURLPart() @@ -25,7 +25,7 @@ fun Routing.webfinger(userService: IUserService) { .substringAfter("acct:") .trimStart('@') - val userEntity = userService.findByNameLocalUser(accountName) + val userEntity = userQueryService.findByNameAndDomain(accountName, Config.configData.domain) val webFinger = WebFinger( subject = acct, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt index 5df6cb1c..3a0cc4b7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt @@ -9,6 +9,7 @@ import dev.usbharu.hideout.domain.model.hideout.dto.RemoteUserCreateDto import dev.usbharu.hideout.exception.UserNotFoundException import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException import dev.usbharu.hideout.plugins.getAp +import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.user.IUserService import dev.usbharu.hideout.util.HttpUtil.Activity import io.ktor.client.* @@ -20,13 +21,14 @@ import org.koin.core.annotation.Single @Single class ActivityPubUserServiceImpl( private val userService: IUserService, - private val httpClient: HttpClient + private val httpClient: HttpClient, + private val userQueryService: UserQueryService ) : ActivityPubUserService { override suspend fun getPersonByName(name: String): Person { // TODO: JOINで書き直し - val userEntity = userService.findByNameLocalUser(name) + val userEntity = userQueryService.findByNameAndDomain(name, Config.configData.domain) val userUrl = "${Config.configData.url}/users/$name" return Person( type = emptyList(), @@ -55,7 +57,7 @@ class ActivityPubUserServiceImpl( override suspend fun fetchPerson(url: String, targetActor: String?): Person { return try { - val userEntity = userService.findByUrl(url) + val userEntity = userQueryService.findByUrl(url) return Person( type = emptyList(), name = userEntity.name, diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt index b0eb28dd..35c8ca50 100644 --- a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt @@ -11,8 +11,8 @@ import dev.usbharu.hideout.domain.model.ap.Key import dev.usbharu.hideout.domain.model.ap.Person import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.plugins.configureSerialization +import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.activitypub.ActivityPubUserService -import dev.usbharu.hideout.service.user.IUserService import dev.usbharu.hideout.util.HttpUtil.Activity import dev.usbharu.hideout.util.HttpUtil.JsonLd import io.ktor.client.request.* @@ -62,8 +62,6 @@ class UsersAPTest { ) person.context = listOf("https://www.w3.org/ns/activitystreams") - val userService = mock {} - val activityPubUserService = mock { onBlocking { getPersonByName(anyString()) } doReturn person } @@ -71,7 +69,7 @@ class UsersAPTest { application { configureSerialization() routing { - usersAP(activityPubUserService, userService) + usersAP(activityPubUserService, mock(), mock()) } } client.get("/users/test") { @@ -122,8 +120,6 @@ class UsersAPTest { ) person.context = listOf("https://www.w3.org/ns/activitystreams") - val userService = mock {} - val activityPubUserService = mock { onBlocking { getPersonByName(anyString()) } doReturn person } @@ -131,7 +127,7 @@ class UsersAPTest { application { configureSerialization() routing { - usersAP(activityPubUserService, userService) + usersAP(activityPubUserService, mock(), mock()) } } client.get("/users/test") { @@ -174,8 +170,8 @@ class UsersAPTest { environment { config = ApplicationConfig("empty.conf") } - val userService = mock { - onBlocking { findByNameLocalUser(eq("test")) } doReturn User( + val userService = mock { + onBlocking { findByNameAndDomain(eq("test"), anyString()) } doReturn User( 1L, "test", "example.com", @@ -192,7 +188,7 @@ class UsersAPTest { } application { routing { - usersAP(mock(), userService) + usersAP(mock(), userService, mock()) } } client.get("/users/test") {