diff --git a/src/main/kotlin/dev/usbharu/hideout/config/Config.kt b/src/main/kotlin/dev/usbharu/hideout/config/Config.kt index 1623ff25..45023914 100644 --- a/src/main/kotlin/dev/usbharu/hideout/config/Config.kt +++ b/src/main/kotlin/dev/usbharu/hideout/config/Config.kt @@ -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 - ) -} diff --git a/src/main/kotlin/dev/usbharu/hideout/config/SpringConfig.kt b/src/main/kotlin/dev/usbharu/hideout/config/SpringConfig.kt index 18ee3845..237b2c6c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/config/SpringConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/config/SpringConfig.kt @@ -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 + ) +} 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 afe45261..d500c2cd 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 @@ -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 = 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 + ) + } + } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt index 8a7b1a95..26a6306b 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt @@ -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(), 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 ac5e54c6..c98ee233 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt @@ -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()) @@ -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,