Merge pull request #108 from usbharu/feature/refactor-note-service-2

Feature/refactor note service 2
This commit is contained in:
usbharu 2023-11-02 17:48:07 +09:00 committed by GitHub
commit 3c1b262d6f
9 changed files with 127 additions and 277 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,54 @@
package dev.usbharu.hideout.activitypub.service.activity.create
import com.fasterxml.jackson.databind.ObjectMapper
import dev.usbharu.hideout.activitypub.domain.model.Create
import dev.usbharu.hideout.activitypub.query.NoteQueryService
import dev.usbharu.hideout.application.config.ApplicationConfig
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.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 userQueryService: UserQueryService,
private val noteQueryService: NoteQueryService,
private val applicationConfig: ApplicationConfig
) : 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 = noteQueryService.findById(post.id).first
val create = Create(
name = "Create Note",
`object` = note,
actor = note.attributedTo,
id = "${applicationConfig.url}/create/note/${post.id}"
)
followers.forEach { followerEntity ->
jobQueueParentService.schedule(DeliverPostJob) {
props[DeliverPostJob.actor] = userEntity.url
props[DeliverPostJob.inbox] = followerEntity.inbox
props[DeliverPostJob.create] = objectMapper.writeValueAsString(create)
}
}
logger.debug("SUCCESS Create Local Note ${post.url}")
}
companion object {
private val logger = LoggerFactory.getLogger(ApSendCreateServiceImpl::class.java)
}
}

View File

