diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Timeline.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Timeline.kt index a7536851..e8e591ea 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Timeline.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Timeline.kt @@ -1,7 +1,6 @@ package dev.usbharu.hideout.domain.model.hideout.entity import org.springframework.data.annotation.Id -import java.time.Instant data class Timeline( @Id @@ -10,9 +9,9 @@ data class Timeline( val timelineId: Long, val postId: Long, val postUserId: Long, - val createdAt: Instant, - val replyId: Long, - val repostId: Long, + val createdAt: Long, + val replyId: Long?, + val repostId: Long?, val visibility: Visibility, val sensitive: Boolean ) diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/MongoTimelineRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/MongoTimelineRepository.kt index 7077fe99..716609f5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/MongoTimelineRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/MongoTimelineRepository.kt @@ -3,7 +3,9 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.Timeline import org.springframework.data.mongodb.repository.MongoRepository -interface MongoTimelineRepository : TimelineRepository, MongoRepository { - override fun findByUserId(id: Long): List - override fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List +interface MongoTimelineRepository : MongoRepository { + + + fun findByUserId(id: Long): List + fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/MongoTimelineRepositoryWrapper.kt b/src/main/kotlin/dev/usbharu/hideout/repository/MongoTimelineRepositoryWrapper.kt new file mode 100644 index 00000000..98674637 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/repository/MongoTimelineRepositoryWrapper.kt @@ -0,0 +1,38 @@ +package dev.usbharu.hideout.repository + +import dev.usbharu.hideout.domain.model.hideout.entity.Timeline +import dev.usbharu.hideout.service.core.IdGenerateService +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.springframework.stereotype.Repository + +@Repository +class MongoTimelineRepositoryWrapper( + private val mongoTimelineRepository: MongoTimelineRepository, + private val idGenerateService: IdGenerateService +) : + TimelineRepository { + override suspend fun generateId(): Long = idGenerateService.generateId() + + override suspend fun save(timeline: Timeline): Timeline { + return withContext(Dispatchers.IO) { + mongoTimelineRepository.save(timeline) + } + } + + override suspend fun saveAll(timelines: List): List { + return mongoTimelineRepository.saveAll(timelines) + } + + override suspend fun findByUserId(id: Long): List { + return withContext(Dispatchers.IO) { + mongoTimelineRepository.findByUserId(id) + } + } + + override suspend fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List { + return withContext(Dispatchers.IO) { + mongoTimelineRepository.findByUserIdAndTimelineId(userId, timelineId) + } + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/TimelineRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/TimelineRepository.kt index 80b88278..76e9755c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/TimelineRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/TimelineRepository.kt @@ -3,6 +3,9 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.Timeline interface TimelineRepository { - fun findByUserId(id: Long): List - fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List + suspend fun generateId(): Long + suspend fun save(timeline: Timeline): Timeline + suspend fun saveAll(timelines: List): List + suspend fun findByUserId(id: Long): List + suspend fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List } 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 d0f91b25..f2531442 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt @@ -13,6 +13,7 @@ import java.util.* class PostServiceImpl( private val postRepository: PostRepository, private val userRepository: UserRepository, + private val timelineService: TimelineService ) : PostService { private val interceptors = Collections.synchronizedList(mutableListOf()) @@ -31,7 +32,7 @@ class PostServiceImpl( } private suspend fun internalCreate(post: Post): Post { - + timelineService.publishTimeline(post) return postRepository.save(post) } @@ -49,6 +50,6 @@ class PostServiceImpl( repostId = null, replyId = null ) - return internalCreate(post) + return internalCreate(createPost) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/post/TimelineService.kt b/src/main/kotlin/dev/usbharu/hideout/service/post/TimelineService.kt new file mode 100644 index 00000000..681893f7 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/TimelineService.kt @@ -0,0 +1,31 @@ +package dev.usbharu.hideout.service.post + +import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.domain.model.hideout.entity.Timeline +import dev.usbharu.hideout.query.FollowerQueryService +import dev.usbharu.hideout.repository.TimelineRepository +import org.springframework.stereotype.Service + +@Service +class TimelineService( + private val followerQueryService: FollowerQueryService, + private val timelineRepository: TimelineRepository +) { + suspend fun publishTimeline(post: Post) { + val findFollowersById = followerQueryService.findFollowersById(post.userId) + timelineRepository.saveAll(findFollowersById.map { + Timeline( + id = timelineRepository.generateId(), + userId = it.id, + timelineId = 0, + postId = post.id, + postUserId = post.userId, + createdAt = post.createdAt, + replyId = post.replyId, + repostId = post.repostId, + visibility = post.visibility, + sensitive = post.sensitive + ) + }) + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 876dbe3d..889d71f4 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -24,7 +24,11 @@ spring: password: "" data: mongodb: - + host: localhost + port: 27017 + database: hideout + # username: hideoutuser + # password: hideoutpass h2: console: