From a205c8608ce27d5a794e085455413c823b03ede1 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 27 Nov 2023 17:29:11 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=95=B7=E3=81=99=E3=81=8E?= =?UTF-8?q?=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=81=AA=E3=81=A9?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/objects/ObjectDeserializer.kt | 11 +- .../service/common/APRequestServiceImpl.kt | 135 +++++++++++------- .../activitypub/service/common/APService.kt | 2 +- .../KjobMongoJobQueueParentService.kt | 3 +- .../hideout/core/service/user/UserService.kt | 1 - 5 files changed, 87 insertions(+), 65 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/ObjectDeserializer.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/ObjectDeserializer.kt index 377dfcff..acbf32e5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/ObjectDeserializer.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/ObjectDeserializer.kt @@ -33,15 +33,8 @@ class ObjectDeserializer : JsonDeserializer() { } return when (activityType) { - ExtendedActivityVocabulary.Follow -> { - val readValue = p.codec.treeToValue(treeNode, Follow::class.java) - readValue - } - - ExtendedActivityVocabulary.Note -> { - p.codec.treeToValue(treeNode, Note::class.java) - } - + ExtendedActivityVocabulary.Follow -> p.codec.treeToValue(treeNode, Follow::class.java) + ExtendedActivityVocabulary.Note -> p.codec.treeToValue(treeNode, Note::class.java) ExtendedActivityVocabulary.Object -> p.codec.treeToValue(treeNode, Object::class.java) ExtendedActivityVocabulary.Link -> TODO() ExtendedActivityVocabulary.Activity -> TODO() diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt index 6e87d402..b2474af1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/APRequestServiceImpl.kt @@ -37,15 +37,29 @@ class APRequestServiceImpl( logger.debug("START ActivityPub Request GET url: {}, signer: {}", url, signer?.url) val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val u = URL(url) - if (signer?.privateKey == null) { - val bodyAsText = httpClient.get(url) { - header("Accept", ContentType.Application.Activity) - header("Date", date) - }.bodyAsText() - logBody(bodyAsText, url) - return objectMapper.readValue(bodyAsText, responseClass) + val httpResponse = if (signer?.privateKey == null) { + apGetNotSign(url, date) + } else { + apGetSign(date, u, signer, url) } + val bodyAsText = httpResponse.bodyAsText() + val readValue = objectMapper.readValue(bodyAsText, responseClass) + logger.debug( + "SUCCESS ActivityPub Request GET status: {} url: {}", + httpResponse.status, + httpResponse.request.url + ) + logBody(bodyAsText, url) + return readValue + } + + private suspend fun apGetSign( + date: String, + u: URL, + signer: User, + url: String + ): HttpResponse { val headers = headers { append("Accept", ContentType.Application.Activity) append("Date", date) @@ -60,7 +74,7 @@ class APRequestServiceImpl( ), privateKey = PrivateKey( keyId = "${signer.url}#pubkey", - privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey), + privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey!!), ), signHeaders = listOf("(request-target)", "date", "host", "accept") ) @@ -75,15 +89,12 @@ class APRequestServiceImpl( } contentType(ContentType.Application.Activity) } - val bodyAsText = httpResponse.bodyAsText() - val readValue = objectMapper.readValue(bodyAsText, responseClass) - logger.debug( - "SUCCESS ActivityPub Request GET status: {} url: {}", - httpResponse.status, - httpResponse.request.url - ) - logBody(bodyAsText, url) - return readValue + return httpResponse + } + + private suspend fun apGetNotSign(url: String, date: String?) = httpClient.get(url) { + header("Accept", ContentType.Application.Activity) + header("Date", date) } override suspend fun apPost( @@ -96,18 +107,9 @@ class APRequestServiceImpl( return objectMapper.readValue(bodyAsText, responseClass) } - @Suppress("LongMethod") override suspend fun apPost(url: String, body: T?, signer: User?): String { logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url) - val requestBody = if (body != null) { - val mutableListOf = mutableListOf() - mutableListOf.add("https://www.w3.org/ns/activitystreams") - mutableListOf.addAll(body.context) - body.context = mutableListOf - objectMapper.writeValueAsString(body) - } else { - null - } + val requestBody = addContextIfNotNull(body) logger.trace( """ @@ -129,20 +131,45 @@ class APRequestServiceImpl( val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val u = URL(url) - if (signer?.privateKey == null) { - val bodyAsText = httpClient.post(url) { - accept(ContentType.Application.Activity) - header("Date", date) - header("Digest", "sha-256=$digest") - if (requestBody != null) { - setBody(requestBody) - contentType(ContentType.Application.Activity) - } - }.bodyAsText() - logBody(bodyAsText, url) - return bodyAsText + val httpResponse = if (signer?.privateKey == null) { + apPostNotSign(url, date, digest, requestBody) + } else { + apPostSign(date, u, digest, signer, url, requestBody) } + val bodyAsText = httpResponse.bodyAsText() + logger.debug( + "SUCCESS ActivityPub Request POST status: {} url: {}", + httpResponse.status, + httpResponse.request.url + ) + logBody(bodyAsText, url) + return bodyAsText + } + + private suspend fun apPostNotSign( + url: String, + date: String?, + digest: String, + requestBody: String? + ) = httpClient.post(url) { + accept(ContentType.Application.Activity) + header("Date", date) + header("Digest", "sha-256=$digest") + if (requestBody != null) { + setBody(requestBody) + contentType(ContentType.Application.Activity) + } + } + + private suspend fun apPostSign( + date: String, + u: URL, + digest: String, + signer: User, + url: String, + requestBody: String? + ): HttpResponse { val headers = headers { append("Accept", ContentType.Application.Activity) append("Date", date) @@ -158,30 +185,32 @@ class APRequestServiceImpl( ), privateKey = PrivateKey( keyId = signer.keyId, - privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey) + privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey!!) ), signHeaders = listOf("(request-target)", "date", "host", "digest") ) val httpResponse = httpClient.post(url) { headers { - headers { - appendAll(headers) - append("Signature", sign.signatureHeader) - remove("Host") - } + appendAll(headers) + append("Signature", sign.signatureHeader) + remove("Host") + } setBody(requestBody) contentType(ContentType.Application.Activity) } - val bodyAsText = httpResponse.bodyAsText() - logger.debug( - "SUCCESS ActivityPub Request POST status: {} url: {}", - httpResponse.status, - httpResponse.request.url - ) - logBody(bodyAsText, url) - return bodyAsText + return httpResponse + } + + private fun addContextIfNotNull(body: T?) = if (body != null) { + val mutableListOf = mutableListOf() + mutableListOf.add("https://www.w3.org/ns/activitystreams") + mutableListOf.addAll(body.context) + body.context = mutableListOf + objectMapper.writeValueAsString(body) + } else { + null } private fun logBody(bodyAsText: String, url: String) { 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 e23ed018..0a4745e6 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 @@ -218,7 +218,7 @@ class APServiceImpl( } } - @Suppress("CyclomaticComplexMethod", "NotImplementedDeclaration") + @Suppress("CyclomaticComplexMethod") override suspend fun processActivity( json: String, type: ActivityType, diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KjobMongoJobQueueParentService.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KjobMongoJobQueueParentService.kt index 846e8dff..f66f58ef 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KjobMongoJobQueueParentService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KjobMongoJobQueueParentService.kt @@ -30,7 +30,8 @@ class KjobMongoJobQueueParentService(private val mongoClient: MongoClient) : Job } override suspend fun > scheduleTypeSafe(job: J, jobProps: T) { - TODO("Not yet implemented") + val convert = job.convert(jobProps) + kjob.schedule(job, convert) } override fun close() { diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt index fdfb5bcf..fc34c36c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt @@ -3,7 +3,6 @@ package dev.usbharu.hideout.core.service.user import dev.usbharu.hideout.core.domain.model.user.User import org.springframework.stereotype.Service -@Suppress("TooManyFunctions") @Service interface UserService {