feat: 予めJsonに変換してからpostするように

This commit is contained in:
usbharu 2023-03-31 12:54:05 +09:00
parent 91f3edce3b
commit 53daf87ecf
2 changed files with 44 additions and 38 deletions

View File

@ -6,8 +6,10 @@ import dev.usbharu.hideout.repository.IUserAuthRepository
import dev.usbharu.hideout.service.IUserAuthService import dev.usbharu.hideout.service.IUserAuthService
import dev.usbharu.hideout.service.UserAuthService import dev.usbharu.hideout.service.UserAuthService
import dev.usbharu.hideout.util.HttpUtil.Activity import dev.usbharu.hideout.util.HttpUtil.Activity
import io.ktor.client.*
import io.ktor.client.plugins.api.* import io.ktor.client.plugins.api.*
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.* import io.ktor.http.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.response.* import io.ktor.server.response.*
@ -38,6 +40,16 @@ suspend fun <T : JsonLd> ApplicationCall.respondAp(message: T, status: HttpStatu
respondText(activityJson, ContentType.Application.Activity, status) 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 { class HttpSignaturePluginConfig {
lateinit var keyMap: KeyMap lateinit var keyMap: KeyMap
} }

View File

@ -2,45 +2,41 @@ package dev.usbharu.hideout.service
import dev.usbharu.hideout.ap.* import dev.usbharu.hideout.ap.*
import dev.usbharu.hideout.config.Config 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.*
import io.ktor.client.call.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.http.*
class ActivityPubUserService( class ActivityPubUserService(
private val httpClient: HttpClient, private val httpClient: HttpClient,
private val userService: UserService, private val userService: UserService,
private val userAuthService: IUserAuthService, private val userAuthService: IUserAuthService,
private val webFingerService: IWebFingerService private val webFingerService: IWebFingerService
) { ) {
suspend fun generateUserModel(name: String): Person { suspend fun generateUserModel(name: String): Person {
val userEntity = userService.findByName(name) val userEntity = userService.findByName(name)
val userAuthEntity = userAuthService.findByUserId(userEntity.id) val userAuthEntity = userAuthService.findByUserId(userEntity.id)
val userUrl = "${Config.configData.url}/users/$name" val userUrl = "${Config.configData.url}/users/$name"
return Person( 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(), type = emptyList(),
name = userEntity.name, name = "$userUrl/icon.png",
id = userUrl, mediaType = "image/png",
preferredUsername = name, url = "$userUrl/icon.png"
summary = userEntity.description, ),
inbox = "$userUrl/inbox", publicKey = Key(
outbox = "$userUrl/outbox", type = emptyList(),
url = userUrl, name = "Public Key",
icon = Image( id = "$userUrl/pubkey",
type = emptyList(), owner = userUrl,
name = "$userUrl/icon.png", publicKeyPem = userAuthEntity.publicKey
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 actor = follow.actor ?: throw IllegalArgumentException("actor is null")
val person = webFingerService.fetchUserModel(actor) ?: throw IllegalArgumentException("actor is not found") val person = webFingerService.fetchUserModel(actor) ?: throw IllegalArgumentException("actor is not found")
val inboxUrl = person.inbox ?: throw IllegalArgumentException("inbox is not found") val inboxUrl = person.inbox ?: throw IllegalArgumentException("inbox is not found")
httpClient.post(inboxUrl) { httpClient.postAp(
contentType(ContentType.Application.Activity) inboxUrl, person.preferredUsername!!, Accept(
header("Signature","keyId=\"${person.preferredUsername}\",algorithm=\"rsa-sha256\",headers=\"(request-target) digest date\"") name = "Follow",
setBody(Accept( `object` = follow,
name = "Follow", actor = follow.`object`.orEmpty()
`object` = follow, )
actor = follow.`object`.orEmpty() )
))
}
} }
} }