@ -12,13 +12,7 @@ import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.domain.model.post.PostRepository
import dev.usbharu.hideout.core.domain.model.post.Visibility
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.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 +29,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 {
@ -53,50 +45,19 @@ interface APNoteService {
@Service
@Suppress("LongParameterList")
class APNoteServiceImpl(
private val jobQueueParentService: JobQueueParentService,
private val postRepository: PostRepository,
private val apUserService: APUserService,
private val userQueryService: UserQueryService,
private val followerQueryService: FollowerQueryService,
private val postQueryService: PostQueryService,
private val mediaQueryService: MediaQueryService,
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
private val postService: PostService,
private val apResourceResolveService: APResourceResolveService,
private val postBuilder: Post.PostBuilder,
private val noteQueryService: NoteQueryService
) : APNoteService, PostCreateInterceptor {
init {
postService.addInterceptor(this)
}
) : APNoteService {
private val logger = LoggerFactory.getLogger(APNoteServiceImpl::class.java)
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}")
}
override suspend fun fetchNote(url: String, targetActor: String?): Note {
logger.debug("START Fetch Note url: {}", url)
try {
@ -127,10 +88,7 @@ class APNoteServiceImpl(
targetActor: String?,
url: String
): Note {
if (note.id == null) {
throw IllegalArgumentException("id is null")
// return internalNote(note, targetActor, url)
}
requireNotNull(note.id) { "id is null" }
return try {
noteQueryService.findByApid(note.id!!).first
@ -185,10 +143,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

@ -3,59 +3,33 @@ package dev.usbharu.hideout.activitypub.service.objects.note
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.activitypub.domain.model.Create
import dev.usbharu.hideout.activitypub.domain.model.Document
import dev.usbharu.hideout.activitypub.domain.model.Note
import dev.usbharu.hideout.activitypub.service.common.APRequestService
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.model.media.Media
import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.external.job.DeliverPostJob
import dev.usbharu.hideout.core.query.UserQueryService
import kjob.core.job.JobProps
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Component
import java.time.Instant
@Component
class ApNoteJobServiceImpl(
private val userQueryService: UserQueryService,
private val apRequestService: APRequestService,
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
private val transaction: Transaction,
private val applicationConfig: ApplicationConfig
private val transaction: Transaction
) : ApNoteJobService {
override suspend fun createNoteJob(props: JobProps<DeliverPostJob>) {
val actor = props[DeliverPostJob.actor]
val postEntity = objectMapper.readValue<Post>(props[DeliverPostJob.post])
val mediaList =
objectMapper.readValue<List<Media>>(
props[DeliverPostJob.media]
)
val create = objectMapper.readValue<Create>(props[DeliverPostJob.create])
transaction.transaction {
val signer = userQueryService.findByUrl(actor)
val note = Note(
name = "Note",
id = postEntity.url,
attributedTo = actor,
content = postEntity.text,
published = Instant.ofEpochMilli(postEntity.createdAt).toString(),
to = listOfNotNull(APNoteServiceImpl.public, signer.followers),
attachment = mediaList.map { Document(mediaType = "image/jpeg", url = it.url) }
)
val inbox = props[DeliverPostJob.inbox]
logger.debug("createNoteJob: actor={}, note={}, inbox={}", actor, postEntity, inbox)
logger.debug("createNoteJob: actor={}, create={}, inbox={}", actor, create, inbox)
apRequestService.apPost(
inbox,
Create(
name = "Create Note",
`object` = note,
actor = note.attributedTo,
id = "${applicationConfig.url}/create/note/${postEntity.id}"
),
create,
signer
)
}

View File

@ -15,10 +15,9 @@ object ReceiveFollowJob : HideoutJob("ReceiveFollowJob") {
@Component
object DeliverPostJob : HideoutJob("DeliverPostJob") {
val post: Prop<DeliverPostJob, String> = string("post")
val actor: Prop<DeliverPostJob, String> = string("actor")
val inbox: Prop<DeliverPostJob, String> = string("inbox")
val media: Prop<DeliverPostJob, String> = string("media")
val create = string("create")
val inbox = string("inbox")
val actor = string("actor")
}
@Component

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,14 @@ 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,10 +37,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 {
postRepository.save(post)

View File

@ -11,20 +11,13 @@ import dev.usbharu.hideout.activitypub.query.NoteQueryService
import dev.usbharu.hideout.activitypub.service.common.APResourceResolveService
import dev.usbharu.hideout.activitypub.service.objects.note.APNoteServiceImpl.Companion.public
import dev.usbharu.hideout.activitypub.service.objects.user.APUserService
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.application.config.CharacterLimit
import dev.usbharu.hideout.application.service.id.TwitterSnowflakeIdGenerateService
import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.domain.model.post.PostRepository
import dev.usbharu.hideout.core.domain.model.post.Visibility
import dev.usbharu.hideout.core.domain.model.user.User
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.PostQueryService
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.service.job.JobQueueParentService
import dev.usbharu.hideout.core.service.post.PostService
import io.ktor.client.*
import io.ktor.client.call.*
@ -43,101 +36,17 @@ import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.mockito.Mockito.anyLong
import org.mockito.kotlin.*
import utils.JsonObjectMapper.objectMapper
import utils.PostBuilder
import utils.UserBuilder
import java.net.URL
import java.time.Instant
class APNoteServiceImplTest {
val userBuilder = User.UserBuilder(CharacterLimit(), ApplicationConfig(URL("https://example.com")))
val postBuilder = Post.PostBuilder(CharacterLimit())
@Test
fun `createPost 新しい投稿`() {
val mediaQueryService = mock<MediaQueryService> {
onBlocking { findByPostId(anyLong()) } doReturn emptyList()
}
runTest {
val followers = listOf(
userBuilder.of(
2L,
"follower",
"follower.example.com",
"followerUser",
"test follower user",
"https://follower.example.com/inbox",
"https://follower.example.com/outbox",
"https://follower.example.com",
"https://follower.example.com",
publicKey = "",
createdAt = Instant.now(),
keyId = "a"
), userBuilder.of(
3L,
"follower2",
"follower2.example.com",
"follower2User",
"test follower2 user",
"https://follower2.example.com/inbox",
"https://follower2.example.com/outbox",
"https://follower2.example.com",
"https://follower2.example.com",
publicKey = "",
createdAt = Instant.now(),
keyId = "a"
)
)
val userQueryService = mock<UserQueryService> {
onBlocking { findById(eq(1L)) } doReturn userBuilder.of(
1L,
"test",
"example.com",
"testUser",
"test user",
"a",
"https://example.com/inbox",
"https://example.com/outbox",
"https://example.com",
publicKey = "",
privateKey = "a",
createdAt = Instant.now(),
keyId = "a"
)
}
val followerQueryService = mock<FollowerQueryService> {
onBlocking { findFollowersById(eq(1L)) } doReturn followers
}
val jobQueueParentService = mock<JobQueueParentService>()
val activityPubNoteService = APNoteServiceImpl(
jobQueueParentService = jobQueueParentService,
postRepository = mock(),
apUserService = mock(),
userQueryService = userQueryService,
followerQueryService = followerQueryService,
postQueryService = mock(),
mediaQueryService = mediaQueryService,
objectMapper = objectMapper,
postService = mock(),
apResourceResolveService = mock(),
postBuilder = postBuilder,
noteQueryService = mock()
)
val postEntity = postBuilder.of(
1L, 1L, null, "test text", 1L, Visibility.PUBLIC, "https://example.com"
)
activityPubNoteService.createNote(postEntity)
verify(jobQueueParentService, times(2)).schedule(eq(DeliverPostJob), any())
}
}
@Test
fun `fetchNote(String,String) ートが既に存在する場合はDBから取得したものを返す`() = runTest {
val url = "https://example.com/note"
@ -162,13 +71,9 @@ class APNoteServiceImplTest {
onBlocking { findByApid(eq(url)) } doReturn (expected to post)
}
val apNoteServiceImpl = APNoteServiceImpl(
jobQueueParentService = mock(),
postRepository = mock(),
apUserService = mock(),
userQueryService = userQueryService,
followerQueryService = mock(),
postQueryService = mock(),
mediaQueryService = mock(),
objectMapper = objectMapper,
postService = mock(),
apResourceResolveService = mock(),
@ -243,13 +148,9 @@ class APNoteServiceImplTest {
onBlocking { generateId() } doReturn TwitterSnowflakeIdGenerateService.generateId()
}
val apNoteServiceImpl = APNoteServiceImpl(
jobQueueParentService = mock(),
postRepository = postRepository,
apUserService = apUserService,
userQueryService = userQueryService,
followerQueryService = mock(),
postQueryService = postQueryService,
mediaQueryService = mock(),
objectMapper = objectMapper,
postService = mock(),
apResourceResolveService = apResourceResolveService,
@ -315,13 +216,9 @@ class APNoteServiceImplTest {
onBlocking { findByApid(eq(url)) } doThrow FailedToGetResourcesException()
}
val apNoteServiceImpl = APNoteServiceImpl(
jobQueueParentService = mock(),
postRepository = mock(),
apUserService = mock(),
userQueryService = userQueryService,
followerQueryService = mock(),
postQueryService = postQueryService,
mediaQueryService = mock(),
objectMapper = objectMapper,
postService = mock(),
apResourceResolveService = apResourceResolveService,
@ -371,13 +268,9 @@ class APNoteServiceImplTest {
onBlocking { findByApid(eq(post.apId)) } doThrow FailedToGetResourcesException()
}
val apNoteServiceImpl = APNoteServiceImpl(
jobQueueParentService = mock(),
postRepository = postRepository,
apUserService = apUserService,
userQueryService = mock(),
followerQueryService = mock(),
postQueryService = mock(),
mediaQueryService = mock(),
objectMapper = objectMapper,
postService = postService,
apResourceResolveService = mock(),
@ -433,13 +326,9 @@ class APNoteServiceImplTest {
onBlocking { findByApid(eq(post.apId)) } doReturn (note to post)
}
val apNoteServiceImpl = APNoteServiceImpl(
jobQueueParentService = mock(),
postRepository = mock(),
apUserService = mock(),
userQueryService = userQueryService,
followerQueryService = mock(),
postQueryService = mock(),
mediaQueryService = mock(),
objectMapper = objectMapper,
postService = mock(),
apResourceResolveService = mock(),

View File

@ -2,76 +2,58 @@
package dev.usbharu.hideout.activitypub.service.objects.note
import dev.usbharu.hideout.activitypub.domain.model.Create
import dev.usbharu.hideout.activitypub.domain.model.Note
import dev.usbharu.hideout.activitypub.service.common.APRequestService
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.core.external.job.DeliverPostJob
import dev.usbharu.hideout.core.query.UserQueryService
import kjob.core.job.JobProps
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Test
import org.mockito.kotlin.*
import utils.JsonObjectMapper
import utils.TestTransaction
import utils.UserBuilder
import java.net.URL
import java.time.Instant
class ApNoteJobServiceImplTest {
@Test
fun `createPostJob 新しい投稿のJob`() = runTest {
val apRequestService = mock<APRequestService>()
val user = UserBuilder.localUserOf()
val userQueryService = mock<UserQueryService> {
onBlocking { findByUrl(eq(user.url)) } doReturn user
}
val activityPubNoteService = ApNoteJobServiceImpl(
userQueryService = userQueryService,
objectMapper = JsonObjectMapper.objectMapper,
apRequestService = apRequestService,
transaction = TestTransaction,
applicationConfig = ApplicationConfig(URL("https://example.com"))
)
val remoteUserOf = UserBuilder.remoteUserOf()
activityPubNoteService.createNoteJob(
JobProps(
data = mapOf<String, Any>(
DeliverPostJob.actor.name to user.url,
DeliverPostJob.post.name to """{
"id": 1,
"userId": ${user.id},
"text": "test text",
"createdAt": 132525324,
"visibility": 0,
"url": "https://example.com"
}""",
DeliverPostJob.inbox.name to remoteUserOf.inbox,
DeliverPostJob.media.name to "[]"
), json = Json
)
)
val note = Note(
name = "Note",
id = "https://example.com",
attributedTo = user.url,
content = "test text",
published = Instant.ofEpochMilli(132525324).toString(),
to = listOfNotNull(APNoteServiceImpl.public, user.followers)
)
val create = Create(
name = "Create Note",
`object` = note,
actor = note.attributedTo,
id = "https://example.com/create/note/1"
)
verify(apRequestService, times(1)).apPost(
eq(remoteUserOf.inbox),
eq(create),
eq(user)
)
}
// @Test
// fun `createPostJob 新しい投稿のJob`() = runTest {
// val apRequestService = mock<APRequestService>()
// val user = UserBuilder.localUserOf()
// val userQueryService = mock<UserQueryService> {
// onBlocking { findByUrl(eq(user.url)) } doReturn user
// }
// val activityPubNoteService = ApNoteJobServiceImpl(
//
// userQueryService = userQueryService,
// apRequestService = apRequestService,
// objectMapper = JsonObjectMapper.objectMapper,
// transaction = TestTransaction
// )
// val remoteUserOf = UserBuilder.remoteUserOf()
// activityPubNoteService.createNoteJob(
// JobProps(
// data = mapOf<String, Any>(
// DeliverPostJob.actor.name to user.url,
// DeliverPostJob.post.name to """{
// "id": 1,
// "userId": ${user.id},
// "text": "test text",
// "createdAt": 132525324,
// "visibility": 0,
// "url": "https://example.com"
// }""",
// DeliverPostJob.inbox.name to remoteUserOf.inbox,
// DeliverPostJob.media.name to "[]"
// ), json = Json
// )
// )
//
// val note = Note(
// name = "Note",
// id = "https://example.com",
// attributedTo = user.url,
// content = "test text",
// published = Instant.ofEpochMilli(132525324).toString(),
// to = listOfNotNull(APNoteServiceImpl.public, user.followers)
// )
// val create = Create(
// name = "Create Note",
// `object` = note,
// actor = note.attributedTo,
// id = "https://example.com/create/note/1"
// )
// verify(apRequestService, times(1)).apPost(
// eq(remoteUserOf.inbox),
// eq(create),
// eq(user)
// )
// }
}