From 11f58f8580c642ab286a253b307f4f10fe6eb6d6 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 16 Aug 2023 17:46:02 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Post=E3=81=AE=E3=83=89=E3=83=A1?= =?UTF-8?q?=E3=82=A4=E3=83=B3=E7=9F=A5=E8=AD=98=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/model/hideout/entity/Post.kt | 62 ++++++++++++++++++- .../hideout/repository/PostRepositoryImpl.kt | 2 +- .../hideout/service/ap/APLikeService.kt | 3 - .../hideout/service/ap/APNoteService.kt | 2 +- .../hideout/service/ap/APReactionService.kt | 2 - .../hideout/service/post/PostServiceImpl.kt | 2 +- .../service/ap/APNoteServiceImplTest.kt | 2 +- 7 files changed, 64 insertions(+), 11 deletions(-) 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 d58870d6..858bea45 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 -data class Post( +import dev.usbharu.hideout.config.Config + +data class Post private constructor( val id: Long, val userId: Long, val overview: String? = null, @@ -12,4 +14,60 @@ data class Post( val replyId: Long? = null, val sensitive: Boolean = false, val apId: String = url -) +) { + companion object { + 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 + ): Post { + val characterLimit = Config.configData.characterLimit + + 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 + ) + } + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index 764bf32d..79698826 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -78,7 +78,7 @@ object Posts : Table() { } fun ResultRow.toPost(): Post { - return Post( + return Post.of( id = this[Posts.id], userId = this[Posts.userId], overview = this[Posts.overview], diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt index 6f88d87a..6bf2132e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt @@ -5,7 +5,6 @@ import dev.usbharu.hideout.domain.model.ActivityPubStringResponse import dev.usbharu.hideout.domain.model.ap.Like import dev.usbharu.hideout.exception.ap.IllegalActivityPubObjectException import dev.usbharu.hideout.query.PostQueryService -import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.core.Transaction import dev.usbharu.hideout.service.reaction.ReactionService import io.ktor.http.* @@ -20,7 +19,6 @@ class APLikeServiceImpl( private val reactionService: ReactionService, private val apUserService: APUserService, private val apNoteService: APNoteService, - private val userQueryService: UserQueryService, private val postQueryService: PostQueryService, private val transaction: Transaction ) : APLikeService { @@ -32,7 +30,6 @@ class APLikeServiceImpl( val person = apUserService.fetchPersonWithEntity(actor) apNoteService.fetchNote(like.`object`!!) - val post = postQueryService.findByUrl(like.`object`!!) reactionService.receiveReaction( 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 45583001..9e1875b6 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt @@ -155,7 +155,7 @@ class APNoteServiceImpl( } postRepository.save( - Post( + Post.of( id = postRepository.generateId(), userId = person.second.id, overview = null, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt index a7ca1533..ba7ff79c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt @@ -11,7 +11,6 @@ import dev.usbharu.hideout.plugins.postAp import dev.usbharu.hideout.query.FollowerQueryService import dev.usbharu.hideout.query.PostQueryService import dev.usbharu.hideout.query.UserQueryService -import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.service.job.JobQueueParentService import io.ktor.client.* import kjob.core.job.JobProps @@ -28,7 +27,6 @@ interface APReactionService { @Single class APReactionServiceImpl( private val jobQueueParentService: JobQueueParentService, - private val postRepository: PostRepository, private val httpClient: HttpClient, private val userQueryService: UserQueryService, private val followerQueryService: FollowerQueryService, 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 d184cbae..c1b58d1b 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt @@ -18,7 +18,7 @@ class PostServiceImpl( override suspend fun createLocal(post: PostCreateDto): Post { val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found") val id = postRepository.generateId() - val createPost = Post( + val createPost = Post.of( id = id, userId = post.userId, overview = post.overview, diff --git a/src/test/kotlin/dev/usbharu/hideout/service/ap/APNoteServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/ap/APNoteServiceImplTest.kt index fd65cbba..071c6625 100644 --- a/src/test/kotlin/dev/usbharu/hideout/service/ap/APNoteServiceImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/service/ap/APNoteServiceImplTest.kt @@ -86,7 +86,7 @@ class APNoteServiceImplTest { followerQueryService, mock() ) - val postEntity = Post( + val postEntity = Post.of( 1L, 1L, null,