From 63c15b35d19dc944d0365924bb3d3a0e04068806 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:52:15 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E3=83=AD=E3=82=B0=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/controller/InboxControllerImpl.kt | 17 ++++++++++++++--- .../controller/wellknown/WebFingerController.kt | 14 +++++++++++++- .../dev/usbharu/hideout/service/ap/APService.kt | 14 ++++++++++++-- .../hideout/service/post/PostServiceImpl.kt | 14 +++++++++++++- .../service/reaction/ReactionServiceImpl.kt | 4 +--- src/main/resources/logback.xml | 1 + 6 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/InboxControllerImpl.kt b/src/main/kotlin/dev/usbharu/hideout/controller/InboxControllerImpl.kt index 7afc1871..235c821f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/InboxControllerImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/InboxControllerImpl.kt @@ -10,13 +10,24 @@ import org.springframework.web.bind.annotation.RestController @RestController class InboxControllerImpl(private val apService: APService) : InboxController { override suspend fun inbox(@RequestBody string: String): ResponseEntity { - val parseActivity = apService.parseActivity(string) + val parseActivity = try { + apService.parseActivity(string) + } catch (e: Exception) { + LOGGER.warn("FAILED Parse Activity", e) + return ResponseEntity.accepted().build() + } LOGGER.info("INBOX Processing Activity Type: {}", parseActivity) - apService.processActivity(string, parseActivity) + try { + apService.processActivity(string, parseActivity) + } catch (e: Exception) { + LOGGER.warn("FAILED Process Activity $parseActivity", e) + return ResponseEntity(HttpStatus.ACCEPTED) + } + LOGGER.info("SUCCESS Processing Activity Type: {}", parseActivity) return ResponseEntity(HttpStatus.ACCEPTED) } companion object { - val LOGGER = LoggerFactory.getLogger(InboxControllerImpl::class.java) + private val LOGGER = LoggerFactory.getLogger(InboxControllerImpl::class.java) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/WebFingerController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/WebFingerController.kt index 070b82fa..a8032d1a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/WebFingerController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/WebFingerController.kt @@ -5,6 +5,7 @@ import dev.usbharu.hideout.domain.model.wellknown.WebFinger import dev.usbharu.hideout.service.api.WebFingerApiService import dev.usbharu.hideout.util.AcctUtil import kotlinx.coroutines.runBlocking +import org.slf4j.LoggerFactory import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.stereotype.Controller @@ -18,7 +19,13 @@ class WebFingerController( ) { @GetMapping("/.well-known/webfinger") fun webfinger(@RequestParam("resource") resource: String): ResponseEntity = runBlocking { - val acct = AcctUtil.parse(resource.replace("acct:", "")) + logger.info("WEBFINGER Lookup webfinger resource: {}", resource) + val acct = try { + AcctUtil.parse(resource.replace("acct:", "")) + } catch (e: IllegalArgumentException) { + logger.warn("FAILED Parse acct.", e) + return@runBlocking ResponseEntity.badRequest().build() + } val user = webFingerApiService.findByNameAndDomain(acct.username, acct.domain ?: applicationConfig.url.host) val webFinger = WebFinger( @@ -31,6 +38,11 @@ class WebFingerController( ) ) ) + logger.info("SUCCESS Lookup webfinger resource: {} acct: {}", resource, acct) ResponseEntity(webFinger, HttpStatus.OK) } + + companion object { + private val logger = LoggerFactory.getLogger(WebFingerController::class.java) + } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APService.kt index 0e14909c..9ea98a66 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APService.kt @@ -188,11 +188,21 @@ class APServiceImpl( val logger: Logger = LoggerFactory.getLogger(APServiceImpl::class.java) override fun parseActivity(json: String): ActivityType { val readTree = objectMapper.readTree(json) - logger.trace("readTree: {}", readTree) + logger.trace( + """ + | + |***** Trace Begin Activity ***** + | + |{} + | + |***** Trace End Activity ***** + | + """.trimMargin(), readTree.toPrettyString() + ) if (readTree.isObject.not()) { throw JsonParseException("Json is not object.") } - val type = readTree["type"] + val type = readTree["type"] ?: throw JsonParseException("Type is null") if (type.isArray) { return type.firstNotNullOf { jsonNode: JsonNode -> ActivityType.values().firstOrNull { it.name.equals(jsonNode.asText(), true) } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt index c98ee233..a22666fa 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt @@ -7,6 +7,7 @@ import dev.usbharu.hideout.query.PostQueryService import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.repository.UserRepository import org.jetbrains.exposed.exceptions.ExposedSQLException +import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import java.time.Instant import java.util.* @@ -22,12 +23,19 @@ class PostServiceImpl( private val interceptors = Collections.synchronizedList(mutableListOf()) override suspend fun createLocal(post: PostCreateDto): Post { + logger.info("START Create Local Post user: {}, media: {}", post.userId, post.mediaIds.size) val create = internalCreate(post, true) interceptors.forEach { it.run(create) } + logger.info("SUCCESS Create Local Post url: {}", create.url) return create } - override suspend fun createRemote(post: Post): Post = internalCreate(post, false) + override suspend fun createRemote(post: Post): Post { + logger.info("START Create Remote Post user: {}, remote url: {}", post.userId, post.apId) + val createdPost = internalCreate(post, false) + logger.info("SUCCESS Create Remote Post url: {}", createdPost.url) + return createdPost + } override fun addInterceptor(postCreateInterceptor: PostCreateInterceptor) { interceptors.add(postCreateInterceptor) @@ -58,4 +66,8 @@ class PostServiceImpl( ) return internalCreate(createPost, isLocal) } + + companion object { + private val logger = LoggerFactory.getLogger(PostServiceImpl::class.java) + } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt index 4ccac212..134e0ba6 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt @@ -20,9 +20,7 @@ class ReactionServiceImpl( reactionRepository.save( Reaction(reactionRepository.generateId(), 0, postId, userId) ) - } catch (e: ExposedSQLException) { - LOGGER.warn("FAILED Failure to persist reaction information.") - LOGGER.debug("FAILED", e) + } catch (_: ExposedSQLException) { } } } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 73cc4b4a..5e4e2bc3 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -14,4 +14,5 @@ +