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 }