From e269e3ac698b33b4559f506ec3198d0ff74153d4 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 7 Apr 2023 18:26:58 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20ActivityPub=E9=96=A2=E4=BF=82=E3=81=AE?= =?UTF-8?q?=E8=BF=94=E3=82=8A=E5=80=A4=E3=82=92JsonLD=E3=81=AE=E3=82=AA?= =?UTF-8?q?=E3=83=96=E3=82=B8=E3=82=A7=E3=82=AF=E3=83=88=E3=81=A7=E3=82=82?= =?UTF-8?q?=E8=BF=94=E3=81=9B=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/usbharu/hideout/Application.kt | 8 +++++++- .../domain/model/ActivityPubResponse.kt | 5 ----- .../domain/model/ActivityPubStringResponse.kt | 11 +++++++++++ .../dev/usbharu/hideout/plugins/Routing.kt | 6 ++++-- .../routing/activitypub/InboxRouting.kt | 18 +++++++++++++----- .../hideout/routing/activitypub/UserRouting.kt | 1 + .../service/activitypub/ActivityPubService.kt | 2 +- 7 files changed, 37 insertions(+), 14 deletions(-) delete mode 100644 src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubResponse.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubStringResponse.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/Application.kt b/src/main/kotlin/dev/usbharu/hideout/Application.kt index 9be1bf1d..3770dd5d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/Application.kt +++ b/src/main/kotlin/dev/usbharu/hideout/Application.kt @@ -9,6 +9,7 @@ import dev.usbharu.hideout.service.IUserAuthService import dev.usbharu.hideout.service.activitypub.ActivityPubService import dev.usbharu.hideout.service.activitypub.ActivityPubServiceImpl import dev.usbharu.hideout.service.impl.UserAuthService +import dev.usbharu.hideout.service.impl.UserService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyServiceImpl import io.ktor.server.application.* @@ -40,6 +41,7 @@ fun Application.module() { single { UserAuthService(get(), get()) } single { HttpSignatureVerifyServiceImpl(get()) } single { ActivityPubServiceImpl() } + single { UserService(get()) } } configureKoin(module) @@ -47,5 +49,9 @@ fun Application.module() { configureSockets() configureMonitoring() configureSerialization() - configureRouting(inject().value, inject().value) + configureRouting( + inject().value, + inject().value, + inject().value + ) } diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubResponse.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubResponse.kt deleted file mode 100644 index 8e487b7c..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubResponse.kt +++ /dev/null @@ -1,5 +0,0 @@ -package dev.usbharu.hideout.domain.model - -import io.ktor.http.* - -data class ActivityPubResponse(val httpStatusCode: HttpStatusCode, val message:String) diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubStringResponse.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubStringResponse.kt new file mode 100644 index 00000000..643502a6 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubStringResponse.kt @@ -0,0 +1,11 @@ +package dev.usbharu.hideout.domain.model + +import dev.usbharu.hideout.ap.JsonLd +import io.ktor.http.* + +sealed class ActivityPubResponse(val httpStatusCode: HttpStatusCode) +class ActivityPubStringResponse(httpStatusCode: HttpStatusCode, val message: String) : + ActivityPubResponse(httpStatusCode) + +class ActivityPubObjectResponse(httpStatusCode: HttpStatusCode, val message: JsonLd) : + ActivityPubResponse(httpStatusCode) diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt index dabc0984..bddeb896 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/Routing.kt @@ -5,6 +5,7 @@ import dev.usbharu.hideout.routing.activitypub.outbox import dev.usbharu.hideout.routing.activitypub.usersAP import dev.usbharu.hideout.routing.wellknown.webfinger import dev.usbharu.hideout.service.activitypub.ActivityPubService +import dev.usbharu.hideout.service.impl.UserService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import io.ktor.server.application.* import io.ktor.server.plugins.autohead.* @@ -12,13 +13,14 @@ import io.ktor.server.routing.* fun Application.configureRouting( httpSignatureVerifyService: HttpSignatureVerifyService, - activityPubService: ActivityPubService + activityPubService: ActivityPubService, + userService:UserService ) { install(AutoHeadResponse) routing { inbox(httpSignatureVerifyService, activityPubService) outbox() usersAP(activityPubService) - webfinger() + webfinger(userService) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRouting.kt index 079ddb98..a1e3b6a9 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRouting.kt @@ -1,5 +1,8 @@ package dev.usbharu.hideout.routing.activitypub +import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.domain.model.ActivityPubObjectResponse +import dev.usbharu.hideout.domain.model.ActivityPubStringResponse import dev.usbharu.hideout.exception.HttpSignatureVerifyException import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import io.ktor.http.* @@ -8,7 +11,10 @@ import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -fun Routing.inbox(httpSignatureVerifyService: HttpSignatureVerifyService,activityPubService: dev.usbharu.hideout.service.activitypub.ActivityPubService){ +fun Routing.inbox( + httpSignatureVerifyService: HttpSignatureVerifyService, + activityPubService: dev.usbharu.hideout.service.activitypub.ActivityPubService +){ route("/inbox") { get { @@ -21,10 +27,12 @@ fun Routing.inbox(httpSignatureVerifyService: HttpSignatureVerifyService,activit val json = call.receiveText() val activityTypes = activityPubService.parseActivity(json) val response = activityPubService.processActivity(json, activityTypes) - return@post if (response != null) { - call.respond(response.httpStatusCode, response.message) - }else { - call.respond(HttpStatusCode.InternalServerError) + when (response) { + is ActivityPubObjectResponse -> call.respond(response.httpStatusCode, Config.configData.objectMapper.writeValueAsString(response.message.apply { context = + listOf("https://www.w3.org/ns/activitystreams") + })) + is ActivityPubStringResponse -> call.respond(response.httpStatusCode, response.message) + null -> call.respond(HttpStatusCode.NotImplemented) } } } 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 f879a400..8848840a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt @@ -11,6 +11,7 @@ import io.ktor.server.routing.* fun Routing.usersAP(activityPubService: ActivityPubService){ route("/users/{name}"){ createChild(ContentTypeRouteSelector(ContentType.Application.Activity)).handle { + call.respond(HttpStatusCode.NotImplemented) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt index 7723ca1b..7558f2c3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt @@ -5,7 +5,7 @@ import dev.usbharu.hideout.domain.model.ActivityPubResponse interface ActivityPubService { fun parseActivity(json:String): ActivityType - fun processActivity(json:String, type: ActivityType):ActivityPubResponse? + fun processActivity(json:String, type: ActivityType): ActivityPubResponse? } enum class ActivityType {