From dfa3bb74fb6e3a5cb818da836b86f0378d881563 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 8 May 2023 11:42:55 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=85=AC=E9=96=8B=E7=AF=84?= =?UTF-8?q?=E5=9B=B2=E3=82=92=E5=88=97=E6=8C=99=E5=9E=8B=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/model/hideout/entity/Post.kt | 2 +- .../domain/model/hideout/entity/Visibility.kt | 8 ++++ .../hideout/repository/PostRepositoryImpl.kt | 5 ++- .../hideout/service/impl/PostService.kt | 5 ++- .../ActivityPubNoteServiceImplTest.kt | 13 +++++-- .../hideout/service/impl/PostServiceTest.kt | 39 ++++++++++--------- 6 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Visibility.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Post.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Post.kt index 3b997549..423009cf 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Post.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Post.kt @@ -6,7 +6,7 @@ data class Post( val overview:String? = null, val text:String, val createdAt:Long, - val visibility: Int, + val visibility: Visibility, val url:String, val repostId:Long? = null, val replyId:Long? = null diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Visibility.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Visibility.kt new file mode 100644 index 00000000..9acf8c16 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Visibility.kt @@ -0,0 +1,8 @@ +package dev.usbharu.hideout.domain.model.hideout.entity + +enum class Visibility { + PUBLIC, + UNLISTED, + FOLLOWERS, + DIRECT +} diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index a2661b6b..9f679901 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -1,6 +1,7 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.domain.model.hideout.entity.Visibility import dev.usbharu.hideout.service.IdGenerateService import kotlinx.coroutines.Dispatchers import org.jetbrains.exposed.sql.* @@ -33,7 +34,7 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe it[overview] = post.overview it[text] = post.text it[createdAt] = post.createdAt - it[visibility] = post.visibility + it[visibility] = post.visibility.ordinal it[url] = post.url it[repostId] = post.repostId it[replyId] = post.replyId @@ -75,7 +76,7 @@ fun ResultRow.toPost(): Post { overview = this[Posts.overview], text = this[Posts.text], createdAt = this[Posts.createdAt], - visibility = this[Posts.visibility], + visibility = Visibility.values().first { visibility -> visibility.ordinal == this[Posts.visibility] }, url = this[Posts.url], repostId = this[Posts.repostId], replyId = this[Posts.replyId] diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt index 8de2594d..d8ec4f5f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt @@ -2,6 +2,7 @@ package dev.usbharu.hideout.service.impl import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.domain.model.hideout.entity.Visibility import dev.usbharu.hideout.repository.IPostRepository import dev.usbharu.hideout.repository.Posts import dev.usbharu.hideout.repository.UsersFollowers @@ -36,7 +37,7 @@ class PostService( val id = postRepository.generateId() val postEntity = Post( id, user.id, null, post.text, - Instant.now().toEpochMilli(), 0, "${user.url}/posts/$id", null, null + Instant.now().toEpochMilli(), Visibility.PUBLIC, "${user.url}/posts/$id", null, null ) postRepository.save(postEntity) } @@ -51,7 +52,7 @@ class PostService( ): List { return transaction { val select = Posts.select { - Posts.visibility.eq(0) + Posts.visibility.eq(Visibility.PUBLIC.ordinal) } if (userId != null) { select.orWhere { 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 9fad103e..0372f88e 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt @@ -7,6 +7,7 @@ import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.config.ConfigData import dev.usbharu.hideout.domain.model.hideout.entity.Post import dev.usbharu.hideout.domain.model.hideout.entity.User +import dev.usbharu.hideout.domain.model.hideout.entity.Visibility import dev.usbharu.hideout.domain.model.job.DeliverPostJob import dev.usbharu.hideout.service.impl.IUserService import dev.usbharu.hideout.service.job.JobQueueParentService @@ -78,7 +79,7 @@ class ActivityPubNoteServiceImplTest { null, "test text", 1L, - 1, + Visibility.PUBLIC, "https://example.com" ) activityPubNoteService.createNote(postEntity) @@ -99,8 +100,14 @@ class ActivityPubNoteServiceImplTest { JobProps( data = mapOf( DeliverPostJob.actor.name to "https://follower.example.com", - DeliverPostJob.post.name to "{\"id\":1,\"userId\":1,\"inReplyToId\":null,\"text\":\"test text\"," + - "\"createdAt\":1,\"updatedAt\":1,\"url\":\"https://example.com\"}", + DeliverPostJob.post.name to """{ + "id": 1, + "userId": 1, + "text": "test text", + "createdAt": 132525324, + "visibility": 0, + "url": "https://example.com" +}""", DeliverPostJob.inbox.name to "https://follower.example.com/inbox" ), json = Json diff --git a/src/test/kotlin/dev/usbharu/hideout/service/impl/PostServiceTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/impl/PostServiceTest.kt index 8758bd3f..46e41fd2 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/impl/PostServiceTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/impl/PostServiceTest.kt @@ -3,6 +3,7 @@ package dev.usbharu.hideout.service.impl import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.domain.model.hideout.entity.Visibility import dev.usbharu.hideout.repository.Posts import dev.usbharu.hideout.repository.UsersFollowers import dev.usbharu.hideout.service.TwitterSnowflakeIdGenerateService @@ -44,7 +45,7 @@ class PostServiceTest { fun `findAll 公開投稿を取得できる`() = runTest { val postService = PostService(mock(), mock(), mock()) - suspend fun createPost(userId: Long, text: String, visibility: Int = 0): Post { + suspend fun createPost(userId: Long, text: String, visibility: Visibility = Visibility.PUBLIC): Post { return Post( TwitterSnowflakeIdGenerateService.generateId(), userId, @@ -67,13 +68,13 @@ class PostServiceTest { createPost(userA, "hello4"), createPost(userA, "hello5"), createPost(userA, "hello6"), - createPost(userB, "good bay", 1), - createPost(userB, "good bay1", 1), - createPost(userB, "good bay2", 1), - createPost(userB, "good bay3", 1), - createPost(userB, "good bay4", 1), - createPost(userB, "good bay5", 1), - createPost(userB, "good bay6", 1), + createPost(userB, "good bay ", Visibility.FOLLOWERS), + createPost(userB, "good bay1", Visibility.FOLLOWERS), + createPost(userB, "good bay2", Visibility.FOLLOWERS), + createPost(userB, "good bay3", Visibility.FOLLOWERS), + createPost(userB, "good bay4", Visibility.FOLLOWERS), + createPost(userB, "good bay5", Visibility.FOLLOWERS), + createPost(userB, "good bay6", Visibility.FOLLOWERS), ) transaction { @@ -83,14 +84,14 @@ class PostServiceTest { this[Posts.overview] = it.overview this[Posts.text] = it.text this[Posts.createdAt] = it.createdAt - this[Posts.visibility] = it.visibility + this[Posts.visibility] = it.visibility.ordinal this[Posts.url] = it.url this[Posts.replyId] = it.replyId this[Posts.repostId] = it.repostId } } - val expect = posts.filter { it.visibility == 0 } + val expect = posts.filter { it.visibility == Visibility.PUBLIC } val actual = postService.findAll() assertContentEquals(expect, actual) @@ -100,7 +101,7 @@ class PostServiceTest { fun `findAll フォロー限定投稿を見れる`() = runTest { val postService = PostService(mock(), mock(), mock()) - suspend fun createPost(userId: Long, text: String, visibility: Int = 0): Post { + suspend fun createPost(userId: Long, text: String, visibility: Visibility = Visibility.PUBLIC): Post { return Post( TwitterSnowflakeIdGenerateService.generateId(), userId, @@ -123,13 +124,13 @@ class PostServiceTest { createPost(userA, "hello4"), createPost(userA, "hello5"), createPost(userA, "hello6"), - createPost(userB, "good bay", 1), - createPost(userB, "good bay1", 1), - createPost(userB, "good bay2", 1), - createPost(userB, "good bay3", 1), - createPost(userB, "good bay4", 1), - createPost(userB, "good bay5", 1), - createPost(userB, "good bay6", 1), + createPost(userB, "good bay ", Visibility.FOLLOWERS), + createPost(userB, "good bay1", Visibility.FOLLOWERS), + createPost(userB, "good bay2", Visibility.FOLLOWERS), + createPost(userB, "good bay3", Visibility.FOLLOWERS), + createPost(userB, "good bay4", Visibility.FOLLOWERS), + createPost(userB, "good bay5", Visibility.FOLLOWERS), + createPost(userB, "good bay6", Visibility.FOLLOWERS), ) transaction(db) { @@ -143,7 +144,7 @@ class PostServiceTest { this[Posts.overview] = it.overview this[Posts.text] = it.text this[Posts.createdAt] = it.createdAt - this[Posts.visibility] = it.visibility + this[Posts.visibility] = it.visibility.ordinal this[Posts.url] = it.url this[Posts.replyId] = it.replyId this[Posts.repostId] = it.repostId