refactor: 投稿作成時のjob発行箇所をリファクタリング

This commit is contained in:
usbharu 2023-11-02 16:19:08 +09:00
parent 36aa131c3d
commit 015376d32e
5 changed files with 61 additions and 23 deletions

View File

@ -0,0 +1,7 @@
package dev.usbharu.hideout.activitypub.service.activity.create
import dev.usbharu.hideout.core.domain.model.post.Post
interface ApSendCreateService {
suspend fun createNote(post: Post)
}

View File

@ -0,0 +1,47 @@
package dev.usbharu.hideout.activitypub.service.activity.create
import com.fasterxml.jackson.databind.ObjectMapper
import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.external.job.DeliverPostJob
import dev.usbharu.hideout.core.query.FollowerQueryService
import dev.usbharu.hideout.core.query.MediaQueryService
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.service.job.JobQueueParentService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class ApSendCreateServiceImpl(
private val followerQueryService: FollowerQueryService,
private val objectMapper: ObjectMapper,
private val jobQueueParentService: JobQueueParentService,
private val mediaQueryService: MediaQueryService,
private val userQueryService: UserQueryService
) : ApSendCreateService {
override suspend fun createNote(post: Post) {
logger.info("CREATE Create Local Note ${post.url}")
logger.debug("START Create Local Note ${post.url}")
logger.trace("{}", post)
val followers = followerQueryService.findFollowersById(post.userId)
logger.debug("DELIVER Deliver Note Create ${followers.size} accounts.")
val userEntity = userQueryService.findById(post.userId)
val note = objectMapper.writeValueAsString(post)
val mediaList = objectMapper.writeValueAsString(mediaQueryService.findByPostId(post.id))
followers.forEach { followerEntity ->
jobQueueParentService.schedule(DeliverPostJob) {
props[DeliverPostJob.actor] = userEntity.url
props[DeliverPostJob.post] = note
props[DeliverPostJob.inbox] = followerEntity.inbox
props[DeliverPostJob.media] = mediaList
}
}
logger.debug("SUCCESS Create Local Note ${post.url}")
}
companion object {
private val logger = LoggerFactory.getLogger(ApSendCreateServiceImpl::class.java)
}
}

View File

@ -18,7 +18,6 @@ import dev.usbharu.hideout.core.query.MediaQueryService
import dev.usbharu.hideout.core.query.PostQueryService
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.service.job.JobQueueParentService
import dev.usbharu.hideout.core.service.post.PostCreateInterceptor
import dev.usbharu.hideout.core.service.post.PostService
import io.ktor.client.plugins.*
import kotlinx.coroutines.CoroutineScope
@ -35,8 +34,6 @@ import java.time.Instant
interface APNoteService {
suspend fun createNote(post: Post)
@Cacheable("fetchNote")
fun fetchNoteAsync(url: String, targetActor: String? = null): Deferred<Note> {
return CoroutineScope(Dispatchers.IO + MDCContext()).async {
@ -66,15 +63,12 @@ class APNoteServiceImpl(
private val postBuilder: Post.PostBuilder,
private val noteQueryService: NoteQueryService
) : APNoteService, PostCreateInterceptor {
) : APNoteService {
init {
postService.addInterceptor(this)
}
private val logger = LoggerFactory.getLogger(APNoteServiceImpl::class.java)
override suspend fun createNote(post: Post) {
suspend fun createNote(post: Post) {
logger.info("CREATE Create Local Note ${post.url}")
logger.debug("START Create Local Note ${post.url}")
logger.trace("{}", post)
@ -185,9 +179,6 @@ class APNoteServiceImpl(
override suspend fun fetchNote(note: Note, targetActor: String?): Note =
saveIfMissing(note, targetActor, note.id ?: throw IllegalArgumentException("note.id is null"))
override suspend fun run(post: Post) {
createNote(post)
}
companion object {
const val public: String = "https://www.w3.org/ns/activitystreams#Public"

View File

@ -7,9 +7,4 @@ import org.springframework.stereotype.Service
interface PostService {
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

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.service.post
import dev.usbharu.hideout.activitypub.service.activity.create.ApSendCreateService
import dev.usbharu.hideout.core.domain.exception.UserNotFoundException
import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.domain.model.post.PostRepository
@ -10,7 +11,6 @@ import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.time.Instant
import java.util.*
@Service
class PostServiceImpl(
@ -18,14 +18,15 @@ class PostServiceImpl(
private val userRepository: UserRepository,
private val timelineService: TimelineService,
private val postQueryService: PostQueryService,
private val postBuilder: Post.PostBuilder
private val postBuilder: Post.PostBuilder,
private val apSendCreateService: ApSendCreateService
) : PostService {
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
override suspend fun createLocal(post: PostCreateDto): Post {
logger.info("START Create Local Post user: {}, media: {}", post.userId, post.mediaIds.size)
val create = internalCreate(post, true)
interceptors.forEach { it.run(create) }
apSendCreateService.createNote(create)
logger.info("SUCCESS Create Local Post url: {}", create.url)
return create
}
@ -37,9 +38,6 @@ class PostServiceImpl(
return createdPost
}
override fun addInterceptor(postCreateInterceptor: PostCreateInterceptor) {
interceptors.add(postCreateInterceptor)
}
private suspend fun internalCreate(post: Post, isLocal: Boolean): Post {
val save = try {