mirror of https://github.com/usbharu/Hideout.git
feat: post content auto truncate
Postのcontentとtextを自動で制限文字数内に切り取るように
This commit is contained in:
parent
f49bec3252
commit
adf1dfc1f3
|
@ -18,11 +18,49 @@ package dev.usbharu.hideout.core.domain.model.post
|
||||||
|
|
||||||
import dev.usbharu.hideout.core.domain.model.emoji.CustomEmojiId
|
import dev.usbharu.hideout.core.domain.model.emoji.CustomEmojiId
|
||||||
|
|
||||||
data class PostContent(val text: String, val content: String, val emojiIds: List<CustomEmojiId>) {
|
class PostContent {
|
||||||
|
|
||||||
|
val text: String
|
||||||
|
val content: String
|
||||||
|
val emojiIds: List<CustomEmojiId>
|
||||||
|
|
||||||
|
constructor(text: String, content: String, emojiIds: List<CustomEmojiId>) {
|
||||||
|
this.text = text.take(TEXT_LENGTH)
|
||||||
|
this.content = content.take(CONTENT_LENGTH)
|
||||||
|
this.emojiIds = emojiIds.distinct()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val empty = PostContent("", "", emptyList())
|
val empty = PostContent("", "", emptyList())
|
||||||
const val CONTENT_LENGTH = 5000
|
const val CONTENT_LENGTH = 5000
|
||||||
const val TEXT_LENGTH = 3000
|
const val TEXT_LENGTH = 3000
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (javaClass != other?.javaClass) return false
|
||||||
|
|
||||||
|
other as PostContent
|
||||||
|
|
||||||
|
if (text != other.text) return false
|
||||||
|
if (content != other.content) return false
|
||||||
|
if (emojiIds != other.emojiIds) return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = text.hashCode()
|
||||||
|
result = 31 * result + content.hashCode()
|
||||||
|
result = 31 * result + emojiIds.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "PostContent(" +
|
||||||
|
"text='$text', " +
|
||||||
|
"content='$content', " +
|
||||||
|
"emojiIds=$emojiIds" +
|
||||||
|
")"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.model.post
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
class PostContentTest {
|
||||||
|
@Test
|
||||||
|
fun textがtext_lengthを超える場合は切り取られる() {
|
||||||
|
val postContent = PostContent(
|
||||||
|
"a".repeat(PostContent.TEXT_LENGTH + 1),
|
||||||
|
"b".repeat(PostContent.CONTENT_LENGTH),
|
||||||
|
emptyList()
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(postContent.text, "a".repeat(PostContent.TEXT_LENGTH))
|
||||||
|
assertEquals(postContent.content, "b".repeat(PostContent.CONTENT_LENGTH))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun contentがcontent_lengthを超える場合は切り取られる() {
|
||||||
|
val postContent = PostContent(
|
||||||
|
"a".repeat(PostContent.TEXT_LENGTH),
|
||||||
|
"b".repeat(PostContent.CONTENT_LENGTH + 1),
|
||||||
|
emptyList()
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(postContent.text, "a".repeat(PostContent.TEXT_LENGTH))
|
||||||
|
assertEquals(postContent.content, "b".repeat(PostContent.CONTENT_LENGTH))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun textとcontentがtext_lengthとcontent_lengthを超えない場合は変わらない() {
|
||||||
|
val postContent = PostContent(
|
||||||
|
"a".repeat(PostContent.TEXT_LENGTH),
|
||||||
|
"b".repeat(PostContent.CONTENT_LENGTH),
|
||||||
|
emptyList()
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(postContent.text, "a".repeat(PostContent.TEXT_LENGTH))
|
||||||
|
assertEquals(postContent.content, "b".repeat(PostContent.CONTENT_LENGTH))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue