feat: ActivityPubの判定をゆるく

This commit is contained in:
usbharu 2023-04-08 16:28:26 +09:00
parent 527750b3a6
commit 2f15b0e3e4
3 changed files with 15 additions and 9 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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
}