From e4eaef7277cf9d95bdedb45b963557f4e9a678c9 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 21 Nov 2023 17:58:13 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20inbox=E3=82=92=E3=82=B8=E3=83=A7?= =?UTF-8?q?=E3=83=96=E3=82=AD=E3=83=A5=E3=83=BC=E3=81=AB=E8=BC=89=E3=81=9B?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activitypub/service/common/APService.kt | 26 +++++------- .../service/common/ApJobServiceImpl.kt | 40 ++++++++++++++++++- .../hideout/core/external/job/HideoutJob.kt | 6 +++ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APService.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APService.kt index c7df1df2..78c2a52a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APService.kt @@ -2,16 +2,17 @@ package dev.usbharu.hideout.activitypub.service.common import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.module.kotlin.readValue import dev.usbharu.hideout.activitypub.domain.exception.JsonParseException -import dev.usbharu.hideout.activitypub.domain.model.Follow import dev.usbharu.hideout.activitypub.interfaces.api.common.ActivityPubResponse +import dev.usbharu.hideout.activitypub.interfaces.api.common.ActivityPubStringResponse import dev.usbharu.hideout.activitypub.service.activity.accept.APAcceptService import dev.usbharu.hideout.activitypub.service.activity.create.APCreateService import dev.usbharu.hideout.activitypub.service.activity.delete.APReceiveDeleteService import dev.usbharu.hideout.activitypub.service.activity.follow.APReceiveFollowService import dev.usbharu.hideout.activitypub.service.activity.like.APLikeService import dev.usbharu.hideout.activitypub.service.activity.undo.APUndoService +import dev.usbharu.hideout.core.external.job.InboxJob +import dev.usbharu.hideout.core.service.job.JobQueueParentService import org.slf4j.Logger import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Qualifier @@ -182,7 +183,8 @@ class APServiceImpl( private val apCreateService: APCreateService, private val apLikeService: APLikeService, private val apReceiveDeleteService: APReceiveDeleteService, - @Qualifier("activitypub") private val objectMapper: ObjectMapper + @Qualifier("activitypub") private val objectMapper: ObjectMapper, + private val jobQueueParentService: JobQueueParentService ) : APService { val logger: Logger = LoggerFactory.getLogger(APServiceImpl::class.java) @@ -227,20 +229,10 @@ class APServiceImpl( @Suppress("CyclomaticComplexMethod", "NotImplementedDeclaration") override suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse { logger.debug("process activity: {}", type) - return when (type) { - ActivityType.Accept -> apAcceptService.receiveAccept(objectMapper.readValue(json)) - ActivityType.Follow -> - apReceiveFollowService - .receiveFollow(objectMapper.readValue(json, Follow::class.java)) - - ActivityType.Create -> apCreateService.receiveCreate(objectMapper.readValue(json)) - ActivityType.Like -> apLikeService.receiveLike(objectMapper.readValue(json)) - ActivityType.Undo -> apUndoService.receiveUndo(objectMapper.readValue(json)) - ActivityType.Delete -> apReceiveDeleteService.receiveDelete(objectMapper.readValue(json)) - - else -> { - throw IllegalArgumentException("$type is not supported.") - } + jobQueueParentService.schedule(InboxJob) { + props[it.json] = json + props[it.type] = type.name } + return ActivityPubStringResponse(message = "") } } diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobServiceImpl.kt index 7057ccc5..13d23f57 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobServiceImpl.kt @@ -1,19 +1,37 @@ package dev.usbharu.hideout.activitypub.service.common +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import dev.usbharu.hideout.activitypub.domain.model.Follow +import dev.usbharu.hideout.activitypub.service.activity.accept.APAcceptServiceImpl +import dev.usbharu.hideout.activitypub.service.activity.create.APCreateServiceImpl +import dev.usbharu.hideout.activitypub.service.activity.delete.APReceiveDeleteServiceImpl import dev.usbharu.hideout.activitypub.service.activity.follow.APReceiveFollowJobService +import dev.usbharu.hideout.activitypub.service.activity.follow.APReceiveFollowServiceImpl +import dev.usbharu.hideout.activitypub.service.activity.like.APLikeServiceImpl import dev.usbharu.hideout.activitypub.service.activity.like.ApReactionJobService +import dev.usbharu.hideout.activitypub.service.activity.undo.APUndoServiceImpl import dev.usbharu.hideout.activitypub.service.objects.note.ApNoteJobService import dev.usbharu.hideout.core.external.job.* + import kjob.core.dsl.JobContextWithProps import kjob.core.job.JobProps import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Qualifier import org.springframework.stereotype.Service @Service class ApJobServiceImpl( private val apReceiveFollowJobService: APReceiveFollowJobService, private val apNoteJobService: ApNoteJobService, - private val apReactionJobService: ApReactionJobService + private val apReactionJobService: ApReactionJobService, + private val APAcceptServiceImpl: APAcceptServiceImpl, + private val APReceiveFollowServiceImpl: APReceiveFollowServiceImpl, + private val APCreateServiceImpl: APCreateServiceImpl, + private val APLikeServiceImpl: APLikeServiceImpl, + private val APUndoServiceImpl: APUndoServiceImpl, + private val APReceiveDeleteServiceImpl: APReceiveDeleteServiceImpl, + @Qualifier("activitypub") private val objectMapper: ObjectMapper ) : ApJobService { @Suppress("REDUNDANT_ELSE_IN_WHEN") override suspend fun processActivity(job: JobContextWithProps, hideoutJob: HideoutJob) { @@ -22,6 +40,26 @@ class ApJobServiceImpl( @Suppress("ElseCaseInsteadOfExhaustiveWhen") // Springで作成されるプロキシの都合上パターンマッチングが壊れるので必須 when (hideoutJob) { + is InboxJob -> { + val typeString = (job.props as JobProps)[InboxJob.type] + val json = (job.props as JobProps)[InboxJob.json] + val type = ActivityType.valueOf(typeString) + when (type) { + ActivityType.Accept -> APAcceptServiceImpl.receiveAccept(objectMapper.readValue(json)) + ActivityType.Follow -> + APReceiveFollowServiceImpl + .receiveFollow(objectMapper.readValue(json, Follow::class.java)) + + ActivityType.Create -> APCreateServiceImpl.receiveCreate(objectMapper.readValue(json)) + ActivityType.Like -> APLikeServiceImpl.receiveLike(objectMapper.readValue(json)) + ActivityType.Undo -> APUndoServiceImpl.receiveUndo(objectMapper.readValue(json)) + ActivityType.Delete -> APReceiveDeleteServiceImpl.receiveDelete(objectMapper.readValue(json)) + + else -> { + throw IllegalArgumentException("$type is not supported.") + } + } + } is ReceiveFollowJob -> { apReceiveFollowJobService.receiveFollowJob( job.props as JobProps diff --git a/src/main/kotlin/dev/usbharu/hideout/core/external/job/HideoutJob.kt b/src/main/kotlin/dev/usbharu/hideout/core/external/job/HideoutJob.kt index 62f989d0..94fa81da 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/external/job/HideoutJob.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/external/job/HideoutJob.kt @@ -36,3 +36,9 @@ object DeliverRemoveReactionJob : HideoutJob("DeliverRemoveReactionJob") { val actor: Prop = string("actor") val like: Prop = string("like") } + +@Component +object InboxJob : HideoutJob("InboxJob") { + val json = string("json") + val type = string("type") +}