refactor: 公開範囲を列挙型に変更

This commit is contained in:
usbharu 2023-05-08 11:42:55 +09:00
parent aaaaf6aff6
commit dfa3bb74fb
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
6 changed files with 45 additions and 27 deletions

View File

@ -6,7 +6,7 @@ data class Post(
val overview:String? = null, val overview:String? = null,
val text:String, val text:String,
val createdAt:Long, val createdAt:Long,
val visibility: Int, val visibility: Visibility,
val url:String, val url:String,
val repostId:Long? = null, val repostId:Long? = null,
val replyId:Long? = null val replyId:Long? = null

View File

@ -0,0 +1,8 @@
package dev.usbharu.hideout.domain.model.hideout.entity
enum class Visibility {
PUBLIC,
UNLISTED,
FOLLOWERS,
DIRECT
}

View File

@ -1,6 +1,7 @@
package dev.usbharu.hideout.repository package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.Post 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 dev.usbharu.hideout.service.IdGenerateService
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
@ -33,7 +34,7 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
it[overview] = post.overview it[overview] = post.overview
it[text] = post.text it[text] = post.text
it[createdAt] = post.createdAt it[createdAt] = post.createdAt
it[visibility] = post.visibility it[visibility] = post.visibility.ordinal
it[url] = post.url it[url] = post.url
it[repostId] = post.repostId it[repostId] = post.repostId
it[replyId] = post.replyId it[replyId] = post.replyId
@ -75,7 +76,7 @@ fun ResultRow.toPost(): Post {
overview = this[Posts.overview], overview = this[Posts.overview],
text = this[Posts.text], text = this[Posts.text],
createdAt = this[Posts.createdAt], createdAt = this[Posts.createdAt],
visibility = this[Posts.visibility], visibility = Visibility.values().first { visibility -> visibility.ordinal == this[Posts.visibility] },
url = this[Posts.url], url = this[Posts.url],
repostId = this[Posts.repostId], repostId = this[Posts.repostId],
replyId = this[Posts.replyId] replyId = this[Posts.replyId]

View File

@ -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.dto.PostCreateDto
import dev.usbharu.hideout.domain.model.hideout.entity.Post 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.IPostRepository
import dev.usbharu.hideout.repository.Posts import dev.usbharu.hideout.repository.Posts
import dev.usbharu.hideout.repository.UsersFollowers import dev.usbharu.hideout.repository.UsersFollowers
@ -36,7 +37,7 @@ class PostService(
val id = postRepository.generateId() val id = postRepository.generateId()
val postEntity = Post( val postEntity = Post(
id, user.id, null, post.text, 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) postRepository.save(postEntity)
} }
@ -51,7 +52,7 @@ class PostService(
): List<Post> { ): List<Post> {
return transaction { return transaction {
val select = Posts.select { val select = Posts.select {
Posts.visibility.eq(0) Posts.visibility.eq(Visibility.PUBLIC.ordinal)
} }
if (userId != null) { if (userId != null) {
select.orWhere { select.orWhere {

View File

@ -7,6 +7,7 @@ import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.config.ConfigData import dev.usbharu.hideout.config.ConfigData
import dev.usbharu.hideout.domain.model.hideout.entity.Post 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.User
import dev.usbharu.hideout.domain.model.hideout.entity.Visibility
import dev.usbharu.hideout.domain.model.job.DeliverPostJob import dev.usbharu.hideout.domain.model.job.DeliverPostJob
import dev.usbharu.hideout.service.impl.IUserService import dev.usbharu.hideout.service.impl.IUserService
import dev.usbharu.hideout.service.job.JobQueueParentService import dev.usbharu.hideout.service.job.JobQueueParentService
@ -78,7 +79,7 @@ class ActivityPubNoteServiceImplTest {
null, null,
"test text", "test text",
1L, 1L,
1, Visibility.PUBLIC,
"https://example.com" "https://example.com"
) )
activityPubNoteService.createNote(postEntity) activityPubNoteService.createNote(postEntity)
@ -99,8 +100,14 @@ class ActivityPubNoteServiceImplTest {
JobProps( JobProps(
data = mapOf<String, Any>( data = mapOf<String, Any>(
DeliverPostJob.actor.name to "https://follower.example.com", DeliverPostJob.actor.name to "https://follower.example.com",
DeliverPostJob.post.name to "{\"id\":1,\"userId\":1,\"inReplyToId\":null,\"text\":\"test text\"," + DeliverPostJob.post.name to """{
"\"createdAt\":1,\"updatedAt\":1,\"url\":\"https://example.com\"}", "id": 1,
"userId": 1,
"text": "test text",
"createdAt": 132525324,
"visibility": 0,
"url": "https://example.com"
}""",
DeliverPostJob.inbox.name to "https://follower.example.com/inbox" DeliverPostJob.inbox.name to "https://follower.example.com/inbox"
), ),
json = Json json = Json

View File

@ -3,6 +3,7 @@
package dev.usbharu.hideout.service.impl package dev.usbharu.hideout.service.impl
import dev.usbharu.hideout.domain.model.hideout.entity.Post 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.Posts
import dev.usbharu.hideout.repository.UsersFollowers import dev.usbharu.hideout.repository.UsersFollowers
import dev.usbharu.hideout.service.TwitterSnowflakeIdGenerateService import dev.usbharu.hideout.service.TwitterSnowflakeIdGenerateService
@ -44,7 +45,7 @@ class PostServiceTest {
fun `findAll 公開投稿を取得できる`() = runTest { fun `findAll 公開投稿を取得できる`() = runTest {
val postService = PostService(mock(), mock(), mock()) 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( return Post(
TwitterSnowflakeIdGenerateService.generateId(), TwitterSnowflakeIdGenerateService.generateId(),
userId, userId,
@ -67,13 +68,13 @@ class PostServiceTest {
createPost(userA, "hello4"), createPost(userA, "hello4"),
createPost(userA, "hello5"), createPost(userA, "hello5"),
createPost(userA, "hello6"), createPost(userA, "hello6"),
createPost(userB, "good bay", 1), createPost(userB, "good bay ", Visibility.FOLLOWERS),
createPost(userB, "good bay1", 1), createPost(userB, "good bay1", Visibility.FOLLOWERS),
createPost(userB, "good bay2", 1), createPost(userB, "good bay2", Visibility.FOLLOWERS),
createPost(userB, "good bay3", 1), createPost(userB, "good bay3", Visibility.FOLLOWERS),
createPost(userB, "good bay4", 1), createPost(userB, "good bay4", Visibility.FOLLOWERS),
createPost(userB, "good bay5", 1), createPost(userB, "good bay5", Visibility.FOLLOWERS),
createPost(userB, "good bay6", 1), createPost(userB, "good bay6", Visibility.FOLLOWERS),
) )
transaction { transaction {
@ -83,14 +84,14 @@ class PostServiceTest {
this[Posts.overview] = it.overview this[Posts.overview] = it.overview
this[Posts.text] = it.text this[Posts.text] = it.text
this[Posts.createdAt] = it.createdAt this[Posts.createdAt] = it.createdAt
this[Posts.visibility] = it.visibility this[Posts.visibility] = it.visibility.ordinal
this[Posts.url] = it.url this[Posts.url] = it.url
this[Posts.replyId] = it.replyId this[Posts.replyId] = it.replyId
this[Posts.repostId] = it.repostId this[Posts.repostId] = it.repostId
} }
} }
val expect = posts.filter { it.visibility == 0 } val expect = posts.filter { it.visibility == Visibility.PUBLIC }
val actual = postService.findAll() val actual = postService.findAll()
assertContentEquals(expect, actual) assertContentEquals(expect, actual)
@ -100,7 +101,7 @@ class PostServiceTest {
fun `findAll フォロー限定投稿を見れる`() = runTest { fun `findAll フォロー限定投稿を見れる`() = runTest {
val postService = PostService(mock(), mock(), mock()) 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( return Post(
TwitterSnowflakeIdGenerateService.generateId(), TwitterSnowflakeIdGenerateService.generateId(),
userId, userId,
@ -123,13 +124,13 @@ class PostServiceTest {
createPost(userA, "hello4"), createPost(userA, "hello4"),
createPost(userA, "hello5"), createPost(userA, "hello5"),
createPost(userA, "hello6"), createPost(userA, "hello6"),
createPost(userB, "good bay", 1), createPost(userB, "good bay ", Visibility.FOLLOWERS),
createPost(userB, "good bay1", 1), createPost(userB, "good bay1", Visibility.FOLLOWERS),
createPost(userB, "good bay2", 1), createPost(userB, "good bay2", Visibility.FOLLOWERS),
createPost(userB, "good bay3", 1), createPost(userB, "good bay3", Visibility.FOLLOWERS),
createPost(userB, "good bay4", 1), createPost(userB, "good bay4", Visibility.FOLLOWERS),
createPost(userB, "good bay5", 1), createPost(userB, "good bay5", Visibility.FOLLOWERS),
createPost(userB, "good bay6", 1), createPost(userB, "good bay6", Visibility.FOLLOWERS),
) )
transaction(db) { transaction(db) {
@ -143,7 +144,7 @@ class PostServiceTest {
this[Posts.overview] = it.overview this[Posts.overview] = it.overview
this[Posts.text] = it.text this[Posts.text] = it.text
this[Posts.createdAt] = it.createdAt this[Posts.createdAt] = it.createdAt
this[Posts.visibility] = it.visibility this[Posts.visibility] = it.visibility.ordinal
this[Posts.url] = it.url this[Posts.url] = it.url
this[Posts.replyId] = it.replyId this[Posts.replyId] = it.replyId
this[Posts.repostId] = it.repostId this[Posts.repostId] = it.repostId