From 31e6b0f8d29bead8a0b5564aa01eae5531f3cd00 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 4 Jun 2023 00:59:47 +0900 Subject: [PATCH] =?UTF-8?q?style:=20=E3=82=B9=E3=82=BF=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/repository/IPostRepository.kt | 40 +++++++++---- .../hideout/repository/PostRepositoryImpl.kt | 32 ++++++++-- .../hideout/routing/api/internal/v1/Users.kt | 8 ++- .../activitypub/ActivityPubNoteServiceImpl.kt | 2 +- .../hideout/service/api/IPostApiService.kt | 30 ++++++---- .../hideout/service/api/PostApiServiceImpl.kt | 60 ++++++++++++++----- .../hideout/service/post/PostServiceImpl.kt | 10 ++-- 7 files changed, 132 insertions(+), 50 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt index b8b3d9eb..8887853e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt @@ -3,22 +3,42 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.Post import java.time.Instant +@Suppress("LongParameterList") 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 findAll(since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List - suspend fun findByUserNameAndDomain(username: String, - s: String, - since: Instant?, - until: Instant?, - minId: Long?, - maxId: Long?, - limit: Int?, - userId: Long?): List + suspend fun findAll( + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? + ): List + + suspend fun findByUserNameAndDomain( + username: String, + s: String, + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? + ): List + + suspend fun findByUserId( + idOrNull: Long, + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? + ): List - suspend fun findByUserId(idOrNull: Long, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List suspend fun findByApId(id: String): Post? } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index 6545d520..859bbd66 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -25,7 +25,7 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe @Suppress("InjectDispatcher") suspend fun query(block: suspend () -> T): T = - newSuspendedTransaction(Dispatchers.IO) { block() } + newSuspendedTransaction(Dispatchers.IO) { block() } override suspend fun save(post: Post): Post { return query { @@ -78,15 +78,39 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe } } - override suspend fun findAll(since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { + override suspend fun findAll( + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? + ): List { TODO("Not yet implemented") } - override suspend fun findByUserNameAndDomain(username: String, s: String, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { + override suspend fun findByUserNameAndDomain( + username: String, + s: String, + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? + ): List { TODO("Not yet implemented") } - override suspend fun findByUserId(idOrNull: Long, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { + override suspend fun findByUserId( + idOrNull: Long, + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? + ): List { TODO("Not yet implemented") } diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Users.kt b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Users.kt index 9f357120..23142bd7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Users.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Users.kt @@ -43,7 +43,9 @@ fun Route.users(userService: IUserService, userApiService: IUserApiService) { get { val userParameter = ( call.parameters["name"] - ?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.") + ?: throw ParameterNotExistException( + "Parameter(name='userName@domain') does not exist." + ) ) if (userParameter.toLongOrNull() != null) { return@get call.respond(userApiService.findById(userParameter.toLong())) @@ -91,7 +93,9 @@ fun Route.users(userService: IUserService, userApiService: IUserApiService) { get { val userParameter = ( call.parameters["name"] - ?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.") + ?: throw ParameterNotExistException( + "Parameter(name='userName@domain') does not exist." + ) ) if (userParameter.toLongOrNull() != null) { return@get call.respond(userApiService.findFollowings(userParameter.toLong())) 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 372d12ad..4ccd223f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt @@ -152,6 +152,6 @@ class ActivityPubNoteServiceImpl( note(note, targetActor, note.id ?: throw IllegalArgumentException("note.id is null")) companion object { - val public: String = "https://www.w3.org/ns/activitystreams#Public" + const val public: String = "https://www.w3.org/ns/activitystreams#Public" } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt index 00b6ff03..3d205faa 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt @@ -6,18 +6,22 @@ import java.time.Instant interface IPostApiService { suspend fun createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, userId: Long): Post suspend fun getById(id: Long, userId: Long?): Post - suspend fun getAll(since: Instant? = null, - until: Instant? = null, - minId: Long? = null, - maxId: Long? = null, - limit: Int? = null, - userId: Long? = null): List + suspend fun getAll( + since: Instant? = null, + until: Instant? = null, + minId: Long? = null, + maxId: Long? = null, + limit: Int? = null, + userId: Long? = null + ): List - suspend fun getByUser(nameOrId: String, - since: Instant? = null, - until: Instant? = null, - minId: Long? = null, - maxId: Long? = null, - limit: Int? = null, - userId: Long? = null): List + suspend fun getByUser( + nameOrId: String, + since: Instant? = null, + until: Instant? = null, + minId: Long? = null, + maxId: Long? = null, + limit: Int? = null, + userId: Long? = null + ): List } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt index 9061224e..92ceb999 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt @@ -9,18 +9,24 @@ import dev.usbharu.hideout.service.post.IPostService import dev.usbharu.hideout.util.AcctUtil import org.koin.core.annotation.Single import java.time.Instant +import dev.usbharu.hideout.domain.model.hideout.form.Post as FormPost @Single -class PostApiServiceImpl(private val postService: IPostService, private val postRepository: IPostRepository) : IPostApiService { - override suspend fun createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, userId: Long): Post { - return postService.createLocal(PostCreateDto( - text = postForm.text, - overview = postForm.overview, - visibility = postForm.visibility, - repostId = postForm.repostId, - repolyId = postForm.replyId, - userId = userId - )) +class PostApiServiceImpl( + private val postService: IPostService, + private val postRepository: IPostRepository +) : IPostApiService { + override suspend fun createPost(postForm: FormPost, userId: Long): Post { + return postService.createLocal( + PostCreateDto( + text = postForm.text, + overview = postForm.overview, + visibility = postForm.visibility, + repostId = postForm.repostId, + repolyId = postForm.replyId, + userId = userId + ) + ) } override suspend fun getById(id: Long, userId: Long?): Post { @@ -28,16 +34,38 @@ class PostApiServiceImpl(private val postService: IPostService, private val post ?: throw PostNotFoundException("$id was not found or is not authorized.") } - override suspend fun getAll(since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { - return postRepository.findAll(since, until, minId, maxId, limit, userId) - } + override suspend fun getAll( + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? + ): List = postRepository.findAll(since, until, minId, maxId, limit, userId) - override suspend fun getByUser(nameOrId: String, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { + override suspend fun getByUser( + nameOrId: String, + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? + ): List { val idOrNull = nameOrId.toLongOrNull() return if (idOrNull == null) { val acct = AcctUtil.parse(nameOrId) - postRepository.findByUserNameAndDomain(acct.username, acct.domain - ?: Config.configData.domain, since, until, minId, maxId, limit, userId) + postRepository.findByUserNameAndDomain( + acct.username, + acct.domain + ?: Config.configData.domain, + since, + until, + minId, + maxId, + limit, + userId + ) } else { postRepository.findByUserId(idOrNull, since, until, minId, maxId, limit, userId) } 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 fa9e20f1..d9384fe5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt @@ -10,7 +10,11 @@ import org.koin.core.annotation.Single import java.time.Instant @Single -class PostServiceImpl(private val postRepository: IPostRepository, private val userRepository: IUserRepository, private val activityPubNoteService: ActivityPubNoteService) : IPostService { +class PostServiceImpl( + private val postRepository: IPostRepository, + private val userRepository: IUserRepository, + private val activityPubNoteService: ActivityPubNoteService +) : IPostService { override suspend fun createLocal(post: PostCreateDto): Post { val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found") val id = postRepository.generateId() @@ -29,7 +33,5 @@ class PostServiceImpl(private val postRepository: IPostRepository, private val u return internalCreate(createPost) } - private suspend fun internalCreate(post: Post): Post { - return postRepository.save(post) - } + private suspend fun internalCreate(post: Post): Post = postRepository.save(post) }