From 72659bf57b7cfc5db80481b832b9e51908784e40 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Wed, 11 Sep 2024 21:54:25 +0000 Subject: [PATCH 01/10] fix(deps): update protobuf to v4.28.1 --- libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs.versions.toml b/libs.versions.toml index b203fa82..277d7171 100644 --- a/libs.versions.toml +++ b/libs.versions.toml @@ -10,7 +10,7 @@ swagger = "2.2.23" tika = "2.9.2" owl = "0.0.1" jackson = "2.17.2" -protobuf = "4.28.0" +protobuf = "4.28.1" grpc-java = "1.66.0" grpc-kotlin = "1.4.1" From 7345d985bdf848dc07f407e06a4415cd9a7f84f1 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 12 Sep 2024 11:43:24 +0000 Subject: [PATCH 02/10] fix(deps): update dependency org.flywaydb:flyway-database-postgresql to v10.18.0 --- libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs.versions.toml b/libs.versions.toml index 277d7171..bf530df7 100644 --- a/libs.versions.toml +++ b/libs.versions.toml @@ -78,7 +78,7 @@ imageio-webp = { module = "com.twelvemonkeys.imageio:imageio-webp", version = "3 thumbnailator = { module = "net.coobird:thumbnailator", version = "0.4.20" } flyway-core = { module = "org.flywaydb:flyway-core" } -flyway-postgresql = { module = "org.flywaydb:flyway-database-postgresql", version = "10.17.3" } +flyway-postgresql = { module = "org.flywaydb:flyway-database-postgresql", version = "10.18.0" } h2db = { module = "com.h2database:h2", version = "2.3.232" } From 0dcd2b1b9ae9587b0f9d38ffb8a2eaea71f51b1d Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 12 Sep 2024 22:02:04 +0900 Subject: [PATCH 03/10] =?UTF-8?q?chore:=20json=E3=81=AE=E3=83=AD=E3=82=B0?= =?UTF-8?q?=E3=82=92=E8=A6=8B=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- develop/docker-compose.yml | 60 +++++++++++++++++++ develop/fluentd/Dockerfile | 4 ++ develop/fluentd/conf/fluent.conf | 19 ++++++ .../http-client.env.json | 0 .../mastodon-api.http | 0 docker-compose.yml | 16 ----- .../src/main/resources/logback-spring.xml | 6 +- 7 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 develop/docker-compose.yml create mode 100644 develop/fluentd/Dockerfile create mode 100644 develop/fluentd/conf/fluent.conf rename http-client.env.json => develop/http-client.env.json (100%) rename mastodon-api.http => develop/mastodon-api.http (100%) delete mode 100644 docker-compose.yml diff --git a/develop/docker-compose.yml b/develop/docker-compose.yml new file mode 100644 index 00000000..4abaed5f --- /dev/null +++ b/develop/docker-compose.yml @@ -0,0 +1,60 @@ +version: "3" + +services: + db: + image: postgres:16 + ports: + - "5432:5432" + environment: + POSTGRES_USER: "postgres" + POSTGRES_PASSWORD: "password" + POSTGRES_DB: "hideout" + + mongodb: + image: mongo:7.0.14 + ports: + - "27017:27017" + + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:7.17.24 + container_name: elasticsearch + environment: + - discovery.type=single-node + - cluster.name=docker-cluster + - bootstrap.memory_lock=true + - xpack.security.enabled=true + - xpack.monitoring.collection.enabled=true + - ELASTIC_PASSWORD=Passw0rd + - "ES_JAVA_OPTS=-Xms512M -Xmx512M" + ports: + - "9200:9200" + volumes: + - elasticsearch-data:/usr/share/elasticsearch/data + restart: always + kibana: + image: docker.elastic.co/kibana/kibana:7.17.24 + container_name: kibana + ports: + - "5601:5601" + environment: + - ELASTICSEARCH_URL=http://elasticsearch:9200 + - ELASTICSEARCH_USERNAME=elastic + - ELASTICSEARCH_PASSWORD=Passw0rd + depends_on: + - elasticsearch + restart: always + + fluentd: + build: ./fluentd + volumes: + - ./fluentd/conf:/fluentd/etc + - ../logs:/in + depends_on: + - "elasticsearch" + ports: + - "24224:24224" + - "24224:24224/udp" + +volumes: + elasticsearch-data: + driver: local \ No newline at end of file diff --git a/develop/fluentd/Dockerfile b/develop/fluentd/Dockerfile new file mode 100644 index 00000000..1070a2ce --- /dev/null +++ b/develop/fluentd/Dockerfile @@ -0,0 +1,4 @@ +FROM fluent/fluentd:v1.16-debian-2 +USER root +RUN fluent-gem install fluent-plugin-elasticsearch --no-document +USER fluent \ No newline at end of file diff --git a/develop/fluentd/conf/fluent.conf b/develop/fluentd/conf/fluent.conf new file mode 100644 index 00000000..51dbd4cf --- /dev/null +++ b/develop/fluentd/conf/fluent.conf @@ -0,0 +1,19 @@ + + @type tail + path /in/logFile.log.json + pos_file /in/logFile.pos + tag hideout + + @type json + + + + + @type elasticsearch + host elasticsearch + port 9200 + index_name logs + include_timestamp true + user elastic + password Passw0rd + \ No newline at end of file diff --git a/http-client.env.json b/develop/http-client.env.json similarity index 100% rename from http-client.env.json rename to develop/http-client.env.json diff --git a/mastodon-api.http b/develop/mastodon-api.http similarity index 100% rename from mastodon-api.http rename to develop/mastodon-api.http diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 274d183c..00000000 --- a/docker-compose.yml +++ /dev/null @@ -1,16 +0,0 @@ -version: "3" - -services: - db: - image: postgres:16 - ports: - - "5432:5432" - environment: - POSTGRES_USER: "postgres" - POSTGRES_PASSWORD: "password" - POSTGRES_DB: "hideout" - - mongodb: - image: mongo:7.0.14 - ports: - - "27017:27017" \ No newline at end of file diff --git a/hideout-core/src/main/resources/logback-spring.xml b/hideout-core/src/main/resources/logback-spring.xml index 62421f27..a29f493e 100644 --- a/hideout-core/src/main/resources/logback-spring.xml +++ b/hideout-core/src/main/resources/logback-spring.xml @@ -1,7 +1,7 @@ - + @@ -12,4 +12,8 @@ + + + + \ No newline at end of file From 94365e09f27fb0607ba5ef96e856c000b2615301 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 13 Sep 2024 01:38:12 +0900 Subject: [PATCH 04/10] =?UTF-8?q?chore:=20=E3=83=AD=E3=82=B0=E3=82=92?= =?UTF-8?q?=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- develop/fluentd/conf/fluent.conf | 5 +- gradle/wrapper/gradle-wrapper.properties | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- .../hideout/core/config/SpringMvcConfig.kt | 16 ++++- .../ApplicationRequestLogInterceptor.kt | 65 +++++++++++++++++++ .../oauth2/UserDetailsServiceImpl.kt | 3 +- .../src/main/resources/logback-spring.xml | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- .../interfaces/api/SpringAccountApi.kt | 3 +- .../interfaces/api/SpringTimelineApi.kt | 3 +- 10 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt diff --git a/develop/fluentd/conf/fluent.conf b/develop/fluentd/conf/fluent.conf index 51dbd4cf..61cf3cd1 100644 --- a/develop/fluentd/conf/fluent.conf +++ b/develop/fluentd/conf/fluent.conf @@ -11,9 +11,12 @@ @type elasticsearch host elasticsearch + include_tag_key true port 9200 - index_name logs include_timestamp true user elastic password Passw0rd + logstash_format true + logstash_prefix hideout + flush_interval 10s \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0aaefbca..9355b415 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/hideout-core/gradle/wrapper/gradle-wrapper.properties b/hideout-core/gradle/wrapper/gradle-wrapper.properties index 0aaefbca..9355b415 100644 --- a/hideout-core/gradle/wrapper/gradle-wrapper.properties +++ b/hideout-core/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SpringMvcConfig.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SpringMvcConfig.kt index 301762d4..0294603a 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SpringMvcConfig.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SpringMvcConfig.kt @@ -16,8 +16,11 @@ package dev.usbharu.hideout.core.config +import ch.qos.logback.classic.helpers.MDCInsertingServletFilter +import dev.usbharu.hideout.core.infrastructure.springframework.ApplicationRequestLogInterceptor import dev.usbharu.hideout.core.infrastructure.springframework.SPAInterceptor import dev.usbharu.hideout.generate.JsonOrFormModelMethodProcessor +import org.springframework.boot.web.servlet.FilterRegistrationBean import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.http.converter.HttpMessageConverter @@ -30,7 +33,8 @@ import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttribu @Configuration class MvcConfigurer( private val jsonOrFormModelMethodProcessor: JsonOrFormModelMethodProcessor, - private val spaInterceptor: SPAInterceptor + private val spaInterceptor: SPAInterceptor, + private val applicationRequestLogInterceptor: ApplicationRequestLogInterceptor ) : WebMvcConfigurer { override fun addArgumentResolvers(resolvers: MutableList) { resolvers.add(jsonOrFormModelMethodProcessor) @@ -38,6 +42,16 @@ class MvcConfigurer( override fun addInterceptors(registry: InterceptorRegistry) { registry.addInterceptor(spaInterceptor) + registry.addInterceptor(applicationRequestLogInterceptor) + } + + @Bean + fun mdcFilter(): FilterRegistrationBean { + val bean = FilterRegistrationBean() + bean.filter = MDCInsertingServletFilter() + bean.addUrlPatterns("/*"); + bean.order = Int.MIN_VALUE + return bean } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt new file mode 100644 index 00000000..c368f482 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt @@ -0,0 +1,65 @@ +package dev.usbharu.hideout.core.infrastructure.springframework + +import dev.usbharu.hideout.core.infrastructure.springframework.oauth2.HideoutUserDetails +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import org.slf4j.MDC +import org.springframework.security.core.context.SecurityContextHolder +import org.springframework.security.oauth2.jwt.Jwt +import org.springframework.stereotype.Component +import org.springframework.web.servlet.AsyncHandlerInterceptor +import java.util.* + +@Component +class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { + override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { + MDC.put(requestId, UUID.randomUUID().toString()) + + val userDetailId = when (val principal = SecurityContextHolder.getContext().authentication?.principal) { + is HideoutUserDetails -> { + principal.userDetailsId + } + + is Jwt -> { + principal.getClaim("uid")?.toLongOrNull() + } + + else -> { + null + } + } + + if (userDetailId != null) { + MDC.put(userId, userDetailId.toString()) + } + + return true + } + + override fun afterCompletion( + request: HttpServletRequest, + response: HttpServletResponse, + handler: Any, + ex: Exception? + ) { + removeMdc() + } + + override fun afterConcurrentHandlingStarted( + request: HttpServletRequest, + response: HttpServletResponse, + handler: Any + ) { + removeMdc() + } + + private fun removeMdc() { + MDC.remove(requestId) + MDC.remove(userId) + } + + companion object { + const val requestId: String = "requestId" + const val userId: String = "userId" + } +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/oauth2/UserDetailsServiceImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/oauth2/UserDetailsServiceImpl.kt index 5ca4964e..713374bd 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/oauth2/UserDetailsServiceImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/oauth2/UserDetailsServiceImpl.kt @@ -21,6 +21,7 @@ import dev.usbharu.hideout.core.config.ApplicationConfig import dev.usbharu.hideout.core.domain.model.actor.ActorRepository import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.slf4j.MDCContext import org.springframework.security.core.userdetails.UserDetails import org.springframework.security.core.userdetails.UserDetailsService import org.springframework.security.core.userdetails.UsernameNotFoundException @@ -33,7 +34,7 @@ class UserDetailsServiceImpl( private val applicationConfig: ApplicationConfig, private val transaction: Transaction, ) : UserDetailsService { - override fun loadUserByUsername(username: String?): UserDetails = runBlocking { + override fun loadUserByUsername(username: String?): UserDetails = runBlocking(MDCContext()) { if (username == null) { throw UsernameNotFoundException("Username not found") } diff --git a/hideout-core/src/main/resources/logback-spring.xml b/hideout-core/src/main/resources/logback-spring.xml index a29f493e..dfb52736 100644 --- a/hideout-core/src/main/resources/logback-spring.xml +++ b/hideout-core/src/main/resources/logback-spring.xml @@ -1,7 +1,7 @@ - + diff --git a/hideout-mastodon/gradle/wrapper/gradle-wrapper.properties b/hideout-mastodon/gradle/wrapper/gradle-wrapper.properties index 0aaefbca..9355b415 100644 --- a/hideout-mastodon/gradle/wrapper/gradle-wrapper.properties +++ b/hideout-mastodon/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringAccountApi.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringAccountApi.kt index caac2717..a9471098 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringAccountApi.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringAccountApi.kt @@ -49,6 +49,7 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.slf4j.MDCContext import org.springframework.http.ResponseEntity import org.springframework.stereotype.Controller @@ -164,7 +165,7 @@ class SpringAccountApi( id: List?, withSuspended: Boolean ): ResponseEntity> { - val principal = runBlocking { principalContextHolder.getPrincipal() } + val principal = runBlocking(MDCContext()) { principalContextHolder.getPrincipal() } return ResponseEntity.ok(id.orEmpty().asFlow().mapNotNull { fetchRelationship(it, principal).body }) } diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringTimelineApi.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringTimelineApi.kt index 9f259736..f3cc511c 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringTimelineApi.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringTimelineApi.kt @@ -28,6 +28,7 @@ import dev.usbharu.hideout.mastodon.interfaces.api.generated.model.Status import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.slf4j.MDCContext import org.springframework.http.ResponseEntity import org.springframework.stereotype.Controller @@ -44,7 +45,7 @@ class SpringTimelineApi( sinceId: String?, minId: String?, limit: Int? - ): ResponseEntity> = runBlocking { + ): ResponseEntity> = runBlocking(MDCContext()) { val principal = principalContextHolder.getPrincipal() val userDetail = transaction.transaction { userDetailRepository.findByActorId(principal.actorId.id) From 831bb17fcac93d8a0267b59e6422e35b85e53a96 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 13 Sep 2024 02:43:44 +0900 Subject: [PATCH 05/10] =?UTF-8?q?chore:=20=E3=83=AD=E3=82=B0=E3=82=92?= =?UTF-8?q?=E6=94=B9=E5=96=842?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/shared/AbstractApplicationService.kt | 4 ++++ .../springframework/ApplicationRequestLogInterceptor.kt | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt index 9006f5fc..14877a92 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt @@ -19,6 +19,7 @@ package dev.usbharu.hideout.core.application.shared import dev.usbharu.hideout.core.domain.model.support.principal.Principal import kotlinx.coroutines.CancellationException import org.slf4j.Logger +import org.slf4j.MDC abstract class AbstractApplicationService( protected val transaction: Transaction, @@ -26,6 +27,7 @@ abstract class AbstractApplicationService( ) : ApplicationService { override suspend fun execute(command: T, principal: Principal): R { return try { + MDC.put("applicationService", this::class.simpleName) logger.debug("START {}", command::class.simpleName) val response = transaction.transaction { internalExecute(command, principal) @@ -39,6 +41,8 @@ abstract class AbstractApplicationService( } catch (@Suppress("TooGenericExceptionCaught") e: Exception) { logger.warn("Command execution error", e) throw e + } finally { + MDC.remove("applicationService") } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt index c368f482..c6986456 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt @@ -3,6 +3,7 @@ package dev.usbharu.hideout.core.infrastructure.springframework import dev.usbharu.hideout.core.infrastructure.springframework.oauth2.HideoutUserDetails import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse +import org.slf4j.LoggerFactory import org.slf4j.MDC import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.oauth2.jwt.Jwt @@ -14,7 +15,7 @@ import java.util.* class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { MDC.put(requestId, UUID.randomUUID().toString()) - + MDC.put(Companion.handler, handler.toString()) val userDetailId = when (val principal = SecurityContextHolder.getContext().authentication?.principal) { is HideoutUserDetails -> { principal.userDetailsId @@ -29,10 +30,13 @@ class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { } } + + if (userDetailId != null) { MDC.put(userId, userDetailId.toString()) } + logger.info("START") return true } @@ -56,10 +60,13 @@ class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { private fun removeMdc() { MDC.remove(requestId) MDC.remove(userId) + MDC.remove(handler) } companion object { const val requestId: String = "requestId" const val userId: String = "userId" + const val handler: String = "handler" + private val logger = LoggerFactory.getLogger(ApplicationRequestLogInterceptor::class.java) } } \ No newline at end of file From 5969371a40d037cf6f18b5080e9bfda82316e8b0 Mon Sep 17 00:00:00 2001 From: usbharu Date: Thu, 12 Sep 2024 17:53:29 +0000 Subject: [PATCH 06/10] style: fix lint (CI) --- .../hideout/core/config/SpringMvcConfig.kt | 2 +- .../ApplicationRequestLogInterceptor.kt | 4 +- .../timeline/MastodonReadTimeline.kt | 4 +- .../MastodonReadTimelineApplicationService.kt | 135 +++++++++--------- .../interfaces/api/SpringTimelineApi.kt | 3 +- 5 files changed, 74 insertions(+), 74 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SpringMvcConfig.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SpringMvcConfig.kt index 0294603a..696e7fca 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SpringMvcConfig.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SpringMvcConfig.kt @@ -49,7 +49,7 @@ class MvcConfigurer( fun mdcFilter(): FilterRegistrationBean { val bean = FilterRegistrationBean() bean.filter = MDCInsertingServletFilter() - bean.addUrlPatterns("/*"); + bean.addUrlPatterns("/*") bean.order = Int.MIN_VALUE return bean } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt index c6986456..9f42749b 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/ApplicationRequestLogInterceptor.kt @@ -30,8 +30,6 @@ class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { } } - - if (userDetailId != null) { MDC.put(userId, userDetailId.toString()) } @@ -69,4 +67,4 @@ class ApplicationRequestLogInterceptor : AsyncHandlerInterceptor { const val handler: String = "handler" private val logger = LoggerFactory.getLogger(ApplicationRequestLogInterceptor::class.java) } -} \ No newline at end of file +} diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimeline.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimeline.kt index 4d76bda5..737cfabf 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimeline.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimeline.kt @@ -8,6 +8,4 @@ class MastodonReadTimeline( val localOnly: Boolean, val remoteOnly: Boolean, val page: Page -) { - -} +) diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimelineApplicationService.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimelineApplicationService.kt index be0319c8..770cb239 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimelineApplicationService.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimelineApplicationService.kt @@ -38,75 +38,78 @@ class MastodonReadTimelineApplicationService( val readTimeline = timelineStore.readTimeline(timeline, readTimelineOption, command.page, principal) - return PaginationList(readTimeline.map { - Status( - it.postId.id.toString(), - it.post.url.toString(), - it.post.createdAt.toString(), - account = Account( - id = it.postActor.id.id.toString(), - username = it.postActor.name.name, - acct = Acct(it.postActor.name.name, it.postActor.domain.domain).toString(), - url = it.postActor.url.toString(), - displayName = it.postActor.screenName.screenName, - note = it.postActor.description.description, - avatar = it.postActorIconMedia?.url.toString(), - avatarStatic = it.postActorIconMedia?.thumbnailUrl.toString(), - header = "", - headerStatic = "", - locked = false, - fields = emptyList(), + return PaginationList( + readTimeline.map { + Status( + it.postId.id.toString(), + it.post.url.toString(), + it.post.createdAt.toString(), + account = Account( + id = it.postActor.id.id.toString(), + username = it.postActor.name.name, + acct = Acct(it.postActor.name.name, it.postActor.domain.domain).toString(), + url = it.postActor.url.toString(), + displayName = it.postActor.screenName.screenName, + note = it.postActor.description.description, + avatar = it.postActorIconMedia?.url.toString(), + avatarStatic = it.postActorIconMedia?.thumbnailUrl.toString(), + header = "", + headerStatic = "", + locked = false, + fields = emptyList(), + emojis = emptyList(), + bot = false, + group = false, + discoverable = true, + createdAt = it.postActor.createdAt.toString(), + statusesCount = it.postActor.postsCount.postsCount, + noindex = true, + moved = it.postActor.moveTo != null, + suspended = it.postActor.suspend, + limited = false, + lastStatusAt = it.postActor.lastPostAt?.toString(), + followersCount = it.postActor.followersCount?.relationshipCount, + followingCount = it.postActor.followingCount?.relationshipCount, + ), + content = it.post.content.content, + visibility = when (it.post.visibility) { + PUBLIC -> Status.Visibility.public + UNLISTED -> Status.Visibility.unlisted + FOLLOWERS -> Status.Visibility.private + DIRECT -> Status.Visibility.direct + }, + sensitive = it.post.sensitive, + spoilerText = it.post.overview?.overview.orEmpty(), + mediaAttachments = it.postMedias.map { MediaAttachment(it.id.id.toString()) }, + mentions = emptyList(), + tags = emptyList(), emojis = emptyList(), - bot = false, - group = false, - discoverable = true, - createdAt = it.postActor.createdAt.toString(), - statusesCount = it.postActor.postsCount.postsCount, - noindex = true, - moved = it.postActor.moveTo != null, - suspended = it.postActor.suspend, - limited = false, - lastStatusAt = it.postActor.lastPostAt?.toString(), - followersCount = it.postActor.followersCount?.relationshipCount, - followingCount = it.postActor.followingCount?.relationshipCount, - ), - content = it.post.content.content, - visibility = when (it.post.visibility) { - PUBLIC -> Status.Visibility.public - UNLISTED -> Status.Visibility.unlisted - FOLLOWERS -> Status.Visibility.private - DIRECT -> Status.Visibility.direct - }, - sensitive = it.post.sensitive, - spoilerText = it.post.overview?.overview.orEmpty(), - mediaAttachments = it.postMedias.map { MediaAttachment(it.id.id.toString()) }, - mentions = emptyList(), - tags = emptyList(), - emojis = emptyList(), - reblogsCount = 0, - favouritesCount = it.reactionsList.sumOf { it.count }, - repliesCount = 0, - url = it.post.url.toString(), - text = it.post.content.text, - application = null, - inReplyToId = it.replyPost?.id?.toString(), - inReplyToAccountId = it.replyPostActor?.id?.toString(), - reblog = null, - poll = null, - card = null, - language = null, - editedAt = null, - favourited = it.favourited, - reblogged = false, - muted = false, - bookmarked = false, - pinned = false, - filtered = emptyList(), - ) - }, readTimeline.next?.id, readTimeline.prev?.id) + reblogsCount = 0, + favouritesCount = it.reactionsList.sumOf { it.count }, + repliesCount = 0, + url = it.post.url.toString(), + text = it.post.content.text, + application = null, + inReplyToId = it.replyPost?.id?.toString(), + inReplyToAccountId = it.replyPostActor?.id?.toString(), + reblog = null, + poll = null, + card = null, + language = null, + editedAt = null, + favourited = it.favourited, + reblogged = false, + muted = false, + bookmarked = false, + pinned = false, + filtered = emptyList(), + ) + }, + readTimeline.next?.id, readTimeline.prev?.id + ) } companion object { private val logger = LoggerFactory.getLogger(MastodonReadTimelineApplicationService::class.java) } -} \ No newline at end of file +} diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringTimelineApi.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringTimelineApi.kt index f3cc511c..081eae11 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringTimelineApi.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringTimelineApi.kt @@ -68,7 +68,8 @@ class SpringTimelineApi( minId?.toLongOrNull(), limit ) - ), principal + ), + principal ).asFlow() ) } From d74101a2b621e8dae4c9cb31600de2a86a721267 Mon Sep 17 00:00:00 2001 From: usbharu Date: Thu, 12 Sep 2024 17:57:49 +0000 Subject: [PATCH 07/10] style: fix lint (CI) --- .../timeline/MastodonReadTimelineApplicationService.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimelineApplicationService.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimelineApplicationService.kt index 770cb239..2390c841 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimelineApplicationService.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/timeline/MastodonReadTimelineApplicationService.kt @@ -105,7 +105,8 @@ class MastodonReadTimelineApplicationService( filtered = emptyList(), ) }, - readTimeline.next?.id, readTimeline.prev?.id + readTimeline.next?.id, + readTimeline.prev?.id ) } From 1c809113651905cb92c05aa58f9af98f095e1657 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 13 Sep 2024 13:40:52 +0900 Subject: [PATCH 08/10] =?UTF-8?q?feat:=20=E3=83=89=E3=83=A1=E3=82=A4?= =?UTF-8?q?=E3=83=B3=E3=82=A4=E3=83=99=E3=83=B3=E3=83=88=E3=81=AE=E3=82=B5?= =?UTF-8?q?=E3=83=96=E3=82=B9=E3=82=AF=E3=83=A9=E3=82=A4=E3=83=90=E3=83=BC?= =?UTF-8?q?=E4=B8=80=E8=A6=A7=E3=82=92=E5=87=BA=E5=8A=9B=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- develop/docker-compose.yml | 2 - .../subscribers/DomainEventSubscriber.kt | 1 + ...isterLocalUserSetHomeTimelineSubscriber.kt | 17 +++++--- ...isterTimelineSetTimelineStoreSubscriber.kt | 11 +++-- .../subscribers/SubscriberRunner.kt | 37 +++++++++++++++- .../TimelinePostCreateSubscriber.kt | 11 +++-- .../TimelineRelationshipFollowSubscriber.kt | 40 +++++++++-------- .../TimelineRelationshipUnfollowSubscriber.kt | 43 ++++++++++--------- .../SpringFrameworkDomainEventPublisher.kt | 12 +++++- .../SpringFrameworkDomainEventSubscriber.kt | 43 +++++++++++++++---- 10 files changed, 151 insertions(+), 66 deletions(-) diff --git a/develop/docker-compose.yml b/develop/docker-compose.yml index 4abaed5f..630152e8 100644 --- a/develop/docker-compose.yml +++ b/develop/docker-compose.yml @@ -30,7 +30,6 @@ services: - "9200:9200" volumes: - elasticsearch-data:/usr/share/elasticsearch/data - restart: always kibana: image: docker.elastic.co/kibana/kibana:7.17.24 container_name: kibana @@ -42,7 +41,6 @@ services: - ELASTICSEARCH_PASSWORD=Passw0rd depends_on: - elasticsearch - restart: always fluentd: build: ./fluentd diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/DomainEventSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/DomainEventSubscriber.kt index 364bf92a..af70e1ef 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/DomainEventSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/DomainEventSubscriber.kt @@ -5,6 +5,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody interface DomainEventSubscriber { fun subscribe(eventName: String, domainEventConsumer: DomainEventConsumer) + fun getSubscribers(): Map>> } typealias DomainEventConsumer = suspend (DomainEvent) -> Unit diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/RegisterLocalUserSetHomeTimelineSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/RegisterLocalUserSetHomeTimelineSubscriber.kt index a63918ee..776c040c 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/RegisterLocalUserSetHomeTimelineSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/RegisterLocalUserSetHomeTimelineSubscriber.kt @@ -3,19 +3,22 @@ package dev.usbharu.hideout.core.application.domainevent.subscribers import dev.usbharu.hideout.core.domain.event.userdetail.UserDetailEvent import dev.usbharu.hideout.core.domain.event.userdetail.UserDetailEventBody import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous +import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent import org.springframework.stereotype.Component @Component class RegisterLocalUserSetHomeTimelineSubscriber( private val domainEventSubscriber: DomainEventSubscriber, private val userRegisterHomeTimelineApplicationService: UserRegisterHomeTimelineApplicationService -) : Subscriber { +) : Subscriber, DomainEventConsumer { override fun init() { - domainEventSubscriber.subscribe(UserDetailEvent.CREATE.eventName) { - userRegisterHomeTimelineApplicationService.execute( - RegisterHomeTimeline(it.body.getUserDetail().id), - Anonymous - ) - } + domainEventSubscriber.subscribe(UserDetailEvent.CREATE.eventName, this) + } + + override suspend fun invoke(p1: DomainEvent) { + userRegisterHomeTimelineApplicationService.execute( + RegisterHomeTimeline(p1.body.getUserDetail().id), + Anonymous + ) } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/RegisterTimelineSetTimelineStoreSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/RegisterTimelineSetTimelineStoreSubscriber.kt index 2b8ec1cb..05c92128 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/RegisterTimelineSetTimelineStoreSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/RegisterTimelineSetTimelineStoreSubscriber.kt @@ -5,17 +5,20 @@ import dev.usbharu.hideout.core.application.timeline.SetTimleineStore import dev.usbharu.hideout.core.domain.event.timeline.TimelineEvent import dev.usbharu.hideout.core.domain.event.timeline.TimelineEventBody import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous +import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent import org.springframework.stereotype.Component @Component class RegisterTimelineSetTimelineStoreSubscriber( private val domainEventSubscriber: DomainEventSubscriber, private val setTimelineToTimelineStoreApplicationService: SetTimelineToTimelineStoreApplicationService -) : Subscriber { +) : Subscriber, DomainEventConsumer { override fun init() { - domainEventSubscriber.subscribe(TimelineEvent.CREATE.eventName) { - setTimelineToTimelineStoreApplicationService.execute(SetTimleineStore(it.body.getTimelineId()), Anonymous) - } + domainEventSubscriber.subscribe(TimelineEvent.CREATE.eventName, this) + } + + override suspend fun invoke(p1: DomainEvent) { + setTimelineToTimelineStoreApplicationService.execute(SetTimleineStore(p1.body.getTimelineId()), Anonymous) } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt index 1990c2f6..fe04ccbc 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt @@ -1,12 +1,47 @@ package dev.usbharu.hideout.core.application.domainevent.subscribers +import org.slf4j.LoggerFactory import org.springframework.boot.ApplicationArguments import org.springframework.boot.ApplicationRunner import org.springframework.stereotype.Component @Component -class SubscriberRunner(private val subscribers: List) : ApplicationRunner { +class SubscriberRunner( + private val subscribers: List, + private val domainEventSubscriber: DomainEventSubscriber +) : ApplicationRunner { override fun run(args: ApplicationArguments?) { subscribers.forEach { it.init() } + + if (logger.isDebugEnabled) { + val stringListMap = domainEventSubscriber.getSubscribers() + + val header = """ + |====== Domain Event Subscribers Report ===== + | + | + | + """.trimMargin() + + val value = stringListMap.map { + it.key + "\n\t" + it.value.joinToString("\n", "[", "]") { suspendFunction1 -> + suspendFunction1::class.qualifiedName.orEmpty() + } + }.joinToString("\n\n\n") + + val footer = """ + | + | + | + |===== Domain Event Subscribers Report ===== + """.trimMargin() + + logger.debug("{}{}{}", header, value, footer) + } + + } + + companion object { + private val logger = LoggerFactory.getLogger(SubscriberRunner::class.java) } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelinePostCreateSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelinePostCreateSubscriber.kt index 663367b2..3252e2bc 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelinePostCreateSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelinePostCreateSubscriber.kt @@ -5,16 +5,19 @@ import dev.usbharu.hideout.core.application.timeline.TimelineAddPostApplicationS import dev.usbharu.hideout.core.domain.event.post.PostEvent import dev.usbharu.hideout.core.domain.event.post.PostEventBody import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous +import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent import org.springframework.stereotype.Component @Component class TimelinePostCreateSubscriber( private val timelineAddPostApplicationService: TimelineAddPostApplicationService, private val domainEventSubscriber: DomainEventSubscriber, -) : Subscriber { +) : Subscriber, DomainEventConsumer { override fun init() { - domainEventSubscriber.subscribe(PostEvent.CREATE.eventName) { - timelineAddPostApplicationService.execute(AddPost(it.body.getPostId()), Anonymous) - } + domainEventSubscriber.subscribe(PostEvent.CREATE.eventName, this) + } + + override suspend fun invoke(p1: DomainEvent) { + timelineAddPostApplicationService.execute(AddPost(p1.body.getPostId()), Anonymous) } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt index 0707c496..cbef86c0 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt @@ -7,6 +7,7 @@ import dev.usbharu.hideout.core.domain.event.relationship.RelationshipEvent import dev.usbharu.hideout.core.domain.event.relationship.RelationshipEventBody import dev.usbharu.hideout.core.domain.model.timelinerelationship.Visible import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository +import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent import org.slf4j.LoggerFactory import org.springframework.stereotype.Component @@ -15,28 +16,31 @@ class TimelineRelationshipFollowSubscriber( private val userAddTimelineRelationshipApplicationService: UserAddTimelineRelationshipApplicationService, private val userDetailRepository: UserDetailRepository, private val domainEventSubscriber: DomainEventSubscriber -) : Subscriber { +) : Subscriber, DomainEventConsumer { override fun init() { - domainEventSubscriber.subscribe(RelationshipEvent.ACCEPT_FOLLOW.eventName) { - val relationship = it.body.getRelationship() - val userDetail = userDetailRepository.findByActorId(relationship.actorId.id) - ?: throw InternalServerException("Userdetail ${relationship.actorId} not found by actorid.") - if (userDetail.homeTimelineId == null) { - logger.warn("Home timeline for ${relationship.actorId} is not found") - return@subscribe - } + domainEventSubscriber.subscribe(RelationshipEvent.ACCEPT_FOLLOW.eventName, this) + } - @Suppress("UnsafeCallOnNullableType") - userAddTimelineRelationshipApplicationService.execute( - AddTimelineRelationship( - userDetail.homeTimelineId!!, - relationship.targetActorId, - Visible.FOLLOWERS - ), - it.body.principal - ) + + override suspend fun invoke(p1: DomainEvent) { + val relationship = p1.body.getRelationship() + val userDetail = userDetailRepository.findByActorId(relationship.actorId.id) + ?: throw InternalServerException("Userdetail ${relationship.actorId} not found by actorid.") + if (userDetail.homeTimelineId == null) { + logger.warn("Home timeline for ${relationship.actorId} is not found") + return } + + @Suppress("UnsafeCallOnNullableType") + userAddTimelineRelationshipApplicationService.execute( + AddTimelineRelationship( + userDetail.homeTimelineId!!, + relationship.targetActorId, + Visible.FOLLOWERS + ), + p1.body.principal + ) } companion object { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipUnfollowSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipUnfollowSubscriber.kt index 0430a7a2..0fc5b665 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipUnfollowSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipUnfollowSubscriber.kt @@ -6,6 +6,7 @@ import dev.usbharu.hideout.core.domain.event.relationship.RelationshipEvent import dev.usbharu.hideout.core.domain.event.relationship.RelationshipEventBody import dev.usbharu.hideout.core.domain.model.timelinerelationship.TimelineRelationshipRepository import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository +import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent import org.slf4j.LoggerFactory import org.springframework.stereotype.Component @@ -15,29 +16,31 @@ class TimelineRelationshipUnfollowSubscriber( private val userRemoveTimelineRelationshipApplicationService: UserRemoveTimelineRelationshipApplicationService, private val userDetailRepository: UserDetailRepository, private val timelineRelationshipRepository: TimelineRelationshipRepository, -) : Subscriber { +) : Subscriber, DomainEventConsumer { override fun init() { - domainEventSubscriber.subscribe(RelationshipEvent.UNFOLLOW.eventName) { - val relationship = it.body.getRelationship() - val userDetail = userDetailRepository.findByActorId(relationship.actorId.id) ?: throw IllegalStateException( - "UserDetail ${relationship.actorId} not found by actorId." - ) - if (userDetail.homeTimelineId == null) { - logger.warn("HomeTimeline for ${userDetail.id} not found.") - return@subscribe - } + domainEventSubscriber.subscribe(RelationshipEvent.UNFOLLOW.eventName, this) + } - val timelineRelationship = timelineRelationshipRepository.findByTimelineIdAndActorId( - userDetail.homeTimelineId!!, - relationship.targetActorId - ) - ?: throw IllegalStateException("TimelineRelationship ${userDetail.homeTimelineId} to ${relationship.targetActorId} not found by timelineId and ActorId") - - userRemoveTimelineRelationshipApplicationService.execute( - RemoveTimelineRelationship(timelineRelationship.id), - it.body.principal - ) + override suspend fun invoke(p1: DomainEvent) { + val relationship = p1.body.getRelationship() + val userDetail = userDetailRepository.findByActorId(relationship.actorId.id) ?: throw IllegalStateException( + "UserDetail ${relationship.actorId} not found by actorId." + ) + if (userDetail.homeTimelineId == null) { + logger.warn("HomeTimeline for ${userDetail.id} not found.") + return } + + val timelineRelationship = timelineRelationshipRepository.findByTimelineIdAndActorId( + userDetail.homeTimelineId!!, + relationship.targetActorId + ) + ?: throw IllegalStateException("TimelineRelationship ${userDetail.homeTimelineId} to ${relationship.targetActorId} not found by timelineId and ActorId") + + userRemoveTimelineRelationshipApplicationService.execute( + RemoveTimelineRelationship(timelineRelationship.id), + p1.body.principal + ) } companion object { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt index bf442ea2..b278e1d9 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt @@ -18,7 +18,9 @@ package dev.usbharu.hideout.core.infrastructure.springframework.domainevent import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher +import dev.usbharu.hideout.core.infrastructure.springframework.ApplicationRequestLogInterceptor import org.slf4j.LoggerFactory +import org.slf4j.MDC import org.springframework.context.ApplicationEventPublisher import org.springframework.stereotype.Component @@ -27,10 +29,18 @@ class SpringFrameworkDomainEventPublisher(private val applicationEventPublisher: DomainEventPublisher { override suspend fun publishEvent(domainEvent: DomainEvent<*>) { logger.trace("Publish ${domainEvent.id} ${domainEvent.name}") - applicationEventPublisher.publishEvent(domainEvent) + + val requestId: String? = MDC.get(ApplicationRequestLogInterceptor.requestId) + val springDomainEvent = SpringDomainEvent( + requestId, domainEvent + ) + + applicationEventPublisher.publishEvent(springDomainEvent) } companion object { private val logger = LoggerFactory.getLogger(SpringFrameworkDomainEventPublisher::class.java) } } + +data class SpringDomainEvent(val requestId: String?, val domainEvent: DomainEvent<*>) \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt index 184cab62..c0617ec0 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt @@ -2,9 +2,14 @@ package dev.usbharu.hideout.core.infrastructure.springframework.domainevent import dev.usbharu.hideout.core.application.domainevent.subscribers.DomainEventConsumer import dev.usbharu.hideout.core.application.domainevent.subscribers.DomainEventSubscriber -import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody +import dev.usbharu.hideout.core.infrastructure.springframework.ApplicationRequestLogInterceptor.Companion.requestId +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.slf4j.MDCContext import org.slf4j.LoggerFactory +import org.slf4j.MDC import org.springframework.context.event.EventListener import org.springframework.stereotype.Component @@ -17,15 +22,35 @@ class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber { map.getOrPut(eventName) { mutableListOf() }.add(domainEventConsumer as DomainEventConsumer<*>) } + override fun getSubscribers(): Map>> { + return map + } + @EventListener - suspend fun onDomainEventPublished(domainEvent: DomainEvent<*>) { - logger.trace("Domain Event Published: $domainEvent") - map[domainEvent.name]?.forEach { - try { - it.invoke(domainEvent) - } catch (@Suppress("TooGenericExceptionCaught") e: Exception) { - logger.error("", e) - } + suspend fun onDomainEventPublished(domainEvent: SpringDomainEvent) { + + logger.debug( + "Domain Event Published: {} id: {} requestId: {}", + domainEvent.domainEvent.name, + domainEvent.domainEvent.id, + domainEvent.requestId + ) + coroutineScope { + + map[domainEvent.domainEvent.name]?.map { + async(MDCContext()) { + try { + MDC.put(requestId, domainEvent.requestId) + it.invoke(domainEvent.domainEvent) + } catch (@Suppress("TooGenericExceptionCaught") e: Exception) { + logger.warn("", e) + null + } finally { + MDC.remove(requestId) + } + } + }?.awaitAll() + } } From f82680236ea0bff13ae8274c547ffe852743cfa7 Mon Sep 17 00:00:00 2001 From: usbharu Date: Fri, 13 Sep 2024 04:46:26 +0000 Subject: [PATCH 09/10] style: fix lint (CI) --- .../application/domainevent/subscribers/SubscriberRunner.kt | 1 - .../subscribers/TimelineRelationshipFollowSubscriber.kt | 1 - .../domainevent/SpringFrameworkDomainEventPublisher.kt | 5 +++-- .../domainevent/SpringFrameworkDomainEventSubscriber.kt | 3 --- 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt index fe04ccbc..7a7caedf 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt @@ -38,7 +38,6 @@ class SubscriberRunner( logger.debug("{}{}{}", header, value, footer) } - } companion object { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt index cbef86c0..c58b1b0f 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt @@ -22,7 +22,6 @@ class TimelineRelationshipFollowSubscriber( domainEventSubscriber.subscribe(RelationshipEvent.ACCEPT_FOLLOW.eventName, this) } - override suspend fun invoke(p1: DomainEvent) { val relationship = p1.body.getRelationship() val userDetail = userDetailRepository.findByActorId(relationship.actorId.id) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt index b278e1d9..82213901 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt @@ -32,7 +32,8 @@ class SpringFrameworkDomainEventPublisher(private val applicationEventPublisher: val requestId: String? = MDC.get(ApplicationRequestLogInterceptor.requestId) val springDomainEvent = SpringDomainEvent( - requestId, domainEvent + requestId, + domainEvent ) applicationEventPublisher.publishEvent(springDomainEvent) @@ -43,4 +44,4 @@ class SpringFrameworkDomainEventPublisher(private val applicationEventPublisher: } } -data class SpringDomainEvent(val requestId: String?, val domainEvent: DomainEvent<*>) \ No newline at end of file +data class SpringDomainEvent(val requestId: String?, val domainEvent: DomainEvent<*>) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt index c0617ec0..ffac11ae 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt @@ -28,7 +28,6 @@ class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber { @EventListener suspend fun onDomainEventPublished(domainEvent: SpringDomainEvent) { - logger.debug( "Domain Event Published: {} id: {} requestId: {}", domainEvent.domainEvent.name, @@ -36,7 +35,6 @@ class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber { domainEvent.requestId ) coroutineScope { - map[domainEvent.domainEvent.name]?.map { async(MDCContext()) { try { @@ -50,7 +48,6 @@ class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber { } } }?.awaitAll() - } } From 9ed85bae07a8a4b004948aef0920cd06a1bb1cdb Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 13 Sep 2024 13:55:06 +0900 Subject: [PATCH 10/10] style: fix lint --- .../domainevent/subscribers/SubscriberRunner.kt | 1 - .../subscribers/TimelineRelationshipFollowSubscriber.kt | 1 - .../domainevent/SpringFrameworkDomainEventPublisher.kt | 5 +++-- .../domainevent/SpringFrameworkDomainEventSubscriber.kt | 7 +------ 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt index fe04ccbc..7a7caedf 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/SubscriberRunner.kt @@ -38,7 +38,6 @@ class SubscriberRunner( logger.debug("{}{}{}", header, value, footer) } - } companion object { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt index cbef86c0..c58b1b0f 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/domainevent/subscribers/TimelineRelationshipFollowSubscriber.kt @@ -22,7 +22,6 @@ class TimelineRelationshipFollowSubscriber( domainEventSubscriber.subscribe(RelationshipEvent.ACCEPT_FOLLOW.eventName, this) } - override suspend fun invoke(p1: DomainEvent) { val relationship = p1.body.getRelationship() val userDetail = userDetailRepository.findByActorId(relationship.actorId.id) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt index b278e1d9..82213901 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventPublisher.kt @@ -32,7 +32,8 @@ class SpringFrameworkDomainEventPublisher(private val applicationEventPublisher: val requestId: String? = MDC.get(ApplicationRequestLogInterceptor.requestId) val springDomainEvent = SpringDomainEvent( - requestId, domainEvent + requestId, + domainEvent ) applicationEventPublisher.publishEvent(springDomainEvent) @@ -43,4 +44,4 @@ class SpringFrameworkDomainEventPublisher(private val applicationEventPublisher: } } -data class SpringDomainEvent(val requestId: String?, val domainEvent: DomainEvent<*>) \ No newline at end of file +data class SpringDomainEvent(val requestId: String?, val domainEvent: DomainEvent<*>) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt index c0617ec0..4230303a 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/domainevent/SpringFrameworkDomainEventSubscriber.kt @@ -22,13 +22,10 @@ class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber { map.getOrPut(eventName) { mutableListOf() }.add(domainEventConsumer as DomainEventConsumer<*>) } - override fun getSubscribers(): Map>> { - return map - } + override fun getSubscribers(): Map>> = map @EventListener suspend fun onDomainEventPublished(domainEvent: SpringDomainEvent) { - logger.debug( "Domain Event Published: {} id: {} requestId: {}", domainEvent.domainEvent.name, @@ -36,7 +33,6 @@ class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber { domainEvent.requestId ) coroutineScope { - map[domainEvent.domainEvent.name]?.map { async(MDCContext()) { try { @@ -50,7 +46,6 @@ class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber { } } }?.awaitAll() - } }