fix: リフレクションを利用せずジョブの種類を分けれられるように

This commit is contained in:
usbharu 2023-04-10 02:29:43 +09:00
parent a5039af630
commit d6316ae107
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
10 changed files with 66 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.config.ConfigData import dev.usbharu.hideout.config.ConfigData
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import dev.usbharu.hideout.plugins.* import dev.usbharu.hideout.plugins.*
import dev.usbharu.hideout.repository.IUserAuthRepository import dev.usbharu.hideout.repository.IUserAuthRepository
import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.repository.IUserRepository
@ -19,10 +20,17 @@ import dev.usbharu.hideout.service.job.JobQueueParentService
import dev.usbharu.hideout.service.job.KJobJobQueueParentService import dev.usbharu.hideout.service.job.KJobJobQueueParentService
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 dev.usbharu.kjob.exposed.ExposedKJob
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.engine.cio.* import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.logging.* import io.ktor.client.plugins.logging.*
import io.ktor.server.application.* import io.ktor.server.application.*
import kjob.core.Job
import kjob.core.KJob
import kjob.core.dsl.JobContextWithProps
import kjob.core.dsl.JobRegisterContext
import kjob.core.dsl.KJobFunctions
import kjob.core.kjob
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.koin.ktor.ext.inject import org.koin.ktor.ext.inject
@ -34,7 +42,7 @@ val Application.property: Application.(propertyName: String) -> String
} }
@Suppress("unused") // application.conf references the main function. This annotation prevents the IDE from marking it as unused. @Suppress("unused") // application.conf references the main function. This annotation prevents the IDE from marking it as unused.
fun Application.module() { fun Application.parent() {
Config.configData = ConfigData( Config.configData = ConfigData(
url = property("hideout.url"), url = property("hideout.url"),
@ -73,7 +81,7 @@ fun Application.module() {
single<ActivityPubFollowService> { ActivityPubFollowServiceImpl(get(), get(), get()) } single<ActivityPubFollowService> { ActivityPubFollowServiceImpl(get(), get(), get()) }
single<ActivityPubService> { ActivityPubServiceImpl(get()) } single<ActivityPubService> { ActivityPubServiceImpl(get()) }
single<UserService> { UserService(get()) } single<UserService> { UserService(get()) }
single<ActivityPubUserService> { ActivityPubUserServiceImpl(get(), get(),get()) } single<ActivityPubUserService> { ActivityPubUserServiceImpl(get(), get(), get()) }
} }
@ -90,3 +98,17 @@ fun Application.module() {
inject<ActivityPubUserService>().value inject<ActivityPubUserService>().value
) )
} }
@Suppress("unused")
fun Application.worker() {
val kJob = kjob(ExposedKJob) {
connectionDatabase = inject<Database>().value
}.start()
val activityPubService = inject<ActivityPubService>().value
kJob.register(ReceiveFollowJob){
execute {
activityPubService.processActivity(this,it)
}
}
}

View File

