From 841289700b91fd3451acce1f8309d01d1053d79b Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 20 Jul 2024 11:22:43 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Mongodb=E3=81=A7=E3=82=BF=E3=82=A4?= =?UTF-8?q?=E3=83=A0=E3=83=A9=E3=82=A4=E3=83=B3=E3=82=92=E6=A7=8B=E7=AF=89?= =?UTF-8?q?=E3=81=A7=E3=81=8D=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 --- .../MongoInternalTimelineObjectRepository.kt | 131 +++++++++++++++++- 1 file changed, 124 insertions(+), 7 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/mongorepository/MongoInternalTimelineObjectRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/mongorepository/MongoInternalTimelineObjectRepository.kt index 888beeae..dd09daf6 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/mongorepository/MongoInternalTimelineObjectRepository.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/mongorepository/MongoInternalTimelineObjectRepository.kt @@ -1,35 +1,152 @@ package dev.usbharu.hideout.core.infrastructure.mongorepository import dev.usbharu.hideout.core.domain.model.actor.ActorId +import dev.usbharu.hideout.core.domain.model.emoji.EmojiId +import dev.usbharu.hideout.core.domain.model.filter.FilterId +import dev.usbharu.hideout.core.domain.model.media.MediaId import dev.usbharu.hideout.core.domain.model.post.PostId +import dev.usbharu.hideout.core.domain.model.post.Visibility import dev.usbharu.hideout.core.domain.model.timeline.TimelineId import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObject +import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObjectId +import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObjectWarnFilter +import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId import dev.usbharu.hideout.core.infrastructure.timeline.InternalTimelineObjectRepository +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.toList +import org.springframework.data.mongodb.core.mapping.Document +import org.springframework.data.repository.kotlin.CoroutineCrudRepository import org.springframework.stereotype.Repository +import java.time.Instant @Repository -class MongoInternalTimelineObjectRepository : InternalTimelineObjectRepository { +class MongoInternalTimelineObjectRepository(private val springDataMongoTimelineObjectRepository: SpringDataMongoTimelineObjectRepository) : + InternalTimelineObjectRepository { override suspend fun save(timelineObject: TimelineObject): TimelineObject { - TODO("Not yet implemented") + springDataMongoTimelineObjectRepository.save(SpringDataMongoTimelineObject.of(timelineObject)) + return timelineObject } override suspend fun saveAll(timelineObjectList: List): List { - TODO("Not yet implemented") + springDataMongoTimelineObjectRepository.saveAll(timelineObjectList.map { SpringDataMongoTimelineObject.of(it) }) + .collect() + return timelineObjectList } override suspend fun findByPostId(postId: PostId): List { - TODO("Not yet implemented") + return springDataMongoTimelineObjectRepository.findByPostId(postId.id).map { it.toTimelineObject() }.toList() } override suspend fun deleteByPostId(postId: PostId) { - TODO("Not yet implemented") + springDataMongoTimelineObjectRepository.deleteByPostId(postId.id) } override suspend fun deleteByTimelineIdAndActorId(timelineId: TimelineId, actorId: ActorId) { - TODO("Not yet implemented") + springDataMongoTimelineObjectRepository.deleteByTimelineIdAndPostActorId(timelineId.value, actorId.id) } override suspend fun deleteByTimelineId(timelineId: TimelineId) { - TODO("Not yet implemented") + springDataMongoTimelineObjectRepository.deleteByTimelineId(timelineId.value) } + + +} + +@Document +data class SpringDataMongoTimelineObject( + val id: Long, + val userDetailId: Long, + val timelineId: Long, + val postId: Long, + val postActorId: Long, + val postCreatedAt: Long, + val replyId: Long?, + val repostId: Long?, + val visibility: Visibility, + val isPureRepost: Boolean, + val mediaIds: List, + val emojiIds: List, + val visibleActors: List, + val hasMediaInRepost: Boolean, + val lastUpdatedAt: Long, + val warnFilters: List +) { + + fun toTimelineObject(): TimelineObject { + return TimelineObject( + TimelineObjectId(id), + UserDetailId(userDetailId), + TimelineId(timelineId), + PostId(postId), + ActorId(postActorId), + Instant.ofEpochSecond(postCreatedAt), + replyId?.let { PostId(it) }, + repostId?.let { PostId(it) }, + visibility, + isPureRepost, + mediaIds.map { MediaId(it) }, + emojiIds.map { EmojiId(it) }, + visibleActors.map { ActorId(it) }, + hasMediaInRepost, + Instant.ofEpochSecond(lastUpdatedAt), + warnFilters.map { it.toTimelineObjectWarnFilter() } + ) + } + + companion object { + fun of(timelineObject: TimelineObject): SpringDataMongoTimelineObject { + return SpringDataMongoTimelineObject( + timelineObject.id.value, + timelineObject.userDetailId.id, + timelineObject.timelineId.value, + timelineObject.postId.id, + timelineObject.postActorId.id, + timelineObject.postCreatedAt.epochSecond, + timelineObject.replyId?.id, + timelineObject.repostId?.id, + timelineObject.visibility, + timelineObject.isPureRepost, + timelineObject.mediaIds.map { it.id }, + timelineObject.emojiIds.map { it.emojiId }, + timelineObject.visibleActors.map { it.id }, + timelineObject.hasMediaInRepost, + timelineObject.lastUpdatedAt.epochSecond, + timelineObject.warnFilters.map { SpringDataMongoTimelineObjectWarnFilter.of(it) } + ) + } + } +} + +data class SpringDataMongoTimelineObjectWarnFilter( + val filterId: Long, + val matchedKeyword: String +) { + + fun toTimelineObjectWarnFilter(): TimelineObjectWarnFilter { + return TimelineObjectWarnFilter( + FilterId(filterId), + matchedKeyword + ) + } + + companion object { + fun of(timelineObjectWarnFilter: TimelineObjectWarnFilter): SpringDataMongoTimelineObjectWarnFilter { + return SpringDataMongoTimelineObjectWarnFilter( + timelineObjectWarnFilter.filterId.id, + timelineObjectWarnFilter.matchedKeyword + ) + } + } +} + +interface SpringDataMongoTimelineObjectRepository : CoroutineCrudRepository { + fun findByPostId(postId: Long): Flow + + suspend fun deleteByPostId(postId: Long) + + suspend fun deleteByTimelineIdAndPostActorId(timelineId: Long, postActorId: Long) + + suspend fun deleteByTimelineId(timelineId: Long) } \ No newline at end of file