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() + ) + ) } }