From 53daf87ecf50bbdd1b1527332c622ae94a605c6a Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 31 Mar 2023 12:54:05 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BA=88=E3=82=81Json=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=8F=9B=E3=81=97=E3=81=A6=E3=81=8B=E3=82=89post?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usbharu/hideout/plugins/ActivityPub.kt | 12 ++++ .../hideout/service/ActivityPubUserService.kt | 70 +++++++++---------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt index 0dfb5c30..e2b83d5e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt @@ -6,8 +6,10 @@ import dev.usbharu.hideout.repository.IUserAuthRepository import dev.usbharu.hideout.service.IUserAuthService import dev.usbharu.hideout.service.UserAuthService import dev.usbharu.hideout.util.HttpUtil.Activity +import io.ktor.client.* import io.ktor.client.plugins.api.* import io.ktor.client.request.* +import io.ktor.client.statement.* import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.response.* @@ -38,6 +40,16 @@ suspend fun ApplicationCall.respondAp(message: T, status: HttpStatu respondText(activityJson, ContentType.Application.Activity, status) } +suspend fun HttpClient.postAp(urlString: String,username:String,jsonLd: JsonLd): HttpResponse { + return this.post(urlString){ + header("Accept", ContentType.Application.Activity) + header("Content-Type", ContentType.Application.Activity) + header("Signature","keyId=\"$username\",algorithm=\"rsa-sha256\",headers=\"(request-target) digest date\"") + val text = Config.configData.objectMapper.writeValueAsString(jsonLd) + setBody(text) + } +} + class HttpSignaturePluginConfig { lateinit var keyMap: KeyMap } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt index 30af9e41..672f59aa 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt @@ -2,45 +2,41 @@ package dev.usbharu.hideout.service import dev.usbharu.hideout.ap.* import dev.usbharu.hideout.config.Config -import dev.usbharu.hideout.util.HttpUtil.Activity +import dev.usbharu.hideout.plugins.postAp import io.ktor.client.* -import io.ktor.client.call.* -import io.ktor.client.plugins.* -import io.ktor.client.request.* -import io.ktor.http.* class ActivityPubUserService( - private val httpClient: HttpClient, - private val userService: UserService, - private val userAuthService: IUserAuthService, - private val webFingerService: IWebFingerService + private val httpClient: HttpClient, + private val userService: UserService, + private val userAuthService: IUserAuthService, + private val webFingerService: IWebFingerService ) { suspend fun generateUserModel(name: String): Person { val userEntity = userService.findByName(name) val userAuthEntity = userAuthService.findByUserId(userEntity.id) val userUrl = "${Config.configData.url}/users/$name" return Person( + type = emptyList(), + name = userEntity.name, + id = userUrl, + preferredUsername = name, + summary = userEntity.description, + inbox = "$userUrl/inbox", + outbox = "$userUrl/outbox", + url = userUrl, + icon = Image( type = emptyList(), - name = userEntity.name, - id = userUrl, - preferredUsername = name, - summary = userEntity.description, - inbox = "$userUrl/inbox", - outbox = "$userUrl/outbox", - url = userUrl, - icon = Image( - type = emptyList(), - name = "$userUrl/icon.png", - mediaType = "image/png", - url = "$userUrl/icon.png" - ), - publicKey = Key( - type = emptyList(), - name = "Public Key", - id = "$userUrl/pubkey", - owner = userUrl, - publicKeyPem = userAuthEntity.publicKey - ) + name = "$userUrl/icon.png", + mediaType = "image/png", + url = "$userUrl/icon.png" + ), + publicKey = Key( + type = emptyList(), + name = "Public Key", + id = "$userUrl/pubkey", + owner = userUrl, + publicKeyPem = userAuthEntity.publicKey + ) ) } @@ -48,14 +44,12 @@ class ActivityPubUserService( val actor = follow.actor ?: throw IllegalArgumentException("actor is null") val person = webFingerService.fetchUserModel(actor) ?: throw IllegalArgumentException("actor is not found") val inboxUrl = person.inbox ?: throw IllegalArgumentException("inbox is not found") - httpClient.post(inboxUrl) { - contentType(ContentType.Application.Activity) - header("Signature","keyId=\"${person.preferredUsername}\",algorithm=\"rsa-sha256\",headers=\"(request-target) digest date\"") - setBody(Accept( - name = "Follow", - `object` = follow, - actor = follow.`object`.orEmpty() - )) - } + httpClient.postAp( + inboxUrl, person.preferredUsername!!, Accept( + name = "Follow", + `object` = follow, + actor = follow.`object`.orEmpty() + ) + ) } }