feat: PostServiceの依存関係を見直し

This commit is contained in:
usbharu 2023-09-29 11:04:31 +09:00
parent fd30e8258a
commit be32f6335f
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
3 changed files with 38 additions and 9 deletions

View File

@ -17,6 +17,8 @@ import dev.usbharu.hideout.query.PostQueryService
import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.repository.PostRepository
import dev.usbharu.hideout.service.job.JobQueueParentService import dev.usbharu.hideout.service.job.JobQueueParentService
import dev.usbharu.hideout.service.post.PostCreateInterceptor
import dev.usbharu.hideout.service.post.PostService
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.statement.* import io.ktor.client.statement.*
import kjob.core.job.JobProps import kjob.core.job.JobProps
@ -45,9 +47,14 @@ class APNoteServiceImpl(
private val followerQueryService: FollowerQueryService, private val followerQueryService: FollowerQueryService,
private val postQueryService: PostQueryService, private val postQueryService: PostQueryService,
@Qualifier("activitypub") private val objectMapper: ObjectMapper, @Qualifier("activitypub") private val objectMapper: ObjectMapper,
private val applicationConfig: ApplicationConfig private val applicationConfig: ApplicationConfig,
private val postService: PostService
) : APNoteService { ) : APNoteService, PostCreateInterceptor {
init {
postService.addInterceptor(this)
}
private val logger = LoggerFactory.getLogger(this::class.java) private val logger = LoggerFactory.getLogger(this::class.java)
@ -161,7 +168,7 @@ class APNoteServiceImpl(
postQueryService.findByUrl(it) postQueryService.findByUrl(it)
} }
postRepository.save( postService.createRemote(
Post.of( Post.of(
id = postRepository.generateId(), id = postRepository.generateId(),
userId = person.second.id, userId = person.second.id,
@ -185,4 +192,8 @@ class APNoteServiceImpl(
companion object { companion object {
const val public: String = "https://www.w3.org/ns/activitystreams#Public" const val public: String = "https://www.w3.org/ns/activitystreams#Public"
} }
override suspend fun run(post: Post) {
createNote(post)
}
} }

View File

@ -7,4 +7,10 @@ import org.springframework.stereotype.Service
@Service @Service
interface PostService { interface PostService {
suspend fun createLocal(post: PostCreateDto): Post suspend fun createLocal(post: PostCreateDto): Post
suspend fun createRemote(post: Post): Post
fun addInterceptor(postCreateInterceptor: PostCreateInterceptor)
}
interface PostCreateInterceptor {
suspend fun run(post: Post)
} }

View File

@ -5,17 +5,32 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Post
import dev.usbharu.hideout.exception.UserNotFoundException import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.repository.PostRepository
import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.repository.UserRepository
import dev.usbharu.hideout.service.ap.APNoteService
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.time.Instant import java.time.Instant
import java.util.*
@Service @Service
class PostServiceImpl( class PostServiceImpl(
private val postRepository: PostRepository, private val postRepository: PostRepository,
private val userRepository: UserRepository, private val userRepository: UserRepository,
private val apNoteService: APNoteService
) : PostService { ) : PostService {
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
override suspend fun createLocal(post: PostCreateDto): Post { override suspend fun createLocal(post: PostCreateDto): Post {
val create = internalCreate(post)
interceptors.forEach { it.run(create) }
return create
}
override suspend fun createRemote(post: Post): Post {
return postRepository.save(post)
}
override fun addInterceptor(postCreateInterceptor: PostCreateInterceptor) {
interceptors.add(postCreateInterceptor)
}
private suspend fun internalCreate(post: PostCreateDto): 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 = Post.of(
@ -29,9 +44,6 @@ class PostServiceImpl(
repostId = null, repostId = null,
replyId = null replyId = null
) )
apNoteService.createNote(createPost) return postRepository.save(createPost)
return internalCreate(createPost)
} }
private suspend fun internalCreate(post: Post): Post = postRepository.save(post)
} }