From 015376d32eafbc9d86fe23a72243011b2724806e Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 2 Nov 2023 16:19:08 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8A=95=E7=A8=BF=E4=BD=9C?= =?UTF-8?q?=E6=88=90=E6=99=82=E3=81=AEjob=E7=99=BA=E8=A1=8C=E7=AE=87?= =?UTF-8?q?=E6=89=80=E3=82=92=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF?= =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activity/create/ApSendCreateService.kt | 7 +++ .../create/ApSendCreateServiceImpl.kt | 47 +++++++++++++++++++ .../service/objects/note/APNoteService.kt | 13 +---- .../hideout/core/service/post/PostService.kt | 5 -- .../core/service/post/PostServiceImpl.kt | 12 ++--- 5 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/create/ApSendCreateService.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/create/ApSendCreateServiceImpl.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/create/ApSendCreateService.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/create/ApSendCreateService.kt new file mode 100644 index 00000000..f1462e1b --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/create/ApSendCreateService.kt @@ -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) +} diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/create/ApSendCreateServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/create/ApSendCreateServiceImpl.kt new file mode 100644 index 00000000..75f2fa7f --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/create/ApSendCreateServiceImpl.kt @@ -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) + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/note/APNoteService.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/note/APNoteService.kt index 39ef3a07..8afafae9 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/note/APNoteService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/note/APNoteService.kt @@ -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 { 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" diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostService.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostService.kt index f311628b..47c4974d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostService.kt @@ -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) } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostServiceImpl.kt index a91a9b72..242f6eba 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/post/PostServiceImpl.kt @@ -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()) + 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 {