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.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<IUserAuthService> { UserAuthService(get(), get()) }
single<HttpSignatureVerifyService> { HttpSignatureVerifyServiceImpl(get()) }
single<ActivityPubService> { ActivityPubServiceImpl() }
single<UserService> { UserService(get()) }
}
configureKoin(module)
@ -47,5 +49,9 @@ fun Application.module() {
configureSockets()
configureMonitoring()
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.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)
}
}

View File

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

View File

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

View File

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