mirror of https://github.com/usbharu/Hideout.git
feat: Postをビルダーを使って生成するように
This commit is contained in:
parent
05142e0a55
commit
657936c3d9
|
@ -15,50 +15,3 @@ data class ConfigData(
|
||||||
val objectMapper: ObjectMapper = jacksonObjectMapper(),
|
val objectMapper: ObjectMapper = jacksonObjectMapper(),
|
||||||
val characterLimit: CharacterLimit = CharacterLimit()
|
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
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ class SpringConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ConfigurationProperties("hideout")
|
@ConfigurationProperties("hideout")
|
||||||
data class ApplicationConfig(
|
data class ApplicationConfig(
|
||||||
val url: URL
|
val url: URL
|
||||||
|
@ -43,3 +44,35 @@ data class StorageConfig(
|
||||||
val accessKey: String,
|
val accessKey: String,
|
||||||
val secretKey: 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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package dev.usbharu.hideout.domain.model.hideout.entity
|
package dev.usbharu.hideout.domain.model.hideout.entity
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.config.CharacterLimit
|
||||||
import dev.usbharu.hideout.config.Config
|
import dev.usbharu.hideout.config.Config
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
data class Post private constructor(
|
data class Post private constructor(
|
||||||
val id: Long,
|
val id: Long,
|
||||||
|
@ -18,6 +20,7 @@ data class Post private constructor(
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@Suppress("FunctionMinLength", "LongParameterList")
|
@Suppress("FunctionMinLength", "LongParameterList")
|
||||||
|
@Deprecated("Static builder is deprecated")
|
||||||
fun of(
|
fun of(
|
||||||
id: Long,
|
id: Long,
|
||||||
userId: 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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,6 +69,7 @@ class APNoteServiceImpl(
|
||||||
private val postService: PostService,
|
private val postService: PostService,
|
||||||
private val apResourceResolveService: APResourceResolveService,
|
private val apResourceResolveService: APResourceResolveService,
|
||||||
private val apRequestService: APRequestService,
|
private val apRequestService: APRequestService,
|
||||||
|
private val postBuilder: Post.PostBuilder,
|
||||||
private val transaction: Transaction
|
private val transaction: Transaction
|
||||||
|
|
||||||
) : APNoteService, PostCreateInterceptor {
|
) : APNoteService, PostCreateInterceptor {
|
||||||
|
@ -224,7 +225,7 @@ class APNoteServiceImpl(
|
||||||
|
|
||||||
// TODO: リモートのメディア処理を追加
|
// TODO: リモートのメディア処理を追加
|
||||||
postService.createRemote(
|
postService.createRemote(
|
||||||
Post.of(
|
postBuilder.of(
|
||||||
id = postRepository.generateId(),
|
id = postRepository.generateId(),
|
||||||
userId = person.second.id,
|
userId = person.second.id,
|
||||||
text = note.content.orEmpty(),
|
text = note.content.orEmpty(),
|
||||||
|
|
|
@ -16,7 +16,8 @@ class PostServiceImpl(
|
||||||
private val postRepository: PostRepository,
|
private val postRepository: PostRepository,
|
||||||
private val userRepository: UserRepository,
|
private val userRepository: UserRepository,
|
||||||
private val timelineService: TimelineService,
|
private val timelineService: TimelineService,
|
||||||
private val postQueryService: PostQueryService
|
private val postQueryService: PostQueryService,
|
||||||
|
private val postBuilder: Post.PostBuilder
|
||||||
) : PostService {
|
) : PostService {
|
||||||
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
|
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
|
||||||
|
|
||||||
|
@ -45,7 +46,7 @@ class PostServiceImpl(
|
||||||
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {
|
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {
|
||||||
val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found")
|
val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found")
|
||||||
val id = postRepository.generateId()
|
val id = postRepository.generateId()
|
||||||
val createPost = Post.of(
|
val createPost = postBuilder.of(
|
||||||
id = id,
|
id = id,
|
||||||
userId = post.userId,
|
userId = post.userId,
|
||||||
overview = post.overview,
|
overview = post.overview,
|
||||||
|
|
Loading…
Reference in New Issue