From 4b935b8c1261a13ba887f1455b6b8735ab52f58d Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 2 Nov 2023 19:55:44 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E3=82=BF=E3=82=A4=E3=83=A0=E3=83=A9?= =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=81=8C=E9=87=8D=E8=A4=87=E3=81=99=E3=82=8B?= =?UTF-8?q?=E7=8F=BE=E8=B1=A1=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/model/post/PostRepository.kt | 2 +- .../hideout/core/domain/model/timeline/Timeline.kt | 2 ++ .../exposedrepository/PostRepositoryImpl.kt | 4 ++-- .../hideout/core/service/post/PostServiceImpl.kt | 13 +++++++++---- src/main/resources/application.yml | 1 + 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostRepository.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostRepository.kt index f3ea8dce..f2feb6f0 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostRepository.kt @@ -6,7 +6,7 @@ import org.springframework.stereotype.Repository @Repository interface PostRepository { suspend fun generateId(): Long - suspend fun save(post: Post): Post + suspend fun save(post: Post): Boolean suspend fun delete(id: Long) suspend fun findById(id: Long): Post } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/timeline/Timeline.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/timeline/Timeline.kt index 6739bd79..5a08816e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/timeline/Timeline.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/timeline/Timeline.kt @@ -2,9 +2,11 @@ package dev.usbharu.hideout.core.domain.model.timeline import dev.usbharu.hideout.core.domain.model.post.Visibility import org.springframework.data.annotation.Id +import org.springframework.data.mongodb.core.index.CompoundIndex import org.springframework.data.mongodb.core.mapping.Document @Document +@CompoundIndex(def = "{'userId':1,'timelineId':1,'postId':1}", unique = true) data class Timeline( @Id val id: Long, diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/PostRepositoryImpl.kt index 32064e6c..39ddff21 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/PostRepositoryImpl.kt @@ -17,7 +17,7 @@ class PostRepositoryImpl( override suspend fun generateId(): Long = idGenerateService.generateId() - override suspend fun save(post: Post): Post { + override suspend fun save(post: Post): Boolean { val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull() if (singleOrNull == null) { Posts.insert { @@ -63,7 +63,7 @@ class PostRepositoryImpl( "Faild to insert" } - return post + return singleOrNull == null } override suspend fun findById(id: Long): Post = diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostServiceImpl.kt index c1f21f21..4876699f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostServiceImpl.kt @@ -1,5 +1,6 @@ package dev.usbharu.hideout.core.service.post +import com.mongodb.DuplicateKeyException import dev.usbharu.hideout.activitypub.service.activity.create.ApSendCreateService import dev.usbharu.hideout.core.domain.exception.UserNotFoundException import dev.usbharu.hideout.core.domain.model.post.Post @@ -38,13 +39,17 @@ class PostServiceImpl( } private suspend fun internalCreate(post: Post, isLocal: Boolean): Post { - val save = try { - postRepository.save(post) + return try { + if (postRepository.save(post)) { + try { + timelineService.publishTimeline(post, isLocal) + } catch (_: DuplicateKeyException) { + } + } + post } catch (_: ExposedSQLException) { postQueryService.findByApId(post.apId) } - timelineService.publishTimeline(save, isLocal) - return save } private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 2d4e4d4b..b792d06d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -24,6 +24,7 @@ spring: password: "" data: mongodb: + auto-index-creation: true host: localhost port: 27017 database: hideout