From 4b0484df64291b1f0690c3fea137af2fdbe1f4e2 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 9 Apr 2023 23:05:06 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Follow=E3=82=92=E5=8F=97=E3=81=91?= =?UTF-8?q?=E5=8F=96=E3=81=A3=E3=81=9F=E3=81=A8=E3=81=8D=E3=81=AE=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/usbharu/hideout/Application.kt | 14 +++++++---- .../domain/model/ActivityPubStringResponse.kt | 23 +++++++++++++++---- .../domain/model/job/AcceptFollowJob.kt | 5 ++++ .../activitypub/ActivityPubFollowService.kt | 8 +++++++ .../ActivityPubFollowServiceImpl.kt | 16 +++++++++++++ .../service/activitypub/ActivityPubService.kt | 2 +- .../activitypub/ActivityPubServiceImpl.kt | 15 ++++++++---- .../hideout/service/job/JobQueueService.kt | 2 +- .../routing/activitypub/UsersAPTest.kt | 2 +- 9 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 src/main/kotlin/dev/usbharu/hideout/domain/model/job/AcceptFollowJob.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowService.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/Application.kt b/src/main/kotlin/dev/usbharu/hideout/Application.kt index 5e386662..127d516d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/Application.kt +++ b/src/main/kotlin/dev/usbharu/hideout/Application.kt @@ -12,12 +12,11 @@ import dev.usbharu.hideout.repository.UserAuthRepository import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.routing.register import dev.usbharu.hideout.service.IUserAuthService -import dev.usbharu.hideout.service.activitypub.ActivityPubService -import dev.usbharu.hideout.service.activitypub.ActivityPubServiceImpl -import dev.usbharu.hideout.service.activitypub.ActivityPubUserService -import dev.usbharu.hideout.service.activitypub.ActivityPubUserServiceImpl +import dev.usbharu.hideout.service.activitypub.* import dev.usbharu.hideout.service.impl.UserAuthService import dev.usbharu.hideout.service.impl.UserService +import dev.usbharu.hideout.service.job.JobQueueService +import dev.usbharu.hideout.service.job.KJobJobQueueService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyServiceImpl import io.ktor.server.application.* @@ -55,7 +54,12 @@ fun Application.module() { single { UserAuthRepository(get()) } single { UserAuthService(get(), get()) } single { HttpSignatureVerifyServiceImpl(get()) } - single { ActivityPubServiceImpl() } + single { val kJobJobQueueService = KJobJobQueueService(get()) + kJobJobQueueService.init(listOf()) + kJobJobQueueService + } + single{ ActivityPubFollowServiceImpl(get()) } + single { ActivityPubServiceImpl(get()) } single { UserService(get()) } single { ActivityPubUserServiceImpl(get(), get()) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubStringResponse.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubStringResponse.kt index 643502a6..294a1513 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubStringResponse.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/ActivityPubStringResponse.kt @@ -1,11 +1,24 @@ package dev.usbharu.hideout.domain.model import dev.usbharu.hideout.ap.JsonLd +import dev.usbharu.hideout.util.HttpUtil.Activity import io.ktor.http.* -sealed class ActivityPubResponse(val httpStatusCode: HttpStatusCode) -class ActivityPubStringResponse(httpStatusCode: HttpStatusCode, val message: String) : - ActivityPubResponse(httpStatusCode) +sealed class ActivityPubResponse( + val httpStatusCode: HttpStatusCode, + val contentType: ContentType = ContentType.Application.Activity +) -class ActivityPubObjectResponse(httpStatusCode: HttpStatusCode, val message: JsonLd) : - ActivityPubResponse(httpStatusCode) +class ActivityPubStringResponse( + httpStatusCode: HttpStatusCode, + val message: String, + contentType: ContentType = ContentType.Application.Activity +) : + ActivityPubResponse(httpStatusCode, contentType) + +class ActivityPubObjectResponse( + httpStatusCode: HttpStatusCode, + val message: JsonLd, + contentType: ContentType = ContentType.Application.Activity +) : + ActivityPubResponse(httpStatusCode, contentType) diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/job/AcceptFollowJob.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/job/AcceptFollowJob.kt new file mode 100644 index 00000000..97748c4e --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/job/AcceptFollowJob.kt @@ -0,0 +1,5 @@ +package dev.usbharu.hideout.domain.model.job + +import kjob.core.Job + +object AcceptFollowJob : Job("AcceptFollowJob") diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowService.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowService.kt new file mode 100644 index 00000000..af86ffcd --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowService.kt @@ -0,0 +1,8 @@ +package dev.usbharu.hideout.service.activitypub + +import dev.usbharu.hideout.ap.Follow +import dev.usbharu.hideout.domain.model.ActivityPubResponse + +interface ActivityPubFollowService { + suspend fun receiveFollow(follow:Follow):ActivityPubResponse +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt new file mode 100644 index 00000000..1fed2d86 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt @@ -0,0 +1,16 @@ +package dev.usbharu.hideout.service.activitypub + +import dev.usbharu.hideout.ap.Follow +import dev.usbharu.hideout.domain.model.ActivityPubResponse +import dev.usbharu.hideout.domain.model.ActivityPubStringResponse +import dev.usbharu.hideout.domain.model.job.AcceptFollowJob +import dev.usbharu.hideout.service.job.JobQueueService +import io.ktor.http.* + +class ActivityPubFollowServiceImpl(private val jobQueueService: JobQueueService) : ActivityPubFollowService { + override suspend fun receiveFollow(follow: Follow): ActivityPubResponse { + // TODO: Verify HTTP Signature + jobQueueService.schedule(AcceptFollowJob) + return ActivityPubStringResponse(HttpStatusCode.OK,"{}",ContentType.Application.Json) + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt index 7558f2c3..5db5a1aa 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt @@ -5,7 +5,7 @@ import dev.usbharu.hideout.domain.model.ActivityPubResponse interface ActivityPubService { fun parseActivity(json:String): ActivityType - fun processActivity(json:String, type: ActivityType): ActivityPubResponse? + suspend fun processActivity(json:String, type: ActivityType): ActivityPubResponse? } enum class ActivityType { 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 a9ea3b7f..10ca0e13 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt @@ -1,11 +1,12 @@ package dev.usbharu.hideout.service.activitypub import com.fasterxml.jackson.databind.JsonNode +import dev.usbharu.hideout.ap.Follow import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.domain.model.ActivityPubResponse import dev.usbharu.hideout.exception.JsonParseException -class ActivityPubServiceImpl : ActivityPubService { +class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFollowService) : ActivityPubService { override fun parseActivity(json: String): ActivityType { val readTree = Config.configData.objectMapper.readTree(json) if (readTree.isObject.not()) { @@ -20,8 +21,8 @@ class ActivityPubServiceImpl : ActivityPubService { return ActivityType.values().first { it.name.equals(type.asText(), true) } } - override fun processActivity(json: String, type: ActivityType): ActivityPubResponse? { - when (type) { + override suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse? { + return when (type) { ActivityType.Accept -> TODO() ActivityType.Add -> TODO() ActivityType.Announce -> TODO() @@ -31,7 +32,13 @@ class ActivityPubServiceImpl : ActivityPubService { ActivityType.Delete -> TODO() ActivityType.Dislike -> TODO() ActivityType.Flag -> TODO() - ActivityType.Follow -> TODO() + ActivityType.Follow -> activityPubFollowService.receiveFollow( + Config.configData.objectMapper.readValue( + json, + Follow::class.java + ) + ) + ActivityType.Ignore -> TODO() ActivityType.Invite -> TODO() ActivityType.Join -> TODO() diff --git a/src/main/kotlin/dev/usbharu/hideout/service/job/JobQueueService.kt b/src/main/kotlin/dev/usbharu/hideout/service/job/JobQueueService.kt index 94486f45..03cc1826 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/job/JobQueueService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/job/JobQueueService.kt @@ -6,5 +6,5 @@ import kjob.core.dsl.ScheduleContext interface JobQueueService { fun init(jobDefines:List) - suspend fun schedule(job: J, block: ScheduleContext.(J) -> Unit) + suspend fun schedule(job: J, block: ScheduleContext.(J) -> Unit = {}) } diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt index 226a5e51..1aa2f7d8 100644 --- a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt @@ -74,7 +74,7 @@ class UsersAPTest { TODO("Not yet implemented") } - override fun processActivity(json: String, type: ActivityType): ActivityPubResponse? { + override suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse? { TODO("Not yet implemented") } }, UserService(object : IUserRepository {