diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubAcceptService.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubAcceptService.kt new file mode 100644 index 00000000..d0746c44 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubAcceptService.kt @@ -0,0 +1,8 @@ +package dev.usbharu.hideout.service.activitypub + +import dev.usbharu.hideout.domain.model.ActivityPubResponse +import dev.usbharu.hideout.domain.model.ap.Accept + +interface ActivityPubAcceptService { + suspend fun receiveAccept(accept: Accept): ActivityPubResponse +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubAcceptServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubAcceptServiceImpl.kt new file mode 100644 index 00000000..f37428c2 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubAcceptServiceImpl.kt @@ -0,0 +1,28 @@ +package dev.usbharu.hideout.service.activitypub + +import dev.usbharu.hideout.domain.model.ActivityPubResponse +import dev.usbharu.hideout.domain.model.ActivityPubStringResponse +import dev.usbharu.hideout.domain.model.ap.Accept +import dev.usbharu.hideout.domain.model.ap.Follow +import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException +import dev.usbharu.hideout.service.impl.IUserService +import io.ktor.http.* +import org.koin.core.annotation.Single + +@Single +class ActivityPubAcceptServiceImpl(private val userService: IUserService) : ActivityPubAcceptService { + override suspend fun receiveAccept(accept: Accept): ActivityPubResponse { + val value = accept.`object` ?: throw IllegalActivityPubObjectException("object is null") + if (value.type.contains("Follow").not()) { + throw IllegalActivityPubObjectException("Invalid type ${value.type}") + } + + val follow = value as Follow + val userUrl = follow.`object` ?: throw IllegalActivityPubObjectException("object is null") + val followerUrl = follow.actor ?: throw IllegalActivityPubObjectException("actor is null") + val user = userService.findByUrl(userUrl) + val follower = userService.findByUrl(followerUrl) + userService.follow(user.id, follower.id) + return ActivityPubStringResponse(HttpStatusCode.OK, "accepted") + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt index dbcc3af2..1c9a11e7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt @@ -2,7 +2,7 @@ package dev.usbharu.hideout.service.activitypub import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.module.kotlin.readValue -import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.config.Config.configData import dev.usbharu.hideout.domain.model.ActivityPubResponse import dev.usbharu.hideout.domain.model.ap.Follow import dev.usbharu.hideout.domain.model.job.DeliverPostJob @@ -19,12 +19,13 @@ import org.slf4j.LoggerFactory class ActivityPubServiceImpl( private val activityPubReceiveFollowService: ActivityPubReceiveFollowService, private val activityPubNoteService: ActivityPubNoteService, - private val activityPubUndoService: ActivityPubUndoService + private val activityPubUndoService: ActivityPubUndoService, + private val activityPubAcceptService: ActivityPubAcceptService ) : ActivityPubService { val logger: Logger = LoggerFactory.getLogger(this::class.java) override fun parseActivity(json: String): ActivityType { - val readTree = Config.configData.objectMapper.readTree(json) + val readTree = configData.objectMapper.readTree(json) logger.debug("readTree: {}", readTree) if (readTree.isObject.not()) { throw JsonParseException("Json is not object.") @@ -41,7 +42,7 @@ class ActivityPubServiceImpl( @Suppress("CyclomaticComplexMethod", "NotImplementedDeclaration") override suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse { return when (type) { - ActivityType.Accept -> TODO() + ActivityType.Accept -> activityPubAcceptService.receiveAccept(configData.objectMapper.readValue(json)) ActivityType.Add -> TODO() ActivityType.Announce -> TODO() ActivityType.Arrive -> TODO() @@ -51,7 +52,7 @@ class ActivityPubServiceImpl( ActivityType.Dislike -> TODO() ActivityType.Flag -> TODO() ActivityType.Follow -> activityPubReceiveFollowService.receiveFollow( - Config.configData.objectMapper.readValue( + configData.objectMapper.readValue( json, Follow::class.java ) @@ -72,7 +73,7 @@ class ActivityPubServiceImpl( ActivityType.TentativeReject -> TODO() ActivityType.TentativeAccept -> TODO() ActivityType.Travel -> TODO() - ActivityType.Undo -> activityPubUndoService.receiveUndo(Config.configData.objectMapper.readValue(json)) + ActivityType.Undo -> activityPubUndoService.receiveUndo(configData.objectMapper.readValue(json)) ActivityType.Update -> TODO() ActivityType.View -> TODO() ActivityType.Other -> TODO()