From 2b567bb1d5fe88e9d2cddcf33b3bf4eb28297d0f Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 8 Sep 2024 15:58:05 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Post=E8=A9=B3=E7=B4=B0=E3=83=9A?= =?UTF-8?q?=E3=83=BC=E3=82=B8=E3=81=A7=E3=83=AA=E3=82=A2=E3=82=AF=E3=82=B7?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E3=82=92=E8=A1=A8=E7=A4=BA=E3=81=A7=E3=81=8D?= =?UTF-8?q?=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 --- .../core/application/model/Reactions.kt | 28 +++++++++++++ .../post/GetPostDetailApplicationService.kt | 16 ++++--- .../core/application/post/PostDetail.kt | 8 +++- .../ReadTimelineApplicationService.kt | 15 ++++--- .../model/reaction/ReactionRepository.kt | 3 ++ .../ExposedReactionsQueryService.kt | 42 +++++++++++++++++++ .../ExposedUserTimelineQueryService.kt | 3 +- .../ExposedReactionRepository.kt | 8 ++++ .../query/reactions/ReactionsQueryService.kt | 8 ++++ 9 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/model/Reactions.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/ExposedReactionsQueryService.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/query/reactions/ReactionsQueryService.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/model/Reactions.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/model/Reactions.kt new file mode 100644 index 00000000..11d95f7e --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/model/Reactions.kt @@ -0,0 +1,28 @@ +package dev.usbharu.hideout.core.application.model + +import dev.usbharu.hideout.core.domain.model.emoji.CustomEmoji +import dev.usbharu.hideout.core.domain.model.reaction.Reaction +import java.net.URI + +data class Reactions( + val postId: Long, + val count: Int, + val name: String, + val domain: String, + val url: URI?, + val actorIds: List, +) { + companion object { + fun of(reactionList: List, customEmoji: CustomEmoji?): Reactions { + val first = reactionList.first() + return Reactions( + first.id.value, + reactionList.size, + customEmoji?.name ?: first.unicodeEmoji.name, + customEmoji?.domain?.domain ?: first.unicodeEmoji.domain.domain, + customEmoji?.url, + reactionList.map { it.actorId.id } + ) + } + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/GetPostDetailApplicationService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/GetPostDetailApplicationService.kt index 7eb38ed3..a9abd30a 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/GetPostDetailApplicationService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/GetPostDetailApplicationService.kt @@ -11,6 +11,7 @@ import dev.usbharu.hideout.core.domain.model.post.PostId import dev.usbharu.hideout.core.domain.model.post.PostRepository import dev.usbharu.hideout.core.domain.model.support.principal.Principal import dev.usbharu.hideout.core.domain.service.post.IPostReadAccessControl +import dev.usbharu.hideout.core.query.reactions.ReactionsQueryService import org.slf4j.LoggerFactory import org.springframework.stereotype.Service @@ -20,7 +21,8 @@ class GetPostDetailApplicationService( private val postRepository: PostRepository, private val actorRepository: ActorRepository, private val mediaRepository: MediaRepository, - private val iPostReadAccessControl: IPostReadAccessControl + private val iPostReadAccessControl: IPostReadAccessControl, + private val reactionsQueryService: ReactionsQueryService, ) : AbstractApplicationService( transaction, logger @@ -38,6 +40,8 @@ class GetPostDetailApplicationService( val mediaList = mediaRepository.findByIds(post.mediaIds) + val reactions = reactionsQueryService.findAllByPostId(post.id) + return PostDetail.of( post = post, actor = actor, @@ -46,6 +50,7 @@ class GetPostDetailApplicationService( reply = post.replyId?.let { fetchChild(it, actor, iconMedia, principal) }, repost = post.repostId?.let { fetchChild(it, actor, iconMedia, principal) }, moveTo = post.moveTo?.let { fetchChild(it, actor, iconMedia, principal) }, + reactionsList = reactions ) } @@ -69,10 +74,11 @@ class GetPostDetailApplicationService( val mediaList = mediaRepository.findByIds(post.mediaIds) return PostDetail.of( - post, - first, - third, - mediaList + post = post, + actor = first, + iconMedia = third, + mediaList = mediaList, + reactionsList = emptyList() ) } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/PostDetail.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/PostDetail.kt index 567848df..3cee2e7c 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/PostDetail.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/post/PostDetail.kt @@ -1,5 +1,6 @@ package dev.usbharu.hideout.core.application.post +import dev.usbharu.hideout.core.application.model.Reactions import dev.usbharu.hideout.core.domain.model.actor.Actor import dev.usbharu.hideout.core.domain.model.media.Media import dev.usbharu.hideout.core.domain.model.post.Post @@ -23,7 +24,8 @@ data class PostDetail( val sensitive: Boolean, val deleted: Boolean, val mediaDetailList: List, - val moveTo: PostDetail? + val moveTo: PostDetail?, + val reactionsList: List ) { companion object { @Suppress("LongParameterList") @@ -35,6 +37,7 @@ data class PostDetail( reply: PostDetail? = null, repost: PostDetail? = null, moveTo: PostDetail? = null, + reactionsList: List ): PostDetail { return PostDetail( id = post.id.id, @@ -52,7 +55,8 @@ data class PostDetail( sensitive = post.sensitive, deleted = false, mediaDetailList = mediaList.map { MediaDetail.of(it) }, - moveTo = moveTo + moveTo = moveTo, + reactionsList = reactionsList ) } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/timeline/ReadTimelineApplicationService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/timeline/ReadTimelineApplicationService.kt index 9facf26c..129aae7a 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/timeline/ReadTimelineApplicationService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/timeline/ReadTimelineApplicationService.kt @@ -47,7 +47,8 @@ class ReadTimelineApplicationService( it.replyPost, it.replyPostActor!!, it.replyPostActorIconMedia, - it.replyPostMedias.orEmpty() + it.replyPostMedias.orEmpty(), + reactionsList = emptyList(), ) } else { null @@ -56,10 +57,11 @@ class ReadTimelineApplicationService( val repost = if (it.repostPost != null) { @Suppress("UnsafeCallOnNullableType") PostDetail.of( - it.repostPost, - it.repostPostActor!!, - it.repostPostActorIconMedia, - it.repostPostMedias.orEmpty() + post = it.repostPost, + actor = it.repostPostActor!!, + iconMedia = it.repostPostActorIconMedia, + mediaList = it.repostPostMedias.orEmpty(), + reactionsList = emptyList() ) } else { null @@ -71,7 +73,8 @@ class ReadTimelineApplicationService( iconMedia = it.postActorIconMedia, mediaList = it.postMedias, reply = reply, - repost = repost + repost = repost, + reactionsList = emptyList() ) } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/reaction/ReactionRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/reaction/ReactionRepository.kt index 83ac26f9..29acd189 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/reaction/ReactionRepository.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/reaction/ReactionRepository.kt @@ -1,7 +1,10 @@ package dev.usbharu.hideout.core.domain.model.reaction +import dev.usbharu.hideout.core.domain.model.post.PostId + interface ReactionRepository { suspend fun save(reaction: Reaction): Reaction suspend fun findById(reactionId: ReactionId): Reaction? + suspend fun findByPostId(postId: PostId): List suspend fun delete(reaction: Reaction) } \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/ExposedReactionsQueryService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/ExposedReactionsQueryService.kt new file mode 100644 index 00000000..89d266b9 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/ExposedReactionsQueryService.kt @@ -0,0 +1,42 @@ +package dev.usbharu.hideout.core.infrastructure.exposedquery + +import dev.usbharu.hideout.core.application.model.Reactions +import dev.usbharu.hideout.core.domain.model.post.PostId +import dev.usbharu.hideout.core.infrastructure.exposedrepository.AbstractRepository +import dev.usbharu.hideout.core.infrastructure.exposedrepository.CustomEmojis +import dev.usbharu.hideout.core.infrastructure.exposedrepository.toCustomEmojiOrNull +import dev.usbharu.hideout.core.infrastructure.exposedrepository.toReaction +import dev.usbharu.hideout.core.query.reactions.ReactionsQueryService +import org.jetbrains.exposed.sql.selectAll +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Repository +import dev.usbharu.hideout.core.infrastructure.exposedrepository.Reactions as ExposedrepositoryReactions + +@Repository +class ExposedReactionsQueryService : ReactionsQueryService, AbstractRepository() { + override suspend fun findAllByPostId(postId: PostId): List { + return query { + ExposedrepositoryReactions.leftJoin(CustomEmojis).selectAll() + .where { ExposedrepositoryReactions.postId eq postId.id } + .groupBy { + it[ExposedrepositoryReactions.customEmojiId]?.toString() + ?: it[ExposedrepositoryReactions.unicodeEmoji] + } + .map { it.value } + .map { + Reactions.of( + it.map { resultRow -> resultRow.toReaction() }, + it.first().toCustomEmojiOrNull() + ) + } + } + } + + override val logger: Logger + get() = Companion.logger + + companion object { + private val logger = LoggerFactory.getLogger(ExposedReactionsQueryService::class.java) + } +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/ExposedUserTimelineQueryService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/ExposedUserTimelineQueryService.kt index 82be6f96..99c5cee4 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/ExposedUserTimelineQueryService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedquery/ExposedUserTimelineQueryService.kt @@ -100,7 +100,8 @@ class ExposedUserTimelineQueryService : UserTimelineQueryService, AbstractReposi sensitive = it[authorizedQuery[Posts.sensitive]], deleted = it[authorizedQuery[Posts.deleted]], mediaDetailList = emptyList(), - moveTo = null + moveTo = null, + emptyList() ) } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedReactionRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedReactionRepository.kt index 92c8ebd9..0b376bd0 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedReactionRepository.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedReactionRepository.kt @@ -48,6 +48,14 @@ class ExposedReactionRepository(override val domainEventPublisher: DomainEventPu } } + override suspend fun findByPostId(postId: PostId): List { + return query { + Reactions.selectAll().where { + Reactions.postId eq postId.id + }.map { it.toReaction() } + } + } + override suspend fun delete(reaction: Reaction) { return query { Reactions.deleteWhere { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/query/reactions/ReactionsQueryService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/query/reactions/ReactionsQueryService.kt new file mode 100644 index 00000000..a9c78909 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/query/reactions/ReactionsQueryService.kt @@ -0,0 +1,8 @@ +package dev.usbharu.hideout.core.query.reactions + +import dev.usbharu.hideout.core.application.model.Reactions +import dev.usbharu.hideout.core.domain.model.post.PostId + +interface ReactionsQueryService { + suspend fun findAllByPostId(postId: PostId): List +} \ No newline at end of file