feat: ActivityPub関係の返り値をJsonLDのオブジェクトでも返せるように

This commit is contained in:
usbharu 2023-04-07 18:26:58 +09:00
parent efdbacf817
commit e269e3ac69
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
7 changed files with 37 additions and 14 deletions

View File

@ -9,6 +9,7 @@ import dev.usbharu.hideout.service.IUserAuthService
import dev.usbharu.hideout.service.activitypub.ActivityPubService import dev.usbharu.hideout.service.activitypub.ActivityPubService
import dev.usbharu.hideout.service.activitypub.ActivityPubServiceImpl import dev.usbharu.hideout.service.activitypub.ActivityPubServiceImpl
import dev.usbharu.hideout.service.impl.UserAuthService 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.HttpSignatureVerifyService
import dev.usbharu.hideout.service.signature.HttpSignatureVerifyServiceImpl import dev.usbharu.hideout.service.signature.HttpSignatureVerifyServiceImpl
import io.ktor.server.application.* import io.ktor.server.application.*
@ -40,6 +41,7 @@ fun Application.module() {
single<IUserAuthService> { UserAuthService(get(), get()) } single<IUserAuthService> { UserAuthService(get(), get()) }
single<HttpSignatureVerifyService> { HttpSignatureVerifyServiceImpl(get()) } single<HttpSignatureVerifyService> { HttpSignatureVerifyServiceImpl(get()) }
single<ActivityPubService> { ActivityPubServiceImpl() } single<ActivityPubService> { ActivityPubServiceImpl() }
single<UserService> { UserService(get()) }
} }
configureKoin(module) configureKoin(module)
@ -47,5 +49,9 @@ fun Application.module() {
configureSockets() configureSockets()
configureMonitoring() configureMonitoring()
configureSerialization() configureSerialization()
configureRouting(inject<HttpSignatureVerifyService>().value, inject<ActivityPubService>().value) configureRouting(
inject<HttpSignatureVerifyService>().value,
inject<ActivityPubService>().value,
inject<UserService>().value
)
} }

View File

@ -1,5 +0,0 @@
package dev.usbharu.hideout.domain.model
import io.ktor.http.*
data class ActivityPubResponse(val httpStatusCode: HttpStatusCode, val message:String)

View File

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

View File

@ -5,6 +5,7 @@ import dev.usbharu.hideout.routing.activitypub.outbox
import dev.usbharu.hideout.routing.activitypub.usersAP import dev.usbharu.hideout.routing.activitypub.usersAP
import dev.usbharu.hideout.routing.wellknown.webfinger import dev.usbharu.hideout.routing.wellknown.webfinger
import dev.usbharu.hideout.service.activitypub.ActivityPubService import dev.usbharu.hideout.service.activitypub.ActivityPubService
import dev.usbharu.hideout.service.impl.UserService
import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.plugins.autohead.* import io.ktor.server.plugins.autohead.*
@ -12,13 +13,14 @@ import io.ktor.server.routing.*
fun Application.configureRouting( fun Application.configureRouting(
httpSignatureVerifyService: HttpSignatureVerifyService, httpSignatureVerifyService: HttpSignatureVerifyService,
activityPubService: ActivityPubService activityPubService: ActivityPubService,
userService:UserService
) { ) {
install(AutoHeadResponse) install(AutoHeadResponse)
routing { routing {
inbox(httpSignatureVerifyService, activityPubService) inbox(httpSignatureVerifyService, activityPubService)
outbox() outbox()
usersAP(activityPubService) usersAP(activityPubService)
webfinger() webfinger(userService)
} }
} }

View File

@ -1,5 +1,8 @@
package dev.usbharu.hideout.routing.activitypub 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.exception.HttpSignatureVerifyException
import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService
import io.ktor.http.* import io.ktor.http.*
@ -8,7 +11,10 @@ import io.ktor.server.request.*
import io.ktor.server.response.* import io.ktor.server.response.*
import io.ktor.server.routing.* 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") { route("/inbox") {
get { get {
@ -21,10 +27,12 @@ fun Routing.inbox(httpSignatureVerifyService: HttpSignatureVerifyService,activit
val json = call.receiveText() val json = call.receiveText()
val activityTypes = activityPubService.parseActivity(json) val activityTypes = activityPubService.parseActivity(json)
val response = activityPubService.processActivity(json, activityTypes) val response = activityPubService.processActivity(json, activityTypes)
return@post if (response != null) { when (response) {
call.respond(response.httpStatusCode, response.message) is ActivityPubObjectResponse -> call.respond(response.httpStatusCode, Config.configData.objectMapper.writeValueAsString(response.message.apply { context =
}else { listOf("https://www.w3.org/ns/activitystreams")
call.respond(HttpStatusCode.InternalServerError) }))
is ActivityPubStringResponse -> call.respond(response.httpStatusCode, response.message)
null -> call.respond(HttpStatusCode.NotImplemented)
} }
} }
} }

View File

@ -11,6 +11,7 @@ import io.ktor.server.routing.*
fun Routing.usersAP(activityPubService: ActivityPubService){ fun Routing.usersAP(activityPubService: ActivityPubService){
route("/users/{name}"){ route("/users/{name}"){
createChild(ContentTypeRouteSelector(ContentType.Application.Activity)).handle { createChild(ContentTypeRouteSelector(ContentType.Application.Activity)).handle {
call.respond(HttpStatusCode.NotImplemented) call.respond(HttpStatusCode.NotImplemented)
} }
} }

View File

@ -5,7 +5,7 @@ import dev.usbharu.hideout.domain.model.ActivityPubResponse
interface ActivityPubService { interface ActivityPubService {
fun parseActivity(json:String): ActivityType fun parseActivity(json:String): ActivityType
fun processActivity(json:String, type: ActivityType):ActivityPubResponse? fun processActivity(json:String, type: ActivityType): ActivityPubResponse?
} }
enum class ActivityType { enum class ActivityType {