@ -2,7 +2,9 @@ package dev.usbharu.hideout.domain.model.job
import kjob.core.Job import kjob.core.Job
object ReceiveFollowJob : Job("ReceiveFollowJob"){ sealed class HideoutJob(name: String = "") : Job(name)
object ReceiveFollowJob : HideoutJob("ReceiveFollowJob"){
val actor = string("actor") val actor = string("actor")
val follow = string("follow") val follow = string("follow")
val targetActor = string("targetActor") val targetActor = string("targetActor")

View File

@ -46,6 +46,7 @@ fun Routing.inbox(
} }
val json = call.receiveText() val json = call.receiveText()
val activityTypes = activityPubService.parseActivity(json) val activityTypes = activityPubService.parseActivity(json)
println(activityTypes)
val response = activityPubService.processActivity(json, activityTypes) val response = activityPubService.processActivity(json, activityTypes)
when (response) { when (response) {
is ActivityPubObjectResponse -> call.respond(response.httpStatusCode, Config.configData.objectMapper.writeValueAsString(response.message.apply { context = is ActivityPubObjectResponse -> call.respond(response.httpStatusCode, Config.configData.objectMapper.writeValueAsString(response.message.apply { context =

View File

@ -2,7 +2,10 @@ package dev.usbharu.hideout.service.activitypub
import dev.usbharu.hideout.ap.Follow import dev.usbharu.hideout.ap.Follow
import dev.usbharu.hideout.domain.model.ActivityPubResponse import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import kjob.core.job.JobProps
interface ActivityPubFollowService { interface ActivityPubFollowService {
suspend fun receiveFollow(follow:Follow):ActivityPubResponse suspend fun receiveFollow(follow:Follow):ActivityPubResponse
suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>)
} }

View File

@ -27,7 +27,7 @@ class ActivityPubFollowServiceImpl(
return ActivityPubStringResponse(HttpStatusCode.OK, "{}", ContentType.Application.Json) return ActivityPubStringResponse(HttpStatusCode.OK, "{}", ContentType.Application.Json)
} }
suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>) { override suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>) {
val actor = props[ReceiveFollowJob.actor] val actor = props[ReceiveFollowJob.actor]
val person = activityPubUserService.fetchPerson(actor) val person = activityPubUserService.fetchPerson(actor)
val follow = Config.configData.objectMapper.readValue<Follow>(props[ReceiveFollowJob.follow]) val follow = Config.configData.objectMapper.readValue<Follow>(props[ReceiveFollowJob.follow])

View File

@ -1,11 +1,15 @@
package dev.usbharu.hideout.service.activitypub package dev.usbharu.hideout.service.activitypub
import dev.usbharu.hideout.domain.model.ActivityPubResponse import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.job.HideoutJob
import kjob.core.dsl.JobContextWithProps
interface ActivityPubService { interface ActivityPubService {
fun parseActivity(json:String): ActivityType fun parseActivity(json: String): ActivityType
suspend fun processActivity(json:String, type: ActivityType): ActivityPubResponse? suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse?
suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>,hideoutJob: HideoutJob)
} }
enum class ActivityType { enum class ActivityType {

View File

@ -4,7 +4,14 @@ import com.fasterxml.jackson.databind.JsonNode
import dev.usbharu.hideout.ap.Follow import dev.usbharu.hideout.ap.Follow
import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.ActivityPubResponse import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.job.HideoutJob
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import dev.usbharu.hideout.exception.JsonParseException import dev.usbharu.hideout.exception.JsonParseException
import kjob.core.Job
import kjob.core.dsl.JobContextWithProps
import kjob.core.job.JobProps
import kotlin.reflect.full.createInstance
import kotlin.reflect.full.primaryConstructor
class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFollowService) : ActivityPubService { class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFollowService) : ActivityPubService {
override fun parseActivity(json: String): ActivityType { override fun parseActivity(json: String): ActivityType {
@ -60,4 +67,12 @@ class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFo
ActivityType.Other -> TODO() ActivityType.Other -> TODO()
} }
} }
override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) {
when (hideoutJob) {
ReceiveFollowJob -> activityPubFollowService.receiveFollowJob(job.props as JobProps<ReceiveFollowJob>)
}
}
} }

View File

@ -9,7 +9,8 @@ import tech.barbero.http.message.signing.SignatureHeaderVerifier
class HttpSignatureVerifyServiceImpl(private val userAuthService: IUserAuthService) : HttpSignatureVerifyService { class HttpSignatureVerifyServiceImpl(private val userAuthService: IUserAuthService) : HttpSignatureVerifyService {
override fun verify(headers: Headers): Boolean { override fun verify(headers: Headers): Boolean {
val build = SignatureHeaderVerifier.builder().keyMap(KtorKeyMap(userAuthService)).build() val build = SignatureHeaderVerifier.builder().keyMap(KtorKeyMap(userAuthService)).build()
return build.verify(object : HttpMessage { return true;
build.verify(object : HttpMessage {
override fun headerValues(name: String?): MutableList<String> { override fun headerValues(name: String?): MutableList<String> {
return name?.let { headers.getAll(it) }?.toMutableList() ?: mutableListOf() return name?.let { headers.getAll(it) }?.toMutableList() ?: mutableListOf()
} }

View File

@ -6,7 +6,7 @@ ktor {
watch = [classes, resources] watch = [classes, resources]
} }
application { application {
modules = [dev.usbharu.hideout.ApplicationKt.module] modules = [dev.usbharu.hideout.ApplicationKt.parent,dev.usbharu.hideout.ApplicationKt.worker]
} }
} }

View File

@ -13,6 +13,7 @@ import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.ActivityPubResponse import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.User import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserEntity import dev.usbharu.hideout.domain.model.UserEntity
import dev.usbharu.hideout.domain.model.job.HideoutJob
import dev.usbharu.hideout.plugins.configureRouting import dev.usbharu.hideout.plugins.configureRouting
import dev.usbharu.hideout.plugins.configureSerialization import dev.usbharu.hideout.plugins.configureSerialization
import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.repository.IUserRepository
@ -28,6 +29,7 @@ import io.ktor.client.statement.*
import io.ktor.http.* import io.ktor.http.*
import io.ktor.server.config.* import io.ktor.server.config.*
import io.ktor.server.testing.* import io.ktor.server.testing.*
import kjob.core.dsl.JobContextWithProps
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -77,6 +79,14 @@ class UsersAPTest {
override suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse? { override suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse? {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override suspend fun <T : HideoutJob> processActivity(
job: JobContextWithProps<T>,
hideoutJob: HideoutJob
) {
TODO("Not yet implemented")
}
}, UserService(object : IUserRepository { }, UserService(object : IUserRepository {
override suspend fun create(user: User): UserEntity { override suspend fun create(user: User): UserEntity {
TODO("Not yet implemented") TODO("Not yet implemented")