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 @Repository
interface PostRepository { interface PostRepository {
suspend fun generateId(): Long suspend fun generateId(): Long
suspend fun save(post: Post): Post suspend fun save(post: Post): Boolean
suspend fun delete(id: Long) suspend fun delete(id: Long)
suspend fun findById(id: Long): Post 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 dev.usbharu.hideout.core.domain.model.post.Visibility
import org.springframework.data.annotation.Id import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.index.CompoundIndex
import org.springframework.data.mongodb.core.mapping.Document import org.springframework.data.mongodb.core.mapping.Document
@Document @Document
@CompoundIndex(def = "{'userId':1,'timelineId':1,'postId':1}", unique = true)
data class Timeline( data class Timeline(
@Id @Id
val id: Long, val id: Long,

View File

@ -17,7 +17,7 @@ class PostRepositoryImpl(
override suspend fun generateId(): Long = idGenerateService.generateId() 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() val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull()
if (singleOrNull == null) { if (singleOrNull == null) {
Posts.insert { Posts.insert {
@ -63,7 +63,7 @@ class PostRepositoryImpl(
"Faild to insert" "Faild to insert"
} }
return post return singleOrNull == null
} }
override suspend fun findById(id: Long): Post = override suspend fun findById(id: Long): Post =

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.service.post package dev.usbharu.hideout.core.service.post
import com.mongodb.DuplicateKeyException
import dev.usbharu.hideout.activitypub.service.activity.create.ApSendCreateService import dev.usbharu.hideout.activitypub.service.activity.create.ApSendCreateService
import dev.usbharu.hideout.core.domain.exception.UserNotFoundException import dev.usbharu.hideout.core.domain.exception.UserNotFoundException
import dev.usbharu.hideout.core.domain.model.post.Post import dev.usbharu.hideout.core.domain.model.post.Post
@ -38,13 +39,17 @@ class PostServiceImpl(
} }
private suspend fun internalCreate(post: Post, isLocal: Boolean): Post { private suspend fun internalCreate(post: Post, isLocal: Boolean): Post {
val save = try { return try {
postRepository.save(post) if (postRepository.save(post)) {
try {
timelineService.publishTimeline(post, isLocal)
} catch (_: DuplicateKeyException) {
}
}
post
} catch (_: ExposedSQLException) { } catch (_: ExposedSQLException) {
postQueryService.findByApId(post.apId) postQueryService.findByApId(post.apId)
} }
timelineService.publishTimeline(save, isLocal)
return save
} }
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post { private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {

View File

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