From 8757d059bed408079250b8d385557351c2cbbdd7 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 27 Nov 2023 11:57:42 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20follow=E3=81=AE=E3=82=B8=E3=83=A7?= =?UTF-8?q?=E3=83=96=E3=83=97=E3=83=AD=E3=82=BB=E3=83=83=E3=82=B5=E3=83=BC?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../follow/APReceiveFollowJobProcessor.kt | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/follow/APReceiveFollowJobProcessor.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/follow/APReceiveFollowJobProcessor.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/follow/APReceiveFollowJobProcessor.kt new file mode 100644 index 00000000..91b88b79 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/follow/APReceiveFollowJobProcessor.kt @@ -0,0 +1,62 @@ +package dev.usbharu.hideout.activitypub.service.activity.follow + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import dev.usbharu.hideout.activitypub.domain.model.Accept +import dev.usbharu.hideout.activitypub.domain.model.Follow +import dev.usbharu.hideout.activitypub.service.common.APRequestService +import dev.usbharu.hideout.activitypub.service.objects.user.APUserService +import dev.usbharu.hideout.application.external.Transaction +import dev.usbharu.hideout.core.external.job.ReceiveFollowJob +import dev.usbharu.hideout.core.external.job.ReceiveFollowJobParam +import dev.usbharu.hideout.core.query.UserQueryService +import dev.usbharu.hideout.core.service.job.JobProcessor +import dev.usbharu.hideout.core.service.user.UserService +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Service + +@Service +class APReceiveFollowJobProcessor( + private val transaction: Transaction, + private val userQueryService: UserQueryService, + private val apUserService: APUserService, + private val objectMapper: ObjectMapper, + private val apRequestService: APRequestService, + private val userService: UserService +) : + JobProcessor { + override suspend fun process(param: ReceiveFollowJobParam) = transaction.transaction { + val person = apUserService.fetchPerson(param.actor, param.targetActor) + val follow = objectMapper.readValue(param.follow) + + + logger.info("START Follow from: {} to {}", param.targetActor, param.actor) + + val signer = userQueryService.findByUrl(param.targetActor) + + val urlString = person.inbox ?: throw IllegalArgumentException("inbox is not found.") + + apRequestService.apPost( + url = urlString, + body = Accept( + name = "Follow", + `object` = follow, + actor = param.targetActor + ), + signer = signer + ) + + val targetEntity = userQueryService.findByUrl(param.targetActor) + val followActorEntity = + userQueryService.findByUrl(follow.actor ?: throw IllegalArgumentException("actor is null")) + + userService.followRequest(targetEntity.id, followActorEntity.id) + logger.info("SUCCESS Follow from: {} to: {}", param.targetActor, param.actor) + } + + override fun job(): ReceiveFollowJob = ReceiveFollowJob + + companion object { + private val logger = LoggerFactory.getLogger(APReceiveFollowJobProcessor::class.java) + } +}