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 {