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 3c719104..c417a861 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt @@ -17,6 +17,8 @@ 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 dev.usbharu.hideout.service.post.PostCreateInterceptor +import dev.usbharu.hideout.service.post.PostService import io.ktor.client.* import io.ktor.client.statement.* import kjob.core.job.JobProps @@ -45,9 +47,14 @@ class APNoteServiceImpl( private val followerQueryService: FollowerQueryService, private val postQueryService: PostQueryService, @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) @@ -161,7 +168,7 @@ class APNoteServiceImpl( postQueryService.findByUrl(it) } - postRepository.save( + postService.createRemote( Post.of( id = postRepository.generateId(), userId = person.second.id, @@ -185,4 +192,8 @@ class APNoteServiceImpl( companion object { const val public: String = "https://www.w3.org/ns/activitystreams#Public" } + + override suspend fun run(post: Post) { + createNote(post) + } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/post/PostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/post/PostService.kt index 0eed9d72..641ac4cd 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/PostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostService.kt @@ -7,4 +7,10 @@ import org.springframework.stereotype.Service @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/service/post/PostServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt index 0ba6b536..2c7cc38a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt @@ -5,17 +5,32 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Post import dev.usbharu.hideout.exception.UserNotFoundException import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.repository.UserRepository -import dev.usbharu.hideout.service.ap.APNoteService import org.springframework.stereotype.Service import java.time.Instant +import java.util.* @Service class PostServiceImpl( private val postRepository: PostRepository, private val userRepository: UserRepository, - private val apNoteService: APNoteService ) : PostService { + private val interceptors = Collections.synchronizedList(mutableListOf()) + 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 id = postRepository.generateId() val createPost = Post.of( @@ -29,9 +44,6 @@ class PostServiceImpl( repostId = null, replyId = null ) - apNoteService.createNote(createPost) - return internalCreate(createPost) + return postRepository.save(createPost) } - - private suspend fun internalCreate(post: Post): Post = postRepository.save(post) }