fix: タイムラインが重複する現象を修正

This commit is contained in:
usbharu 2023-11-02 19:55:44 +09:00
parent 38b34b0424
commit 4b935b8c12
5 changed files with 15 additions and 7 deletions

View File

@ -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
}

View File

@ -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,

View File

@ -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 =

View File

@ -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 {

View File

@ -24,6 +24,7 @@ spring:
password: ""
data:
mongodb:
auto-index-creation: true
host: localhost
port: 27017
database: hideout