feat: inboxをジョブキューに載せた

This commit is contained in:
usbharu 2023-11-21 17:58:13 +09:00
parent f4d4380c7f
commit e4eaef7277
3 changed files with 54 additions and 18 deletions

View File

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

View File

@ -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 <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) {
@ -22,6 +40,26 @@ class ApJobServiceImpl(
@Suppress("ElseCaseInsteadOfExhaustiveWhen")
// Springで作成されるプロキシの都合上パターンマッチングが壊れるので必須
when (hideoutJob) {
is InboxJob -> {
val typeString = (job.props as JobProps<InboxJob>)[InboxJob.type]
val json = (job.props as JobProps<InboxJob>)[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<ReceiveFollowJob>

View File

@ -36,3 +36,9 @@ object DeliverRemoveReactionJob : HideoutJob("DeliverRemoveReactionJob") {
val actor: Prop<DeliverRemoveReactionJob, String> = string("actor")
val like: Prop<DeliverRemoveReactionJob, String> = string("like")
}
@Component
object InboxJob : HideoutJob("InboxJob") {
val json = string("json")
val type = string("type")
}