From 5a5be82c5037b85560f242c13910468057ac02ce Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 10 Aug 2023 20:29:18 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Post=E3=81=AEQueryService=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usbharu/hideout/query/PostQueryService.kt | 9 +++++++ .../hideout/query/PostQueryServiceImpl.kt | 16 ++++++++++++ .../hideout/repository/IPostRepository.kt | 5 +--- .../hideout/repository/PostRepositoryImpl.kt | 16 ++---------- .../activitypub/ActivityPubLikeServiceImpl.kt | 10 +++----- .../activitypub/ActivityPubNoteServiceImpl.kt | 25 +++++++++++++------ .../ActivityPubReactionServiceImpl.kt | 9 ++++--- .../ActivityPubNoteServiceImplTest.kt | 13 ++++++++-- 8 files changed, 65 insertions(+), 38 deletions(-) create mode 100644 src/main/kotlin/dev/usbharu/hideout/query/PostQueryService.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/query/PostQueryServiceImpl.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/query/PostQueryService.kt b/src/main/kotlin/dev/usbharu/hideout/query/PostQueryService.kt new file mode 100644 index 00000000..8ad102ab --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/query/PostQueryService.kt @@ -0,0 +1,9 @@ +package dev.usbharu.hideout.query + +import dev.usbharu.hideout.domain.model.hideout.entity.Post + +interface PostQueryService { + suspend fun findById(id: Long): Post + suspend fun findByUrl(url: String): Post + suspend fun findByApId(string: String): Post +} diff --git a/src/main/kotlin/dev/usbharu/hideout/query/PostQueryServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/query/PostQueryServiceImpl.kt new file mode 100644 index 00000000..1b3969c7 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/query/PostQueryServiceImpl.kt @@ -0,0 +1,16 @@ +package dev.usbharu.hideout.query + +import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.repository.Posts +import dev.usbharu.hideout.repository.toPost +import org.jetbrains.exposed.sql.select +import org.koin.core.annotation.Single + +@Single +class PostQueryServiceImpl : PostQueryService { + override suspend fun findById(id: Long): Post = Posts.select { Posts.id eq id }.single().toPost() + + override suspend fun findByUrl(url: String): Post = Posts.select { Posts.url eq url }.single().toPost() + + override suspend fun findByApId(string: String): Post = Posts.select { Posts.apId eq string }.single().toPost() +} diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt index 4e135f70..2e0faf91 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt @@ -6,9 +6,6 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Post interface IPostRepository { suspend fun generateId(): Long suspend fun save(post: Post): Post - suspend fun findOneById(id: Long, userId: Long? = null): Post? - suspend fun findByUrl(url: String): Post? suspend fun delete(id: Long) - - suspend fun findByApId(id: String): Post? + suspend fun findById(id: Long): Post } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index c8a327d9..3de58561 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -61,15 +61,9 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe } } - override suspend fun findOneById(id: Long, userId: Long?): Post? { + override suspend fun findById(id: Long): Post { return query { - Posts.select { Posts.id eq id }.singleOrNull()?.toPost() - } - } - - override suspend fun findByUrl(url: String): Post? { - return query { - Posts.select { Posts.url eq url }.singleOrNull()?.toPost() + Posts.select { Posts.id eq id }.single().toPost() } } @@ -78,12 +72,6 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe Posts.deleteWhere { Posts.id eq id } } } - - override suspend fun findByApId(id: String): Post? { - return query { - Posts.select { Posts.apId eq id }.singleOrNull()?.toPost() - } - } } object Posts : Table() { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubLikeServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubLikeServiceImpl.kt index 72a50f8a..f73a5e37 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubLikeServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubLikeServiceImpl.kt @@ -3,10 +3,9 @@ package dev.usbharu.hideout.service.activitypub import dev.usbharu.hideout.domain.model.ActivityPubResponse import dev.usbharu.hideout.domain.model.ActivityPubStringResponse import dev.usbharu.hideout.domain.model.ap.Like -import dev.usbharu.hideout.exception.PostNotFoundException import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException +import dev.usbharu.hideout.query.PostQueryService import dev.usbharu.hideout.query.UserQueryService -import dev.usbharu.hideout.repository.IPostRepository import dev.usbharu.hideout.service.reaction.IReactionService import io.ktor.http.* import org.koin.core.annotation.Single @@ -15,9 +14,9 @@ import org.koin.core.annotation.Single class ActivityPubLikeServiceImpl( private val reactionService: IReactionService, private val activityPubUserService: ActivityPubUserService, - private val postService: IPostRepository, private val activityPubNoteService: ActivityPubNoteService, - private val userQueryService: UserQueryService + private val userQueryService: UserQueryService, + private val postQueryService: PostQueryService ) : ActivityPubLikeService { override suspend fun receiveLike(like: Like): ActivityPubResponse { val actor = like.actor ?: throw IllegalActivityPubObjectException("actor is null") @@ -31,8 +30,7 @@ class ActivityPubLikeServiceImpl( ?: throw IllegalActivityPubObjectException("actor is not found") ) - val post = postService.findByUrl(like.`object`!!) - ?: throw PostNotFoundException("${like.`object`} was not found") + val post = postQueryService.findByUrl(like.`object`!!) reactionService.receiveReaction(content, actor.substringAfter("://").substringBefore("/"), user.id, post.id) return ActivityPubStringResponse(HttpStatusCode.OK, "") diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt index 29be3294..a6cdce08 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt @@ -11,6 +11,7 @@ import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException import dev.usbharu.hideout.plugins.getAp import dev.usbharu.hideout.plugins.postAp import dev.usbharu.hideout.query.FollowerQueryService +import dev.usbharu.hideout.query.PostQueryService import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.repository.IPostRepository import dev.usbharu.hideout.service.job.JobQueueParentService @@ -28,7 +29,8 @@ class ActivityPubNoteServiceImpl( private val postRepository: IPostRepository, private val activityPubUserService: ActivityPubUserService, private val userQueryService: UserQueryService, - private val followerQueryService: FollowerQueryService + private val followerQueryService: FollowerQueryService, + private val postQueryService: PostQueryService ) : ActivityPubNoteService { private val logger = LoggerFactory.getLogger(this::class.java) @@ -72,7 +74,7 @@ class ActivityPubNoteServiceImpl( } override suspend fun fetchNote(url: String, targetActor: String?): Note { - val post = postRepository.findByUrl(url) + val post = postQueryService.findByUrl(url) if (post != null) { return postToNote(post) } @@ -86,7 +88,7 @@ class ActivityPubNoteServiceImpl( private suspend fun postToNote(post: Post): Note { val user = userQueryService.findById(post.userId) - val reply = post.replyId?.let { postRepository.findOneById(it) } + val reply = post.replyId?.let { postQueryService.findById(it) } return Note( name = "Post", id = post.apId, @@ -100,15 +102,22 @@ class ActivityPubNoteServiceImpl( ) } - private suspend fun ActivityPubNoteServiceImpl.note( + private suspend fun note( note: Note, targetActor: String?, url: String ): Note { - val findByApId = postRepository.findByApId(url) - if (findByApId != null) { - return postToNote(findByApId) + val findByApId = try { + postQueryService.findByApId(url) + } catch (_: NoSuchElementException) { + return internalNote(note, targetActor, url) + } catch (_: IllegalArgumentException) { + return internalNote(note, targetActor, url) } + return postToNote(findByApId) + } + + private suspend fun internalNote(note: Note, targetActor: String?, url: String): Note { val person = activityPubUserService.fetchPerson( note.attributedTo ?: throw IllegalActivityPubObjectException("note.attributedTo is null"), targetActor @@ -129,7 +138,7 @@ class ActivityPubNoteServiceImpl( val reply = note.inReplyTo?.let { fetchNote(it, targetActor) - postRepository.findByUrl(it) + postQueryService.findByUrl(it) } postRepository.save( diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubReactionServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubReactionServiceImpl.kt index 9fe4ff2e..3a6f1771 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubReactionServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubReactionServiceImpl.kt @@ -7,9 +7,9 @@ import dev.usbharu.hideout.domain.model.ap.Undo import dev.usbharu.hideout.domain.model.hideout.entity.Reaction import dev.usbharu.hideout.domain.model.job.DeliverReactionJob import dev.usbharu.hideout.domain.model.job.DeliverRemoveReactionJob -import dev.usbharu.hideout.exception.PostNotFoundException import dev.usbharu.hideout.plugins.postAp import dev.usbharu.hideout.query.FollowerQueryService +import dev.usbharu.hideout.query.PostQueryService import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.repository.IPostRepository import dev.usbharu.hideout.service.job.JobQueueParentService @@ -24,13 +24,14 @@ class ActivityPubReactionServiceImpl( private val iPostRepository: IPostRepository, private val httpClient: HttpClient, private val userQueryService: UserQueryService, - private val followerQueryService: FollowerQueryService + private val followerQueryService: FollowerQueryService, + private val postQueryService: PostQueryService ) : ActivityPubReactionService { override suspend fun reaction(like: Reaction) { val followers = followerQueryService.findFollowersById(like.userId) val user = userQueryService.findById(like.userId) val post = - iPostRepository.findOneById(like.postId) ?: throw PostNotFoundException("${like.postId} was not found.") + postQueryService.findById(like.postId) followers.forEach { follower -> jobQueueParentService.schedule(DeliverReactionJob) { props[it.actor] = user.url @@ -46,7 +47,7 @@ class ActivityPubReactionServiceImpl( val followers = followerQueryService.findFollowersById(like.userId) val user = userQueryService.findById(like.userId) val post = - iPostRepository.findOneById(like.postId) ?: throw PostNotFoundException("${like.postId} was not found.") + postQueryService.findById(like.postId) followers.forEach { follower -> jobQueueParentService.schedule(DeliverRemoveReactionJob) { props[it.actor] = user.url diff --git a/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt index 3b831cba..c02f65ae 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt @@ -82,7 +82,8 @@ class ActivityPubNoteServiceImplTest { mock(), mock(), userQueryService, - followerQueryService + followerQueryService, + mock() ) val postEntity = Post( 1L, @@ -106,7 +107,15 @@ class ActivityPubNoteServiceImplTest { respondOk() } ) - val activityPubNoteService = ActivityPubNoteServiceImpl(httpClient, mock(), mock(), mock(), mock(), mock()) + val activityPubNoteService = ActivityPubNoteServiceImpl( + httpClient, + mock(), + mock(), + mock(), + mock(), + mock(), + mock() + ) activityPubNoteService.createNoteJob( JobProps( data = mapOf(