feat: 既存のgetAPの箇所を apResourceResolveServiceに切り替え

This commit is contained in:
usbharu 2023-10-12 16:35:04 +09:00
parent 5b65630556
commit 3e7d05a128
4 changed files with 20 additions and 26 deletions

View File

@ -12,19 +12,19 @@ import dev.usbharu.hideout.domain.model.job.DeliverPostJob
import dev.usbharu.hideout.exception.FailedToGetResourcesException import dev.usbharu.hideout.exception.FailedToGetResourcesException
import dev.usbharu.hideout.exception.ap.FailedToGetActivityPubResourceException import dev.usbharu.hideout.exception.ap.FailedToGetActivityPubResourceException
import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException
import dev.usbharu.hideout.plugins.getAp
import dev.usbharu.hideout.plugins.postAp import dev.usbharu.hideout.plugins.postAp
import dev.usbharu.hideout.query.FollowerQueryService import dev.usbharu.hideout.query.FollowerQueryService
import dev.usbharu.hideout.query.MediaQueryService import dev.usbharu.hideout.query.MediaQueryService
import dev.usbharu.hideout.query.PostQueryService import dev.usbharu.hideout.query.PostQueryService
import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.repository.PostRepository
import dev.usbharu.hideout.service.ap.resource.APResourceResolveService
import dev.usbharu.hideout.service.ap.resource.resolve
import dev.usbharu.hideout.service.job.JobQueueParentService import dev.usbharu.hideout.service.job.JobQueueParentService
import dev.usbharu.hideout.service.post.PostCreateInterceptor import dev.usbharu.hideout.service.post.PostCreateInterceptor
import dev.usbharu.hideout.service.post.PostService import dev.usbharu.hideout.service.post.PostService
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.plugins.* import io.ktor.client.plugins.*
import io.ktor.client.statement.*
import kjob.core.job.JobProps import kjob.core.job.JobProps
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred import kotlinx.coroutines.Deferred
@ -68,7 +68,8 @@ class APNoteServiceImpl(
private val mediaQueryService: MediaQueryService, private val mediaQueryService: MediaQueryService,
@Qualifier("activitypub") private val objectMapper: ObjectMapper, @Qualifier("activitypub") private val objectMapper: ObjectMapper,
private val applicationConfig: ApplicationConfig, private val applicationConfig: ApplicationConfig,
private val postService: PostService private val postService: PostService,
private val apResourceResolveService: APResourceResolveService
) : APNoteService, PostCreateInterceptor { ) : APNoteService, PostCreateInterceptor {
@ -143,11 +144,8 @@ class APNoteServiceImpl(
} }
logger.info("AP GET url: {}", url) logger.info("AP GET url: {}", url)
val response = try { val note = try {
httpClient.getAp( apResourceResolveService.resolve<Note>(url, null as Long?)
url,
targetActor?.let { "$targetActor#pubkey" }
)
} catch (e: ClientRequestException) { } catch (e: ClientRequestException) {
logger.warn( logger.warn(
"FAILED Failed to retrieve ActivityPub resource. HTTP Status Code: {} url: {}", "FAILED Failed to retrieve ActivityPub resource. HTTP Status Code: {} url: {}",
@ -156,7 +154,6 @@ class APNoteServiceImpl(
) )
throw FailedToGetActivityPubResourceException("Could not retrieve $url.", e) throw FailedToGetActivityPubResourceException("Could not retrieve $url.", e)
} }
val note = objectMapper.readValue<Note>(response.bodyAsText())
val savedNote = saveIfMissing(note, targetActor, url) val savedNote = saveIfMissing(note, targetActor, url)
logger.debug("SUCCESS Fetch Note url: {}", url) logger.debug("SUCCESS Fetch Note url: {}", url)
return savedNote return savedNote

View File

@ -1,7 +1,6 @@
package dev.usbharu.hideout.service.ap package dev.usbharu.hideout.service.ap
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.config.ApplicationConfig import dev.usbharu.hideout.config.ApplicationConfig
import dev.usbharu.hideout.domain.model.ap.Image import dev.usbharu.hideout.domain.model.ap.Image
import dev.usbharu.hideout.domain.model.ap.Key import dev.usbharu.hideout.domain.model.ap.Key
@ -10,15 +9,12 @@ import dev.usbharu.hideout.domain.model.hideout.dto.RemoteUserCreateDto
import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.exception.FailedToGetResourcesException import dev.usbharu.hideout.exception.FailedToGetResourcesException
import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException
import dev.usbharu.hideout.plugins.getAp
import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.service.ap.resource.APResourceResolveService
import dev.usbharu.hideout.service.ap.resource.resolve
import dev.usbharu.hideout.service.core.Transaction import dev.usbharu.hideout.service.core.Transaction
import dev.usbharu.hideout.service.user.UserService import dev.usbharu.hideout.service.user.UserService
import dev.usbharu.hideout.util.HttpUtil.Activity
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import org.springframework.beans.factory.annotation.Qualifier import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@ -44,6 +40,7 @@ class APUserServiceImpl(
private val userQueryService: UserQueryService, private val userQueryService: UserQueryService,
private val transaction: Transaction, private val transaction: Transaction,
private val applicationConfig: ApplicationConfig, private val applicationConfig: ApplicationConfig,
private val apResourceResolveService: APResourceResolveService,
@Qualifier("activitypub") private val objectMapper: ObjectMapper @Qualifier("activitypub") private val objectMapper: ObjectMapper
) : ) :
APUserService { APUserService {
@ -111,14 +108,8 @@ class APUserServiceImpl(
endpoints = mapOf("sharedInbox" to "${applicationConfig.url}/inbox") endpoints = mapOf("sharedInbox" to "${applicationConfig.url}/inbox")
) to userEntity ) to userEntity
} catch (ignore: FailedToGetResourcesException) { } catch (ignore: FailedToGetResourcesException) {
val httpResponse = if (targetActor != null) {
httpClient.getAp(url, "$targetActor#pubkey") val person = apResourceResolveService.resolve<Person>(url, null as Long?)
} else {
httpClient.get(url) {
accept(ContentType.Application.Activity)
}
}
val person = objectMapper.readValue<Person>(httpResponse.bodyAsText())
person to userService.createRemoteUser( person to userService.createRemoteUser(
RemoteUserCreateDto( RemoteUserCreateDto(

View File

@ -4,9 +4,11 @@ import com.fasterxml.jackson.databind.ObjectMapper
import dev.usbharu.hideout.domain.model.ap.Object import dev.usbharu.hideout.domain.model.ap.Object
import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.repository.UserRepository
import dev.usbharu.hideout.util.HttpUtil.Activity
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.statement.* import io.ktor.client.statement.*
import io.ktor.http.*
import org.springframework.beans.factory.annotation.Qualifier import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@ -46,7 +48,9 @@ class APResourceResolveServiceImpl(
} }
private suspend fun <T : Object> runResolve(url: String, singer: User?, clazz: Class<T>): Object { private suspend fun <T : Object> runResolve(url: String, singer: User?, clazz: Class<T>): Object {
val bodyAsText = httpClient.get(url).bodyAsText() val bodyAsText = httpClient.get(url) {
header("Accept", ContentType.Application.Activity)
}.bodyAsText()
return objectMapper.readValue(bodyAsText, clazz) return objectMapper.readValue(bodyAsText, clazz)
} }

View File

@ -96,7 +96,8 @@ class APNoteServiceImplTest {
objectMapper = objectMapper, objectMapper = objectMapper,
applicationConfig = testApplicationConfig, applicationConfig = testApplicationConfig,
postService = mock(), postService = mock(),
mediaQueryService = mediaQueryService mediaQueryService = mediaQueryService,
apResourceResolveService = mock()
) )
val postEntity = Post.of( val postEntity = Post.of(
1L, 1L,
@ -136,7 +137,8 @@ class APNoteServiceImplTest {
objectMapper = objectMapper, objectMapper = objectMapper,
applicationConfig = testApplicationConfig, applicationConfig = testApplicationConfig,
postService = mock(), postService = mock(),
mediaQueryService = mediaQueryService mediaQueryService = mediaQueryService,
apResourceResolveService = mock()
) )
activityPubNoteService.createNoteJob( activityPubNoteService.createNoteJob(
JobProps( JobProps(