diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt index cbb6cab4..87397704 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt @@ -44,6 +44,13 @@ suspend fun HttpClient.postAp(urlString: String, username: String, jsonLd: JsonL } } +suspend fun HttpClient.getAp(urlString: String,username: String):HttpResponse { + return this.get(urlString){ + header("Accept",ContentType.Application.Activity) + header("Signature","keyId=\"$username\",algorithm=\"#rsa-sha\",headers=\"(request-target) digest date\"") + } +} + class HttpSignaturePluginConfig { lateinit var keyMap: KeyMap } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt index fcd94662..988cb81e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt @@ -32,9 +32,9 @@ class ActivityPubFollowServiceImpl( override suspend fun receiveFollowJob(props: JobProps) { val actor = props[ReceiveFollowJob.actor] - val person = activityPubUserService.fetchPerson(actor) - val follow = Config.configData.objectMapper.readValue(props[ReceiveFollowJob.follow]) val targetActor = props[ReceiveFollowJob.targetActor] + val person = activityPubUserService.fetchPerson(actor,targetActor) + val follow = Config.configData.objectMapper.readValue(props[ReceiveFollowJob.follow]) httpClient.postAp( urlString = person.inbox ?: throw IllegalArgumentException("inbox is not found"), username = "$targetActor#pubkey", diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserService.kt index a698670b..3446da24 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserService.kt @@ -5,5 +5,5 @@ import dev.usbharu.hideout.domain.model.ap.Person interface ActivityPubUserService { suspend fun getPersonByName(name:String): Person - suspend fun fetchPerson(url:String): Person + suspend fun fetchPerson(url: String, targetActor: String? = null): Person } 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 e8444ba0..94c8557a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt @@ -8,6 +8,7 @@ import dev.usbharu.hideout.domain.model.ap.Person 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.service.impl.IUserService import dev.usbharu.hideout.util.HttpUtil.Activity import io.ktor.client.* @@ -52,7 +53,7 @@ class ActivityPubUserServiceImpl( ) } - override suspend fun fetchPerson(url: String): Person { + override suspend fun fetchPerson(url: String, targetActor: String?): Person { return try { val userEntity = userService.findByUrl(url) return Person( @@ -80,8 +81,12 @@ class ActivityPubUserServiceImpl( ) } catch (e: UserNotFoundException) { - val httpResponse = httpClient.get(url) { - accept(ContentType.Application.Activity) + val httpResponse = if (targetActor != null) { + httpClient.getAp(url,"$targetActor#pubkey") + }else { + httpClient.get(url) { + accept(ContentType.Application.Activity) + } } val person = Config.configData.objectMapper.readValue(httpResponse.bodyAsText()) diff --git a/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImplTest.kt index e37652dd..6a668e34 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImplTest.kt @@ -85,7 +85,7 @@ class ActivityPubFollowServiceImplTest { ) val activityPubUserService = mock { - onBlocking { fetchPerson(anyString()) } doReturn person + onBlocking { fetchPerson(anyString(), any()) } doReturn person } val userService = mock { onBlocking { findByUrls(any()) } doReturn listOf(