feat: Postをビルダーを使って生成するように

This commit is contained in:
usbharu 2023-10-23 12:17:48 +09:00
parent 05142e0a55
commit 657936c3d9
5 changed files with 99 additions and 50 deletions

View File

@ -15,50 +15,3 @@ data class ConfigData(
val objectMapper: ObjectMapper = jacksonObjectMapper(),
val characterLimit: CharacterLimit = CharacterLimit()
)
@Deprecated("Config is deprecated")
data class CharacterLimit(
val general: General = General.of(),
val post: Post = Post(),
val account: Account = Account(),
val instance: Instance = Instance()
) {
@Deprecated("Config is deprecated")
data class General private constructor(
val url: Int,
val domain: Int,
val publicKey: Int,
val privateKey: Int
) {
companion object {
@Suppress("FunctionMinLength")
fun of(url: Int? = null, domain: Int? = null, publicKey: Int? = null, privateKey: Int? = null): General {
return General(
url ?: 1000,
domain ?: 1000,
publicKey ?: 10000,
privateKey ?: 10000
)
}
}
}
@Deprecated("Config is deprecated")
data class Post(
val text: Int = 3000,
val overview: Int = 3000
)
@Deprecated("Config is deprecated")
data class Account(
val id: Int = 300,
val name: Int = 300,
val description: Int = 10000
)
@Deprecated("Config is deprecated")
data class Instance(
val name: Int = 600,
val description: Int = 10000
)
}

View File

@ -28,6 +28,7 @@ class SpringConfig {
}
}
@ConfigurationProperties("hideout")
data class ApplicationConfig(
val url: URL
@ -43,3 +44,35 @@ data class StorageConfig(
val accessKey: String,
val secretKey: String
)
@ConfigurationProperties("hideout.character-limit")
data class CharacterLimit(
val general: General = General(),
val post: Post = Post(),
val account: Account = Account(),
val instance: Instance = Instance()
) {
data class General(
val url: Int = 1000,
val domain: Int = 1000,
val publicKey: Int = 10000,
val privateKey: Int = 10000
)
data class Post(
val text: Int = 3000,
val overview: Int = 3000
)
data class Account(
val id: Int = 300,
val name: Int = 300,
val description: Int = 10000
)
data class Instance(
val name: Int = 600,
val description: Int = 10000
)
}

View File

@ -1,6 +1,8 @@
package dev.usbharu.hideout.domain.model.hideout.entity
import dev.usbharu.hideout.config.CharacterLimit
import dev.usbharu.hideout.config.Config
import org.springframework.stereotype.Component
data class Post private constructor(
val id: Long,
@ -18,6 +20,7 @@ data class Post private constructor(
) {
companion object {
@Suppress("FunctionMinLength", "LongParameterList")
@Deprecated("Static builder is deprecated")
fun of(
id: Long,
userId: Long,
@ -74,4 +77,62 @@ data class Post private constructor(
)
}
}
@Component
class PostBuilder(private val characterLimit: CharacterLimit) {
@Suppress("FunctionMinLength", "LongParameterList")
fun of(
id: Long,
userId: Long,
overview: String? = null,
text: String,
createdAt: Long,
visibility: Visibility,
url: String,
repostId: Long? = null,
replyId: Long? = null,
sensitive: Boolean = false,
apId: String = url,
mediaIds: List<Long> = emptyList()
): Post {
require(id >= 0) { "id must be greater than or equal to 0." }
require(userId >= 0) { "userId must be greater than or equal to 0." }
val limitedOverview = if ((overview?.length ?: 0) >= characterLimit.post.overview) {
overview?.substring(0, characterLimit.post.overview)
} else {
overview
}
val limitedText = if (text.length >= characterLimit.post.text) {
text.substring(0, characterLimit.post.text)
} else {
text
}
require(url.isNotBlank()) { "url must contain non-blank characters" }
require(url.length <= characterLimit.general.url) {
"url must not exceed ${characterLimit.general.url} characters."
}
require((repostId ?: 0) >= 0) { "repostId must be greater then or equal to 0." }
require((replyId ?: 0) >= 0) { "replyId must be greater then or equal to 0." }
return Post(
id = id,
userId = userId,
overview = limitedOverview,
text = limitedText,
createdAt = createdAt,
visibility = visibility,
url = url,
repostId = repostId,
replyId = replyId,
sensitive = sensitive,
apId = apId,
mediaIds = mediaIds
)
}
}
}

View File

@ -69,6 +69,7 @@ class APNoteServiceImpl(
private val postService: PostService,
private val apResourceResolveService: APResourceResolveService,
private val apRequestService: APRequestService,
private val postBuilder: Post.PostBuilder,
private val transaction: Transaction
) : APNoteService, PostCreateInterceptor {
@ -224,7 +225,7 @@ class APNoteServiceImpl(
// TODO: リモートのメディア処理を追加
postService.createRemote(
Post.of(
postBuilder.of(
id = postRepository.generateId(),
userId = person.second.id,
text = note.content.orEmpty(),

View File

@ -16,7 +16,8 @@ class PostServiceImpl(
private val postRepository: PostRepository,
private val userRepository: UserRepository,
private val timelineService: TimelineService,
private val postQueryService: PostQueryService
private val postQueryService: PostQueryService,
private val postBuilder: Post.PostBuilder
) : PostService {
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
@ -45,7 +46,7 @@ class PostServiceImpl(
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {
val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found")
val id = postRepository.generateId()
val createPost = Post.of(
val createPost = postBuilder.of(
id = id,
userId = post.userId,
overview = post.overview,