refactor: 長すぎるメソッドなどを修正

This commit is contained in:
usbharu 2023-11-27 17:29:11 +09:00
parent 5580a8af53
commit 8925c321bd
5 changed files with 87 additions and 65 deletions

View File

@ -33,15 +33,8 @@ class ObjectDeserializer : JsonDeserializer<Object>() {
} }
return when (activityType) { return when (activityType) {
ExtendedActivityVocabulary.Follow -> { ExtendedActivityVocabulary.Follow -> p.codec.treeToValue(treeNode, Follow::class.java)
val readValue = p.codec.treeToValue(treeNode, Follow::class.java) ExtendedActivityVocabulary.Note -> p.codec.treeToValue(treeNode, Note::class.java)
readValue
}
ExtendedActivityVocabulary.Note -> {
p.codec.treeToValue(treeNode, Note::class.java)
}
ExtendedActivityVocabulary.Object -> p.codec.treeToValue(treeNode, Object::class.java) ExtendedActivityVocabulary.Object -> p.codec.treeToValue(treeNode, Object::class.java)
ExtendedActivityVocabulary.Link -> TODO() ExtendedActivityVocabulary.Link -> TODO()
ExtendedActivityVocabulary.Activity -> TODO() ExtendedActivityVocabulary.Activity -> TODO()

View File

@ -37,15 +37,29 @@ class APRequestServiceImpl(
logger.debug("START ActivityPub Request GET url: {}, signer: {}", url, signer?.url) logger.debug("START ActivityPub Request GET url: {}, signer: {}", url, signer?.url)
val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT")))
val u = URL(url) val u = URL(url)
if (signer?.privateKey == null) { val httpResponse = if (signer?.privateKey == null) {
val bodyAsText = httpClient.get(url) { apGetNotSign(url, date)
header("Accept", ContentType.Application.Activity) } else {
header("Date", date) apGetSign(date, u, signer, url)
}.bodyAsText()
logBody(bodyAsText, url)
return objectMapper.readValue(bodyAsText, responseClass)
} }
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 { val headers = headers {
append("Accept", ContentType.Application.Activity) append("Accept", ContentType.Application.Activity)
append("Date", date) append("Date", date)
@ -60,7 +74,7 @@ class APRequestServiceImpl(
), ),
privateKey = PrivateKey( privateKey = PrivateKey(
keyId = "${signer.url}#pubkey", keyId = "${signer.url}#pubkey",
privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey), privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey!!),
), ),
signHeaders = listOf("(request-target)", "date", "host", "accept") signHeaders = listOf("(request-target)", "date", "host", "accept")
) )
@ -75,15 +89,12 @@ class APRequestServiceImpl(
} }
contentType(ContentType.Application.Activity) contentType(ContentType.Application.Activity)
} }
val bodyAsText = httpResponse.bodyAsText() return httpResponse
val readValue = objectMapper.readValue(bodyAsText, responseClass) }
logger.debug(
"SUCCESS ActivityPub Request GET status: {} url: {}", private suspend fun apGetNotSign(url: String, date: String?) = httpClient.get(url) {
httpResponse.status, header("Accept", ContentType.Application.Activity)
httpResponse.request.url header("Date", date)
)
logBody(bodyAsText, url)
return readValue
} }
override suspend fun <T : Object, R : Object> apPost( override suspend fun <T : Object, R : Object> apPost(
@ -96,18 +107,9 @@ class APRequestServiceImpl(
return objectMapper.readValue(bodyAsText, responseClass) return objectMapper.readValue(bodyAsText, responseClass)
} }
@Suppress("LongMethod")
override suspend fun <T : Object> apPost(url: String, body: T?, signer: User?): String { override suspend fun <T : Object> apPost(url: String, body: T?, signer: User?): String {
logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url) logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url)
val requestBody = if (body != null) { val requestBody = addContextIfNotNull(body)
val mutableListOf = mutableListOf<String>()
mutableListOf.add("https://www.w3.org/ns/activitystreams")
mutableListOf.addAll(body.context)
body.context = mutableListOf
objectMapper.writeValueAsString(body)
} else {
null
}
logger.trace( logger.trace(
""" """
@ -129,8 +131,28 @@ class APRequestServiceImpl(
val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT")))
val u = URL(url) val u = URL(url)
if (signer?.privateKey == null) { val httpResponse = if (signer?.privateKey == null) {
val bodyAsText = httpClient.post(url) { 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) accept(ContentType.Application.Activity)
header("Date", date) header("Date", date)
header("Digest", "sha-256=$digest") header("Digest", "sha-256=$digest")
@ -138,11 +160,16 @@ class APRequestServiceImpl(
setBody(requestBody) setBody(requestBody)
contentType(ContentType.Application.Activity) contentType(ContentType.Application.Activity)
} }
}.bodyAsText()
logBody(bodyAsText, url)
return bodyAsText
} }
private suspend fun apPostSign(
date: String,
u: URL,
digest: String,
signer: User,
url: String,
requestBody: String?
): HttpResponse {
val headers = headers { val headers = headers {
append("Accept", ContentType.Application.Activity) append("Accept", ContentType.Application.Activity)
append("Date", date) append("Date", date)
@ -158,30 +185,32 @@ class APRequestServiceImpl(
), ),
privateKey = PrivateKey( privateKey = PrivateKey(
keyId = signer.keyId, keyId = signer.keyId,
privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey) privateKey = RsaUtil.decodeRsaPrivateKeyPem(signer.privateKey!!)
), ),
signHeaders = listOf("(request-target)", "date", "host", "digest") signHeaders = listOf("(request-target)", "date", "host", "digest")
) )
val httpResponse = httpClient.post(url) { val httpResponse = httpClient.post(url) {
headers {
headers { headers {
appendAll(headers) appendAll(headers)
append("Signature", sign.signatureHeader) append("Signature", sign.signatureHeader)
remove("Host") remove("Host")
}
} }
setBody(requestBody) setBody(requestBody)
contentType(ContentType.Application.Activity) contentType(ContentType.Application.Activity)
} }
val bodyAsText = httpResponse.bodyAsText() return httpResponse
logger.debug( }
"SUCCESS ActivityPub Request POST status: {} url: {}",
httpResponse.status, private fun <T : Object> addContextIfNotNull(body: T?) = if (body != null) {
httpResponse.request.url val mutableListOf = mutableListOf<String>()
) mutableListOf.add("https://www.w3.org/ns/activitystreams")
logBody(bodyAsText, url) mutableListOf.addAll(body.context)
return bodyAsText body.context = mutableListOf
objectMapper.writeValueAsString(body)
} else {
null
} }
private fun logBody(bodyAsText: String, url: String) { private fun logBody(bodyAsText: String, url: String) {

View File

@ -218,7 +218,7 @@ class APServiceImpl(
} }
} }
@Suppress("CyclomaticComplexMethod", "NotImplementedDeclaration") @Suppress("CyclomaticComplexMethod")
override suspend fun processActivity( override suspend fun processActivity(
json: String, json: String,
type: ActivityType, type: ActivityType,

View File

@ -30,7 +30,8 @@ class KjobMongoJobQueueParentService(private val mongoClient: MongoClient) : Job
} }
override suspend fun <T, J : HideoutJob<T, J>> scheduleTypeSafe(job: J, jobProps: T) { override suspend fun <T, J : HideoutJob<T, J>> scheduleTypeSafe(job: J, jobProps: T) {
TODO("Not yet implemented") val convert = job.convert(jobProps)
kjob.schedule(job, convert)
} }
override fun close() { override fun close() {

View File

@ -3,7 +3,6 @@ package dev.usbharu.hideout.core.service.user
import dev.usbharu.hideout.core.domain.model.user.User import dev.usbharu.hideout.core.domain.model.user.User
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Suppress("TooManyFunctions")
@Service @Service
interface UserService { interface UserService {