From 2f15b0e3e4c45137e39b5ccaf3f41f2a93f73f46 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 8 Apr 2023 16:28:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20ActivityPub=E3=81=AE=E5=88=A4=E5=AE=9A?= =?UTF-8?q?=E3=82=92=E3=82=86=E3=82=8B=E3=81=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usbharu/hideout/plugins/StatusPages.kt | 1 - .../routing/activitypub/UserRouting.kt | 20 +++++++++++-------- .../dev/usbharu/hideout/util/HttpUtil.kt | 3 +++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/StatusPages.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/StatusPages.kt index 990a7acb..cd078e28 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/StatusPages.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/StatusPages.kt @@ -4,7 +4,6 @@ import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.plugins.statuspages.* import io.ktor.server.response.* -import io.ktor.server.routing.* fun Application.configureStatusPages() { install(StatusPages) { diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt index 05ab3f2c..37ac3dc4 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt @@ -2,29 +2,33 @@ package dev.usbharu.hideout.routing.activitypub import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.exception.ParameterNotExistException -import dev.usbharu.hideout.service.activitypub.ActivityPubService import dev.usbharu.hideout.service.activitypub.ActivityPubUserService import dev.usbharu.hideout.util.HttpUtil.Activity +import dev.usbharu.hideout.util.HttpUtil.JsonLd import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -fun Routing.usersAP(activityPubUserService:ActivityPubUserService){ - route("/users/{name}"){ - createChild(ContentTypeRouteSelector(ContentType.Application.Activity)).handle { - val name = call.parameters["name"] ?: throw ParameterNotExistException("Parameter(name='name') does not exist.") +fun Routing.usersAP(activityPubUserService: ActivityPubUserService) { + route("/users/{name}") { + createChild(ContentTypeRouteSelector(ContentType.Application.Activity, ContentType.Application.JsonLd)).handle { + val name = + call.parameters["name"] ?: throw ParameterNotExistException("Parameter(name='name') does not exist.") val person = activityPubUserService.getPersonByName(name) call.response.header("Content-Type", ContentType.Application.Activity.toString()) - call.respond(HttpStatusCode.OK,Config.configData.objectMapper.writeValueAsString(person)) + call.respond(HttpStatusCode.OK, Config.configData.objectMapper.writeValueAsString(person)) } } } -class ContentTypeRouteSelector(private val contentType: ContentType) : RouteSelector() { +class ContentTypeRouteSelector(private vararg val contentType: ContentType) : RouteSelector() { override fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation { - return if (ContentType.parse(context.call.request.accept() ?: return RouteSelectorEvaluation.FailedParameter) == contentType) { + + val requestContentType = + ContentType.parse(context.call.request.accept() ?: return RouteSelectorEvaluation.FailedParameter) + return if (contentType.any { contentType -> contentType.match(requestContentType) }) { RouteSelectorEvaluation.Constant } else { RouteSelectorEvaluation.FailedParameter diff --git a/src/main/kotlin/dev/usbharu/hideout/util/HttpUtil.kt b/src/main/kotlin/dev/usbharu/hideout/util/HttpUtil.kt index 05c827d3..979b8302 100644 --- a/src/main/kotlin/dev/usbharu/hideout/util/HttpUtil.kt +++ b/src/main/kotlin/dev/usbharu/hideout/util/HttpUtil.kt @@ -28,5 +28,8 @@ object HttpUtil { val ContentType.Application.Activity: ContentType get() = ContentType("application", "activity+json") + + val ContentType.Application.JsonLd: ContentType + get() = ContentType("application", "ld+json", listOf(HeaderValueParam("profile", "https://www.w3.org/ns/activitystreams"))) // fun }